@streamflow/distributor 7.0.0-alpha.15 → 7.0.0-alpha.17

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 (35) hide show
  1. package/dist/cjs/solana/{client.js → clients/BaseDistributorClient.js} +65 -62
  2. package/dist/cjs/solana/clients/SolanaAlignedDistributorClient.js +98 -0
  3. package/dist/cjs/solana/clients/SolanaDistributorClient.js +17 -0
  4. package/dist/cjs/solana/clients/index.js +10 -0
  5. package/dist/cjs/solana/constants.js +4 -1
  6. package/dist/cjs/solana/descriptor/aligned_distributor.js +2 -0
  7. package/dist/cjs/solana/descriptor/idl/aligned_distributor.json +1242 -0
  8. package/dist/cjs/solana/generated/accounts/ClaimStatus.js +5 -5
  9. package/dist/cjs/solana/generated/accounts/MerkleDistributor.js +31 -6
  10. package/dist/cjs/solana/index.js +2 -6
  11. package/dist/cjs/solana/utils.js +14 -6
  12. package/dist/esm/solana/clients/BaseDistributorClient.d.ts +38 -0
  13. package/dist/esm/solana/{client.js → clients/BaseDistributorClient.js} +65 -62
  14. package/dist/esm/solana/clients/SolanaAlignedDistributorClient.d.ts +12 -0
  15. package/dist/esm/solana/clients/SolanaAlignedDistributorClient.js +93 -0
  16. package/dist/esm/solana/clients/SolanaDistributorClient.d.ts +8 -0
  17. package/dist/esm/solana/clients/SolanaDistributorClient.js +11 -0
  18. package/dist/esm/solana/clients/index.d.ts +2 -0
  19. package/dist/esm/solana/clients/index.js +2 -0
  20. package/dist/esm/solana/constants.d.ts +4 -0
  21. package/dist/esm/solana/constants.js +3 -0
  22. package/dist/esm/solana/descriptor/aligned_distributor.d.ts +918 -0
  23. package/dist/esm/solana/descriptor/aligned_distributor.js +1 -0
  24. package/dist/esm/solana/descriptor/idl/aligned_distributor.json +1242 -0
  25. package/dist/esm/solana/generated/accounts/ClaimStatus.d.ts +6 -6
  26. package/dist/esm/solana/generated/accounts/ClaimStatus.js +7 -7
  27. package/dist/esm/solana/generated/accounts/MerkleDistributor.d.ts +36 -6
  28. package/dist/esm/solana/generated/accounts/MerkleDistributor.js +43 -8
  29. package/dist/esm/solana/index.d.ts +1 -1
  30. package/dist/esm/solana/index.js +1 -1
  31. package/dist/esm/solana/types.d.ts +40 -0
  32. package/dist/esm/solana/utils.d.ts +2 -0
  33. package/dist/esm/solana/utils.js +8 -2
  34. package/package.json +5 -4
  35. package/dist/esm/solana/client.d.ts +0 -38
@@ -41,7 +41,7 @@ class ClaimStatus {
41
41
  this.lastClaimTs = fields.lastClaimTs;
42
42
  this.lastAmountPerUnlock = fields.lastAmountPerUnlock;
43
43
  this.closed = fields.closed;
44
- this.buffer1 = fields.buffer1;
44
+ this.distributor = fields.distributor;
45
45
  this.buffer2 = fields.buffer2;
46
46
  }
47
47
  static async fetch(c, address, programId = programId_1.PROGRAM_ID) {
@@ -79,7 +79,7 @@ class ClaimStatus {
79
79
  lastClaimTs: dec.lastClaimTs,
80
80
  lastAmountPerUnlock: dec.lastAmountPerUnlock,
81
81
  closed: dec.closed,
82
- buffer1: dec.buffer1,
82
+ distributor: dec.distributor,
83
83
  buffer2: dec.buffer2,
84
84
  });
85
85
  }
@@ -92,7 +92,7 @@ class ClaimStatus {
92
92
  lastClaimTs: this.lastClaimTs.toString(),
93
93
  lastAmountPerUnlock: this.lastAmountPerUnlock.toString(),
94
94
  closed: this.closed,
95
- buffer1: this.buffer1,
95
+ distributor: this.distributor.toString(),
96
96
  buffer2: this.buffer2,
97
97
  };
98
98
  }
@@ -105,7 +105,7 @@ class ClaimStatus {
105
105
  lastClaimTs: new bn_js_1.default(obj.lastClaimTs),
106
106
  lastAmountPerUnlock: new bn_js_1.default(obj.lastAmountPerUnlock),
107
107
  closed: obj.closed,
108
- buffer1: obj.buffer1,
108
+ distributor: new web3_js_1.PublicKey(obj.distributor),
109
109
  buffer2: obj.buffer2,
110
110
  });
111
111
  }
@@ -120,6 +120,6 @@ ClaimStatus.layout = borsh.struct([
120
120
  borsh.u64("lastClaimTs"),
121
121
  borsh.u64("lastAmountPerUnlock"),
122
122
  borsh.bool("closed"),
123
- borsh.array(borsh.u8(), 32, "buffer1"),
123
+ borsh.publicKey("distributor"),
124
124
  borsh.array(borsh.u8(), 32, "buffer2"),
125
125
  ]);
@@ -51,7 +51,12 @@ class MerkleDistributor {
51
51
  this.admin = fields.admin;
52
52
  this.clawedBack = fields.clawedBack;
53
53
  this.claimsClosable = fields.claimsClosable;
54
- this.buffer1 = fields.buffer1;
54
+ this.canUpdateDuration = fields.canUpdateDuration;
55
+ this.totalAmountUnlocked = fields.totalAmountUnlocked;
56
+ this.totalAmountLocked = fields.totalAmountLocked;
57
+ this.lastDurationUpdateTs = fields.lastDurationUpdateTs;
58
+ this.totalClaimablePreUpdate = fields.totalClaimablePreUpdate;
59
+ this.clawedBackTs = fields.clawedBackTs;
55
60
  this.buffer2 = fields.buffer2;
56
61
  this.buffer3 = fields.buffer3;
57
62
  }
@@ -100,7 +105,12 @@ class MerkleDistributor {
100
105
  admin: dec.admin,
101
106
  clawedBack: dec.clawedBack,
102
107
  claimsClosable: dec.claimsClosable,
103
- buffer1: dec.buffer1,
108
+ canUpdateDuration: dec.canUpdateDuration,
109
+ totalAmountUnlocked: dec.totalAmountUnlocked,
110
+ totalAmountLocked: dec.totalAmountLocked,
111
+ lastDurationUpdateTs: dec.lastDurationUpdateTs,
112
+ totalClaimablePreUpdate: dec.totalClaimablePreUpdate,
113
+ clawedBackTs: dec.clawedBackTs,
104
114
  buffer2: dec.buffer2,
105
115
  buffer3: dec.buffer3,
106
116
  });
@@ -124,7 +134,12 @@ class MerkleDistributor {
124
134
  admin: this.admin.toString(),
125
135
  clawedBack: this.clawedBack,
126
136
  claimsClosable: this.claimsClosable,
127
- buffer1: this.buffer1,
137
+ canUpdateDuration: this.canUpdateDuration,
138
+ totalAmountUnlocked: this.totalAmountUnlocked.toString(),
139
+ totalAmountLocked: this.totalAmountLocked.toString(),
140
+ lastDurationUpdateTs: this.lastDurationUpdateTs.toString(),
141
+ totalClaimablePreUpdate: this.totalClaimablePreUpdate.toString(),
142
+ clawedBackTs: this.clawedBackTs.toString(),
128
143
  buffer2: this.buffer2,
129
144
  buffer3: this.buffer3,
130
145
  };
@@ -148,7 +163,12 @@ class MerkleDistributor {
148
163
  admin: new web3_js_1.PublicKey(obj.admin),
149
164
  clawedBack: obj.clawedBack,
150
165
  claimsClosable: obj.claimsClosable,
151
- buffer1: obj.buffer1,
166
+ canUpdateDuration: obj.canUpdateDuration,
167
+ totalAmountUnlocked: new bn_js_1.default(obj.totalAmountUnlocked),
168
+ totalAmountLocked: new bn_js_1.default(obj.totalAmountLocked),
169
+ lastDurationUpdateTs: new bn_js_1.default(obj.lastDurationUpdateTs),
170
+ totalClaimablePreUpdate: new bn_js_1.default(obj.totalClaimablePreUpdate),
171
+ clawedBackTs: new bn_js_1.default(obj.clawedBackTs),
152
172
  buffer2: obj.buffer2,
153
173
  buffer3: obj.buffer3,
154
174
  });
@@ -174,7 +194,12 @@ MerkleDistributor.layout = borsh.struct([
174
194
  borsh.publicKey("admin"),
175
195
  borsh.bool("clawedBack"),
176
196
  borsh.bool("claimsClosable"),
177
- borsh.array(borsh.u8(), 32, "buffer1"),
178
- borsh.array(borsh.u8(), 32, "buffer2"),
197
+ borsh.bool("canUpdateDuration"),
198
+ borsh.u64("totalAmountUnlocked"),
199
+ borsh.u64("totalAmountLocked"),
200
+ borsh.u64("lastDurationUpdateTs"),
201
+ borsh.u64("totalClaimablePreUpdate"),
202
+ borsh.u64("clawedBackTs"),
203
+ borsh.array(borsh.u8(), 23, "buffer2"),
179
204
  borsh.array(borsh.u8(), 32, "buffer3"),
180
205
  ]);
@@ -25,13 +25,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  __setModuleDefault(result, mod);
26
26
  return result;
27
27
  };
28
- var __importDefault = (this && this.__importDefault) || function (mod) {
29
- return (mod && mod.__esModule) ? mod : { "default": mod };
30
- };
31
28
  Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.constants = exports.SolanaDistributorClient = void 0;
33
- var client_1 = require("./client");
34
- Object.defineProperty(exports, "SolanaDistributorClient", { enumerable: true, get: function () { return __importDefault(client_1).default; } });
29
+ exports.constants = void 0;
30
+ __exportStar(require("./clients/index.js"), exports);
35
31
  __exportStar(require("./generated/accounts/index.js"), exports);
36
32
  __exportStar(require("./generated/errors/index.js"), exports);
37
33
  __exportStar(require("./generated/instructions/index.js"), exports);
@@ -1,12 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateAmountWithTransferFees = exports.wrappedSignAndExecuteTransaction = exports.getEventAuthorityPda = exports.getClaimantStatusPda = exports.getDistributorPda = void 0;
3
+ exports.calculateAmountWithTransferFees = exports.wrappedSignAndExecuteTransaction = exports.getEventAuthorityPda = exports.getClaimantStatusPda = exports.getDistributorPda = exports.getTestOraclePda = exports.getAlignedDistributorPda = void 0;
4
4
  const buffer_1 = require("buffer");
5
5
  const web3_js_1 = require("@solana/web3.js");
6
6
  const common_1 = require("@streamflow/common");
7
7
  const solana_1 = require("@streamflow/common/solana");
8
- const errors_1 = require("./generated/errors");
9
- const constants_1 = require("./constants");
8
+ const index_js_1 = require("./generated/errors/index.js");
9
+ const constants_js_1 = require("./constants.js");
10
+ const getAlignedDistributorPda = (programId, distributor) => {
11
+ return web3_js_1.PublicKey.findProgramAddressSync([constants_js_1.ALIGNED_DISTRIBUTOR_PREFIX, distributor.toBuffer()], programId)[0];
12
+ };
13
+ exports.getAlignedDistributorPda = getAlignedDistributorPda;
14
+ const getTestOraclePda = (programId, mint, creator) => {
15
+ return web3_js_1.PublicKey.findProgramAddressSync([constants_js_1.TEST_ORACLE_PREFIX, mint.toBuffer(), creator.toBuffer()], programId)[0];
16
+ };
17
+ exports.getTestOraclePda = getTestOraclePda;
10
18
  function getDistributorPda(programId, mint, version) {
11
19
  // Constructing the seed for the PDA
12
20
  const seeds = [
@@ -38,7 +46,7 @@ async function wrappedSignAndExecuteTransaction(connection, invoker, tx, confirm
38
46
  }
39
47
  catch (err) {
40
48
  if (err instanceof Error) {
41
- const parsed = (0, errors_1.fromTxError)(err);
49
+ const parsed = (0, index_js_1.fromTxError)(err);
42
50
  if (parsed) {
43
51
  throw new common_1.ContractError(err, parsed.name, parsed.msg);
44
52
  }
@@ -55,8 +63,8 @@ async function calculateAmountWithTransferFees(connection, transferFeeConfig, tr
55
63
  const transferFeeBasisPoints = BigInt(transferFee.transferFeeBasisPoints);
56
64
  let feeCharged = BigInt(0);
57
65
  if (transferFeeBasisPoints !== BigInt(0)) {
58
- const numerator = transferAmount * constants_1.ONE_IN_BASIS_POINTS;
59
- const denominator = constants_1.ONE_IN_BASIS_POINTS - transferFeeBasisPoints;
66
+ const numerator = transferAmount * constants_js_1.ONE_IN_BASIS_POINTS;
67
+ const denominator = constants_js_1.ONE_IN_BASIS_POINTS - transferFeeBasisPoints;
60
68
  const rawPreFeeAmount = (0, common_1.divCeilN)(numerator, denominator);
61
69
  const fee = rawPreFeeAmount - transferAmount;
62
70
  transferAmount = rawPreFeeAmount;
@@ -0,0 +1,38 @@
1
+ import PQueue from "p-queue";
2
+ import { Connection, PublicKey, TransactionInstruction, Commitment, ConnectionConfig } from "@solana/web3.js";
3
+ import { ICluster, ITransactionResult } from "@streamflow/common";
4
+ import { IProgramAccount } from "@streamflow/common/solana";
5
+ import { IClaimData, IClawbackData, ICreateDistributorResult, IGetClaimData, IGetDistributors, ICreateSolanaExt, IInteractSolanaExt, ICreateDistributorData, ICreateAlignedDistributorData, ISearchDistributors } from "../types";
6
+ import { ClawbackAccounts, NewDistributorAccounts, NewDistributorArgs } from "../generated/instructions";
7
+ import { ClaimStatus, MerkleDistributor } from "../generated/accounts";
8
+ export interface IInitOptions {
9
+ clusterUrl: string;
10
+ cluster?: ICluster;
11
+ commitment?: Commitment | ConnectionConfig;
12
+ programId?: string;
13
+ sendRate?: number;
14
+ sendThrottler?: PQueue;
15
+ }
16
+ export default abstract class BaseDistributorClient {
17
+ protected connection: Connection;
18
+ protected programId: PublicKey;
19
+ protected commitment: Commitment | ConnectionConfig;
20
+ protected sendThrottler: PQueue;
21
+ constructor({ clusterUrl, cluster, commitment, programId, sendRate, sendThrottler, }: IInitOptions);
22
+ protected abstract getNewDistributorInstruction(data: ICreateDistributorData | ICreateAlignedDistributorData, accounts: NewDistributorAccounts): Promise<TransactionInstruction>;
23
+ protected abstract getClawbackInstruction(account: ClawbackAccounts): Promise<TransactionInstruction>;
24
+ getCommitment(): Commitment | undefined;
25
+ prepareCreateInstructions(data: ICreateDistributorData | ICreateAlignedDistributorData, extParams: ICreateSolanaExt): Promise<{
26
+ distributorPublicKey: PublicKey;
27
+ ixs: TransactionInstruction[];
28
+ }>;
29
+ create(data: ICreateDistributorData | ICreateAlignedDistributorData, extParams: ICreateSolanaExt): Promise<ICreateDistributorResult>;
30
+ claim(data: IClaimData, extParams: IInteractSolanaExt): Promise<ITransactionResult>;
31
+ prepareClaimInstructions(data: IClaimData, extParams: IInteractSolanaExt): Promise<TransactionInstruction[]>;
32
+ prepareClawbackInstructions(data: IClawbackData, extParams: IInteractSolanaExt): Promise<TransactionInstruction[]>;
33
+ clawback(data: IClawbackData, extParams: IInteractSolanaExt): Promise<ITransactionResult>;
34
+ getClaims(data: IGetClaimData[]): Promise<(ClaimStatus | null)[]>;
35
+ getDistributors(data: IGetDistributors): Promise<(MerkleDistributor | null)[]>;
36
+ searchDistributors(data: ISearchDistributors): Promise<IProgramAccount<MerkleDistributor>[]>;
37
+ protected getNewDistributorArgs(data: ICreateDistributorData): NewDistributorArgs;
38
+ }
@@ -1,21 +1,18 @@
1
- import bs58 from "bs58";
2
1
  import BN from "bn.js";
3
- import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT, createTransferCheckedInstruction, createTransferCheckedWithFeeInstruction, getTransferFeeConfig, } from "@solana/spl-token";
4
2
  import { Connection, PublicKey, SystemProgram, } from "@solana/web3.js";
5
3
  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 {
4
+ import { ata, checkOrCreateAtaBatch, prepareBaseInstructions, prepareTransaction, getMintAndProgram, buildSendThrottler, prepareWrappedAccount, } from "@streamflow/common/solana";
5
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, createTransferCheckedInstruction, createTransferCheckedWithFeeInstruction, getTransferFeeConfig, NATIVE_MINT, } from "@solana/spl-token";
6
+ import bs58 from "bs58";
7
+ import { DISTRIBUTOR_ADMIN_OFFSET, DISTRIBUTOR_MINT_OFFSET, DISTRIBUTOR_PROGRAM_ID } from "../constants";
8
+ import { claimLocked, newClaim, } from "../generated/instructions";
9
+ import { ClaimStatus, MerkleDistributor } from "../generated/accounts";
10
+ import { calculateAmountWithTransferFees, getClaimantStatusPda, getDistributorPda, getEventAuthorityPda, wrappedSignAndExecuteTransaction, } from "../utils";
11
+ export default class BaseDistributorClient {
12
12
  connection;
13
13
  programId;
14
14
  commitment;
15
15
  sendThrottler;
16
- /**
17
- * Create Stream instance
18
- */
19
16
  constructor({ clusterUrl, cluster = ICluster.Mainnet, commitment = "confirmed", programId = "", sendRate = 1, sendThrottler, }) {
20
17
  this.commitment = commitment;
21
18
  this.connection = new Connection(clusterUrl, this.commitment);
@@ -25,46 +22,22 @@ export default class SolanaDistributorClient {
25
22
  getCommitment() {
26
23
  return typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
27
24
  }
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
25
  async prepareCreateInstructions(data, extParams) {
43
26
  if (!extParams.invoker.publicKey) {
44
27
  throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
45
28
  }
29
+ const { version, mint } = data;
46
30
  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);
31
+ const mintPublicKey = extParams.isNative ? NATIVE_MINT : new PublicKey(mint);
32
+ const { mint: mintAccount, tokenProgramId } = await getMintAndProgram(this.connection, mintPublicKey);
49
33
  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: new BN(data.version),
55
- root: data.root,
56
- maxTotalClaim: new BN(data.maxTotalClaim),
57
- maxNumNodes: new BN(data.maxNumNodes),
58
- unlockPeriod: new BN(data.unlockPeriod),
59
- startVestingTs: new BN(data.startVestingTs),
60
- endVestingTs: new BN(data.endVestingTs),
61
- clawbackStartTs: new BN(data.clawbackStartTs),
62
- claimsClosable: data.claimsClosable,
63
- };
34
+ const distributorPublicKey = getDistributorPda(this.programId, mintPublicKey, version);
35
+ const tokenVault = await ata(mintPublicKey, distributorPublicKey, tokenProgramId);
36
+ const senderTokens = await ata(mintPublicKey, extParams.invoker.publicKey, tokenProgramId);
64
37
  const accounts = {
65
38
  distributor: distributorPublicKey,
66
39
  clawbackReceiver: senderTokens,
67
- mint,
40
+ mint: mintPublicKey,
68
41
  tokenVault,
69
42
  admin: extParams.invoker.publicKey,
70
43
  systemProgram: SystemProgram.programId,
@@ -74,22 +47,31 @@ export default class SolanaDistributorClient {
74
47
  if (extParams.isNative) {
75
48
  ixs.push(...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, new BN(data.maxTotalClaim))));
76
49
  }
77
- const nowTs = new BN(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.sub(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));
50
+ const newDistributorInstruction = await this.getNewDistributorInstruction(data, accounts);
51
+ ixs.push(newDistributorInstruction);
84
52
  if (transferFeeConfig) {
85
53
  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));
54
+ ixs.push(createTransferCheckedWithFeeInstruction(senderTokens, mintPublicKey, tokenVault, extParams.invoker.publicKey, transferAmount, mintAccount.decimals, feeCharged, undefined, tokenProgramId));
87
55
  }
88
56
  else {
89
- ixs.push(createTransferCheckedInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
57
+ ixs.push(createTransferCheckedInstruction(senderTokens, mintPublicKey, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
90
58
  }
91
59
  return { distributorPublicKey, ixs };
92
60
  }
61
+ async create(data, extParams) {
62
+ const { ixs, distributorPublicKey } = await this.prepareCreateInstructions(data, extParams);
63
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
64
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
65
+ hash,
66
+ context,
67
+ commitment: this.getCommitment(),
68
+ }, { sendThrottler: this.sendThrottler });
69
+ return {
70
+ ixs,
71
+ txId: signature,
72
+ metadataId: distributorPublicKey.toBase58(),
73
+ };
74
+ }
93
75
  async claim(data, extParams) {
94
76
  const ixs = await this.prepareClaimInstructions(data, extParams);
95
77
  const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
@@ -143,16 +125,6 @@ export default class SolanaDistributorClient {
143
125
  }
144
126
  return ixs;
145
127
  }
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
128
  async prepareClawbackInstructions(data, extParams) {
157
129
  if (!extParams.invoker.publicKey) {
158
130
  throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
@@ -174,9 +146,20 @@ export default class SolanaDistributorClient {
174
146
  systemProgram: SystemProgram.programId,
175
147
  tokenProgram: tokenProgramId,
176
148
  };
177
- ixs.push(clawback(accounts, this.programId));
149
+ const clawbackInstruction = await this.getClawbackInstruction(accounts);
150
+ ixs.push(clawbackInstruction);
178
151
  return ixs;
179
152
  }
153
+ async clawback(data, extParams) {
154
+ const ixs = await this.prepareClawbackInstructions(data, extParams);
155
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
156
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
157
+ hash,
158
+ context,
159
+ commitment: this.getCommitment(),
160
+ }, { sendThrottler: this.sendThrottler });
161
+ return { ixs, txId: signature };
162
+ }
180
163
  async getClaims(data) {
181
164
  const claimStatusPublicKeys = data.map(({ id, recipient }) => {
182
165
  return getClaimantStatusPda(this.programId, new PublicKey(id), new PublicKey(recipient));
@@ -211,4 +194,24 @@ export default class SolanaDistributorClient {
211
194
  account: MerkleDistributor.decode(account.data),
212
195
  }));
213
196
  }
197
+ getNewDistributorArgs(data) {
198
+ const args = {
199
+ version: new BN(data.version),
200
+ root: data.root,
201
+ maxTotalClaim: new BN(data.maxTotalClaim),
202
+ maxNumNodes: new BN(data.maxNumNodes),
203
+ unlockPeriod: new BN(data.unlockPeriod),
204
+ startVestingTs: new BN(data.startVestingTs),
205
+ endVestingTs: new BN(data.endVestingTs),
206
+ clawbackStartTs: new BN(data.clawbackStartTs),
207
+ claimsClosable: data.claimsClosable,
208
+ };
209
+ const nowTs = new BN(Math.floor(Date.now() / 1000));
210
+ const endVestingTs = args.endVestingTs.isZero() ? nowTs : args.endVestingTs;
211
+ const startVestingTs = args.startVestingTs.isZero() ? nowTs : args.startVestingTs;
212
+ if (endVestingTs.gt(startVestingTs) && endVestingTs.sub(startVestingTs).lt(args.unlockPeriod)) {
213
+ throw new Error("The unlock period cannot be longer than the total vesting duration!");
214
+ }
215
+ return args;
216
+ }
214
217
  }
@@ -0,0 +1,12 @@
1
+ import { TransactionInstruction } from "@solana/web3.js";
2
+ import { ICreateAlignedDistributorData, AlignedDistributorData, NewAlignedDistributorArgs } from "../types";
3
+ import { ClawbackAccounts, NewDistributorAccounts } from "../generated/instructions";
4
+ import BaseDistributorClient, { IInitOptions } from "./BaseDistributorClient.js";
5
+ export default class SolanaAlignedDistributorClient extends BaseDistributorClient {
6
+ private alignedProxyProgram;
7
+ constructor({ clusterUrl, cluster, commitment, programId, sendRate, sendThrottler }: IInitOptions);
8
+ getAlignedDistributorData(distributorAddress: string): Promise<AlignedDistributorData>;
9
+ protected getNewDistributorInstruction(data: ICreateAlignedDistributorData, accounts: NewDistributorAccounts): Promise<TransactionInstruction>;
10
+ protected getClawbackInstruction(accounts: ClawbackAccounts): Promise<TransactionInstruction>;
11
+ protected getNewAlignedDistributorArgs(data: ICreateAlignedDistributorData): NewAlignedDistributorArgs;
12
+ }
@@ -0,0 +1,93 @@
1
+ import BN from "bn.js";
2
+ import { PublicKey } from "@solana/web3.js";
3
+ import { Program } from "@coral-xyz/anchor";
4
+ import { getBN, getNumberFromBN, invariant } from "@streamflow/common";
5
+ import BaseDistributorClient from "./BaseDistributorClient.js";
6
+ import StreamflowAlignedAirdropsIDL from "../descriptor/idl/aligned_distributor.json";
7
+ import { ALIGNED_PRECISION_FACTOR_POW } from "../constants.js";
8
+ import { getAlignedDistributorPda, getTestOraclePda } from "../utils.js";
9
+ export default class SolanaAlignedDistributorClient extends BaseDistributorClient {
10
+ alignedProxyProgram;
11
+ constructor({ clusterUrl, cluster, commitment, programId, sendRate, sendThrottler }) {
12
+ super({ clusterUrl, cluster, commitment, programId, sendRate, sendThrottler });
13
+ const alignedAirdropsProgram = {
14
+ ...StreamflowAlignedAirdropsIDL,
15
+ };
16
+ this.alignedProxyProgram = new Program(alignedAirdropsProgram, { connection: this.connection });
17
+ }
18
+ async getAlignedDistributorData(distributorAddress) {
19
+ const distributorKey = new PublicKey(distributorAddress);
20
+ const alignedDistributorKey = getAlignedDistributorPda(this.alignedProxyProgram.programId, distributorKey);
21
+ const alignedProxy = await this.alignedProxyProgram.account.alignedDistributor.fetch(alignedDistributorKey);
22
+ invariant(alignedProxy, "Aligned Distributor proxy account not found");
23
+ const oracleType = Object.keys(alignedProxy.priceOracleType).find((key) => !!key);
24
+ return {
25
+ oracleType,
26
+ minPrice: getNumberFromBN(alignedProxy.minPrice, ALIGNED_PRECISION_FACTOR_POW),
27
+ maxPrice: getNumberFromBN(alignedProxy.maxPrice, ALIGNED_PRECISION_FACTOR_POW),
28
+ minPercentage: getNumberFromBN(alignedProxy.minPercentage, ALIGNED_PRECISION_FACTOR_POW),
29
+ maxPercentage: getNumberFromBN(alignedProxy.maxPercentage, ALIGNED_PRECISION_FACTOR_POW),
30
+ priceOracle: oracleType === "none" ? undefined : alignedProxy.priceOracle.toBase58(),
31
+ sender: alignedProxy.admin.toBase58(),
32
+ updatePeriod: getNumberFromBN(alignedProxy.updatePeriod, ALIGNED_PRECISION_FACTOR_POW),
33
+ clawedBack: alignedProxy.distributorClawedBack,
34
+ };
35
+ }
36
+ async getNewDistributorInstruction(data, accounts) {
37
+ const { distributor, mint, clawbackReceiver, tokenProgram, tokenVault, admin } = accounts;
38
+ const baseArgs = this.getNewDistributorArgs(data);
39
+ const alignedArgs = this.getNewAlignedDistributorArgs(data);
40
+ const oracle = data.oracleAddress
41
+ ? new PublicKey(data.oracleAddress)
42
+ : getTestOraclePda(this.alignedProxyProgram.programId, mint, admin);
43
+ const newDistributorIx = await this.alignedProxyProgram.methods
44
+ .newDistributor({
45
+ ...baseArgs,
46
+ ...alignedArgs,
47
+ })
48
+ .accounts({
49
+ admin,
50
+ tokenVault,
51
+ distributor,
52
+ clawbackReceiver,
53
+ mint,
54
+ tokenProgram,
55
+ priceOracle: oracle,
56
+ })
57
+ .instruction();
58
+ return newDistributorIx;
59
+ }
60
+ async getClawbackInstruction(accounts) {
61
+ const { distributor, from, to, mint, tokenProgram, admin } = accounts;
62
+ const alignedDistributorKey = getAlignedDistributorPda(this.alignedProxyProgram.programId, distributor);
63
+ const alignedProxy = await this.alignedProxyProgram.account.alignedDistributor.fetch(alignedDistributorKey);
64
+ invariant(alignedProxy, "Aligned Distributor proxy account not found");
65
+ const clawbackInstruction = await this.alignedProxyProgram.methods
66
+ .clawback()
67
+ .accounts({
68
+ admin,
69
+ distributor,
70
+ from,
71
+ to,
72
+ mint,
73
+ tokenProgram,
74
+ })
75
+ .instruction();
76
+ return clawbackInstruction;
77
+ }
78
+ getNewAlignedDistributorArgs(data) {
79
+ const { oracleType, minPrice, maxPrice, minPercentage, maxPercentage, tickSize, skipInitial, totalAmountLocked, totalAmountUnlocked, unlockPeriod, } = data;
80
+ return {
81
+ totalAmountLocked: new BN(totalAmountLocked),
82
+ totalAmountUnlocked: new BN(totalAmountUnlocked),
83
+ oracleType: (!!oracleType ? { [oracleType]: {} } : { none: {} }),
84
+ minPrice: getBN(minPrice, ALIGNED_PRECISION_FACTOR_POW),
85
+ maxPrice: getBN(maxPrice, ALIGNED_PRECISION_FACTOR_POW),
86
+ minPercentage: getBN(minPercentage, ALIGNED_PRECISION_FACTOR_POW),
87
+ maxPercentage: getBN(maxPercentage, ALIGNED_PRECISION_FACTOR_POW),
88
+ tickSize: new BN(tickSize || 1),
89
+ skipInitial: skipInitial ?? false,
90
+ updatePeriod: unlockPeriod < 30 ? new BN(30) : new BN(unlockPeriod),
91
+ };
92
+ }
93
+ }
@@ -0,0 +1,8 @@
1
+ import { TransactionInstruction } from "@solana/web3.js";
2
+ import { ICreateDistributorData } from "../types";
3
+ import { ClawbackAccounts, NewDistributorAccounts } from "../generated/instructions";
4
+ import BaseDistributorClient from "./BaseDistributorClient.js";
5
+ export default class SolanaDistributorClient extends BaseDistributorClient {
6
+ protected getNewDistributorInstruction(data: ICreateDistributorData, accounts: NewDistributorAccounts): Promise<TransactionInstruction>;
7
+ protected getClawbackInstruction(accounts: ClawbackAccounts): Promise<TransactionInstruction>;
8
+ }
@@ -0,0 +1,11 @@
1
+ import { clawback, newDistributor } from "../generated/instructions";
2
+ import BaseDistributorClient from "./BaseDistributorClient.js";
3
+ export default class SolanaDistributorClient extends BaseDistributorClient {
4
+ async getNewDistributorInstruction(data, accounts) {
5
+ const args = this.getNewDistributorArgs(data);
6
+ return newDistributor(args, accounts, this.programId);
7
+ }
8
+ async getClawbackInstruction(accounts) {
9
+ return clawback(accounts, this.programId);
10
+ }
11
+ }
@@ -0,0 +1,2 @@
1
+ export { default as SolanaDistributorClient } from "./SolanaDistributorClient.js";
2
+ export { default as SolanaAlignedDistributorClient } from "./SolanaAlignedDistributorClient.js";
@@ -0,0 +1,2 @@
1
+ export { default as SolanaDistributorClient } from "./SolanaDistributorClient.js";
2
+ export { default as SolanaAlignedDistributorClient } from "./SolanaAlignedDistributorClient.js";
@@ -1,6 +1,10 @@
1
+ /// <reference types="node" />
1
2
  export declare const ANCHOR_DISCRIMINATOR_OFFSET = 8;
2
3
  export declare const DISTRIBUTOR_MINT_OFFSET: number;
3
4
  export declare const DISTRIBUTOR_ADMIN_OFFSET: number;
5
+ export declare const ALIGNED_DISTRIBUTOR_PREFIX: Buffer;
6
+ export declare const TEST_ORACLE_PREFIX: Buffer;
7
+ export declare const ALIGNED_PRECISION_FACTOR_POW = 9;
4
8
  export declare const ONE_IN_BASIS_POINTS: bigint;
5
9
  export declare const DISTRIBUTOR_PROGRAM_ID: {
6
10
  devnet: string;
@@ -2,6 +2,9 @@ import { ICluster } from "@streamflow/common";
2
2
  export const ANCHOR_DISCRIMINATOR_OFFSET = 8;
3
3
  export const DISTRIBUTOR_MINT_OFFSET = ANCHOR_DISCRIMINATOR_OFFSET + 41;
4
4
  export const DISTRIBUTOR_ADMIN_OFFSET = ANCHOR_DISCRIMINATOR_OFFSET + 201;
5
+ export const ALIGNED_DISTRIBUTOR_PREFIX = Buffer.from("aligned-distributor", "utf-8");
6
+ export const TEST_ORACLE_PREFIX = Buffer.from("test-oracle", "utf-8");
7
+ export const ALIGNED_PRECISION_FACTOR_POW = 9;
5
8
  export const ONE_IN_BASIS_POINTS = BigInt(10000);
6
9
  export const DISTRIBUTOR_PROGRAM_ID = {
7
10
  [ICluster.Devnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",