Depositing or Withdrawing

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 {
    enum EventFlag {
        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)
     */
    function depositWei(
        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)
     */
    function depositPayable(
        uint256 _isolationModeMarketId,
        uint256 _toAccountNumber,
        EventFlag _eventFlag
    ) external payable;
    
    /**
     * @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)
     */
    function depositPar(
        uint256 _isolationModeMarketId,
        uint256 _toAccountNumber,
        uint256 _marketId, 
        uint256 _amountPar,
        EventFlag _eventFlag
    ) external;
}

Examples

Depositing 1 WETH into the default account number (account number = 0):

WETH.safeApprove(address(DepositWithdrawalRouter), 1 ether);

uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(address(WETH));
DepositWithdrawalRouter.depositWei(
    /* isolationModeMarketId = */ 0,
    /* toAccountNumber = */ 0,
    marketId,
    /* amountWei = */ 1 ether,
    EventFlag.None
);    

Depositing 1 WETH into account number 123 of a GLP isolation mode vault via deposit payable:

address dfsGlp = 0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;
uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp);
DepositWithdrawalRouter.depositPayable{ value: 1 ether }(
    /* isolationModeMarketId = */ marketId,
    /* toAccountNumber = */ 123,
    EventFlag.None
);    

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), 1 ether);

// The token address will be the Dolomite DFSGLP factory address, NOT the glp address
address dfsGlp = 0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;
uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp); 
DepositWithdrawalRouter.depositWei(
    /* isolationModeMarketId = */ marketId,
    /* toAccountNumber = */ 0,
    marketId,
    /* amountWei = */ 1 ether,
    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 id
address dfsGlp = 0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;
uint256 glpMarketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp);

// marketId will be the USDC market id
uint256 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.

library AccountBalanceLib {
    /// Checks that either BOTH, FROM, or TO accounts do not have negative balances
    enum BalanceCheckFlag {
        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
 */
function withdrawWei(
    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
 */
function withdrawPayable(
    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
 */
function withdrawPar(
    uint256 _isolationModeMarketId,
    uint256 _fromAccountNumber,
    uint256 _marketId,
    uint256 _amountPar,
    AccountBalanceLib.BalanceCheckFlag _balanceCheckFlag
) external;

Examples

Withdrawing 1 WETH from the default account number (account number = 0):

uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(address(WETH));
DepositWithdrawalRouter.withdrawWei(
    /* isolationModeMarketId = */ 0,
    /* fromAccountNumber = */ 0,
    /* marketId = */ marketId,
    /* amountWei = */ 1 ether,
    EventFlag.None
);  

Withdrawing 1 WETH as ETH from the default account number (account number = 0):

DepositWithdrawalRouter.withdrawPayable(
    /* isolationModeMarketId = */ 0,
    /* fromAccountNumber = */ 0,
    /* amountWei = */ 1 ether,
    EventFlag.None
); 

Withdrawing 1 GLP, an isolation mode asset, from the default account:

// The token address will be the Dolomite DFSGLP factory address, NOT the glp address
address dfsGlp = 0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;
uint256 marketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp); 
DepositWithdrawalRouter.withdrawWei(
    /* fromAccountNumber = */ 0,
    /* marketId = */ marketId,
    /* amountWei = */ 1 ether,
    EventFlag.None
);

Withdrawing 1 WETH from account number 123 of a GLP isolation mode token vault:

// The token address will be the Dolomite DFSGLP factory address, NOT the glp address
address dfsGlp = 0x34DF4E8062A8C8Ae97E3382B452bd7BF60542698;
uint256 glpMarketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(dfsGlp);
uint256 wethMarketId = DOLOMITE_MARGIN.getMarketIdByTokenAddress(address(WETH));
DepositWithdrawalRouter.withdrawWei(
    /* isolationModeMarketId = */ glpMarketId
    /* fromAccountNumber = */ 123,
    /* marketId = */ wethMarketId,
    /* amountWei = */ 1 ether,
    EventFlag.From
);

Last updated