Depositing or withdrawing into Dolomite is easy with the DepositWithdrawalRouter. You can find the address on the Smart Contract Addresses pages.
Depositing
Depositing an asset requires an approval for the corresponding amount on the DepositWithdrawalRouter
interface IDepositWithdrawalRouter {enumEventFlag { None, Borrow }/** * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _toAccountNumber The account number to deposit into * @param _marketId The ID of the market being deposited * @param _amountWei The amount in Wei to deposit. Use type(uint256).max to deposit * mgs.sender's entire balance * @param _eventFlag Flag indicating if this deposit should emit * special events (e.g. opening a borrow position) */functiondepositWei(uint256_isolationModeMarketId,uint256_toAccountNumber,uint256_marketId,uint256_amountWei,EventFlag_eventFlag ) external;/** * @notice Deposits native ETH by wrapping it to WETH first * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _toAccountNumber The account number to deposit the wrapped ETH into * @param _eventFlag Flag indicating if this deposit should emit special * events (e.g. opening a borrow position) */functiondepositPayable(uint256_isolationModeMarketId,uint256_toAccountNumber,EventFlag_eventFlag ) externalpayable;/** * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _toAccountNumber The account number to deposit into * @param _marketId The ID of the market being deposited * @param _amountPar The amount in Par units to deposit * @param _eventFlag Flag indicating if this deposit should emit special * events (e.g. opening a borrow position) */functiondepositPar(uint256_isolationModeMarketId,uint256_toAccountNumber,uint256_marketId,uint256_amountPar,EventFlag_eventFlag ) external;}
Examples
Depositing 1 WETH into the default account number (account number = 0):
Depositing 1 GLP, an isolation mode asset, into the default account number:
For isolation mode vaults, only the underlying token can be deposited into account number 0. All other assets must be deposited into account numbers ≥ 100.
GLP.safeApprove(address(DepositWithdrawalRouter),1ether);// The token address will be the Dolomite DFSGLP factory address, NOT the glp addressaddress dfsGlp =0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp); DepositWithdrawalRouter.depositWei(/* isolationModeMarketId = */ marketId,/* toAccountNumber = */0, marketId,/* amountWei = */1ether, EventFlag.None);
Depositing 100 USDC into account number 123 for a GLP isolation mode token vault:
USDC.safeApprove(address(DepositWithdrawalRouter),100e6);// isolationModeMarketId will be the GLP market idaddress dfsGlp =0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;uint256 glpMarketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp);// marketId will be the USDC market iduint256 usdcMarketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(address(USDC));DepositWithdrawalRouter.depositWei(/* isolationModeMarketId = */ glpMarketId,/* toAccountNumber = */123,/* marketId = */ usdcMarketId,/* amountWei = */100e6, EventFlag.None);
Withdrawing
For isolation mode, the underlying asset can only be withdrawn from account number 0 (default account number). All other assets can only be deposited/withdrawn from account numbers ≥ 100.
Withdrawing more than your supplied balance (to a negative number) will put you in a borrowed state. You can use the AccountBalanceLib.BalanceCheckFlag to protect your balance from going negative.
Your withdrawal may fail if you attempt to withdraw more liquidity than there is available in Dolomite. If this happens, you must wait until more liquidity becomes available or you can attempt to swap your virtual balance to another asset that is withdrawable.
libraryAccountBalanceLib {/// Checks that either BOTH, FROM, or TO accounts do not have negative balancesenumBalanceCheckFlag { Both, From, To, None }}/** * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _fromAccountNumber The account number to withdraw from * @param _marketId The ID of the market being withdrawn * @param _amountWei The amount in Wei to withdraw. Use type(uint256).max * to withdraw entire balance * @param _balanceCheckFlag Flag indicating how to validate account balances after * withdrawal */functionwithdrawWei(uint256_isolationModeMarketId,uint256_fromAccountNumber,uint256_marketId,uint256_amountWei,AccountBalanceLib.BalanceCheckFlag_balanceCheckFlag) external;/** * @notice Withdraws WETH and unwraps it to native ETH * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _fromAccountNumber The account number to withdraw from * @param _amountWei The amount of WETH in Wei to withdraw and unwrap * @param _balanceCheckFlag Flag indicating how to validate account balances after * withdrawal */functionwithdrawPayable(uint256_isolationModeMarketId,uint256_fromAccountNumber,uint256_amountWei,AccountBalanceLib.BalanceCheckFlag_balanceCheckFlag) external;/** * @param _isolationModeMarketId The market ID of the isolation mode token vault * (0 if not using isolation mode) * @param _fromAccountNumber The account number to withdraw from * @param _marketId The ID of the market being withdrawn * @param _amountPar The amount in Par units to withdraw * @param _balanceCheckFlag Flag indicating how to validate account balances after * withdrawal */functionwithdrawPar(uint256_isolationModeMarketId,uint256_fromAccountNumber,uint256_marketId,uint256_amountPar,AccountBalanceLib.BalanceCheckFlag_balanceCheckFlag) external;
Examples
Withdrawing 1 WETH from the default account number (account number = 0):