@streamflow/distributor 6.5.0 → 7.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +4 -5
  2. package/dist/{index.js → cjs/index.js} +1 -1
  3. package/dist/cjs/solana/client.js +216 -0
  4. package/dist/{solana → cjs/solana}/constants.js +7 -8
  5. package/dist/cjs/solana/generated/accounts/ClaimStatus.js +125 -0
  6. package/dist/cjs/solana/generated/accounts/MerkleDistributor.js +180 -0
  7. package/dist/cjs/solana/generated/errors/anchor.js +712 -0
  8. package/dist/cjs/solana/generated/errors/custom.js +256 -0
  9. package/dist/cjs/solana/generated/errors/index.js +65 -0
  10. package/dist/{solana → cjs/solana}/generated/instructions/claimLocked.js +7 -8
  11. package/dist/{solana → cjs/solana}/generated/instructions/clawback.js +8 -8
  12. package/dist/{solana → cjs/solana}/generated/instructions/closeClaim.js +8 -8
  13. package/dist/{solana → cjs/solana}/generated/instructions/newClaim.js +10 -11
  14. package/dist/{solana → cjs/solana}/generated/instructions/newDistributor.js +11 -11
  15. package/dist/cjs/solana/generated/instructions/setAdmin.js +18 -0
  16. package/dist/cjs/solana/generated/instructions/setClawbackReceiver.js +18 -0
  17. package/dist/{solana → cjs/solana}/generated/programId.js +1 -1
  18. package/dist/{solana → cjs/solana}/index.js +6 -6
  19. package/dist/cjs/solana/utils.js +69 -0
  20. package/dist/esm/index.d.ts +1 -0
  21. package/dist/esm/index.js +1 -0
  22. package/dist/esm/solana/client.js +214 -0
  23. package/dist/esm/solana/constants.js +11 -0
  24. package/dist/{solana → esm/solana}/generated/accounts/ClaimStatus.d.ts +11 -11
  25. package/dist/esm/solana/generated/accounts/ClaimStatus.js +113 -0
  26. package/dist/{solana → esm/solana}/generated/accounts/MerkleDistributor.d.ts +19 -19
  27. package/dist/esm/solana/generated/accounts/MerkleDistributor.js +190 -0
  28. package/dist/esm/solana/generated/accounts/index.js +2 -0
  29. package/dist/esm/solana/generated/errors/anchor.js +707 -0
  30. package/dist/esm/solana/generated/errors/custom.js +252 -0
  31. package/dist/esm/solana/generated/errors/index.js +37 -0
  32. package/dist/esm/solana/generated/instructions/claimLocked.js +19 -0
  33. package/dist/esm/solana/generated/instructions/clawback.js +18 -0
  34. package/dist/esm/solana/generated/instructions/closeClaim.js +18 -0
  35. package/dist/esm/solana/generated/instructions/index.js +7 -0
  36. package/dist/{solana → esm/solana}/generated/instructions/newClaim.d.ts +3 -3
  37. package/dist/esm/solana/generated/instructions/newClaim.js +32 -0
  38. package/dist/{solana → esm/solana}/generated/instructions/newDistributor.d.ts +8 -8
  39. package/dist/esm/solana/generated/instructions/newDistributor.js +73 -0
  40. package/dist/esm/solana/generated/instructions/setAdmin.js +14 -0
  41. package/dist/esm/solana/generated/instructions/setClawbackReceiver.js +14 -0
  42. package/dist/esm/solana/generated/programId.js +5 -0
  43. package/dist/esm/solana/index.d.ts +7 -0
  44. package/dist/esm/solana/index.js +7 -0
  45. package/dist/{solana → esm/solana}/types.d.ts +5 -5
  46. package/dist/esm/solana/types.js +156 -0
  47. package/dist/esm/solana/utils.js +60 -0
  48. package/package.json +21 -21
  49. package/dist/index.d.ts +0 -1
  50. package/dist/solana/client.js +0 -402
  51. package/dist/solana/generated/accounts/ClaimStatus.js +0 -183
  52. package/dist/solana/generated/accounts/MerkleDistributor.js +0 -238
  53. package/dist/solana/generated/errors/anchor.js +0 -889
  54. package/dist/solana/generated/errors/custom.js +0 -328
  55. package/dist/solana/generated/errors/index.js +0 -104
  56. package/dist/solana/generated/instructions/setAdmin.js +0 -18
  57. package/dist/solana/generated/instructions/setClawbackReceiver.js +0 -18
  58. package/dist/solana/index.d.ts +0 -7
  59. package/dist/solana/utils.js +0 -122
  60. /package/dist/{solana → cjs/solana}/generated/accounts/index.js +0 -0
  61. /package/dist/{solana → cjs/solana}/generated/instructions/index.js +0 -0
  62. /package/dist/{solana → cjs/solana}/types.js +0 -0
  63. /package/dist/{solana → esm/solana}/client.d.ts +0 -0
  64. /package/dist/{solana → esm/solana}/constants.d.ts +0 -0
  65. /package/dist/{solana → esm/solana}/generated/accounts/index.d.ts +0 -0
  66. /package/dist/{solana → esm/solana}/generated/errors/anchor.d.ts +0 -0
  67. /package/dist/{solana → esm/solana}/generated/errors/custom.d.ts +0 -0
  68. /package/dist/{solana → esm/solana}/generated/errors/index.d.ts +0 -0
  69. /package/dist/{solana → esm/solana}/generated/instructions/claimLocked.d.ts +0 -0
  70. /package/dist/{solana → esm/solana}/generated/instructions/clawback.d.ts +0 -0
  71. /package/dist/{solana → esm/solana}/generated/instructions/closeClaim.d.ts +0 -0
  72. /package/dist/{solana → esm/solana}/generated/instructions/index.d.ts +0 -0
  73. /package/dist/{solana → esm/solana}/generated/instructions/setAdmin.d.ts +0 -0
  74. /package/dist/{solana → esm/solana}/generated/instructions/setClawbackReceiver.d.ts +0 -0
  75. /package/dist/{solana → esm/solana}/generated/programId.d.ts +0 -0
  76. /package/dist/{solana → esm/solana}/utils.d.ts +0 -0
@@ -0,0 +1,214 @@
1
+ import bs58 from "bs58";
2
+ import BigNumber from "bignumber.js";
3
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT, createTransferCheckedInstruction, createTransferCheckedWithFeeInstruction, getTransferFeeConfig, } from "@solana/spl-token";
4
+ import { Connection, PublicKey, SystemProgram, } from "@solana/web3.js";
5
+ import { ICluster } from "@streamflow/common";
6
+ import { ata, checkOrCreateAtaBatch, prepareWrappedAccount, prepareBaseInstructions, prepareTransaction, getMintAndProgram, buildSendThrottler, } from "@streamflow/common/solana";
7
+ import { DISTRIBUTOR_ADMIN_OFFSET, DISTRIBUTOR_MINT_OFFSET, DISTRIBUTOR_PROGRAM_ID } from "./constants";
8
+ import { claimLocked, clawback, newClaim, newDistributor, } from "./generated/instructions";
9
+ import { ClaimStatus, MerkleDistributor } from "./generated/accounts";
10
+ import { calculateAmountWithTransferFees, getClaimantStatusPda, getDistributorPda, getEventAuthorityPda, wrappedSignAndExecuteTransaction, } from "./utils";
11
+ export default class SolanaDistributorClient {
12
+ connection;
13
+ programId;
14
+ commitment;
15
+ sendThrottler;
16
+ /**
17
+ * Create Stream instance
18
+ */
19
+ constructor({ clusterUrl, cluster = ICluster.Mainnet, commitment = "confirmed", programId = "", sendRate = 1, sendThrottler, }) {
20
+ this.commitment = commitment;
21
+ this.connection = new Connection(clusterUrl, this.commitment);
22
+ this.programId = programId !== "" ? new PublicKey(programId) : new PublicKey(DISTRIBUTOR_PROGRAM_ID[cluster]);
23
+ this.sendThrottler = sendThrottler ?? buildSendThrottler(sendRate);
24
+ }
25
+ getCommitment() {
26
+ return typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
27
+ }
28
+ async create(data, extParams) {
29
+ const { ixs, distributorPublicKey } = await this.prepareCreateInstructions(data, extParams);
30
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
31
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
32
+ hash,
33
+ context,
34
+ commitment: this.getCommitment(),
35
+ }, { sendThrottler: this.sendThrottler });
36
+ return {
37
+ ixs,
38
+ txId: signature,
39
+ metadataId: distributorPublicKey.toBase58(),
40
+ };
41
+ }
42
+ async prepareCreateInstructions(data, extParams) {
43
+ if (!extParams.invoker.publicKey) {
44
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
45
+ }
46
+ const ixs = prepareBaseInstructions(this.connection, extParams);
47
+ const mint = extParams.isNative ? NATIVE_MINT : new PublicKey(data.mint);
48
+ const { mint: mintAccount, tokenProgramId } = await getMintAndProgram(this.connection, mint);
49
+ const transferFeeConfig = getTransferFeeConfig(mintAccount);
50
+ const distributorPublicKey = getDistributorPda(this.programId, mint, data.version);
51
+ const tokenVault = await ata(mint, distributorPublicKey, tokenProgramId);
52
+ const senderTokens = await ata(mint, extParams.invoker.publicKey, tokenProgramId);
53
+ const args = {
54
+ version: BigNumber(data.version),
55
+ root: data.root,
56
+ maxTotalClaim: BigNumber(data.maxTotalClaim),
57
+ maxNumNodes: BigNumber(data.maxNumNodes),
58
+ unlockPeriod: BigNumber(data.unlockPeriod),
59
+ startVestingTs: BigNumber(data.startVestingTs),
60
+ endVestingTs: BigNumber(data.endVestingTs),
61
+ clawbackStartTs: BigNumber(data.clawbackStartTs),
62
+ claimsClosable: data.claimsClosable,
63
+ };
64
+ const accounts = {
65
+ distributor: distributorPublicKey,
66
+ clawbackReceiver: senderTokens,
67
+ mint,
68
+ tokenVault,
69
+ admin: extParams.invoker.publicKey,
70
+ systemProgram: SystemProgram.programId,
71
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
72
+ tokenProgram: tokenProgramId,
73
+ };
74
+ if (extParams.isNative) {
75
+ ixs.push(...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, BigNumber(data.maxTotalClaim))));
76
+ }
77
+ const nowTs = BigNumber(Math.floor(Date.now() / 1000));
78
+ const endVestingTs = args.endVestingTs.isZero() ? nowTs : args.endVestingTs;
79
+ const startVestingTs = args.startVestingTs.isZero() ? nowTs : args.startVestingTs;
80
+ if (endVestingTs.gt(startVestingTs) && endVestingTs.minus(startVestingTs).lt(args.unlockPeriod)) {
81
+ throw new Error("The unlock period cannot be longer than the total vesting duration!");
82
+ }
83
+ ixs.push(newDistributor(args, accounts, this.programId));
84
+ if (transferFeeConfig) {
85
+ const { transferAmount, feeCharged } = await calculateAmountWithTransferFees(this.connection, transferFeeConfig, BigInt(data.maxTotalClaim.toString()));
86
+ ixs.push(createTransferCheckedWithFeeInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, transferAmount, mintAccount.decimals, feeCharged, undefined, tokenProgramId));
87
+ }
88
+ else {
89
+ ixs.push(createTransferCheckedInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
90
+ }
91
+ return { distributorPublicKey, ixs };
92
+ }
93
+ async claim(data, extParams) {
94
+ const ixs = await this.prepareClaimInstructions(data, extParams);
95
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
96
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
97
+ hash,
98
+ context,
99
+ commitment: this.getCommitment(),
100
+ }, { sendThrottler: this.sendThrottler });
101
+ return { ixs, txId: signature };
102
+ }
103
+ async prepareClaimInstructions(data, extParams) {
104
+ if (!extParams.invoker.publicKey) {
105
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
106
+ }
107
+ const distributorPublicKey = new PublicKey(data.id);
108
+ const distributor = await MerkleDistributor.fetch(this.connection, distributorPublicKey);
109
+ if (!distributor) {
110
+ throw new Error("Couldn't get account info");
111
+ }
112
+ const { tokenProgramId } = await getMintAndProgram(this.connection, distributor.mint);
113
+ const ixs = prepareBaseInstructions(this.connection, extParams);
114
+ ixs.push(...(await checkOrCreateAtaBatch(this.connection, [extParams.invoker.publicKey], distributor.mint, extParams.invoker, tokenProgramId)));
115
+ const invokerTokens = await ata(distributor.mint, extParams.invoker.publicKey, tokenProgramId);
116
+ const claimStatusPublicKey = getClaimantStatusPda(this.programId, distributorPublicKey, extParams.invoker.publicKey);
117
+ const eventAuthorityPublicKey = getEventAuthorityPda(this.programId);
118
+ const claimStatus = await ClaimStatus.fetch(this.connection, claimStatusPublicKey);
119
+ const accounts = {
120
+ distributor: distributorPublicKey,
121
+ claimStatus: claimStatusPublicKey,
122
+ from: distributor.tokenVault,
123
+ to: invokerTokens,
124
+ claimant: extParams.invoker.publicKey,
125
+ mint: distributor.mint,
126
+ tokenProgram: tokenProgramId,
127
+ systemProgram: SystemProgram.programId,
128
+ eventAuthority: eventAuthorityPublicKey,
129
+ program: this.programId,
130
+ };
131
+ if (!claimStatus) {
132
+ const args = {
133
+ amountLocked: BigNumber(data.amountLocked),
134
+ amountUnlocked: BigNumber(data.amountUnlocked),
135
+ proof: data.proof,
136
+ };
137
+ ixs.push(newClaim(args, accounts, this.programId));
138
+ }
139
+ const nowTs = BigNumber(Math.floor(Date.now() / 1000));
140
+ if (claimStatus ||
141
+ (BigNumber(data.amountLocked).gt(0) && nowTs.minus(distributor.startTs).gte(distributor.unlockPeriod))) {
142
+ ixs.push(claimLocked(accounts, this.programId));
143
+ }
144
+ return ixs;
145
+ }
146
+ async clawback(data, extParams) {
147
+ const ixs = await this.prepareClawbackInstructions(data, extParams);
148
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
149
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
150
+ hash,
151
+ context,
152
+ commitment: this.getCommitment(),
153
+ }, { sendThrottler: this.sendThrottler });
154
+ return { ixs, txId: signature };
155
+ }
156
+ async prepareClawbackInstructions(data, extParams) {
157
+ if (!extParams.invoker.publicKey) {
158
+ throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
159
+ }
160
+ const distributorPublicKey = new PublicKey(data.id);
161
+ const distributor = await MerkleDistributor.fetch(this.connection, distributorPublicKey);
162
+ if (!distributor) {
163
+ throw new Error("Couldn't get account info");
164
+ }
165
+ const { tokenProgramId } = await getMintAndProgram(this.connection, distributor.mint);
166
+ const ixs = prepareBaseInstructions(this.connection, extParams);
167
+ ixs.push(...(await checkOrCreateAtaBatch(this.connection, [extParams.invoker.publicKey], distributor.mint, extParams.invoker, tokenProgramId)));
168
+ const accounts = {
169
+ distributor: distributorPublicKey,
170
+ from: distributor.tokenVault,
171
+ to: distributor.clawbackReceiver,
172
+ admin: extParams.invoker.publicKey,
173
+ mint: distributor.mint,
174
+ systemProgram: SystemProgram.programId,
175
+ tokenProgram: tokenProgramId,
176
+ };
177
+ ixs.push(clawback(accounts, this.programId));
178
+ return ixs;
179
+ }
180
+ async getClaims(data) {
181
+ const claimStatusPublicKeys = data.map(({ id, recipient }) => {
182
+ return getClaimantStatusPda(this.programId, new PublicKey(id), new PublicKey(recipient));
183
+ });
184
+ return ClaimStatus.fetchMultiple(this.connection, claimStatusPublicKeys, this.programId);
185
+ }
186
+ async getDistributors(data) {
187
+ const distributorPublicKeys = data.ids.map((distributorId) => new PublicKey(distributorId));
188
+ return MerkleDistributor.fetchMultiple(this.connection, distributorPublicKeys, this.programId);
189
+ }
190
+ async searchDistributors(data) {
191
+ const filters = [{ memcmp: { offset: 0, bytes: bs58.encode(MerkleDistributor.discriminator) } }];
192
+ if (data.mint) {
193
+ filters.push({
194
+ memcmp: {
195
+ offset: DISTRIBUTOR_MINT_OFFSET,
196
+ bytes: data.mint,
197
+ },
198
+ });
199
+ }
200
+ if (data.admin) {
201
+ filters.push({
202
+ memcmp: {
203
+ offset: DISTRIBUTOR_ADMIN_OFFSET,
204
+ bytes: data.admin,
205
+ },
206
+ });
207
+ }
208
+ const accounts = await this.connection.getProgramAccounts(this.programId, { filters });
209
+ return accounts.map(({ pubkey, account }) => ({
210
+ publicKey: pubkey,
211
+ account: MerkleDistributor.decode(account.data),
212
+ }));
213
+ }
214
+ }
@@ -0,0 +1,11 @@
1
+ import { ICluster } from "@streamflow/common";
2
+ export const ANCHOR_DISCRIMINATOR_OFFSET = 8;
3
+ export const DISTRIBUTOR_MINT_OFFSET = ANCHOR_DISCRIMINATOR_OFFSET + 41;
4
+ export const DISTRIBUTOR_ADMIN_OFFSET = ANCHOR_DISCRIMINATOR_OFFSET + 201;
5
+ export const ONE_IN_BASIS_POINTS = BigInt(10000);
6
+ export const DISTRIBUTOR_PROGRAM_ID = {
7
+ [ICluster.Devnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
8
+ [ICluster.Mainnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
9
+ [ICluster.Testnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
10
+ [ICluster.Local]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
11
+ };
@@ -1,19 +1,19 @@
1
1
  /// <reference types="node" />
2
2
  import { PublicKey, Connection } from "@solana/web3.js";
3
- import BN from "bn.js";
3
+ import BigNumber from "bignumber.js";
4
4
  export interface ClaimStatusFields {
5
5
  /** Authority that claimed the tokens. */
6
6
  claimant: PublicKey;
7
7
  /** Locked amount */
8
- lockedAmount: BN;
8
+ lockedAmount: BigNumber;
9
9
  /** Locked amount withdrawn */
10
- lockedAmountWithdrawn: BN;
10
+ lockedAmountWithdrawn: BigNumber;
11
11
  /** Unlocked amount */
12
- unlockedAmount: BN;
12
+ unlockedAmount: BigNumber;
13
13
  /** Last claim time */
14
- lastClaimTs: BN;
14
+ lastClaimTs: BigNumber;
15
15
  /** Track amount per unlock, can be useful for non-linear vesting */
16
- lastAmountPerUnlock: BN;
16
+ lastAmountPerUnlock: BigNumber;
17
17
  /** Whether claim is closed */
18
18
  closed: boolean;
19
19
  /** Buffer for additional fields */
@@ -46,15 +46,15 @@ export declare class ClaimStatus {
46
46
  /** Authority that claimed the tokens. */
47
47
  readonly claimant: PublicKey;
48
48
  /** Locked amount */
49
- readonly lockedAmount: BN;
49
+ readonly lockedAmount: BigNumber;
50
50
  /** Locked amount withdrawn */
51
- readonly lockedAmountWithdrawn: BN;
51
+ readonly lockedAmountWithdrawn: BigNumber;
52
52
  /** Unlocked amount */
53
- readonly unlockedAmount: BN;
53
+ readonly unlockedAmount: BigNumber;
54
54
  /** Last claim time */
55
- readonly lastClaimTs: BN;
55
+ readonly lastClaimTs: BigNumber;
56
56
  /** Track amount per unlock, can be useful for non-linear vesting */
57
- readonly lastAmountPerUnlock: BN;
57
+ readonly lastAmountPerUnlock: BigNumber;
58
58
  /** Whether claim is closed */
59
59
  readonly closed: boolean;
60
60
  /** Buffer for additional fields */
@@ -0,0 +1,113 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import BigNumber from "bignumber.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
3
+ import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars
4
+ import { PROGRAM_ID } from "../programId";
5
+ /** Holds whether or not a claimant has claimed tokens. */
6
+ export class ClaimStatus {
7
+ /** Authority that claimed the tokens. */
8
+ claimant;
9
+ /** Locked amount */
10
+ lockedAmount;
11
+ /** Locked amount withdrawn */
12
+ lockedAmountWithdrawn;
13
+ /** Unlocked amount */
14
+ unlockedAmount;
15
+ /** Last claim time */
16
+ lastClaimTs;
17
+ /** Track amount per unlock, can be useful for non-linear vesting */
18
+ lastAmountPerUnlock;
19
+ /** Whether claim is closed */
20
+ closed;
21
+ /** Buffer for additional fields */
22
+ buffer1;
23
+ /** Buffer for additional fields */
24
+ buffer2;
25
+ static discriminator = Buffer.from([22, 183, 249, 157, 247, 95, 150, 96]);
26
+ static layout = borsh.struct([
27
+ borsh.publicKey("claimant"),
28
+ borsh.u64("lockedAmount"),
29
+ borsh.u64("lockedAmountWithdrawn"),
30
+ borsh.u64("unlockedAmount"),
31
+ borsh.u64("lastClaimTs"),
32
+ borsh.u64("lastAmountPerUnlock"),
33
+ borsh.bool("closed"),
34
+ borsh.array(borsh.u8(), 32, "buffer1"),
35
+ borsh.array(borsh.u8(), 32, "buffer2"),
36
+ ]);
37
+ constructor(fields) {
38
+ this.claimant = fields.claimant;
39
+ this.lockedAmount = fields.lockedAmount;
40
+ this.lockedAmountWithdrawn = fields.lockedAmountWithdrawn;
41
+ this.unlockedAmount = fields.unlockedAmount;
42
+ this.lastClaimTs = fields.lastClaimTs;
43
+ this.lastAmountPerUnlock = fields.lastAmountPerUnlock;
44
+ this.closed = fields.closed;
45
+ this.buffer1 = fields.buffer1;
46
+ this.buffer2 = fields.buffer2;
47
+ }
48
+ static async fetch(c, address, programId = PROGRAM_ID) {
49
+ const info = await c.getAccountInfo(address);
50
+ if (info === null) {
51
+ return null;
52
+ }
53
+ if (!info.owner.equals(programId)) {
54
+ throw new Error("account doesn't belong to this program");
55
+ }
56
+ return this.decode(info.data);
57
+ }
58
+ static async fetchMultiple(c, addresses, programId = PROGRAM_ID) {
59
+ const infos = await c.getMultipleAccountsInfo(addresses);
60
+ return infos.map((info) => {
61
+ if (info === null) {
62
+ return null;
63
+ }
64
+ if (!info.owner.equals(programId)) {
65
+ throw new Error("account doesn't belong to this program");
66
+ }
67
+ return this.decode(info.data);
68
+ });
69
+ }
70
+ static decode(data) {
71
+ if (!data.slice(0, 8).equals(ClaimStatus.discriminator)) {
72
+ throw new Error("invalid account discriminator");
73
+ }
74
+ const dec = ClaimStatus.layout.decode(data.slice(8));
75
+ return new ClaimStatus({
76
+ claimant: dec.claimant,
77
+ lockedAmount: dec.lockedAmount,
78
+ lockedAmountWithdrawn: dec.lockedAmountWithdrawn,
79
+ unlockedAmount: dec.unlockedAmount,
80
+ lastClaimTs: dec.lastClaimTs,
81
+ lastAmountPerUnlock: dec.lastAmountPerUnlock,
82
+ closed: dec.closed,
83
+ buffer1: dec.buffer1,
84
+ buffer2: dec.buffer2,
85
+ });
86
+ }
87
+ toJSON() {
88
+ return {
89
+ claimant: this.claimant.toString(),
90
+ lockedAmount: this.lockedAmount.toString(),
91
+ lockedAmountWithdrawn: this.lockedAmountWithdrawn.toString(),
92
+ unlockedAmount: this.unlockedAmount.toString(),
93
+ lastClaimTs: this.lastClaimTs.toString(),
94
+ lastAmountPerUnlock: this.lastAmountPerUnlock.toString(),
95
+ closed: this.closed,
96
+ buffer1: this.buffer1,
97
+ buffer2: this.buffer2,
98
+ };
99
+ }
100
+ static fromJSON(obj) {
101
+ return new ClaimStatus({
102
+ claimant: new PublicKey(obj.claimant),
103
+ lockedAmount: BigNumber(obj.lockedAmount),
104
+ lockedAmountWithdrawn: BigNumber(obj.lockedAmountWithdrawn),
105
+ unlockedAmount: BigNumber(obj.unlockedAmount),
106
+ lastClaimTs: BigNumber(obj.lastClaimTs),
107
+ lastAmountPerUnlock: BigNumber(obj.lastAmountPerUnlock),
108
+ closed: obj.closed,
109
+ buffer1: obj.buffer1,
110
+ buffer2: obj.buffer2,
111
+ });
112
+ }
113
+ }
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  import { PublicKey, Connection } from "@solana/web3.js";
3
- import BN from "bn.js";
3
+ import BigNumber from "bignumber.js";
4
4
  export interface MerkleDistributorFields {
5
5
  /** Bump seed. */
6
6
  bump: number;
7
7
  /** Version of the airdrop */
8
- version: BN;
8
+ version: BigNumber;
9
9
  /** The 256-bit merkle root. */
10
10
  root: Array<number>;
11
11
  /** [Mint] of the token to be distributed. */
@@ -13,21 +13,21 @@ export interface MerkleDistributorFields {
13
13
  /** Token Address of the vault */
14
14
  tokenVault: PublicKey;
15
15
  /** Maximum number of tokens that can ever be claimed from this [MerkleDistributor]. */
16
- maxTotalClaim: BN;
16
+ maxTotalClaim: BigNumber;
17
17
  /** Maximum number of nodes in [MerkleDistributor]. */
18
- maxNumNodes: BN;
18
+ maxNumNodes: BigNumber;
19
19
  /** Time step (period) in seconds per which the unlock occurs */
20
- unlockPeriod: BN;
20
+ unlockPeriod: BigNumber;
21
21
  /** Total amount of tokens that have been claimed. */
22
- totalAmountClaimed: BN;
22
+ totalAmountClaimed: BigNumber;
23
23
  /** Number of nodes that have been claimed. */
24
- numNodesClaimed: BN;
24
+ numNodesClaimed: BigNumber;
25
25
  /** Lockup time start (Unix Timestamp) */
26
- startTs: BN;
26
+ startTs: BigNumber;
27
27
  /** Lockup time end (Unix Timestamp) */
28
- endTs: BN;
28
+ endTs: BigNumber;
29
29
  /** Clawback start (Unix Timestamp) */
30
- clawbackStartTs: BN;
30
+ clawbackStartTs: BigNumber;
31
31
  /** Clawback receiver */
32
32
  clawbackReceiver: PublicKey;
33
33
  /** Admin wallet */
@@ -90,7 +90,7 @@ export declare class MerkleDistributor {
90
90
  /** Bump seed. */
91
91
  readonly bump: number;
92
92
  /** Version of the airdrop */
93
- readonly version: BN;
93
+ readonly version: BigNumber;
94
94
  /** The 256-bit merkle root. */
95
95
  readonly root: Array<number>;
96
96
  /** [Mint] of the token to be distributed. */
@@ -98,21 +98,21 @@ export declare class MerkleDistributor {
98
98
  /** Token Address of the vault */
99
99
  readonly tokenVault: PublicKey;
100
100
  /** Maximum number of tokens that can ever be claimed from this [MerkleDistributor]. */
101
- readonly maxTotalClaim: BN;
101
+ readonly maxTotalClaim: BigNumber;
102
102
  /** Maximum number of nodes in [MerkleDistributor]. */
103
- readonly maxNumNodes: BN;
103
+ readonly maxNumNodes: BigNumber;
104
104
  /** Time step (period) in seconds per which the unlock occurs */
105
- readonly unlockPeriod: BN;
105
+ readonly unlockPeriod: BigNumber;
106
106
  /** Total amount of tokens that have been claimed. */
107
- readonly totalAmountClaimed: BN;
107
+ readonly totalAmountClaimed: BigNumber;
108
108
  /** Number of nodes that have been claimed. */
109
- readonly numNodesClaimed: BN;
109
+ readonly numNodesClaimed: BigNumber;
110
110
  /** Lockup time start (Unix Timestamp) */
111
- readonly startTs: BN;
111
+ readonly startTs: BigNumber;
112
112
  /** Lockup time end (Unix Timestamp) */
113
- readonly endTs: BN;
113
+ readonly endTs: BigNumber;
114
114
  /** Clawback start (Unix Timestamp) */
115
- readonly clawbackStartTs: BN;
115
+ readonly clawbackStartTs: BigNumber;
116
116
  /** Clawback receiver */
117
117
  readonly clawbackReceiver: PublicKey;
118
118
  /** Admin wallet */
@@ -0,0 +1,190 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import BigNumber from "bignumber.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
3
+ import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars
4
+ import { PROGRAM_ID } from "../programId";
5
+ /** State for the account which distributes tokens. */
6
+ export class MerkleDistributor {
7
+ /** Bump seed. */
8
+ bump;
9
+ /** Version of the airdrop */
10
+ version;
11
+ /** The 256-bit merkle root. */
12
+ root;
13
+ /** [Mint] of the token to be distributed. */
14
+ mint;
15
+ /** Token Address of the vault */
16
+ tokenVault;
17
+ /** Maximum number of tokens that can ever be claimed from this [MerkleDistributor]. */
18
+ maxTotalClaim;
19
+ /** Maximum number of nodes in [MerkleDistributor]. */
20
+ maxNumNodes;
21
+ /** Time step (period) in seconds per which the unlock occurs */
22
+ unlockPeriod;
23
+ /** Total amount of tokens that have been claimed. */
24
+ totalAmountClaimed;
25
+ /** Number of nodes that have been claimed. */
26
+ numNodesClaimed;
27
+ /** Lockup time start (Unix Timestamp) */
28
+ startTs;
29
+ /** Lockup time end (Unix Timestamp) */
30
+ endTs;
31
+ /** Clawback start (Unix Timestamp) */
32
+ clawbackStartTs;
33
+ /** Clawback receiver */
34
+ clawbackReceiver;
35
+ /** Admin wallet */
36
+ admin;
37
+ /** Whether or not the distributor has been clawed back */
38
+ clawedBack;
39
+ /** Whether claims are closable by the admin or not */
40
+ claimsClosable;
41
+ /** Buffer for additional fields */
42
+ buffer1;
43
+ /** Buffer for additional fields */
44
+ buffer2;
45
+ /** Buffer for additional fields */
46
+ buffer3;
47
+ static discriminator = Buffer.from([77, 119, 139, 70, 84, 247, 12, 26]);
48
+ static layout = borsh.struct([
49
+ borsh.u8("bump"),
50
+ borsh.u64("version"),
51
+ borsh.array(borsh.u8(), 32, "root"),
52
+ borsh.publicKey("mint"),
53
+ borsh.publicKey("tokenVault"),
54
+ borsh.u64("maxTotalClaim"),
55
+ borsh.u64("maxNumNodes"),
56
+ borsh.u64("unlockPeriod"),
57
+ borsh.u64("totalAmountClaimed"),
58
+ borsh.u64("numNodesClaimed"),
59
+ borsh.u64("startTs"),
60
+ borsh.u64("endTs"),
61
+ borsh.u64("clawbackStartTs"),
62
+ borsh.publicKey("clawbackReceiver"),
63
+ borsh.publicKey("admin"),
64
+ borsh.bool("clawedBack"),
65
+ borsh.bool("claimsClosable"),
66
+ borsh.array(borsh.u8(), 32, "buffer1"),
67
+ borsh.array(borsh.u8(), 32, "buffer2"),
68
+ borsh.array(borsh.u8(), 32, "buffer3"),
69
+ ]);
70
+ constructor(fields) {
71
+ this.bump = fields.bump;
72
+ this.version = fields.version;
73
+ this.root = fields.root;
74
+ this.mint = fields.mint;
75
+ this.tokenVault = fields.tokenVault;
76
+ this.maxTotalClaim = fields.maxTotalClaim;
77
+ this.maxNumNodes = fields.maxNumNodes;
78
+ this.unlockPeriod = fields.unlockPeriod;
79
+ this.totalAmountClaimed = fields.totalAmountClaimed;
80
+ this.numNodesClaimed = fields.numNodesClaimed;
81
+ this.startTs = fields.startTs;
82
+ this.endTs = fields.endTs;
83
+ this.clawbackStartTs = fields.clawbackStartTs;
84
+ this.clawbackReceiver = fields.clawbackReceiver;
85
+ this.admin = fields.admin;
86
+ this.clawedBack = fields.clawedBack;
87
+ this.claimsClosable = fields.claimsClosable;
88
+ this.buffer1 = fields.buffer1;
89
+ this.buffer2 = fields.buffer2;
90
+ this.buffer3 = fields.buffer3;
91
+ }
92
+ static async fetch(c, address, programId = PROGRAM_ID) {
93
+ const info = await c.getAccountInfo(address);
94
+ if (info === null) {
95
+ return null;
96
+ }
97
+ if (!info.owner.equals(programId)) {
98
+ throw new Error("account doesn't belong to this program");
99
+ }
100
+ return this.decode(info.data);
101
+ }
102
+ static async fetchMultiple(c, addresses, programId = PROGRAM_ID) {
103
+ const infos = await c.getMultipleAccountsInfo(addresses);
104
+ return infos.map((info) => {
105
+ if (info === null) {
106
+ return null;
107
+ }
108
+ if (!info.owner.equals(programId)) {
109
+ throw new Error("account doesn't belong to this program");
110
+ }
111
+ return this.decode(info.data);
112
+ });
113
+ }
114
+ static decode(data) {
115
+ if (!data.slice(0, 8).equals(MerkleDistributor.discriminator)) {
116
+ throw new Error("invalid account discriminator");
117
+ }
118
+ const dec = MerkleDistributor.layout.decode(data.slice(8));
119
+ return new MerkleDistributor({
120
+ bump: dec.bump,
121
+ version: dec.version,
122
+ root: dec.root,
123
+ mint: dec.mint,
124
+ tokenVault: dec.tokenVault,
125
+ maxTotalClaim: dec.maxTotalClaim,
126
+ maxNumNodes: dec.maxNumNodes,
127
+ unlockPeriod: dec.unlockPeriod,
128
+ totalAmountClaimed: dec.totalAmountClaimed,
129
+ numNodesClaimed: dec.numNodesClaimed,
130
+ startTs: dec.startTs,
131
+ endTs: dec.endTs,
132
+ clawbackStartTs: dec.clawbackStartTs,
133
+ clawbackReceiver: dec.clawbackReceiver,
134
+ admin: dec.admin,
135
+ clawedBack: dec.clawedBack,
136
+ claimsClosable: dec.claimsClosable,
137
+ buffer1: dec.buffer1,
138
+ buffer2: dec.buffer2,
139
+ buffer3: dec.buffer3,
140
+ });
141
+ }
142
+ toJSON() {
143
+ return {
144
+ bump: this.bump,
145
+ version: this.version.toString(),
146
+ root: this.root,
147
+ mint: this.mint.toString(),
148
+ tokenVault: this.tokenVault.toString(),
149
+ maxTotalClaim: this.maxTotalClaim.toString(),
150
+ maxNumNodes: this.maxNumNodes.toString(),
151
+ unlockPeriod: this.unlockPeriod.toString(),
152
+ totalAmountClaimed: this.totalAmountClaimed.toString(),
153
+ numNodesClaimed: this.numNodesClaimed.toString(),
154
+ startTs: this.startTs.toString(),
155
+ endTs: this.endTs.toString(),
156
+ clawbackStartTs: this.clawbackStartTs.toString(),
157
+ clawbackReceiver: this.clawbackReceiver.toString(),
158
+ admin: this.admin.toString(),
159
+ clawedBack: this.clawedBack,
160
+ claimsClosable: this.claimsClosable,
161
+ buffer1: this.buffer1,
162
+ buffer2: this.buffer2,
163
+ buffer3: this.buffer3,
164
+ };
165
+ }
166
+ static fromJSON(obj) {
167
+ return new MerkleDistributor({
168
+ bump: obj.bump,
169
+ version: BigNumber(obj.version),
170
+ root: obj.root,
171
+ mint: new PublicKey(obj.mint),
172
+ tokenVault: new PublicKey(obj.tokenVault),
173
+ maxTotalClaim: BigNumber(obj.maxTotalClaim),
174
+ maxNumNodes: BigNumber(obj.maxNumNodes),
175
+ unlockPeriod: BigNumber(obj.unlockPeriod),
176
+ totalAmountClaimed: BigNumber(obj.totalAmountClaimed),
177
+ numNodesClaimed: BigNumber(obj.numNodesClaimed),
178
+ startTs: BigNumber(obj.startTs),
179
+ endTs: BigNumber(obj.endTs),
180
+ clawbackStartTs: BigNumber(obj.clawbackStartTs),
181
+ clawbackReceiver: new PublicKey(obj.clawbackReceiver),
182
+ admin: new PublicKey(obj.admin),
183
+ clawedBack: obj.clawedBack,
184
+ claimsClosable: obj.claimsClosable,
185
+ buffer1: obj.buffer1,
186
+ buffer2: obj.buffer2,
187
+ buffer3: obj.buffer3,
188
+ });
189
+ }
190
+ }
@@ -0,0 +1,2 @@
1
+ export { ClaimStatus } from "./ClaimStatus";
2
+ export { MerkleDistributor } from "./MerkleDistributor";