Skip to content
LogoLogo

Withdraw Exact Earn Assets

earn.withdrawExact

Withdraws an exact asset amount while limiting the Earn shares burned.

Usage

import {  } from './viem.config'
 
const  = await ..({
  : 40_000_000n,
  : 50,
  : '0x0000000000000000000000000000000000000001',
})
{
// assetAmount: 40_000_000n,// caller: '0x...', // receipt: { ... }, // recipient: '0x...', // shareAmount: 40_100_000n, // }

When only slippageBps is provided, the action gets a live earn.getWithdrawQuote and derives the maximum Earn share input. The transaction approves and burns the required shares atomically.

Set an Exact Maximum

Use shareAmountMax when you already know the most Earn shares you will burn.

import {  } from './viem.config'
 
const  = await ..({
  : 40_000_000n,
  : 40_250_000n,
  : '0x0000000000000000000000000000000000000001',
})
{ assetAmount: 40_000_000n, shareAmount: 40_100_000n, ... }

Asynchronous Usage

earn.withdrawExactSync waits for inclusion and returns the decoded withdrawal event. Use earn.withdrawExact to return the transaction hash immediately.

import {  } from './viem.config'
 
const  = await ..({
  : 40_000_000n,
  : 40_250_000n,
  : '0x0000000000000000000000000000000000000001',
})
0x1234...abcd

Recipes

Skim Yield Above a Target Float

Combine earn.getPosition with an exact withdrawal to pull only the value above a working balance target.

import {  } from './viem.config'
 
const  = 100_000_000_000n
const  = '0x0000000000000000000000000000000000000001'
 
const {  } = await ..({  })
 
if ( > ) {
  const  = await ..({
    :  - ,
    : 50,
    ,
  })
{ assetAmount: 1_250_000_000n, shareAmount: 1_243_800_000n, ... }
}

value already includes accrued yield and pending fees, so the skim keeps the deployed position at the target.

Pay an Exact Invoice from the Vault

Pass recipient with a formatted assetAmount to settle an invoice for its exact amount directly from vault holdings.

import {  } from './viem.config'
 
const  = await ..({
  : { : 6, : '1249.99' },
  : '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
  : 50,
  : '0x0000000000000000000000000000000000000001',
})
{ assetAmount: 1_249_990_000n, recipient: '0x742d...bEbb', shareAmount: 1_244_000_000n, ... }

The counterparty receives the exact assetAmount; slippage only affects the shares burned.

Return Value

type ReturnValue = {
  /** Exact assets received. */
  assetAmount: bigint
  /** Withdrawing caller. */
  caller: Address
  /** Transaction receipt. */
  receipt: TransactionReceipt
  /** Asset recipient. */
  recipient: Address
  /** Earn shares burned. */
  shareAmount: bigint
}

The asynchronous action returns the transaction hash instead.

Parameters

assetAmount

  • Type: bigint | { decimals?: number | undefined; formatted: string }

Exact assets to receive. A bigint uses base units; a formatted value uses the vault asset's decimals unless decimals is provided.

recipient (optional)

  • Type: Address
  • Default: account.address

Asset recipient.

shareAmount (optional)

  • Type: bigint

Quoted Earn share input. Provide it with slippageBps to avoid a live quote.

shareAmountMax (optional)

  • Type: bigint

Maximum Earn shares to burn. It cannot be combined with shareAmount or slippageBps.

slippageBps (optional)

  • Type: number

Slippage headroom above the provided or live quote, in basis points. For example, 50 means 0.5%.

vault

  • Type: Address

Vault address.

account (optional)

  • Type: Account | Address

Account that will be used to send the transaction.

feePayer (optional)

  • Type: Account | boolean

Fee payer for the transaction (TIP-1 gas sponsorship).

Pass true to defer the fee token to an external fee payer (e.g. a relay), or a local Account to co-sign the transaction as the fee payer.

feeToken (optional)

  • Type: Address | bigint

Fee token for the transaction.

Can be an unpaused USD-denominated TIP-20 token address or ID.

gas (optional)

  • Type: bigint

Gas limit for the transaction.

keyAuthorization (optional)

  • Type: KeyAuthorization

Signed key authorization to include with the transaction, authorizing an access key to act for the sending account.

maxFeePerGas (optional)

  • Type: bigint

Max fee per gas for the transaction.

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas for the transaction.

nonce (optional)

  • Type: number

Nonce for the transaction.

nonceKey (optional)

  • Type: 'expiring' | 'random' | bigint

Nonce key for the transaction (TIP-1009 2D nonces).

Use 'expiring' to select an expiring nonce, which enables concurrent transaction submission without nonce ordering. Use 'random' to select a random key.

throwOnReceiptRevert (optional)

  • Type: boolean
  • Default: true

Whether a Sync action throws when the receipt reports a revert.

validAfter (optional)

  • Type: number

Unix timestamp after which the transaction can be included.

validBefore (optional)

  • Type: number

Unix timestamp before which the transaction must be included.