Withdraw

This guide will go over withdawB and closePosition.

The full example can be found on github here.

Setup

//import { Drip, DripVault, findVaultPubkey, Network, VaultAccount } from '@dcaf-labs/drip-sdk';
import { Address, AnchorProvider, BN } from '@project-serum/anchor';
import NodeWallet from '@project-serum/anchor/dist/cjs/nodewallet';
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';

async function main() {
    // export your wallet as such
    // export EXAMPLE_WALLET="[92,116,...,245,129]"
    const keypairData = process.env.EXAMPLE_WALLET;
    const programID = Uint8Array.from(JSON.parse(keypairData));
    const walletKeypair = Keypair.fromSecretKey(programID);
    console.log('connected wallet', walletKeypair.publicKey.toString());
    
    // Setup
    const programId = 'dripTrkvSyQKvkyWg7oi4jmeEGMA5scSYowHArJ9Vwk';
    const network = Network.Devnet;
    const clientEnv = ClientEnv.Production;
    const provider = new AnchorProvider(
        new Connection('https://api.devnet.solana.com', 'confirmed'),
        new NodeWallet(walletKeypair),
        AnchorProvider.defaultOptions()
    );
    const drip = Drip.fromNetworkClient(network, provider, programId, clientEnv);
    
    // Devnet Drip USDT
    const tokenA = new PublicKey('H9gBUJs5Kc5zyiKRTzZcYom4Hpj9VPHLy4VzExTVPgxa');
    
    // Given a tokenA, get valid tokenBs
    const tokenBs = await drip.config.getAllTokenBs(tokenA);
    // For the example's sake, lets pick the first token available
    const tokenB = tokenBs[Object.keys(tokenBs)[0]].mint;
    console.log('tokenA', tokenA.toString(), 'tokenB', tokenB.toString());
    
    const vaultProtoConfigs = await drip.config.getSupportedVaultProtoConfigsForPair(
        tokenA,
        tokenB
    );
    // For the example's sake, lets pick the first proto config
    const vaultProtoConfig = vaultProtoConfigs[0];
    console.log(
        'vaultProtoConfig',
        vaultProtoConfig.pubkey.toString(),
        'granularity',
        vaultProtoConfig.granularity.toString()
    );
    
    const vaultPubkey = findVaultPubkey(drip.programId, {
        protoConfig: vaultProtoConfig.pubkey,
        tokenAMint: tokenA,
        tokenBMint: tokenB
    });
    console.log('vault', vaultPubkey.toString());
    
    const dripVault = await drip.getVault(vaultPubkey);
    // Any function from the deposit guide will work here 
    // the specifc one is not relevant, therefore it is not shown
    const positionPubkey = await deposit(dripVault);
    const dripPosition = await drip.getPosition(positionPubkey);
    // ...
}

Withdraw

This function will withdraw all the accrued destination token (token B) for a position. Under the hood it calls instruction withdrawB. Note, this function will fail if there is no destination token accrued.

Close Position

This function will withdraw all accrued destination token (token B) for a position along with all remaining source token (token A), burn the position nft token, and close the position nft token account (refunding the lamports to the user).

Last updated

Was this helpful?