@streamflow/distributor 6.3.8 → 7.0.0-alpha.1
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/{solana → cjs/solana}/generated/instructions/clawback.js +2 -1
- package/dist/{solana → cjs/solana}/generated/instructions/closeClaim.js +2 -1
- package/dist/{solana → cjs/solana}/generated/instructions/newDistributor.js +4 -3
- package/dist/{solana → cjs/solana}/generated/instructions/setAdmin.js +2 -1
- package/dist/{solana → cjs/solana}/generated/instructions/setClawbackReceiver.js +2 -1
- package/dist/{solana → cjs/solana}/utils.js +5 -4
- package/dist/esm/index.js +1 -0
- package/dist/esm/solana/client.js +188 -0
- package/dist/esm/solana/constants.js +8 -0
- package/dist/esm/solana/generated/accounts/ClaimStatus.js +113 -0
- package/dist/esm/solana/generated/accounts/MerkleDistributor.js +190 -0
- package/dist/esm/solana/generated/accounts/index.js +2 -0
- package/dist/esm/solana/generated/errors/anchor.js +707 -0
- package/dist/esm/solana/generated/errors/custom.js +252 -0
- package/dist/esm/solana/generated/errors/index.js +37 -0
- package/dist/esm/solana/generated/instructions/claimLocked.js +19 -0
- package/dist/esm/solana/generated/instructions/clawback.js +18 -0
- package/dist/esm/solana/generated/instructions/closeClaim.js +18 -0
- package/dist/esm/solana/generated/instructions/index.js +7 -0
- package/dist/esm/solana/generated/instructions/newClaim.js +32 -0
- package/dist/esm/solana/generated/instructions/newDistributor.js +67 -0
- package/dist/esm/solana/generated/instructions/setAdmin.js +14 -0
- package/dist/esm/solana/generated/instructions/setClawbackReceiver.js +14 -0
- package/dist/esm/solana/generated/programId.js +5 -0
- package/dist/esm/solana/index.js +7 -0
- package/dist/esm/solana/types.js +156 -0
- package/dist/esm/solana/utils.js +60 -0
- package/package.json +20 -19
- /package/dist/{index.js → cjs/index.js} +0 -0
- /package/dist/{solana → cjs/solana}/client.js +0 -0
- /package/dist/{solana → cjs/solana}/constants.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/accounts/ClaimStatus.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/accounts/MerkleDistributor.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/accounts/index.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/errors/anchor.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/errors/custom.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/errors/index.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/instructions/claimLocked.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/instructions/index.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/instructions/newClaim.js +0 -0
- /package/dist/{solana → cjs/solana}/generated/programId.js +0 -0
- /package/dist/{solana → cjs/solana}/index.js +0 -0
- /package/dist/{solana → cjs/solana}/types.js +0 -0
- /package/dist/{index.d.ts → esm/index.d.ts} +0 -0
- /package/dist/{solana → esm/solana}/client.d.ts +0 -0
- /package/dist/{solana → esm/solana}/constants.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/accounts/ClaimStatus.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/accounts/MerkleDistributor.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/accounts/index.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/errors/anchor.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/errors/custom.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/errors/index.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/claimLocked.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/clawback.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/closeClaim.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/index.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/newClaim.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/newDistributor.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/setAdmin.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/instructions/setClawbackReceiver.d.ts +0 -0
- /package/dist/{solana → esm/solana}/generated/programId.d.ts +0 -0
- /package/dist/{solana → esm/solana}/index.d.ts +0 -0
- /package/dist/{solana → esm/solana}/types.d.ts +0 -0
- /package/dist/{solana → esm/solana}/utils.d.ts +0 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.clawback = void 0;
|
|
4
4
|
var web3_js_1 = require("@solana/web3.js"); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
|
+
var buffer_1 = require("buffer");
|
|
5
6
|
var programId_1 = require("../programId");
|
|
6
7
|
function clawback(accounts, programId) {
|
|
7
8
|
if (programId === void 0) { programId = programId_1.PROGRAM_ID; }
|
|
@@ -14,7 +15,7 @@ function clawback(accounts, programId) {
|
|
|
14
15
|
{ pubkey: accounts.systemProgram, isSigner: false, isWritable: false },
|
|
15
16
|
{ pubkey: accounts.tokenProgram, isSigner: false, isWritable: false },
|
|
16
17
|
];
|
|
17
|
-
var identifier = Buffer.from([111, 92, 142, 79, 33, 234, 82, 27]);
|
|
18
|
+
var identifier = buffer_1.Buffer.from([111, 92, 142, 79, 33, 234, 82, 27]);
|
|
18
19
|
var data = identifier;
|
|
19
20
|
var ix = new web3_js_1.TransactionInstruction({ keys: keys, programId: programId, data: data });
|
|
20
21
|
return ix;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.closeClaim = void 0;
|
|
4
4
|
var web3_js_1 = require("@solana/web3.js"); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
|
+
var buffer_1 = require("buffer");
|
|
5
6
|
var programId_1 = require("../programId");
|
|
6
7
|
function closeClaim(accounts, programId) {
|
|
7
8
|
if (programId === void 0) { programId = programId_1.PROGRAM_ID; }
|
|
@@ -14,7 +15,7 @@ function closeClaim(accounts, programId) {
|
|
|
14
15
|
{ pubkey: accounts.eventAuthority, isSigner: false, isWritable: false },
|
|
15
16
|
{ pubkey: accounts.program, isSigner: false, isWritable: false },
|
|
16
17
|
];
|
|
17
|
-
var identifier = Buffer.from([42, 177, 165, 35, 213, 179, 211, 19]);
|
|
18
|
+
var identifier = buffer_1.Buffer.from([42, 177, 165, 35, 213, 179, 211, 19]);
|
|
18
19
|
var data = identifier;
|
|
19
20
|
var ix = new web3_js_1.TransactionInstruction({ keys: keys, programId: programId, data: data });
|
|
20
21
|
return ix;
|
|
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.newDistributor = exports.layout = void 0;
|
|
27
27
|
var web3_js_1 = require("@solana/web3.js"); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
28
28
|
var borsh = __importStar(require("@coral-xyz/borsh")); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
29
|
+
var buffer_1 = require("buffer");
|
|
29
30
|
var programId_1 = require("../programId");
|
|
30
31
|
exports.layout = borsh.struct([
|
|
31
32
|
borsh.u64("version"),
|
|
@@ -74,8 +75,8 @@ function newDistributor(args, accounts, programId) {
|
|
|
74
75
|
},
|
|
75
76
|
{ pubkey: accounts.tokenProgram, isSigner: false, isWritable: false },
|
|
76
77
|
];
|
|
77
|
-
var identifier = Buffer.from([32, 139, 112, 171, 0, 2, 225, 155]);
|
|
78
|
-
var buffer = Buffer.alloc(1000);
|
|
78
|
+
var identifier = buffer_1.Buffer.from([32, 139, 112, 171, 0, 2, 225, 155]);
|
|
79
|
+
var buffer = buffer_1.Buffer.alloc(1000);
|
|
79
80
|
var len = exports.layout.encode({
|
|
80
81
|
version: args.version,
|
|
81
82
|
root: args.root,
|
|
@@ -87,7 +88,7 @@ function newDistributor(args, accounts, programId) {
|
|
|
87
88
|
clawbackStartTs: args.clawbackStartTs,
|
|
88
89
|
claimsClosable: args.claimsClosable,
|
|
89
90
|
}, buffer);
|
|
90
|
-
var data = Buffer.concat([identifier, buffer]).slice(0, 8 + len);
|
|
91
|
+
var data = buffer_1.Buffer.concat([identifier, buffer]).slice(0, 8 + len);
|
|
91
92
|
var ix = new web3_js_1.TransactionInstruction({ keys: keys, programId: programId, data: data });
|
|
92
93
|
return ix;
|
|
93
94
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.setAdmin = void 0;
|
|
4
4
|
var web3_js_1 = require("@solana/web3.js"); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
|
+
var buffer_1 = require("buffer");
|
|
5
6
|
var programId_1 = require("../programId");
|
|
6
7
|
function setAdmin(accounts, programId) {
|
|
7
8
|
if (programId === void 0) { programId = programId_1.PROGRAM_ID; }
|
|
@@ -10,7 +11,7 @@ function setAdmin(accounts, programId) {
|
|
|
10
11
|
{ pubkey: accounts.admin, isSigner: true, isWritable: true },
|
|
11
12
|
{ pubkey: accounts.newAdmin, isSigner: false, isWritable: true },
|
|
12
13
|
];
|
|
13
|
-
var identifier = Buffer.from([251, 163, 0, 52, 91, 194, 187, 92]);
|
|
14
|
+
var identifier = buffer_1.Buffer.from([251, 163, 0, 52, 91, 194, 187, 92]);
|
|
14
15
|
var data = identifier;
|
|
15
16
|
var ix = new web3_js_1.TransactionInstruction({ keys: keys, programId: programId, data: data });
|
|
16
17
|
return ix;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.setClawbackReceiver = void 0;
|
|
4
4
|
var web3_js_1 = require("@solana/web3.js"); // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
5
|
+
var buffer_1 = require("buffer");
|
|
5
6
|
var programId_1 = require("../programId");
|
|
6
7
|
function setClawbackReceiver(accounts, programId) {
|
|
7
8
|
if (programId === void 0) { programId = programId_1.PROGRAM_ID; }
|
|
@@ -10,7 +11,7 @@ function setClawbackReceiver(accounts, programId) {
|
|
|
10
11
|
{ pubkey: accounts.newClawbackAccount, isSigner: false, isWritable: false },
|
|
11
12
|
{ pubkey: accounts.admin, isSigner: true, isWritable: true },
|
|
12
13
|
];
|
|
13
|
-
var identifier = Buffer.from([153, 217, 34, 20, 19, 29, 229, 75]);
|
|
14
|
+
var identifier = buffer_1.Buffer.from([153, 217, 34, 20, 19, 29, 229, 75]);
|
|
14
15
|
var data = identifier;
|
|
15
16
|
var ix = new web3_js_1.TransactionInstruction({ keys: keys, programId: programId, data: data });
|
|
16
17
|
return ix;
|
|
@@ -37,6 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.calculateAmountWithTransferFees = exports.wrappedSignAndExecuteTransaction = exports.getEventAuthorityPda = exports.getClaimantStatusPda = exports.getDistributorPda = exports.divCeilN = void 0;
|
|
40
|
+
var buffer_1 = require("buffer");
|
|
40
41
|
var web3_js_1 = require("@solana/web3.js");
|
|
41
42
|
var common_1 = require("@streamflow/common");
|
|
42
43
|
var solana_1 = require("@streamflow/common/solana");
|
|
@@ -47,9 +48,9 @@ exports.divCeilN = divCeilN;
|
|
|
47
48
|
function getDistributorPda(programId, mint, version) {
|
|
48
49
|
// Constructing the seed for the PDA
|
|
49
50
|
var seeds = [
|
|
50
|
-
Buffer.from("MerkleDistributor"),
|
|
51
|
+
buffer_1.Buffer.from("MerkleDistributor"),
|
|
51
52
|
mint.toBuffer(),
|
|
52
|
-
Buffer.from(new Uint8Array(new BigUint64Array([BigInt(version)]).buffer)),
|
|
53
|
+
buffer_1.Buffer.from(new Uint8Array(new BigUint64Array([BigInt(version)]).buffer)),
|
|
53
54
|
];
|
|
54
55
|
// Finding the PDA
|
|
55
56
|
return web3_js_1.PublicKey.findProgramAddressSync(seeds, programId)[0];
|
|
@@ -57,14 +58,14 @@ function getDistributorPda(programId, mint, version) {
|
|
|
57
58
|
exports.getDistributorPda = getDistributorPda;
|
|
58
59
|
function getClaimantStatusPda(programId, distributor, claimant) {
|
|
59
60
|
// Constructing the seed for the PDA
|
|
60
|
-
var seeds = [Buffer.from("ClaimStatus"), claimant.toBuffer(), distributor.toBuffer()];
|
|
61
|
+
var seeds = [buffer_1.Buffer.from("ClaimStatus"), claimant.toBuffer(), distributor.toBuffer()];
|
|
61
62
|
// Finding the PDA
|
|
62
63
|
return web3_js_1.PublicKey.findProgramAddressSync(seeds, programId)[0];
|
|
63
64
|
}
|
|
64
65
|
exports.getClaimantStatusPda = getClaimantStatusPda;
|
|
65
66
|
function getEventAuthorityPda(programId) {
|
|
66
67
|
// Constructing the seed for the PDA
|
|
67
|
-
var seeds = [Buffer.from("__event_authority")];
|
|
68
|
+
var seeds = [buffer_1.Buffer.from("__event_authority")];
|
|
68
69
|
// Finding the PDA
|
|
69
70
|
return web3_js_1.PublicKey.findProgramAddressSync(seeds, programId)[0];
|
|
70
71
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as StreamflowDistributorSolana from "./solana";
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT, createTransferCheckedInstruction, createTransferCheckedWithFeeInstruction, getTransferFeeConfig, } from "@solana/spl-token";
|
|
3
|
+
import { Connection, PublicKey, SystemProgram, } from "@solana/web3.js";
|
|
4
|
+
import { ICluster } from "@streamflow/common";
|
|
5
|
+
import { ata, checkOrCreateAtaBatch, prepareWrappedAccount, prepareBaseInstructions, prepareTransaction, getMintAndProgram, buildSendThrottler, } from "@streamflow/common/solana";
|
|
6
|
+
import { DISTRIBUTOR_PROGRAM_ID } from "./constants";
|
|
7
|
+
import { claimLocked, clawback, newClaim, newDistributor, } from "./generated/instructions";
|
|
8
|
+
import { ClaimStatus, MerkleDistributor } from "./generated/accounts";
|
|
9
|
+
import { calculateAmountWithTransferFees, getClaimantStatusPda, getDistributorPda, getEventAuthorityPda, wrappedSignAndExecuteTransaction, } from "./utils";
|
|
10
|
+
export default class SolanaDistributorClient {
|
|
11
|
+
connection;
|
|
12
|
+
programId;
|
|
13
|
+
commitment;
|
|
14
|
+
sendThrottler;
|
|
15
|
+
/**
|
|
16
|
+
* Create Stream instance
|
|
17
|
+
*/
|
|
18
|
+
constructor({ clusterUrl, cluster = ICluster.Mainnet, commitment = "confirmed", programId = "", sendRate = 1, sendThrottler, }) {
|
|
19
|
+
this.commitment = commitment;
|
|
20
|
+
this.connection = new Connection(clusterUrl, this.commitment);
|
|
21
|
+
this.programId = programId !== "" ? new PublicKey(programId) : new PublicKey(DISTRIBUTOR_PROGRAM_ID[cluster]);
|
|
22
|
+
this.sendThrottler = sendThrottler ?? buildSendThrottler(sendRate);
|
|
23
|
+
}
|
|
24
|
+
getCommitment() {
|
|
25
|
+
return typeof this.commitment == "string" ? this.commitment : this.commitment.commitment;
|
|
26
|
+
}
|
|
27
|
+
async create(data, extParams) {
|
|
28
|
+
const { ixs, distributorPublicKey } = await this.prepareCreateInstructions(data, extParams);
|
|
29
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
30
|
+
const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
31
|
+
hash,
|
|
32
|
+
context,
|
|
33
|
+
commitment: this.getCommitment(),
|
|
34
|
+
}, { sendThrottler: this.sendThrottler });
|
|
35
|
+
return {
|
|
36
|
+
ixs,
|
|
37
|
+
txId: signature,
|
|
38
|
+
metadataId: distributorPublicKey.toBase58(),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async prepareCreateInstructions(data, extParams) {
|
|
42
|
+
if (!extParams.invoker.publicKey) {
|
|
43
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
44
|
+
}
|
|
45
|
+
const ixs = prepareBaseInstructions(this.connection, extParams);
|
|
46
|
+
const mint = extParams.isNative ? NATIVE_MINT : new PublicKey(data.mint);
|
|
47
|
+
const { mint: mintAccount, tokenProgramId } = await getMintAndProgram(this.connection, mint);
|
|
48
|
+
const transferFeeConfig = getTransferFeeConfig(mintAccount);
|
|
49
|
+
const distributorPublicKey = getDistributorPda(this.programId, mint, data.version);
|
|
50
|
+
const tokenVault = await ata(mint, distributorPublicKey, tokenProgramId);
|
|
51
|
+
const senderTokens = await ata(mint, extParams.invoker.publicKey, tokenProgramId);
|
|
52
|
+
const args = {
|
|
53
|
+
version: new BN(data.version, 10),
|
|
54
|
+
root: data.root,
|
|
55
|
+
maxTotalClaim: data.maxTotalClaim,
|
|
56
|
+
maxNumNodes: data.maxNumNodes,
|
|
57
|
+
unlockPeriod: new BN(data.unlockPeriod, 10),
|
|
58
|
+
startVestingTs: new BN(data.startVestingTs, 10),
|
|
59
|
+
endVestingTs: new BN(data.endVestingTs, 10),
|
|
60
|
+
clawbackStartTs: new BN(data.clawbackStartTs, 10),
|
|
61
|
+
claimsClosable: data.claimsClosable,
|
|
62
|
+
};
|
|
63
|
+
const accounts = {
|
|
64
|
+
distributor: distributorPublicKey,
|
|
65
|
+
clawbackReceiver: senderTokens,
|
|
66
|
+
mint,
|
|
67
|
+
tokenVault,
|
|
68
|
+
admin: extParams.invoker.publicKey,
|
|
69
|
+
systemProgram: SystemProgram.programId,
|
|
70
|
+
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
71
|
+
tokenProgram: tokenProgramId,
|
|
72
|
+
};
|
|
73
|
+
if (extParams.isNative) {
|
|
74
|
+
ixs.push(...(await prepareWrappedAccount(this.connection, extParams.invoker.publicKey, data.maxTotalClaim)));
|
|
75
|
+
}
|
|
76
|
+
const nowTs = new BN(Math.floor(Date.now() / 1000));
|
|
77
|
+
const endVestingTs = args.endVestingTs.eqn(0) ? nowTs : args.endVestingTs;
|
|
78
|
+
const startVestingTs = args.startVestingTs.eqn(0) ? nowTs : args.startVestingTs;
|
|
79
|
+
if (endVestingTs.gt(startVestingTs) && endVestingTs.sub(startVestingTs).lt(args.unlockPeriod)) {
|
|
80
|
+
throw new Error("The unlock period cannot be longer than the total vesting duration!");
|
|
81
|
+
}
|
|
82
|
+
ixs.push(newDistributor(args, accounts, this.programId));
|
|
83
|
+
if (transferFeeConfig) {
|
|
84
|
+
const { transferAmount, feeCharged } = await calculateAmountWithTransferFees(this.connection, transferFeeConfig, BigInt(data.maxTotalClaim.toString()));
|
|
85
|
+
ixs.push(createTransferCheckedWithFeeInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, transferAmount, mintAccount.decimals, feeCharged, undefined, tokenProgramId));
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
ixs.push(createTransferCheckedInstruction(senderTokens, mint, tokenVault, extParams.invoker.publicKey, BigInt(data.maxTotalClaim.toString()), mintAccount.decimals, undefined, tokenProgramId));
|
|
89
|
+
}
|
|
90
|
+
return { distributorPublicKey, ixs };
|
|
91
|
+
}
|
|
92
|
+
async claim(data, extParams) {
|
|
93
|
+
const ixs = await this.prepareClaimInstructions(data, extParams);
|
|
94
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
95
|
+
const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
96
|
+
hash,
|
|
97
|
+
context,
|
|
98
|
+
commitment: this.getCommitment(),
|
|
99
|
+
}, { sendThrottler: this.sendThrottler });
|
|
100
|
+
return { ixs, txId: signature };
|
|
101
|
+
}
|
|
102
|
+
async prepareClaimInstructions(data, extParams) {
|
|
103
|
+
if (!extParams.invoker.publicKey) {
|
|
104
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
105
|
+
}
|
|
106
|
+
const distributorPublicKey = new PublicKey(data.id);
|
|
107
|
+
const distributor = await MerkleDistributor.fetch(this.connection, distributorPublicKey);
|
|
108
|
+
if (!distributor) {
|
|
109
|
+
throw new Error("Couldn't get account info");
|
|
110
|
+
}
|
|
111
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, distributor.mint);
|
|
112
|
+
const ixs = prepareBaseInstructions(this.connection, extParams);
|
|
113
|
+
ixs.push(...(await checkOrCreateAtaBatch(this.connection, [extParams.invoker.publicKey], distributor.mint, extParams.invoker, tokenProgramId)));
|
|
114
|
+
const invokerTokens = await ata(distributor.mint, extParams.invoker.publicKey, tokenProgramId);
|
|
115
|
+
const claimStatusPublicKey = getClaimantStatusPda(this.programId, distributorPublicKey, extParams.invoker.publicKey);
|
|
116
|
+
const eventAuthorityPublicKey = getEventAuthorityPda(this.programId);
|
|
117
|
+
const claimStatus = await ClaimStatus.fetch(this.connection, claimStatusPublicKey);
|
|
118
|
+
const accounts = {
|
|
119
|
+
distributor: distributorPublicKey,
|
|
120
|
+
claimStatus: claimStatusPublicKey,
|
|
121
|
+
from: distributor.tokenVault,
|
|
122
|
+
to: invokerTokens,
|
|
123
|
+
claimant: extParams.invoker.publicKey,
|
|
124
|
+
mint: distributor.mint,
|
|
125
|
+
tokenProgram: tokenProgramId,
|
|
126
|
+
systemProgram: SystemProgram.programId,
|
|
127
|
+
eventAuthority: eventAuthorityPublicKey,
|
|
128
|
+
program: this.programId,
|
|
129
|
+
};
|
|
130
|
+
if (!claimStatus) {
|
|
131
|
+
const args = {
|
|
132
|
+
amountLocked: data.amountLocked,
|
|
133
|
+
amountUnlocked: data.amountUnlocked,
|
|
134
|
+
proof: data.proof,
|
|
135
|
+
};
|
|
136
|
+
ixs.push(newClaim(args, accounts, this.programId));
|
|
137
|
+
}
|
|
138
|
+
const nowTs = new BN(Math.floor(Date.now() / 1000));
|
|
139
|
+
if (claimStatus || (data.amountLocked.gtn(0) && nowTs.sub(distributor.startTs).gte(distributor.unlockPeriod))) {
|
|
140
|
+
ixs.push(claimLocked(accounts, this.programId));
|
|
141
|
+
}
|
|
142
|
+
return ixs;
|
|
143
|
+
}
|
|
144
|
+
async clawback(data, extParams) {
|
|
145
|
+
const ixs = await this.prepareClawbackInstructions(data, extParams);
|
|
146
|
+
const { tx, hash, context } = await prepareTransaction(this.connection, ixs, extParams.invoker.publicKey);
|
|
147
|
+
const signature = await wrappedSignAndExecuteTransaction(this.connection, extParams.invoker, tx, {
|
|
148
|
+
hash,
|
|
149
|
+
context,
|
|
150
|
+
commitment: this.getCommitment(),
|
|
151
|
+
}, { sendThrottler: this.sendThrottler });
|
|
152
|
+
return { ixs, txId: signature };
|
|
153
|
+
}
|
|
154
|
+
async prepareClawbackInstructions(data, extParams) {
|
|
155
|
+
if (!extParams.invoker.publicKey) {
|
|
156
|
+
throw new Error("Invoker's PublicKey is not available, check passed wallet adapter!");
|
|
157
|
+
}
|
|
158
|
+
const distributorPublicKey = new PublicKey(data.id);
|
|
159
|
+
const distributor = await MerkleDistributor.fetch(this.connection, distributorPublicKey);
|
|
160
|
+
if (!distributor) {
|
|
161
|
+
throw new Error("Couldn't get account info");
|
|
162
|
+
}
|
|
163
|
+
const { tokenProgramId } = await getMintAndProgram(this.connection, distributor.mint);
|
|
164
|
+
const ixs = prepareBaseInstructions(this.connection, extParams);
|
|
165
|
+
ixs.push(...(await checkOrCreateAtaBatch(this.connection, [extParams.invoker.publicKey], distributor.mint, extParams.invoker, tokenProgramId)));
|
|
166
|
+
const accounts = {
|
|
167
|
+
distributor: distributorPublicKey,
|
|
168
|
+
from: distributor.tokenVault,
|
|
169
|
+
to: distributor.clawbackReceiver,
|
|
170
|
+
admin: extParams.invoker.publicKey,
|
|
171
|
+
mint: distributor.mint,
|
|
172
|
+
systemProgram: SystemProgram.programId,
|
|
173
|
+
tokenProgram: tokenProgramId,
|
|
174
|
+
};
|
|
175
|
+
ixs.push(clawback(accounts, this.programId));
|
|
176
|
+
return ixs;
|
|
177
|
+
}
|
|
178
|
+
async getClaims(data) {
|
|
179
|
+
const claimStatusPublicKeys = data.map(({ id, recipient }) => {
|
|
180
|
+
return getClaimantStatusPda(this.programId, new PublicKey(id), new PublicKey(recipient));
|
|
181
|
+
});
|
|
182
|
+
return ClaimStatus.fetchMultiple(this.connection, claimStatusPublicKeys, this.programId);
|
|
183
|
+
}
|
|
184
|
+
async getDistributors(data) {
|
|
185
|
+
const distributorPublicKeys = data.ids.map((distributorId) => new PublicKey(distributorId));
|
|
186
|
+
return MerkleDistributor.fetchMultiple(this.connection, distributorPublicKeys, this.programId);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ICluster } from "@streamflow/common";
|
|
2
|
+
export const ONE_IN_BASIS_POINTS = BigInt(10000);
|
|
3
|
+
export const DISTRIBUTOR_PROGRAM_ID = {
|
|
4
|
+
[ICluster.Devnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
|
|
5
|
+
[ICluster.Mainnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
|
|
6
|
+
[ICluster.Testnet]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
|
|
7
|
+
[ICluster.Local]: "MErKy6nZVoVAkryxAejJz2juifQ4ArgLgHmaJCQkU7N",
|
|
8
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import BN from "bn.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: new BN(obj.lockedAmount),
|
|
104
|
+
lockedAmountWithdrawn: new BN(obj.lockedAmountWithdrawn),
|
|
105
|
+
unlockedAmount: new BN(obj.unlockedAmount),
|
|
106
|
+
lastClaimTs: new BN(obj.lastClaimTs),
|
|
107
|
+
lastAmountPerUnlock: new BN(obj.lastAmountPerUnlock),
|
|
108
|
+
closed: obj.closed,
|
|
109
|
+
buffer1: obj.buffer1,
|
|
110
|
+
buffer2: obj.buffer2,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import BN from "bn.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: new BN(obj.version),
|
|
170
|
+
root: obj.root,
|
|
171
|
+
mint: new PublicKey(obj.mint),
|
|
172
|
+
tokenVault: new PublicKey(obj.tokenVault),
|
|
173
|
+
maxTotalClaim: new BN(obj.maxTotalClaim),
|
|
174
|
+
maxNumNodes: new BN(obj.maxNumNodes),
|
|
175
|
+
unlockPeriod: new BN(obj.unlockPeriod),
|
|
176
|
+
totalAmountClaimed: new BN(obj.totalAmountClaimed),
|
|
177
|
+
numNodesClaimed: new BN(obj.numNodesClaimed),
|
|
178
|
+
startTs: new BN(obj.startTs),
|
|
179
|
+
endTs: new BN(obj.endTs),
|
|
180
|
+
clawbackStartTs: new BN(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
|
+
}
|