@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
169 lines • 11.4 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericTraderProxyV1 = exports.GenericEventEmissionType = exports.GenericTraderType = void 0;
var BytesHelper_1 = require("../lib/BytesHelper");
var GenericTraderType;
(function (GenericTraderType) {
GenericTraderType[GenericTraderType["ExternalLiquidity"] = 0] = "ExternalLiquidity";
GenericTraderType[GenericTraderType["InternalLiquidity"] = 1] = "InternalLiquidity";
GenericTraderType[GenericTraderType["IsolationModeUnwrapper"] = 2] = "IsolationModeUnwrapper";
GenericTraderType[GenericTraderType["IsolationModeWrapper"] = 3] = "IsolationModeWrapper";
})(GenericTraderType = exports.GenericTraderType || (exports.GenericTraderType = {}));
var GenericEventEmissionType;
(function (GenericEventEmissionType) {
GenericEventEmissionType[GenericEventEmissionType["None"] = 0] = "None";
GenericEventEmissionType[GenericEventEmissionType["BorrowPosition"] = 1] = "BorrowPosition";
GenericEventEmissionType[GenericEventEmissionType["MarginPosition"] = 2] = "MarginPosition";
})(GenericEventEmissionType = exports.GenericEventEmissionType || (exports.GenericEventEmissionType = {}));
var GenericTraderProxyV1 = /** @class */ (function () {
function GenericTraderProxyV1(contracts) {
this.contracts = contracts;
}
Object.defineProperty(GenericTraderProxyV1.prototype, "address", {
get: function () {
return this.contracts.genericTraderProxyV1.options.address;
},
enumerable: false,
configurable: true
});
GenericTraderProxyV1.genericTraderParamsToCalldata = function (traderParams) {
return traderParams.map(function (traderParam) { return ({
traderType: traderParam.traderType,
makerAccountIndex: traderParam.makerAccountIndex,
trader: traderParam.trader,
tradeData: BytesHelper_1.toBytesNoPadding(traderParam.tradeData),
}); });
};
GenericTraderProxyV1.genericTransferParamToCalldata = function (transferParam) {
return {
fromAccountNumber: transferParam.fromAccountNumber.toFixed(0),
toAccountNumber: transferParam.toAccountNumber.toFixed(0),
transferAmounts: transferParam.transferAmounts.map(function (transferAmount) { return ({
marketId: transferAmount.marketId.toFixed(0),
amountWei: transferAmount.amountWei.toFixed(0),
}); }),
};
};
GenericTraderProxyV1.genericExpiryToCalldata = function (expiryParam) {
return {
marketId: expiryParam.marketId.toFixed(0),
expiryTimeDelta: expiryParam.expiryTimeDelta.toFixed(0),
};
};
// ============ State-Changing Functions ============
GenericTraderProxyV1.prototype.ownerSetEventEmitterRegistry = function (eventEmitter, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.contracts.callContractFunction(this.contracts.genericTraderProxyV1.methods.ownerSetEventEmitterRegistry(eventEmitter), options)];
});
});
};
/**
* Executes a trade using the path of markets provided and the provided traders.
*
* @param tradeAccountNumber The account number `msg.sender` will trade from
* @param marketIdsPath The market IDs that will be traded, in order. The first marketId is the input and the
* last is the output.
* @param inputAmountWei The amount of the input token to be traded, in wei.
* @param minOutputAmountWei The minimum amount of the output token to be received, in wei.
* @param traderParams The traders to be used for each action. The length should be `marketIdsPath.length - 1`.
* @param makerAccounts The accounts that will be used as makers for each trade of type
* `TradeType.InternalLiquidity`. The length should be equal to the number of unique maker
* accounts needed to execute the trade with the provided `tradersPath`.
* @param userConfig The user config to be used for the trade.
* @param options Additional options to be passed through to the web3 call.
*/
GenericTraderProxyV1.prototype.swapExactInputForOutput = function (tradeAccountNumber, marketIdsPath, inputAmountWei, minOutputAmountWei, traderParams, makerAccounts, userConfig, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.contracts.callContractFunction(this.contracts.genericTraderProxyV1.methods.swapExactInputForOutput(tradeAccountNumber.toFixed(0), marketIdsPath.map(function (marketId) { return marketId.toFixed(0); }), inputAmountWei.toFixed(), minOutputAmountWei.toFixed(), GenericTraderProxyV1.genericTraderParamsToCalldata(traderParams), makerAccounts, userConfig), options)];
});
});
};
/**
* Executes a trade using the path of markets provided and the provided traders. After the trades are executed (and
* within the same operation) the transfers and expirations are executed.
*
* @param tradeAccountNumber The account number msg.sender will trade from
* @param marketIdsPath The market IDs that will be traded, in order. The first marketId is the input and
* the last is the output.
* @param inputAmountWei The amount of the input token to be traded, in wei.
* @param minOutputAmountWei The minimum amount of the output token to be received, in wei.
* @param traderParams The traders to be used for each action. The length should be
* `marketIdsPath.length - 1`.
* @param makerAccounts The accounts that will be used as makers for each trade of type
* `TradeType.InternalLiquidity`. The length should be equal to the number of unique
* maker accounts needed to execute the trade with the provided `tradersPath`.
* @param transferCollateralParam The transfers to be executed after the trades. The length of the amounts should be
* non-zero.
* @param expiryParam The expirations to be executed after the trades. Expirations can only be set on
* negative amounts (debt).
* @param userConfig
* @param options Additional options to be passed through to the web3 call.
*/
GenericTraderProxyV1.prototype.swapExactInputForOutputAndModifyPosition = function (tradeAccountNumber, marketIdsPath, inputAmountWei, minOutputAmountWei, traderParams, makerAccounts, transferCollateralParam, expiryParam, userConfig, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.contracts.callContractFunction(this.contracts.genericTraderProxyV1.methods.swapExactInputForOutputAndModifyPosition(tradeAccountNumber.toFixed(0), marketIdsPath.map(function (marketId) { return marketId.toFixed(0); }), inputAmountWei.toFixed(), minOutputAmountWei.toFixed(), GenericTraderProxyV1.genericTraderParamsToCalldata(traderParams), makerAccounts, GenericTraderProxyV1.genericTransferParamToCalldata(transferCollateralParam), GenericTraderProxyV1.genericExpiryToCalldata(expiryParam), userConfig), options)];
});
});
};
GenericTraderProxyV1.prototype.eventEmitterRegistry = function (options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.contracts.callConstantContractFunction(this.contracts.genericTraderProxyV1.methods.EVENT_EMITTER_REGISTRY(), options)];
});
});
};
GenericTraderProxyV1.prototype.isIsolationModeMarket = function (marketId, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.contracts.callConstantContractFunction(this.contracts.genericTraderProxyV1.methods.isIsolationModeMarket(this.contracts.dolomiteMargin.options.address, marketId.toFixed(0)), options)];
});
});
};
return GenericTraderProxyV1;
}());
exports.GenericTraderProxyV1 = GenericTraderProxyV1;
//# sourceMappingURL=GenericTraderProxyV1.js.map