UNPKG

@dolomite-exchange/dolomite-margin

Version:

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

336 lines (331 loc) 20.4 kB
"use strict"; /* Copyright 2019 dYdX Trading Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Contracts = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); // JSON const AAVECopyCatAltCoinInterestSetter_json_1 = __importDefault(require("../../build/published_contracts/AAVECopyCatAltCoinInterestSetter.json")); const AAVECopyCatStableCoinInterestSetter_json_1 = __importDefault(require("../../build/published_contracts/AAVECopyCatStableCoinInterestSetter.json")); const IAccountRiskOverrideSetter_json_1 = __importDefault(require("../../build/published_contracts/IAccountRiskOverrideSetter.json")); const AccountValuesReader_json_1 = __importDefault(require("../../build/published_contracts/AccountValuesReader.json")); const IArbitrumGasInfo_json_1 = __importDefault(require("../../build/published_contracts/IArbitrumGasInfo.json")); const ArbitrumMultiCall_json_1 = __importDefault(require("../../build/published_contracts/ArbitrumMultiCall.json")); const ChainlinkPriceOracleV1_json_1 = __importDefault(require("../../build/published_contracts/ChainlinkPriceOracleV1.json")); const BorrowPositionProxyV1_json_1 = __importDefault(require("../../build/published_contracts/BorrowPositionProxyV1.json")); const BorrowPositionProxyV2_json_1 = __importDefault(require("../../build/published_contracts/BorrowPositionProxyV2.json")); const DepositWithdrawalProxy_json_1 = __importDefault(require("../../build/published_contracts/DepositWithdrawalProxy.json")); const DolomiteMargin_json_1 = __importDefault(require("../../build/published_contracts/DolomiteMargin.json")); const DoubleExponentInterestSetter_json_1 = __importDefault(require("../../build/published_contracts/DoubleExponentInterestSetter.json")); const IERC20Detailed_json_1 = __importDefault(require("../../build/published_contracts/IERC20Detailed.json")); const EventEmitterRegistry_json_1 = __importDefault(require("../../build/published_contracts/EventEmitterRegistry.json")); const Expiry_json_1 = __importDefault(require("../../build/published_contracts/Expiry.json")); const ExpiryProxy_json_1 = __importDefault(require("../../build/published_contracts/ExpiryProxy.json")); const GenericTraderProxyV1_json_1 = __importDefault(require("../../build/published_contracts/GenericTraderProxyV1.json")); const IInterestSetter_json_1 = __importDefault(require("../../build/published_contracts/IInterestSetter.json")); const IIsolationModeUnwrapperTrader_json_1 = __importDefault(require("../../build/published_contracts/IIsolationModeUnwrapperTrader.json")); const IIsolationModeWrapperTrader_json_1 = __importDefault(require("../../build/published_contracts/IIsolationModeWrapperTrader.json")); const LiquidatorAssetRegistry_json_1 = __importDefault(require("../../build/published_contracts/LiquidatorAssetRegistry.json")); const LiquidatorProxyV1_json_1 = __importDefault(require("../../build/published_contracts/LiquidatorProxyV1.json")); const LiquidatorProxyV4WithGenericTrader_json_1 = __importDefault(require("../../build/published_contracts/LiquidatorProxyV4WithGenericTrader.json")); const IMantleGasInfo_json_1 = __importDefault(require("../../build/published_contracts/IMantleGasInfo.json")); const MultiCall_json_1 = __importDefault(require("../../build/published_contracts/MultiCall.json")); const IOracleSentinel_json_1 = __importDefault(require("../../build/published_contracts/IOracleSentinel.json")); const PayableProxy_json_1 = __importDefault(require("../../build/published_contracts/PayableProxy.json")); const PolynomialInterestSetter_json_1 = __importDefault(require("../../build/published_contracts/PolynomialInterestSetter.json")); const IPriceOracle_json_1 = __importDefault(require("../../build/published_contracts/IPriceOracle.json")); const SignedOperationProxy_json_1 = __importDefault(require("../../build/published_contracts/SignedOperationProxy.json")); const TransferProxy_json_1 = __importDefault(require("../../build/published_contracts/TransferProxy.json")); const WETH_json_1 = __importDefault(require("../../build/published_contracts/WETH.json")); const types_1 = require("../types"); const Constants_1 = require("./Constants"); class Contracts { constructor(provider, networkId, web3, options) { this.provider = provider; this.networkId = networkId; this.web3 = web3; this.defaultConfirmations = options.defaultConfirmations; this.autoGasMultiplier = options.autoGasMultiplier || 1.5; this.confirmationType = options.confirmationType || types_1.ConfirmationType.Confirmed; this.defaultGas = options.defaultGas; this.defaultGasPrice = options.defaultGasPrice; this.blockGasLimit = options.blockGasLimit; // Contracts this.aaveCopyCatAltCoinInterestSetter = new this.web3.eth.Contract(AAVECopyCatAltCoinInterestSetter_json_1.default.abi); this.aaveCopyCatStableCoinInterestSetter = new this.web3.eth.Contract(AAVECopyCatStableCoinInterestSetter_json_1.default.abi); this.accountValuesReader = new this.web3.eth.Contract(AccountValuesReader_json_1.default.abi); this.arbitrumGasInfo = new this.web3.eth.Contract(IArbitrumGasInfo_json_1.default.abi); this.arbitrumMultiCall = new this.web3.eth.Contract(ArbitrumMultiCall_json_1.default.abi); this.borrowPositionProxyV1 = new this.web3.eth.Contract(BorrowPositionProxyV1_json_1.default.abi); this.borrowPositionProxyV2 = new this.web3.eth.Contract(BorrowPositionProxyV2_json_1.default.abi); this.chainlinkPriceOracleV1 = new this.web3.eth.Contract(ChainlinkPriceOracleV1_json_1.default.abi); this.depositProxy = new this.web3.eth.Contract(DepositWithdrawalProxy_json_1.default.abi); this.dolomiteMargin = new this.web3.eth.Contract(DolomiteMargin_json_1.default.abi); this.doubleExponentInterestSetter = new this.web3.eth.Contract(DoubleExponentInterestSetter_json_1.default.abi); this.erc20 = new this.web3.eth.Contract(IERC20Detailed_json_1.default.abi); this.expiry = new this.web3.eth.Contract(Expiry_json_1.default.abi); this.expiryProxy = new this.web3.eth.Contract(ExpiryProxy_json_1.default.abi); this.genericTraderProxyV1 = new this.web3.eth.Contract(GenericTraderProxyV1_json_1.default.abi); this.interestSetter = new this.web3.eth.Contract(IInterestSetter_json_1.default.abi); this.liquidatorAssetRegistry = new this.web3.eth.Contract(LiquidatorAssetRegistry_json_1.default.abi); this.liquidatorProxyV1 = new this.web3.eth.Contract(LiquidatorProxyV1_json_1.default.abi); this.liquidatorProxyV4WithGenericTrader = new this.web3.eth.Contract(LiquidatorProxyV4WithGenericTrader_json_1.default.abi); this.eventEmitterRegistry = new this.web3.eth.Contract(EventEmitterRegistry_json_1.default.abi); this.mantleGasInfo = new this.web3.eth.Contract(IMantleGasInfo_json_1.default.abi); this.multiCall = new this.web3.eth.Contract(MultiCall_json_1.default.abi); this.payableProxy = new this.web3.eth.Contract(PayableProxy_json_1.default.abi); this.polynomialInterestSetter = new this.web3.eth.Contract(PolynomialInterestSetter_json_1.default.abi); this.priceOracle = new this.web3.eth.Contract(IPriceOracle_json_1.default.abi); this.signedOperationProxy = new this.web3.eth.Contract(SignedOperationProxy_json_1.default.abi); this.transferProxy = new this.web3.eth.Contract(TransferProxy_json_1.default.abi); this.payableToken = new this.web3.eth.Contract(WETH_json_1.default.abi); this.setProvider(provider, networkId); this.setDefaultAccount(this.web3.eth.defaultAccount); } getNetworkId() { return this.networkId; } getIsolationModeUnwrapper(contractAddress) { const unwrapper = new this.web3.eth.Contract(IIsolationModeUnwrapperTrader_json_1.default.abi, contractAddress); unwrapper.setProvider(this.provider); unwrapper.options.from = this.dolomiteMargin.options.from; return unwrapper; } getIsolationModeWrapper(contractAddress) { const unwrapper = new this.web3.eth.Contract(IIsolationModeWrapperTrader_json_1.default.abi, contractAddress); unwrapper.setProvider(this.provider); unwrapper.options.from = this.dolomiteMargin.options.from; return unwrapper; } getOracleSentinel(contractAddress) { const oracleSentinel = new this.web3.eth.Contract(IOracleSentinel_json_1.default.abi, contractAddress); oracleSentinel.setProvider(this.provider); oracleSentinel.options.from = this.dolomiteMargin.options.from; return oracleSentinel; } getAccountRiskOverrideSetter(contractAddress) { const accountRiskOverrideSetter = new this.web3.eth.Contract(IAccountRiskOverrideSetter_json_1.default.abi, contractAddress); accountRiskOverrideSetter.setProvider(this.provider); accountRiskOverrideSetter.options.from = this.dolomiteMargin.options.from; return accountRiskOverrideSetter; } setProvider(provider, networkId) { this.dolomiteMargin.setProvider(provider); this.provider = provider; this.networkId = networkId; const contracts = [ // contracts { contract: this.aaveCopyCatAltCoinInterestSetter, json: AAVECopyCatAltCoinInterestSetter_json_1.default }, { contract: this.aaveCopyCatStableCoinInterestSetter, json: AAVECopyCatStableCoinInterestSetter_json_1.default }, { contract: this.accountValuesReader, json: AccountValuesReader_json_1.default }, { contract: this.arbitrumGasInfo, json: IArbitrumGasInfo_json_1.default }, { contract: this.arbitrumMultiCall, json: ArbitrumMultiCall_json_1.default }, { contract: this.borrowPositionProxyV1, json: BorrowPositionProxyV1_json_1.default }, { contract: this.borrowPositionProxyV2, json: BorrowPositionProxyV2_json_1.default }, { contract: this.chainlinkPriceOracleV1, json: ChainlinkPriceOracleV1_json_1.default }, { contract: this.depositProxy, json: DepositWithdrawalProxy_json_1.default }, { contract: this.dolomiteMargin, json: DolomiteMargin_json_1.default }, { contract: this.doubleExponentInterestSetter, json: DoubleExponentInterestSetter_json_1.default }, { contract: this.erc20, json: IERC20Detailed_json_1.default }, { contract: this.eventEmitterRegistry, json: EventEmitterRegistry_json_1.default }, { contract: this.expiry, json: Expiry_json_1.default }, { contract: this.expiryProxy, json: ExpiryProxy_json_1.default }, { contract: this.genericTraderProxyV1, json: GenericTraderProxyV1_json_1.default }, { contract: this.interestSetter, json: IInterestSetter_json_1.default }, { contract: this.liquidatorAssetRegistry, json: LiquidatorAssetRegistry_json_1.default }, { contract: this.liquidatorProxyV1, json: LiquidatorProxyV1_json_1.default }, { contract: this.liquidatorProxyV4WithGenericTrader, json: LiquidatorProxyV4WithGenericTrader_json_1.default }, { contract: this.mantleGasInfo, json: IMantleGasInfo_json_1.default }, { contract: this.multiCall, json: MultiCall_json_1.default }, { contract: this.payableProxy, json: PayableProxy_json_1.default }, { contract: this.polynomialInterestSetter, json: PolynomialInterestSetter_json_1.default }, { contract: this.priceOracle, json: IPriceOracle_json_1.default }, { contract: this.signedOperationProxy, json: SignedOperationProxy_json_1.default }, { contract: this.transferProxy, json: TransferProxy_json_1.default }, { contract: this.payableToken, json: WETH_json_1.default }, ]; contracts.forEach(contract => this.setContractProvider(contract.contract, contract.json, provider, networkId, {})); } setDefaultAccount(account) { // Contracts this.aaveCopyCatAltCoinInterestSetter.options.from = account; this.aaveCopyCatStableCoinInterestSetter.options.from = account; this.accountValuesReader.options.from = account; this.arbitrumGasInfo.options.from = account; this.arbitrumMultiCall.options.from = account; this.borrowPositionProxyV1.options.from = account; this.borrowPositionProxyV2.options.from = account; this.chainlinkPriceOracleV1.options.from = account; this.depositProxy.options.from = account; this.dolomiteMargin.options.from = account; this.doubleExponentInterestSetter.options.from = account; this.erc20.options.from = account; this.eventEmitterRegistry.options.from = account; this.expiry.options.from = account; this.expiryProxy.options.from = account; this.genericTraderProxyV1.options.from = account; this.interestSetter.options.from = account; this.liquidatorAssetRegistry.options.from = account; this.liquidatorProxyV1.options.from = account; this.liquidatorProxyV4WithGenericTrader.options.from = account; this.mantleGasInfo.options.from = account; this.multiCall.options.from = account; this.payableProxy.options.from = account; this.polynomialInterestSetter.options.from = account; this.priceOracle.options.from = account; this.signedOperationProxy.options.from = account; this.transferProxy.options.from = account; this.payableToken.options.from = account; } async callContractFunction(method, options = {}) { const { confirmations, confirmationType, autoGasMultiplier, ...txOptions } = options; if (!this.blockGasLimit) { await this.setGasLimit(); } if (!txOptions.gasPrice && this.defaultGasPrice) { txOptions.gasPrice = this.defaultGasPrice; } if (confirmationType === types_1.ConfirmationType.Simulate || !options.gas) { let gasEstimate; if (this.defaultGas && confirmationType !== types_1.ConfirmationType.Simulate) { txOptions.gas = this.defaultGas; } else { try { gasEstimate = await method.estimateGas(txOptions); } catch (error) { const data = method.encodeABI(); const { from, value } = options; const to = method._parent._address; error.transactionData = { from, value, data, to }; throw error; } const multiplier = autoGasMultiplier || this.autoGasMultiplier; const totalGas = Math.floor(gasEstimate * multiplier); txOptions.gas = totalGas < this.blockGasLimit ? totalGas : this.blockGasLimit; } if (confirmationType === types_1.ConfirmationType.Simulate) { return { gasEstimate, gas: Number(txOptions.gas) }; } } if (txOptions.value) { txOptions.value = new bignumber_js_1.default(txOptions.value).toFixed(0); } else { txOptions.value = '0'; } const promi = method.send(txOptions); const OUTCOMES = { INITIAL: 0, RESOLVED: 1, REJECTED: 2, }; let hashOutcome = OUTCOMES.INITIAL; let confirmationOutcome = OUTCOMES.INITIAL; const t = confirmationType !== undefined ? confirmationType : this.confirmationType; if (!Object.values(types_1.ConfirmationType).includes(t)) { throw new Error(`Invalid confirmation type: ${t}`); } let hashPromise; let confirmationPromise; if (t === types_1.ConfirmationType.Hash || t === types_1.ConfirmationType.Both) { hashPromise = new Promise((resolve, reject) => { promi.on('error', (error) => { if (hashOutcome === OUTCOMES.INITIAL) { hashOutcome = OUTCOMES.REJECTED; reject(error); const anyPromi = promi; anyPromi.off(); } }); promi.on('transactionHash', (txHash) => { if (hashOutcome === OUTCOMES.INITIAL) { hashOutcome = OUTCOMES.RESOLVED; resolve(txHash); if (t !== types_1.ConfirmationType.Both) { const anyPromi = promi; anyPromi.off(); } } }); }); } if (t === types_1.ConfirmationType.Confirmed || t === types_1.ConfirmationType.Both) { confirmationPromise = new Promise((resolve, reject) => { promi.on('error', (error) => { if ((t === types_1.ConfirmationType.Confirmed || hashOutcome === OUTCOMES.RESOLVED) && confirmationOutcome === OUTCOMES.INITIAL) { confirmationOutcome = OUTCOMES.REJECTED; reject(error); const anyPromi = promi; anyPromi.off(); } }); const desiredConf = confirmations || this.defaultConfirmations; if (desiredConf) { promi.on('confirmation', (confNumber, receipt) => { if (confNumber >= desiredConf) { if (confirmationOutcome === OUTCOMES.INITIAL) { confirmationOutcome = OUTCOMES.RESOLVED; resolve(receipt); const anyPromi = promi; anyPromi.off(); } } }); } else { promi.on('receipt', (receipt) => { confirmationOutcome = OUTCOMES.RESOLVED; resolve(receipt); const anyPromi = promi; anyPromi.off(); }); } }); } if (t === types_1.ConfirmationType.Hash) { const transactionHash = await hashPromise; return { transactionHash }; } if (t === types_1.ConfirmationType.Confirmed) { return confirmationPromise; } const transactionHash = await hashPromise; return { transactionHash, confirmation: confirmationPromise, }; } async callConstantContractFunction(method, options = {}) { const m2 = method; const { blockNumber, ...txOptions } = options; return m2.call(txOptions, blockNumber); } setContractProvider(contract, contractJson, provider, networkId, overrides) { contract.setProvider(provider); const contractAddress = contractJson.networks[networkId] && contractJson.networks[networkId].address; const overrideAddress = overrides && overrides[networkId]; contract.options.address = overrideAddress || contractAddress; } async setGasLimit() { const block = await this.web3.eth.getBlock('latest'); this.blockGasLimit = block.gasLimit - Constants_1.SUBTRACT_GAS_LIMIT; } } exports.Contracts = Contracts; //# sourceMappingURL=Contracts.js.map