UNPKG

@dolomite-exchange/dolomite-margin

Version:

Ethereum Smart Contracts and TypeScript library used for the DolomiteMargin trading protocol

489 lines 27.7 kB
"use strict"; // noinspection JSUnusedGlobalSymbols Object.defineProperty(exports, "__esModule", { value: true }); exports.Getters = void 0; const bignumber_js_1 = require("bignumber.js"); const Helpers_1 = require("../lib/Helpers"); const types_1 = require("../types"); const AccountRiskOverrideSetter_1 = require("./AccountRiskOverrideSetter"); const OracleSentinel_1 = require("./OracleSentinel"); const Constants_1 = require("../lib/Constants"); class Getters { constructor(contracts) { this.contracts = contracts; } // ============ Getters for Risk ============ static parseIndex({ borrow, supply, lastUpdate, }) { return { borrow: Helpers_1.stringToDecimal(borrow), supply: Helpers_1.stringToDecimal(supply), lastUpdate: new bignumber_js_1.BigNumber(lastUpdate), }; } static parseTotalPar({ supply, borrow }) { return { borrow: new bignumber_js_1.BigNumber(borrow), supply: new bignumber_js_1.BigNumber(supply), }; } async getMarginRatio(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarginRatio(), options); return Helpers_1.stringToDecimal(result.value); } async getMarginRatioForAccount(account, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return this.getMarginRatio(options); } const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarginRatioForAccount(account), options); return Helpers_1.stringToDecimal(result.value); } async getLiquidationSpread(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getLiquidationSpread(), options); return Helpers_1.stringToDecimal(result.value); } async getLiquidationSpreadForPair(heldMarketId, owedMarketId, options) { const liquidationSpread = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getLiquidationSpreadForPair(heldMarketId.toFixed(0), owedMarketId.toFixed(0)), options); return Helpers_1.stringToDecimal(liquidationSpread.value); } async getLiquidationSpreadForAccountAndPair(account, heldMarketId, owedMarketId, options) { const liquidationSpread = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getLiquidationSpreadForAccountAndPair(account, heldMarketId.toFixed(0), owedMarketId.toFixed(0)), options); return Helpers_1.stringToDecimal(liquidationSpread.value); } async getEarningsRate(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getEarningsRate(), options); return Helpers_1.stringToDecimal(result.value); } async getMinBorrowedValue(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMinBorrowedValue(), options); return new bignumber_js_1.BigNumber(result.value); } async getOracleSentinel(options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(new OracleSentinel_1.OracleSentinel(this.contracts, Constants_1.ADDRESSES.ZERO)); } const oracleSentinelAddress = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getOracleSentinel(), options); return new OracleSentinel_1.OracleSentinel(this.contracts, oracleSentinelAddress); } async getIsBorrowAllowed(options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(true); } return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getIsBorrowAllowed(), options); } async getIsLiquidationAllowed(options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(true); } return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getIsLiquidationAllowed(), options); } async getCallbackGasLimit(options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(Constants_1.INTEGERS.ZERO); } const value = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getCallbackGasLimit(), options); return new bignumber_js_1.BigNumber(value); } async getDefaultAccountRiskOverrideSetter(options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(new AccountRiskOverrideSetter_1.AccountRiskOverrideSetter(this.contracts, Constants_1.ADDRESSES.ZERO)); } const accountRiskOverrideSetterAddress = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getDefaultAccountRiskOverrideSetter(), options); return new AccountRiskOverrideSetter_1.AccountRiskOverrideSetter(this.contracts, accountRiskOverrideSetterAddress); } async getAccountRiskOverrideSetterByAccountOwner(accountOwner, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(new AccountRiskOverrideSetter_1.AccountRiskOverrideSetter(this.contracts, Constants_1.ADDRESSES.ZERO)); } const accountRiskOverrideSetterAddress = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountRiskOverrideSetterByAccountOwner(accountOwner), options); return new AccountRiskOverrideSetter_1.AccountRiskOverrideSetter(this.contracts, accountRiskOverrideSetterAddress); } async getAccountRiskOverrideByAccount(account, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve({ marginRatioOverride: Constants_1.INTEGERS.ZERO, liquidationSpreadOverride: Constants_1.INTEGERS.ZERO, }); } const { marginRatioOverride, liquidationSpreadOverride } = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountRiskOverrideByAccount(account), options); return { marginRatioOverride: Helpers_1.stringToDecimal(marginRatioOverride.value), liquidationSpreadOverride: Helpers_1.stringToDecimal(liquidationSpreadOverride.value), }; } async getMarginRatioOverrideByAccount(account, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(Constants_1.INTEGERS.ZERO); } const marginRatioOverride = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarginRatioOverrideByAccount(account), options); return Helpers_1.stringToDecimal(marginRatioOverride.value); } async getLiquidationSpreadOverrideByAccount(account, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(Constants_1.INTEGERS.ZERO); } const liquidationSpreadOverride = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getLiquidationSpreadOverrideByAccount(account), options); return Helpers_1.stringToDecimal(liquidationSpreadOverride.value); } async getRiskLimits(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getRiskLimits(), options); return { marginRatioMax: Helpers_1.stringToDecimal(result[0]), liquidationSpreadMax: Helpers_1.stringToDecimal(result[1]), earningsRateMax: Helpers_1.stringToDecimal(result[2]), marginPremiumMax: Helpers_1.stringToDecimal(result[3]), liquidationSpreadPremiumMax: Helpers_1.stringToDecimal(result[4]), interestRateMax: Helpers_1.stringToDecimal(result[5]), minBorrowedValueMax: new bignumber_js_1.BigNumber(result[6]), }; } async getRiskParams(options) { const result = await Promise.all([ this.getMarginRatio(options), this.getLiquidationSpread(options), this.getEarningsRate(options), this.getMinBorrowedValue(options), this.getAccountMaxNumberOfMarketsWithBalances(options), this.getOracleSentinel(options), this.getCallbackGasLimit(options), ]); return { marginRatio: result[0], liquidationSpread: result[1], earningsRate: result[2], minBorrowedValue: result[3], accountMaxNumberOfMarketsWithBalances: result[4], oracleSentinel: result[5], callbackGasLimit: result[6], }; } // ============ Getters for Markets ============ async getAccountMaxNumberOfMarketsWithBalances(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountMaxNumberOfMarketsWithBalances(), options); return new bignumber_js_1.BigNumber(result); } async getNumMarkets(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getNumMarkets(), options); return new bignumber_js_1.BigNumber(result); } async getMarketIdByTokenAddress(token, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketIdByTokenAddress(token), options); return new bignumber_js_1.BigNumber(result); } async getMarketTokenAddress(marketId, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketTokenAddress(marketId.toFixed(0)), options); } async getMarketIsClosing(marketId, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketIsClosing(marketId.toFixed(0)), options); } async getMarketPrice(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketPrice(marketId.toFixed(0)), options); return new bignumber_js_1.BigNumber(result.value); } async getMarketTotalPar(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketTotalPar(marketId.toFixed(0)), options); return { borrow: new bignumber_js_1.BigNumber(result[0]), supply: new bignumber_js_1.BigNumber(result[1]), }; } async getMarketTotalWei(marketId, options) { const result = await this.getMarketTotalPar(marketId, options); const index = await this.getMarketCurrentIndex(marketId, options); return { borrow: result.borrow.times(index.borrow), supply: result.supply.times(index.supply), }; } async getMarketCachedIndex(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketCachedIndex(marketId.toFixed(0)), options); return Getters.parseIndex(result); } async getMarketCurrentIndex(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketCurrentIndex(marketId.toFixed(0)), options); return Getters.parseIndex(result); } async getMarketPriceOracle(marketId, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketPriceOracle(marketId.toFixed(0)), options); } async getMarketInterestSetter(marketId, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketInterestSetter(marketId.toFixed(0)), options); } async getMarketMarginPremium(marketId, options) { const marginPremium = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketMarginPremium(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(marginPremium.value); } /** * @deprecated */ async getMarketSpreadPremium(marketId, options) { const spreadPremium = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketSpreadPremium(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(spreadPremium.value); } async getMarketLiquidationSpreadPremium(marketId, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return this.getMarketSpreadPremium(marketId, options); } const spreadPremium = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketLiquidationSpreadPremium(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(spreadPremium.value); } /** * @deprecated */ async getMarketMaxWei(marketId, options) { const maxSupplyWei = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketMaxWei(marketId.toFixed(0)), options); return Helpers_1.valueToInteger(maxSupplyWei); } async getMarketMaxSupplyWei(marketId, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return this.getMarketMaxWei(marketId, options); } const maxSupplyWei = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketMaxSupplyWei(marketId.toFixed(0)), options); return Helpers_1.valueToInteger(maxSupplyWei); } async getMarketMaxBorrowWei(marketId, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(Constants_1.INTEGERS.ZERO); } const maxBorrowWei = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketMaxBorrowWei(marketId.toFixed(0)), options); return Helpers_1.valueToInteger(maxBorrowWei); } async getMarketEarningsRateOverride(marketId, options) { if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE) { return Promise.resolve(Constants_1.INTEGERS.ZERO); } const earningsRateOverride = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketEarningsRateOverride(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(earningsRateOverride.value); } async getMarketUtilization(marketId, options) { const totalWei = await this.getMarketTotalWei(marketId, options); return totalWei.borrow.div(totalWei.supply); } /** * @deprecated */ async getMarketInterestRate(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketInterestRate(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(result.value); } async getMarketBorrowInterestRatePerSecond(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketBorrowInterestRatePerSecond(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(result.value); } async getMarketBorrowInterestRateApr(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketBorrowInterestRateApr(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(result.value); } async getMarketSupplyInterestRateApr(marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketSupplyInterestRateApr(marketId.toFixed(0)), options); return Helpers_1.stringToDecimal(result.value); } async getMarket(marketId, options) { const market = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarket(marketId.toFixed(0)), options); return { ...market, totalPar: Getters.parseTotalPar(market.totalPar), index: Getters.parseIndex(market.index), marginPremium: Helpers_1.stringToDecimal(market.marginPremium.value), liquidationSpreadPremium: Helpers_1.stringToDecimal(market.liquidationSpreadPremium.value), maxSupplyWei: new bignumber_js_1.BigNumber(market.maxSupplyWei.value), maxBorrowWei: new bignumber_js_1.BigNumber(market.maxBorrowWei.value), earningsRateOverride: Helpers_1.stringToDecimal(market.earningsRateOverride.value), }; } async getMarketWithInfo(marketId, options) { const marketWithInfo = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getMarketWithInfo(marketId.toFixed(0)), options); const market = marketWithInfo[0]; const currentIndex = marketWithInfo[1]; const currentPrice = marketWithInfo[2]; const currentInterestRate = marketWithInfo[3]; return { market: { ...market, totalPar: Getters.parseTotalPar(market.totalPar), index: Getters.parseIndex(market.index), marginPremium: Helpers_1.stringToDecimal(market.marginPremium.value), liquidationSpreadPremium: Helpers_1.stringToDecimal(market.liquidationSpreadPremium.value), maxSupplyWei: Helpers_1.valueToInteger(market.maxSupplyWei), maxBorrowWei: Helpers_1.valueToInteger(market.maxBorrowWei), earningsRateOverride: Helpers_1.stringToDecimal(market.earningsRateOverride.value), }, currentIndex: Getters.parseIndex(currentIndex), currentPrice: new bignumber_js_1.BigNumber(currentPrice.value), currentInterestRate: Helpers_1.stringToDecimal(currentInterestRate.value), }; } async getNumExcessTokens(marketId, options) { const numExcessTokens = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getNumExcessTokens(marketId.toFixed(0)), options); return Helpers_1.valueToInteger(numExcessTokens); } // ============ Getters for Accounts ============ async getAccountPar(accountOwner, accountNumber, marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountPar({ owner: accountOwner, number: accountNumber.toFixed(0), }, marketId.toFixed(0)), options); return Helpers_1.valueToInteger(result); } async getAccountWei(accountOwner, accountNumber, marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountWei({ owner: accountOwner, number: accountNumber.toFixed(0), }, marketId.toFixed(0)), options); return Helpers_1.valueToInteger(result); } async getAccountStatus(accountOwner, accountNumber, options) { const rawStatus = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountStatus({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); switch (rawStatus) { case '0': return types_1.AccountStatus.Normal; case '1': return types_1.AccountStatus.Liquidating; case '2': return types_1.AccountStatus.Vaporizing; default: throw new Error(`invalid account status ${rawStatus}`); } } async getAccountMarketsWithBalances(accountOwner, accountNumber, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountMarketsWithBalances({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); return result.map(marketIdString => new bignumber_js_1.BigNumber(marketIdString)); } async getAccountNumberOfMarketsWithBalances(accountOwner, accountNumber, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountNumberOfMarketsWithBalances({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); return new bignumber_js_1.BigNumber(result); } async getAccountMarketWithBalanceAtIndex(accountOwner, accountNumber, index, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountMarketWithBalanceAtIndex({ owner: accountOwner, number: accountNumber.toFixed(0), }, index.toFixed()), options); return new bignumber_js_1.BigNumber(result); } async getAccountNumberOfMarketsWithDebt(accountOwner, accountNumber, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountNumberOfMarketsWithDebt({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); return new bignumber_js_1.BigNumber(result); } async getAccountValues(accountOwner, accountNumber, options) { let result; if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE || process.env.USE_READER === 'true') { result = await this.contracts.callConstantContractFunction(this.contracts.accountValuesReader.methods.getAccountValues({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); } else { result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountValues({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); } return { supply: new bignumber_js_1.BigNumber(result[0].value), borrow: new bignumber_js_1.BigNumber(result[1].value), }; } async getAdjustedAccountValues(accountOwner, accountNumber, options) { let result; if (this.contracts.getNetworkId() === types_1.Networks.ARBITRUM_ONE || process.env.USE_READER === 'true') { result = await this.contracts.callConstantContractFunction(this.contracts.accountValuesReader.methods.getAdjustedAccountValues({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); } else { result = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAdjustedAccountValues({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); } return { supply: new bignumber_js_1.BigNumber(result[0].value), borrow: new bignumber_js_1.BigNumber(result[1].value), }; } async getAccountBalances(accountOwner, accountNumber, options) { const balances = await this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getAccountBalances({ owner: accountOwner, number: accountNumber.toFixed(0), }), options); const marketIds = balances[0]; const tokens = balances[1]; const pars = balances[2]; const weis = balances[3]; const result = []; for (let i = 0; i < tokens.length; i += 1) { result.push({ marketId: new bignumber_js_1.BigNumber(marketIds[i]), tokenAddress: tokens[i], par: Helpers_1.valueToInteger(pars[i]), wei: Helpers_1.valueToInteger(weis[i]), }); } return result; } async isAccountLiquidatable(liquidOwner, liquidNumber, options = {}) { const [accountStatus, marginRatio, accountValues] = await Promise.all([ this.getAccountStatus(liquidOwner, liquidNumber), this.getMarginRatioForAccount({ owner: liquidOwner, number: liquidNumber.toFixed() }, options), this.getAdjustedAccountValues(liquidOwner, liquidNumber, options), ]); // return true if account has been partially liquidated if (accountValues.borrow.gt(0) && accountValues.supply.gt(0) && accountStatus === types_1.AccountStatus.Liquidating) { return true; } // return false if account is vaporizable if (accountValues.supply.isZero()) { return false; } // return true if account is undercollateralized const marginRequirement = accountValues.borrow.times(marginRatio); return accountValues.supply.lt(accountValues.borrow.plus(marginRequirement)); } // ============ Getters for Permissions ============ async getIsLocalOperator(owner, operator, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getIsLocalOperator(owner, operator), options); } async getIsGlobalOperator(operator, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getIsGlobalOperator(operator), options); } async getIsAutoTraderSpecial(autoTrader, options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.getIsAutoTraderSpecial(autoTrader), options); } // ============ Getters for Admin ============ async getAdmin(options) { return this.contracts.callConstantContractFunction(this.contracts.dolomiteMargin.methods.owner(), options); } async getExpiry(accountOwner, accountNumber, marketId, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.expiry.methods.getExpiry({ owner: accountOwner, number: accountNumber.toFixed(0), }, marketId.toFixed(0)), options); return new bignumber_js_1.BigNumber(result); } // ============ Getters for Expiry ============ async getExpiryPrices(account, heldMarketId, owedMarketId, expiryTimestamp, options) { const result = await this.contracts.callConstantContractFunction(this.contracts.expiry.methods.getLiquidationSpreadAdjustedPrices(account, heldMarketId.toFixed(0), owedMarketId.toFixed(0), expiryTimestamp.toFixed(0)), options); return { heldPrice: new bignumber_js_1.BigNumber(result[0].value), owedPrice: new bignumber_js_1.BigNumber(result[1].value), }; } async getExpiryRampTime(options) { const result = await this.contracts.callConstantContractFunction(this.contracts.expiry.methods.g_expiryRampTime(), options); return new bignumber_js_1.BigNumber(result); } } exports.Getters = Getters; //# sourceMappingURL=Getters.js.map