UNPKG

@dolomite-exchange/dolomite-margin

Version:

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

136 lines (131 loc) 7.44 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.DolomiteMargin = void 0; const web3_1 = __importDefault(require("web3")); const Contracts_1 = require("./lib/Contracts"); const Interest_1 = require("./lib/Interest"); const Admin_1 = require("./modules/Admin"); const ArbitrumGasInfo_1 = require("./modules/ArbitrumGasInfo"); const BorrowPositionProxyV1_1 = require("./modules/BorrowPositionProxyV1"); const BorrowPositionProxyV2_1 = require("./modules/BorrowPositionProxyV2"); const DepositProxy_1 = require("./modules/DepositProxy"); const Expiry_1 = require("./modules/Expiry"); const GenericTraderProxyV1_1 = require("./modules/GenericTraderProxyV1"); const Getters_1 = require("./modules/Getters"); const LiquidatorAssetRegistry_1 = require("./modules/LiquidatorAssetRegistry"); const LiquidatorProxyV1_1 = require("./modules/LiquidatorProxyV1"); const LiquidatorProxyV4WithGenericTrader_1 = require("./modules/LiquidatorProxyV4WithGenericTrader"); const IsolationModeUnwrapper_1 = require("./modules/IsolationModeUnwrapper"); const Logs_1 = require("./modules/Logs"); const MultiCall_1 = require("./modules/MultiCall"); const Operation_1 = require("./modules/operate/Operation"); const ChainlinkPriceOracleV1_1 = require("./modules/oracles/ChainlinkPriceOracleV1"); const OrderMapper_1 = require("./modules/OrderMapper"); const Permissions_1 = require("./modules/Permissions"); const SignedOperations_1 = require("./modules/SignedOperations"); const StandardActions_1 = require("./modules/StandardActions"); const Token_1 = require("./modules/Token"); const TransferProxy_1 = require("./modules/TransferProxy"); const WalletLogin_1 = require("./modules/WalletLogin"); const PayableToken_1 = require("./modules/PayableToken"); const types_1 = require("./types"); const IsolationModeWrapper_1 = require("./modules/IsolationModeWrapper"); const ExpiryProxy_1 = require("./modules/ExpiryProxy"); const MantleGasInfo_1 = require("./modules/MantleGasInfo"); class DolomiteMargin { constructor(provider, networkId = types_1.Networks.ARBITRUM_ONE, options = {}) { let realProvider; if (typeof provider === 'string') { realProvider = new web3_1.default.providers.HttpProvider(provider, options.ethereumNodeTimeout || 10000); } else { realProvider = provider; } this.web3 = new web3_1.default(realProvider); if (options.defaultAccount) { this.web3.eth.defaultAccount = options.defaultAccount; } this.contracts = this.createContractsModule(realProvider, networkId, this.web3, options); this.admin = new Admin_1.Admin(this.contracts); this.arbitrumGasInfo = networkId === types_1.Networks.ARBITRUM_ONE ? new ArbitrumGasInfo_1.ArbitrumGasInfo(this.contracts) : undefined; this.borrowPositionProxyV1 = new BorrowPositionProxyV1_1.BorrowPositionProxyV1(this.contracts); this.borrowPositionProxyV2 = new BorrowPositionProxyV2_1.BorrowPositionProxyV2(this.contracts); this.chainlinkPriceOracle = new ChainlinkPriceOracleV1_1.ChainlinkPriceOracleV1(this.contracts); this.depositWithdrawalProxy = new DepositProxy_1.DepositProxy(this.contracts); this.expiry = new Expiry_1.Expiry(this.contracts); this.expiryProxy = new ExpiryProxy_1.ExpiryProxy(this.contracts); this.genericTraderProxyV1 = new GenericTraderProxyV1_1.GenericTraderProxyV1(this.contracts); this.getters = new Getters_1.Getters(this.contracts); this.interest = new Interest_1.Interest(networkId); this.liquidatorAssetRegistry = new LiquidatorAssetRegistry_1.LiquidatorAssetRegistry(this.contracts); this.liquidatorProxyV1 = new LiquidatorProxyV1_1.LiquidatorProxyV1(this.contracts); this.liquidatorProxyV4WithGenericTrader = new LiquidatorProxyV4WithGenericTrader_1.LiquidatorProxyV4WithGenericTrader(this.contracts); this.logs = new Logs_1.Logs(this.contracts, this.web3); this.mantleGasInfo = networkId === types_1.Networks.MANTLE ? new MantleGasInfo_1.MantleGasInfo(this.contracts) : undefined; this.multiCall = new MultiCall_1.MultiCall(this.contracts); this.networkId = networkId; this.operation = new Operation_1.Operation(this.contracts, new OrderMapper_1.OrderMapper(this.contracts), networkId); this.permissions = new Permissions_1.Permissions(this.contracts); this.signedOperations = new SignedOperations_1.SignedOperations(this.contracts, this.web3, networkId); this.standardActions = new StandardActions_1.StandardActions(this.operation, this.contracts); this.token = new Token_1.Token(this.contracts); this.transferProxy = new TransferProxy_1.TransferProxy(this.contracts); this.walletLogin = new WalletLogin_1.WalletLogin(this.web3, networkId); this.payableToken = new PayableToken_1.PayableToken(this.contracts, this.token); if (options.accounts) { options.accounts.forEach(a => this.loadAccount(a)); } } /** * @return The address of the main DolomiteMargin smart contract */ get address() { return this.contracts.dolomiteMargin.options.address; } setProvider(provider, networkId) { this.web3.setProvider(provider); this.contracts.setProvider(provider, networkId); this.interest.setNetworkId(networkId); } setDefaultAccount(account) { this.web3.eth.defaultAccount = account; this.contracts.setDefaultAccount(account); } getDefaultAccount() { return this.web3.eth.defaultAccount; } // ============ Helper Functions ============ loadAccount(account) { const newAccount = this.web3.eth.accounts.wallet.add(account.privateKey); if (!newAccount || (account.address && account.address.toLowerCase() !== newAccount.address.toLowerCase())) { throw new Error(`Loaded account address mismatch. Expected ${account.address}, got ${newAccount ? newAccount.address : null}`); } } getIsolationModeUnwrapper(unwrapperAddress) { return new IsolationModeUnwrapper_1.IsolationModeUnwrapper(this.contracts, this.contracts.getIsolationModeUnwrapper(unwrapperAddress)); } getIsolationModeWrapper(unwrapperAddress) { return new IsolationModeWrapper_1.IsolationModeWrapper(this.contracts, this.contracts.getIsolationModeWrapper(unwrapperAddress)); } createContractsModule(provider, networkId, web3, options) { return new Contracts_1.Contracts(provider, networkId, web3, options); } } exports.DolomiteMargin = DolomiteMargin; //# sourceMappingURL=DolomiteMargin.js.map