depositToYieldVault
Deposits assets into an Earn vault, then signs and sends the transaction for you. You pass avaultId and a plain amount like '10.5', and Dynamic looks up the vault’s address and decimal places for you, so you never have to handle those yourself.
For EVM vaults, a wallet needs to give the vault contract permission to move its tokens before its first deposit. This is called an approval (or allowance). The contract that receives the deposit, and therefore the one the approval authorizes, depends on the vault’s provider, and depositToYieldVault resolves it for you. If the wallet hasn’t approved enough yet, depositToYieldVault signs and sends that approval automatically, before the deposit. onStepChange tells you which of the two is happening so you can reflect it in your UI.
Solana vaults have no approval step. A Solana deposit is instead an ordered sequence of up to three transactions, such as the deposit followed by a transaction that stakes the new shares in the vault’s farm. depositToYieldVault signs, sends, and confirms each transaction before it starts the next, and onTransactionStepChange reports progress through the sequence.
Usage
Parameters
Returns
Promise<{ transactionHash: string }> - The hash of the deposit transaction. For a Solana vault, this is the signature of the last transaction in the sequence.
Step Lifecycle
Waiting for confirmation
For an EVM vault,transactionHash is returned as soon as the deposit is broadcast to the network, and it can still fail or revert after that. Wait for a receipt before telling the user their deposit succeeded:
Solana vaults
For a Solana vault,depositToYieldVault polls each transaction until it reaches the confirmed commitment level before it starts the next one, and it resolves only after the last transaction has confirmed. A resolved call therefore means every transaction in the sequence landed, although a confirmed transaction is not yet finalized. You do not need to wait for a receipt the way you do for an EVM vault.
All of the transactions in the sequence are built against one blockhash, which stays valid only briefly. If the user takes too long to approve, a later transaction expires before it can be signed. When a transaction fails, expires, or is rejected by the user, depositToYieldVault stops and rejects with a SolanaYieldStepError. Every transaction before the failed one has already confirmed, so the deposit may be partially applied. Read the position with getYieldPosition before you retry.
SolanaYieldStepError is exported from @dynamic-labs-sdk/solana and has these properties:
Examples
With progress UI
This example uses React; the JavaScript SDK is framework-agnostic and can be used with any frontend or in Node.
Solana vault with transaction progress
Supported Chains
depositToYieldVault supports EVM wallet accounts for EVM vaults and Solana wallet accounts for Solana vaults. Pass a wallet account from the same chain family as the vault’s chainName: the API rejects an address from the other family with an HTTP 400 error. Wallet accounts on other chains are not supported, and a wallet provider that does not implement the underlying executeYieldTransaction method throws a WalletProviderMethodUnavailableError.
React
useDepositToYieldVault accepts the same parameters, including onTransactionStepChange. It invalidates useGetYieldPosition on success, so the position refetches automatically. That refetch can still briefly show the old balance, since the deposit transaction may not be mined yet (see Waiting for confirmation).
Related
withdrawFromYieldVault- Withdraw from a vaultgetYieldPosition- Read the resulting positionlistYieldVaults- Find a vault to deposit into