-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeposit.ts
87 lines (74 loc) · 2.5 KB
/
deposit.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 { TransactionEnvelope } from '@saberhq/solana-contrib';
import { getATAAddressSync, getOrCreateATA } from '@saberhq/token-utils';
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
import type { PublicKey } from '@solana/web3.js';
import { DepositData } from '../programs';
import { CykuraStakerSDK } from '../sdk';
import { findStakeManagerAddress } from './pda';
export class DepositWrapper {
private _deposit: DepositData | null = null;
constructor(
readonly sdk: CykuraStakerSDK,
readonly depositKey: PublicKey
) {}
get provider() {
return this.sdk.provider;
}
get program() {
return this.sdk.programs.CykuraStaker;
}
async reload(): Promise<DepositData> {
return this.program.account.deposit.fetch(this.depositKey);
}
async data(): Promise<DepositData> {
if (!this._deposit) {
this._deposit = await this.reload();
}
return this._deposit;
}
// Returns a transaction to withdraw a deposited token
async withdrawToken(): Promise<TransactionEnvelope> {
const [stakeManager] = await findStakeManagerAddress();
const { mint } = await this.data();
const depositVault = await getATAAddressSync({
mint,
owner: stakeManager,
});
const { address: to, instruction: createToAccountIx } =
await getOrCreateATA({
provider: this.provider,
mint,
});
const tx = new TransactionEnvelope(this.provider, []);
if (createToAccountIx) {
tx.append(createToAccountIx);
}
tx.append(
await this.program.methods
.withdrawToken()
.accounts({
deposit: this.depositKey,
depositVault,
stakeManager,
owner: this.provider.wallet.publicKey,
to,
tokenProgram: TOKEN_PROGRAM_ID,
})
.instruction()
);
return tx;
}
async transferDeposit(to: PublicKey): Promise<TransactionEnvelope> {
const { owner } = await this.data();
return new TransactionEnvelope(this.provider, [
await this.program.methods
.transferDeposit()
.accounts({
deposit: this.depositKey,
owner,
to,
})
.instruction(),
]);
}
}