# Withdraw

The full example can be found on [github here](https://github.com/dcaf-labs/drip-sdk-example/blob/main/withdraw.ts).

### Setup

```typescript
//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.

```typescript
const res = await dripPosition.withdrawB();
console.log('withdraw txId', res.id);
```

### 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).

```typescript
const res = await dripPosition.closePosition();
console.log('close position txId', res.id);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dcaf.so/products/drip/typescript-guides/withdraw.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
