Earn Withdraw to Privy
1
Import Dependencies
Import the required packages for Privy client, Solana RPC communication, and Kamino SDK operations.2
Configure Privy and Initialize Client
Set up your Privy credentials and initialize the Privy client and RPC connection.3
Get Wallet and Load the Vault
Retrieve the Privy wallet, then load the vault’s state, reserves, and farm state once. The withdraw instructions need all three.A noop signer is used to build instructions without requiring the actual private key. Privy will handle signing later.
4
Check User Shares
Query the wallet’s vault shares to determine how much can be withdrawn.The total shares represent the user’s position in the vault. Both staked and unstaked shares are included in the withdrawal.
5
Build Withdrawal Instructions
Generate the unstake and withdraw instructions for all of the wallet’s shares.Staked shares are unstaked from the vault’s farm in the same transaction, before they are withdrawn. Without
unstakeFromFarmIfNeededIxs, withdrawing staked shares fails with CannotWithdrawZeroShares. The last argument closes the wallet’s farm account after a full withdrawal and returns its rent.6
7
Build Transaction Message
Fetch the latest blockhash and construct the compressed withdrawal transaction message.8
Sign Transaction with Privy
Compile the transaction, serialize it, and sign using Privy’s wallet API.Privy handles the signing securely using the embedded wallet. The private key never leaves Privy’s infrastructure.
9
Send and Confirm Transaction
Decode the transaction Privy signed, then send it and wait for confirmation.Privy returns the signed transaction as a base64 string. Decoding it and adding the blockhash lifetime from the compiled transaction lets
sendAndConfirmTransactionFactory send it and wait until it is confirmed or its blockhash expires.10
Check USDC Balance
Query the wallet’s USDC balance using Privy’s balance API.Your withdrawal is complete! The user’s vault shares have been redeemed for USDC, and the balance is now available in their Privy wallet.