@dolomite-exchange/dolomite-margin
Version:
Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol
103 lines • 7.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericTraderProxyV1 = exports.GenericEventEmissionType = exports.GenericTraderType = void 0;
const 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 = {}));
class GenericTraderProxyV1 {
constructor(contracts) {
this.contracts = contracts;
}
get address() {
return this.contracts.genericTraderProxyV1.options.address;
}
static genericTraderParamsToCalldata(traderParams) {
return traderParams.map(traderParam => ({
traderType: traderParam.traderType,
makerAccountIndex: traderParam.makerAccountIndex,
trader: traderParam.trader,
tradeData: BytesHelper_1.toBytesNoPadding(traderParam.tradeData),
}));
}
static genericTransferParamToCalldata(transferParam) {
return {
fromAccountNumber: transferParam.fromAccountNumber.toFixed(0),
toAccountNumber: transferParam.toAccountNumber.toFixed(0),
transferAmounts: transferParam.transferAmounts.map(transferAmount => ({
marketId: transferAmount.marketId.toFixed(0),
amountWei: transferAmount.amountWei.toFixed(0),
})),
};
}
static genericExpiryToCalldata(expiryParam) {
return {
marketId: expiryParam.marketId.toFixed(0),
expiryTimeDelta: expiryParam.expiryTimeDelta.toFixed(0),
};
}
// ============ State-Changing Functions ============
async ownerSetEventEmitterRegistry(eventEmitter, options = {}) {
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.
*/
async swapExactInputForOutput(tradeAccountNumber, marketIdsPath, inputAmountWei, minOutputAmountWei, traderParams, makerAccounts, userConfig, options = {}) {
return this.contracts.callContractFunction(this.contracts.genericTraderProxyV1.methods.swapExactInputForOutput(tradeAccountNumber.toFixed(0), marketIdsPath.map(marketId => 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.
*/
async swapExactInputForOutputAndModifyPosition(tradeAccountNumber, marketIdsPath, inputAmountWei, minOutputAmountWei, traderParams, makerAccounts, transferCollateralParam, expiryParam, userConfig, options = {}) {
return this.contracts.callContractFunction(this.contracts.genericTraderProxyV1.methods.swapExactInputForOutputAndModifyPosition(tradeAccountNumber.toFixed(0), marketIdsPath.map(marketId => marketId.toFixed(0)), inputAmountWei.toFixed(), minOutputAmountWei.toFixed(), GenericTraderProxyV1.genericTraderParamsToCalldata(traderParams), makerAccounts, GenericTraderProxyV1.genericTransferParamToCalldata(transferCollateralParam), GenericTraderProxyV1.genericExpiryToCalldata(expiryParam), userConfig), options);
}
async eventEmitterRegistry(options = {}) {
return this.contracts.callConstantContractFunction(this.contracts.genericTraderProxyV1.methods.EVENT_EMITTER_REGISTRY(), options);
}
async isIsolationModeMarket(marketId, options = {}) {
return this.contracts.callConstantContractFunction(this.contracts.genericTraderProxyV1.methods.isIsolationModeMarket(this.contracts.dolomiteMargin.options.address, marketId.toFixed(0)), options);
}
}
exports.GenericTraderProxyV1 = GenericTraderProxyV1;
//# sourceMappingURL=GenericTraderProxyV1.js.map