Withdraw

Querying Redeem Amounts

If you would like your platform to display the amount of underlying tokens the user would receive for redeeming cTokens, you may use the convertToAsset() function present in all cToken contracts using the following arguments:

Type
Name
Description

uint256

tokens

The number of cToken shares to theoretically convert to underlying assets

Conversely, if you want to let the user choose how much underlying assets to withdraw, you may use the convertToShares() function:

Type
Name
Description

uint256

amount

The number of underlying tokens to theoretically convert to eTokens

Withdraw from cTokens (cUSDC example)

To withdraw USDC from cUSDC directly, you may use the withdraw(assets) or redeem(shares) functions present in all cToken contracts using the following arguments:

Withdrawing/Redeeming does not force collateral removal.

Type
Name
Description

uint256

assets (if withdraw) shares (if redeem)

The number of assets/shares to withdraw/redeem.

address

recipient

The account who will receive the underlying assets

Below is an example of using withdraw():

// Get the cUSDC contract with signer
const cUSDC = new ethers.Contract(ADDRESSES.EUSDC, ETOKEN_ABI, signer);

// Get the amount of shares to redeem from the user's input amount
// userInputAmount being equal to "1000", representing 1000 USDC
// which should be formatted to 1000 * 10^6
var USDCWithdrawAmountFormatted = ethers.utils.parseUnits(userInputAmount, 6);

// Convert the underlying amount to shares
const cUSDCSharesToRedeem = await cUSDC.convertToShares(USDCWithdrawAmountFormatted);

// Redeem cUSDC for USDC
const underlyingRedeemed = await cUSDC.redeem(depositAmount, recipientAddress);

Below is an implementation of using redeem(), letting the user choose how much underlying to redeem:

// Get the cUSDC contract with signer
const cUSDC = new ethers.Contract(ADDRESSES.CUSDC, CTOKEN_ABI, signer);

// Get the amount of shares to redeem from the user's input amount
// userInputAmount being equal to "1000", representing 1000 USDC
// which should be formatted to 1000 * 10^6
var USDCWithdrawAmountFormatted = ethers.utils.parseUnits(userInputAmount, 6);

// Convert the underlying amount to shares
const cUSDCSharesToRedeem = await cUSDC.convertToShares(USDCWithdrawAmountFormatted);

// Redeem cUSDC for USDC
const underlyingRedeemed = await cUSDC.redeem(depositAmount);

Using redeemFor

If your app requires you to withdraw on users' behalf, you can use the redeemFor() function in the cToken contract using the following function arguments:

Make sure your platforms' address is approved as a delegate. Visit Plugin Integration for more information.

Type
Parameter
Description

uint256

assets (if withdrawFor) shares (if redeemFor)

The number of assets/shares to withdraw/redeem.

address

recipient

The account that should receive the underlying assets.

Withdrawing Collateral

When withdrawing from collateralized cTokens, you have a few choices of functions to call depending on the needs of your platform.

  • redeemCollateral() - If you would like to redeem cTokens that are currently being used as collateral.

  • redeemCollateralFor() - If you would like to redeem cTokens that are currently being used as collateral, on behalf of a user. Requires the user to delegate your platform.

ERC20 approval is not required for redeeming cTokens.

Last updated