@streamflow/distributor 7.0.0-alpha.16 → 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.
- package/dist/cjs/solana/{client.js → clients/BaseDistributorClient.js} +65 -62
- package/dist/cjs/solana/clients/SolanaAlignedDistributorClient.js +98 -0
- package/dist/cjs/solana/clients/SolanaDistributorClient.js +17 -0
- package/dist/cjs/solana/clients/index.js +10 -0
- package/dist/cjs/solana/constants.js +4 -1
- package/dist/cjs/solana/descriptor/aligned_distributor.js +2 -0
- package/dist/cjs/solana/descriptor/idl/aligned_distributor.json +1242 -0
- package/dist/cjs/solana/index.js +2 -6
- package/dist/cjs/solana/utils.js +14 -6
- package/dist/esm/solana/clients/BaseDistributorClient.d.ts +38 -0
- package/dist/esm/solana/{client.js → clients/BaseDistributorClient.js} +65 -62
- package/dist/esm/solana/clients/SolanaAlignedDistributorClient.d.ts +12 -0
- package/dist/esm/solana/clients/SolanaAlignedDistributorClient.js +93 -0
- package/dist/esm/solana/clients/SolanaDistributorClient.d.ts +8 -0
- package/dist/esm/solana/clients/SolanaDistributorClient.js +11 -0
- package/dist/esm/solana/clients/index.d.ts +2 -0
- package/dist/esm/solana/clients/index.js +2 -0
- package/dist/esm/solana/constants.d.ts +4 -0
- package/dist/esm/solana/constants.js +3 -0
- package/dist/esm/solana/descriptor/aligned_distributor.d.ts +918 -0
- package/dist/esm/solana/descriptor/aligned_distributor.js +1 -0
- package/dist/esm/solana/descriptor/idl/aligned_distributor.json +1242 -0
- package/dist/esm/solana/index.d.ts +1 -1
- package/dist/esm/solana/index.js +1 -1
- package/dist/esm/solana/types.d.ts +40 -0
- package/dist/esm/solana/utils.d.ts +2 -0
- package/dist/esm/solana/utils.js +8 -2
- package/package.json +5 -4
- package/dist/esm/solana/client.d.ts +0 -38
package/dist/cjs/solana/index.js
CHANGED
|
@@ -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 =
|
|
33
|
-
|
|
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);
|
package/dist/cjs/solana/utils.js
CHANGED
|
@@ -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
|
|
9
|
-
const
|
|
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,
|
|
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 *
|
|
59
|
-
const denominator =
|
|
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,
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
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
|
|
48
|
-
const { mint: mintAccount, tokenProgramId } = await getMintAndProgram(this.connection,
|
|
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,
|
|
51
|
-
const tokenVault = await ata(
|
|
52
|
-
const senderTokens = await ata(
|
|
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
|
|
78
|
-
|
|
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,
|
|
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,
|
|
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
|
-
|
|
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
|
+
}
|
|
@@ -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",
|