import { StaticTokenListResolutionStrategy, TokenInfo } from "@solana/spl-token-registry";import VaultImpl from '@meteora-ag/vault-sdk';import { BN } from 'bn.js';
As this demo will be done using a devnet wallet, we can use the Solana airdrop function to deposit some initial SOL into the wallet.Define the SOL airdrop function as shown below. We have already included this function in the util.ts file for easy access and use:
Retrieve the token info required to initialize the vault instance. We use the SOL token as an example here:
Copy
Ask AI
// Retrieve the token info for the vault (example: SOL)const tokenMap = new StaticTokenListResolutionStrategy().resolve();const SOL_TOKEN_INFO = tokenMap.find(token => token.symbol === 'SOL') as TokenInfo;
Set up the vault instance using the connection and the token info:
If you are an affiliate partner, you have to pass in an additional parameter - affiliateId when setting up the vault instance. The affiliateId is the partner wallet address that was used to set up the affiliate relationship with the Meteora Dynamic Yield layer:
Copy
Ask AI
// Getting a Vault Implementation instance (SOL)const vaultImpl: VaultImpl = await VaultImpl.create( devnetConnection, SOL_TOKEN_INFO, { cluster: 'devnet', // Replace with your own Partner ID! affiliateId: new PublicKey('7236FoaWTXJyzbfFPZcrzg3tBpPhGiTgXsGWvjwrYfiF') });
Retrieve on-chain data from the vault and off-chain APY data from API. Key info includes total amount currently unlocked for user withdrawals, virtual price and the various strategies connected to the vault. The code for this can be found in getVaultDetails.ts:
Copy
Ask AI
// Get on-chain data from the vault and off-chain data from the apiexport const getVaultDetails = async (vaultImpl: VaultImpl) => { //Get the total unlocked amount in vault that is withdrawable by users const vaultUnlockedAmount = (await vaultImpl.getWithdrawableAmount()).toNumber(); //Calculate virtual price using the vault's unlocked amount and lp supply const virtualPrice = (vaultUnlockedAmount / vaultImpl.lpSupply.toNumber()) || 0; // Get the off-chain data from API const URL = KEEPER_URL['devnet']; const vaultStateAPI: VaultStateAPI = await (await fetch(`${URL}/vault_state/${SOL_TOKEN_INFO.address}`)).json(); const totalAllocation = vaultStateAPI.strategies.reduce((acc, item) => acc + item.liquidity, vaultStateAPI.token_amount) return { lpSupply: (await vaultImpl.getVaultSupply()).toString(), withdrawableAmount: vaultUnlockedAmount, virtualPrice, usd_rate: vaultStateAPI.usd_rate, closest_apy: vaultStateAPI.closest_apy, // 1 hour average APY average_apy: vaultStateAPI.average_apy, // 24 hour average APY long_apy: vaultStateAPI.long_apy, // 7 day average APY earned_amount: vaultStateAPI.earned_amount, // total fees earned by vault virtual_price: vaultStateAPI.virtual_price, total_amount: vaultStateAPI.total_amount, total_amount_with_profit: vaultStateAPI.total_amount_with_profit, token_amount: vaultStateAPI.token_amount, fee_amount: vaultStateAPI.fee_amount, lp_supply: vaultStateAPI.lp_supply, strategyAllocation: vaultStateAPI.strategies .map(item => ({ name: item.strategy_name, liquidity: item.liquidity, allocation: ((item.liquidity / totalAllocation) * 100).toFixed(0), maxAllocation: item.max_allocation, })) .concat({ name: 'Vault Reserves', liquidity: vaultStateAPI.token_amount, allocation: ((vaultStateAPI.token_amount / totalAllocation) * 100).toFixed(0), maxAllocation: 0, }) .sort((a, b) => b.liquidity - a.liquidity), }}
You can trigger the function and get the vault details via the following:
The steps below will allow you to withdraw tokens from the vault by specifying the destination wallet and the amount you want to withdraw from the vault:
Copy
Ask AI
// Withdraw from the vaultconst withdrawAmount = 0.05;const withdrawTx = await vault.withdraw(mockWallet.publicKey, new BN(withdrawAmount * 10 ** SOL_TOKEN_INFO.decimals)); // 0.05 SOLconst withdrawResult = await provider.sendAndConfirm(withdrawTx); // Transaction hash