/
withdraw.ts
87 lines (78 loc) · 2.92 KB
/
withdraw.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
ClientEnv,
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 deposit(dripVault: DripVault): Promise<PublicKey> {
const res = await dripVault.deposit({
// units are base units
amount: new BN(100),
dripParams: {
numberOfSwaps: 10
}
});
console.log('position', res.metadata.position.toString());
return res.metadata.position;
}
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('tokeA', 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);
const positionPubkey = await deposit(dripVault);
const dripPosition = await drip.getPosition(positionPubkey);
{
const res = await dripPosition.withdrawB();
console.log('withdraw txId', res.id);
}
{
const res = await dripPosition.closePosition();
console.log('close position txId', res.id);
}
}
main();