@streamflow/distributor 7.0.0-alpha.16 → 7.0.0-alpha.18

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 (29) hide show
  1. package/dist/cjs/solana/{client.js → clients/BaseDistributorClient.js} +67 -61
  2. package/dist/cjs/solana/clients/SolanaAlignedDistributorClient.js +104 -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 +12 -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/index.js +2 -6
  9. package/dist/cjs/solana/utils.js +16 -8
  10. package/dist/esm/solana/clients/BaseDistributorClient.d.ts +39 -0
  11. package/dist/esm/solana/{client.js → clients/BaseDistributorClient.js} +67 -61
  12. package/dist/esm/solana/clients/SolanaAlignedDistributorClient.d.ts +14 -0
  13. package/dist/esm/solana/clients/SolanaAlignedDistributorClient.js +99 -0
  14. package/dist/esm/solana/clients/SolanaDistributorClient.d.ts +8 -0
  15. package/dist/esm/solana/clients/SolanaDistributorClient.js +11 -0
  16. package/dist/esm/solana/clients/index.d.ts +2 -0
  17. package/dist/esm/solana/clients/index.js +2 -0
  18. package/dist/esm/solana/constants.d.ts +12 -0
  19. package/dist/esm/solana/constants.js +11 -0
  20. package/dist/esm/solana/descriptor/aligned_distributor.d.ts +918 -0
  21. package/dist/esm/solana/descriptor/aligned_distributor.js +1 -0
  22. package/dist/esm/solana/descriptor/idl/aligned_distributor.json +1242 -0
  23. package/dist/esm/solana/index.d.ts +1 -1
  24. package/dist/esm/solana/index.js +1 -1
  25. package/dist/esm/solana/types.d.ts +40 -0
  26. package/dist/esm/solana/utils.d.ts +2 -0
  27. package/dist/esm/solana/utils.js +10 -4
  28. package/package.json +5 -4
  29. package/dist/esm/solana/client.d.ts +0 -38
@@ -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,16 +1,24 @@
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 = [
13
- buffer_1.Buffer.from("MerkleDistributor"),
21
+ constants_js_1.DISTRIBUTOR_PREFIX,
14
22
  mint.toBuffer(),
15
23
  buffer_1.Buffer.from(new Uint8Array(new BigUint64Array([BigInt(version)]).buffer)),
16
24
  ];
@@ -20,7 +28,7 @@ function getDistributorPda(programId, mint, version) {
20
28
  exports.getDistributorPda = getDistributorPda;
21
29
  function getClaimantStatusPda(programId, distributor, claimant) {
22
30
  // Constructing the seed for the PDA
23
- const seeds = [buffer_1.Buffer.from("ClaimStatus"), claimant.toBuffer(), distributor.toBuffer()];
31
+ const seeds = [constants_js_1.CLAIM_STATUS_PREFIX, claimant.toBuffer(), distributor.toBuffer()];
24
32
  // Finding the PDA
25
33
  return web3_js_1.PublicKey.findProgramAddressSync(seeds, programId)[0];
26
34
  }
@@ -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,39 @@
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
+ getDistributorProgramId(): PublicKey;
26
+ prepareCreateInstructions(data: ICreateDistributorData | ICreateAlignedDistributorData, extParams: ICreateSolanaExt): Promise<{
27
+ distributorPublicKey: PublicKey;
28
+ ixs: TransactionInstruction[];
29
+ }>;
30
+ create(data: ICreateDistributorData | ICreateAlignedDistributorData, extParams: ICreateSolanaExt): Promise<ICreateDistributorResult>;
31
+ claim(data: IClaimData, extParams: IInteractSolanaExt): Promise<ITransactionResult>;
32
+ prepareClaimInstructions(data: IClaimData, extParams: IInteractSolanaExt): Promise<TransactionInstruction[]>;
33
+ prepareClawbackInstructions(data: IClawbackData, extParams: IInteractSolanaExt): Promise<TransactionInstruction[]>;
34
+ clawback(data: IClawbackData, extParams: IInteractSolanaExt): Promise<ITransactionResult>;
35
+ getClaims(data: IGetClaimData[]): Promise<(ClaimStatus | null)[]>;
36
+ getDistributors(data: IGetDistributors): Promise<(MerkleDistributor | null)[]>;
37
+ searchDistributors(data: ISearchDistributors): Promise<IProgramAccount<MerkleDistributor>[]>;
38
+ protected getNewDistributorArgs(data: ICreateDistributorData): NewDistributorArgs;
39
+ }
@@ -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,25 @@ 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
- };
25
+ getDistributorProgramId() {
26
+ return this.programId;
41
27
  }
42
28
  async prepareCreateInstructions(data, extParams) {
43
29
  if (!extParams.invoker.publicKey) {
44
30
  throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
45
31
  }
32
+ const { version, mint } = data;
46
33
  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);
34
+ const mintPublicKey = extParams.isNative ? NATIVE_MINT : new PublicKey(mint);
35
+ const { mint: mintAccount, tokenProgramId } = await getMintAndProgram(this.connection, mintPublicKey);
49
36
  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
- };
37
+ const distributorPublicKey = getDistributorPda(this.programId, mintPublicKey, version);
38
+ const tokenVault = await ata(mintPublicKey, distributorPublicKey, tokenProgramId);
39
+ const senderTokens = await ata(mintPublicKey, extParams.invoker.publicKey, tokenProgramId);
64
40
  const accounts = {
65
41
  distributor: distributorPublicKey,
66
42
  clawbackReceiver: senderTokens,
67
- mint,
43
+ mint: mintPublicKey,
68
44
  tokenVault,
69
45
  admin: extParams.invoker.publicKey,
70
46
  systemProgram: SystemProgram.programId,
@@ -74,22 +50,31 @@ export default class SolanaDistributorClient {
74
50
  if (extParams.isNative) {
75
51
  ixs.push(...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, new BN(data.maxTotalClaim))));
76
52
  }
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));
53
+ const newDistributorInstruction = await this.getNewDistributorInstruction(data, accounts);
54
+ ixs.push(newDistributorInstruction);
84
55
  if (transferFeeConfig) {
85
56
  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));
57
+ ixs.push(createTransferCheckedWithFeeInstruction(senderTokens, mintPublicKey, tokenVault, extParams.invoker.publicKey, transferAmount, mintAccount.decimals, feeCharged, undefined, tokenProgramId));
87
58
  }
88
59
  else {
89
- ixs.push(createTransferCheckedInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
60
+ ixs.push(createTransferCheckedInstruction(senderTokens, mintPublicKey, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
90
61
  }
91
62
  return { distributorPublicKey, ixs };
92
63
  }
64
+ async create(data, extParams) {
65
+ const { ixs, distributorPublicKey } = await this.prepareCreateInstructions(data, extParams);
66
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
67
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
68
+ hash,
69
+ context,
70
+ commitment: this.getCommitment(),
71
+ }, { sendThrottler: this.sendThrottler });
72
+ return {
73
+ ixs,
74
+ txId: signature,
75
+ metadataId: distributorPublicKey.toBase58(),
76
+ };
77
+ }
93
78
  async claim(data, extParams) {
94
79
  const ixs = await this.prepareClaimInstructions(data, extParams);
95
80
  const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
@@ -143,16 +128,6 @@ export default class SolanaDistributorClient {
143
128
  }
144
129
  return ixs;
145
130
  }
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
131
  async prepareClawbackInstructions(data, extParams) {
157
132
  if (!extParams.invoker.publicKey) {
158
133
  throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
@@ -174,9 +149,20 @@ export default class SolanaDistributorClient {
174
149
  systemProgram: SystemProgram.programId,
175
150
  tokenProgram: tokenProgramId,
176
151
  };
177
- ixs.push(clawback(accounts, this.programId));
152
+ const clawbackInstruction = await this.getClawbackInstruction(accounts);
153
+ ixs.push(clawbackInstruction);
178
154
  return ixs;
179
155
  }
156
+ async clawback(data, extParams) {
157
+ const ixs = await this.prepareClawbackInstructions(data, extParams);
158
+ const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
159
+ const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
160
+ hash,
161
+ context,
162
+ commitment: this.getCommitment(),
163
+ }, { sendThrottler: this.sendThrottler });
164
+ return { ixs, txId: signature };
165
+ }
180
166
  async getClaims(data) {
181
167
  const claimStatusPublicKeys = data.map(({ id, recipient }) => {
182
168
  return getClaimantStatusPda(this.programId, new PublicKey(id), new PublicKey(recipient));
@@ -211,4 +197,24 @@ export default class SolanaDistributorClient {
211
197
  account: MerkleDistributor.decode(account.data),
212
198
  }));
213
199
  }
200
+ getNewDistributorArgs(data) {
201
+ const args = {
202
+ version: new BN(data.version),
203
+ root: data.root,
204
+ maxTotalClaim: new BN(data.maxTotalClaim),
205
+ maxNumNodes: new BN(data.maxNumNodes),
206
+ unlockPeriod: new BN(data.unlockPeriod),
207
+ startVestingTs: new BN(data.startVestingTs),
208
+ endVestingTs: new BN(data.endVestingTs),
209
+ clawbackStartTs: new BN(data.clawbackStartTs),
210
+ claimsClosable: data.claimsClosable,
211
+ };
212
+ const nowTs = new BN(Math.floor(Date.now() / 1000));
213
+ const endVestingTs = args.endVestingTs.isZero() ? nowTs : args.endVestingTs;
214
+ const startVestingTs = args.startVestingTs.isZero() ? nowTs : args.startVestingTs;
215
+ if (endVestingTs.gt(startVestingTs) && endVestingTs.sub(startVestingTs).lt(args.unlockPeriod)) {
216
+ throw new Error("The unlock period cannot be longer than the total vesting duration!");
217
+ }
218
+ return args;
219
+ }
214
220
  }
@@ -0,0 +1,14 @@
1
+ import { PublicKey, 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
+ getAlignedDistributorProgramId(): PublicKey;
9
+ getAlignedDistributorAddress(distributorAddress: string): PublicKey;
10
+ getAlignedDistributorData(distributorAddress: string): Promise<AlignedDistributorData>;
11
+ protected getNewDistributorInstruction(data: ICreateAlignedDistributorData, accounts: NewDistributorAccounts): Promise<TransactionInstruction>;
12
+ protected getClawbackInstruction(accounts: ClawbackAccounts): Promise<TransactionInstruction>;
13
+ protected getNewAlignedDistributorArgs(data: ICreateAlignedDistributorData): NewAlignedDistributorArgs;
14
+ }
@@ -0,0 +1,99 @@
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
+ getAlignedDistributorProgramId() {
19
+ return this.alignedProxyProgram.programId;
20
+ }
21
+ getAlignedDistributorAddress(distributorAddress) {
22
+ const distributorKey = new PublicKey(distributorAddress);
23
+ return getAlignedDistributorPda(this.alignedProxyProgram.programId, distributorKey);
24
+ }
25
+ async getAlignedDistributorData(distributorAddress) {
26
+ const alignedDistributorKey = this.getAlignedDistributorAddress(distributorAddress);
27
+ const alignedProxy = await this.alignedProxyProgram.account.alignedDistributor.fetch(alignedDistributorKey);
28
+ invariant(alignedProxy, "Aligned Distributor proxy account not found");
29
+ const oracleType = Object.keys(alignedProxy.priceOracleType).find((key) => !!key);
30
+ return {
31
+ oracleType,
32
+ minPrice: getNumberFromBN(alignedProxy.minPrice, ALIGNED_PRECISION_FACTOR_POW),
33
+ maxPrice: getNumberFromBN(alignedProxy.maxPrice, ALIGNED_PRECISION_FACTOR_POW),
34
+ minPercentage: getNumberFromBN(alignedProxy.minPercentage, ALIGNED_PRECISION_FACTOR_POW),
35
+ maxPercentage: getNumberFromBN(alignedProxy.maxPercentage, ALIGNED_PRECISION_FACTOR_POW),
36
+ priceOracle: oracleType === "none" ? undefined : alignedProxy.priceOracle.toBase58(),
37
+ sender: alignedProxy.admin.toBase58(),
38
+ updatePeriod: getNumberFromBN(alignedProxy.updatePeriod, ALIGNED_PRECISION_FACTOR_POW),
39
+ clawedBack: alignedProxy.distributorClawedBack,
40
+ };
41
+ }
42
+ async getNewDistributorInstruction(data, accounts) {
43
+ const { distributor, mint, clawbackReceiver, tokenProgram, tokenVault, admin } = accounts;
44
+ const baseArgs = this.getNewDistributorArgs(data);
45
+ const alignedArgs = this.getNewAlignedDistributorArgs(data);
46
+ const oracle = data.oracleAddress
47
+ ? new PublicKey(data.oracleAddress)
48
+ : getTestOraclePda(this.alignedProxyProgram.programId, mint, admin);
49
+ const newDistributorIx = await this.alignedProxyProgram.methods
50
+ .newDistributor({
51
+ ...baseArgs,
52
+ ...alignedArgs,
53
+ })
54
+ .accounts({
55
+ admin,
56
+ tokenVault,
57
+ distributor,
58
+ clawbackReceiver,
59
+ mint,
60
+ tokenProgram,
61
+ priceOracle: oracle,
62
+ })
63
+ .instruction();
64
+ return newDistributorIx;
65
+ }
66
+ async getClawbackInstruction(accounts) {
67
+ const { distributor, from, to, mint, tokenProgram, admin } = accounts;
68
+ const alignedDistributorKey = getAlignedDistributorPda(this.alignedProxyProgram.programId, distributor);
69
+ const alignedProxy = await this.alignedProxyProgram.account.alignedDistributor.fetch(alignedDistributorKey);
70
+ invariant(alignedProxy, "Aligned Distributor proxy account not found");
71
+ const clawbackInstruction = await this.alignedProxyProgram.methods
72
+ .clawback()
73
+ .accounts({
74
+ admin,
75
+ distributor,
76
+ from,
77
+ to,
78
+ mint,
79
+ tokenProgram,
80
+ })
81
+ .instruction();
82
+ return clawbackInstruction;
83
+ }
84
+ getNewAlignedDistributorArgs(data) {
85
+ const { oracleType, minPrice, maxPrice, minPercentage, maxPercentage, tickSize, skipInitial, totalAmountLocked, totalAmountUnlocked, unlockPeriod, } = data;
86
+ return {
87
+ totalAmountLocked: new BN(totalAmountLocked),
88
+ totalAmountUnlocked: new BN(totalAmountUnlocked),
89
+ oracleType: (!!oracleType ? { [oracleType]: {} } : { none: {} }),
90
+ minPrice: getBN(minPrice, ALIGNED_PRECISION_FACTOR_POW),
91
+ maxPrice: getBN(maxPrice, ALIGNED_PRECISION_FACTOR_POW),
92
+ minPercentage: getBN(minPercentage, ALIGNED_PRECISION_FACTOR_POW),
93
+ maxPercentage: getBN(maxPercentage, ALIGNED_PRECISION_FACTOR_POW),
94
+ tickSize: new BN(tickSize || 1),
95
+ skipInitial: skipInitial ?? false,
96
+ updatePeriod: unlockPeriod < 30 ? new BN(30) : new BN(unlockPeriod),
97
+ };
98
+ }
99
+ }
@@ -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,7 +1,19 @@
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 DISTRIBUTOR_PREFIX: Buffer;
6
+ export declare const ALIGNED_DISTRIBUTOR_PREFIX: Buffer;
7
+ export declare const TEST_ORACLE_PREFIX: Buffer;
8
+ export declare const CLAIM_STATUS_PREFIX: Buffer;
9
+ export declare const ALIGNED_PRECISION_FACTOR_POW = 9;
4
10
  export declare const ONE_IN_BASIS_POINTS: bigint;
11
+ export declare const ALIGNED_DISTRIBUTOR_PROGRAM_ID: {
12
+ devnet: string;
13
+ mainnet: string;
14
+ testnet: string;
15
+ local: string;
16
+ };
5
17
  export declare const DISTRIBUTOR_PROGRAM_ID: {
6
18
  devnet: string;
7
19
  mainnet: string;
@@ -2,7 +2,18 @@ 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 DISTRIBUTOR_PREFIX = Buffer.from("MerkleDistributor", "utf-8");
6
+ export const ALIGNED_DISTRIBUTOR_PREFIX = Buffer.from("aligned-distributor", "utf-8");
7
+ export const TEST_ORACLE_PREFIX = Buffer.from("test-oracle", "utf-8");
8
+ export const CLAIM_STATUS_PREFIX = Buffer.from("ClaimStatus", "utf-8");
9
+ export const ALIGNED_PRECISION_FACTOR_POW = 9;
5
10
  export const ONE_IN_BASIS_POINTS = BigInt(10000);
11
+ export const ALIGNED_DISTRIBUTOR_PROGRAM_ID = {
12
+ [ICluster.Devnet]: "aMERKpFAWoChCi5oZwPvgsSCoGpZKBiU7fi76bdZjt2",
13
+ [ICluster.Mainnet]: "aMERKpFAWoChCi5oZwPvgsSCoGpZKBiU7fi76bdZjt2",
14
+ [ICluster.Testnet]: "aMERKpFAWoChCi5oZwPvgsSCoGpZKBiU7fi76bdZjt2",
15
+ [ICluster.Local]: "aMERKpFAWoChCi5oZwPvgsSCoGpZKBiU7fi76bdZjt2",
16
+ };
6
17
  export const DISTRIBUTOR_PROGRAM_ID = {
7
18
  [ICluster.Devnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
8
19
  [ICluster.Mainnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",