You will need to initialize DolomiteMargin using a Web3 provider / Web3 node endpoint and Network.
import { DolomiteMargin, Networks } from'@dolomite-exchange/dolomite-margin';// --- Initialize with Web3 provider ---constdolomiteMargin=newDolomiteMargin( provider,// Valid web3 providerNetworks.ARBITRtUM, { defaultAccount:'0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',// Optional accounts: [ { address:'0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',// Optional privateKey:'0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d', }, ],// Optional: loading in an account for signing transactions },// Optional);// --- OR Initialize with Web3 Provider node endpoint ---constdolomiteMargin=newDolomiteMargin('https://arbitrum-mainnet.infura.io/v3/YOUR-PROJECT-ID',Networks.ARBITRUM, { defaultAccount:'0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',// Optional - but needed if using Infura accounts: [ { address:'0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',// Optional privateKey:'0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d', }, ],// Optional: loading in an account for signing transactions },// Optional);
Standard Actions
DolomiteMargin exposes a number of "standard" actions for interacting with the protocol. These are a subset of what is possible with Operations, but are simpler to use.
Deposit
Deposit funds to DolomiteMargin
import { MarketId, BigNumber } from'@dolomite-exchange/dolomite-margin';// Deposits a certain amount of tokens for some asset.// By default resolves when transaction is received by the node - not when minedconstresult=awaitdolomiteMargin.standardActions.deposit({ accountOwner:'0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',// Your address marketId:MarketId.ETH,// Base units of the token, so 1e18 = 1 ETH// NOTE: USDC has 6 decimal places, so 1e6 = 1 USDC amount:newBigNumber('1e18'),});
MarketId.ETH will send ETH whereas MarketId.WETH will send WETH. Both are the same market on the protocol
For all markets except MarketId.ETH, you will first need to set allowance on that token. See Tokens
Withdraw
Withdraw funds from DolomiteMargin
import { MarketId, BigNumber } from'@dolomite-exchange/dolomite-margin';// Withdraws a certain amount of tokens for some asset.// By default resolves when transaction is received by the node - not when minedconstresult=awaitdolomiteMargin.standardActions.withdraw({ accountOwner:'0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',// Your address marketId:MarketId.ETH,// Base units of the token, so 1e18 = 1 ETH// NOTE: USDC has 6 decimal places, so 1e6 = 1 USDC amount:newBigNumber('1e18'),});// Withdraws all of your tokens for some asset.// By default resolves when transaction is received by the node - not when minedconstresult=awaitdolomiteMargin.standardActions.withdrawToZero({ accountOwner:'0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',// Your address marketId:MarketId.ETH,});
MarketId.ETH will withdraw as ETH whereas MarketId.WETH will withdraw as WETH. Both are the same market on the protocol
Operations
The main way to interact with DolomiteMargin is through Operations. See Operations.
DolomiteMargin also provides a Payable Proxy contract that will automatically wrap and unwrap ETH <-> WETH, so that users can interact with DolomiteMargin using only ETH. You can use it by:
See AccountOperation for the full list of Actions available to add to an Operation.
Commit
After Actions have been added to the operation, it can be committed. This is what sends the transaction to the blockchain to execute the Operation on the protocol.
DolomiteMargin provides a number of read-only getter functions which read information off the smart contracts on the blockchain. You can find them here.
DolomiteMargin provides helper functions to help with interacting with ERC20 tokens. You can find them all here.
Example of setting DAI token allowance on DolomiteMargin:
awaitdolomiteMargin.token.setMaximumDolomiteMarginAllowance('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359',// DAI Contract Address'0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5',// My Address { from:'0x52bc44d5378309ee2abf1539bf71de1b7d7be3b5' },// My Address);