@nosana/kit 1.0.2 → 1.0.7

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 (49) hide show
  1. package/.gitlab-ci.yml +2 -2
  2. package/dist/config/defaultConfigs.js +2 -0
  3. package/dist/config/types.d.ts +1 -0
  4. package/dist/generated_clients/merkle_distributor/accounts/claimStatus.d.ts +53 -0
  5. package/dist/generated_clients/merkle_distributor/accounts/claimStatus.js +65 -0
  6. package/dist/generated_clients/merkle_distributor/accounts/index.d.ts +9 -0
  7. package/dist/generated_clients/merkle_distributor/accounts/index.js +9 -0
  8. package/dist/generated_clients/merkle_distributor/accounts/merkleDistributor.d.ts +109 -0
  9. package/dist/generated_clients/merkle_distributor/accounts/merkleDistributor.js +93 -0
  10. package/dist/generated_clients/merkle_distributor/errors/index.d.ts +8 -0
  11. package/dist/generated_clients/merkle_distributor/errors/index.js +8 -0
  12. package/dist/generated_clients/merkle_distributor/errors/merkleDistributor.d.ts +63 -0
  13. package/dist/generated_clients/merkle_distributor/errors/merkleDistributor.js +89 -0
  14. package/dist/generated_clients/merkle_distributor/index.d.ts +11 -0
  15. package/dist/generated_clients/merkle_distributor/index.js +11 -0
  16. package/dist/generated_clients/merkle_distributor/instructions/claimLocked.d.ts +69 -0
  17. package/dist/generated_clients/merkle_distributor/instructions/claimLocked.js +84 -0
  18. package/dist/generated_clients/merkle_distributor/instructions/clawback.d.ts +69 -0
  19. package/dist/generated_clients/merkle_distributor/instructions/clawback.js +88 -0
  20. package/dist/generated_clients/merkle_distributor/instructions/closeClaimStatus.d.ts +45 -0
  21. package/dist/generated_clients/merkle_distributor/instructions/closeClaimStatus.js +73 -0
  22. package/dist/generated_clients/merkle_distributor/instructions/closeDistributor.d.ts +64 -0
  23. package/dist/generated_clients/merkle_distributor/instructions/closeDistributor.js +84 -0
  24. package/dist/generated_clients/merkle_distributor/instructions/index.d.ts +16 -0
  25. package/dist/generated_clients/merkle_distributor/instructions/index.js +16 -0
  26. package/dist/generated_clients/merkle_distributor/instructions/newClaim.d.ts +78 -0
  27. package/dist/generated_clients/merkle_distributor/instructions/newClaim.js +101 -0
  28. package/dist/generated_clients/merkle_distributor/instructions/newDistributor.d.ts +113 -0
  29. package/dist/generated_clients/merkle_distributor/instructions/newDistributor.js +122 -0
  30. package/dist/generated_clients/merkle_distributor/instructions/setAdmin.d.ts +48 -0
  31. package/dist/generated_clients/merkle_distributor/instructions/setAdmin.js +70 -0
  32. package/dist/generated_clients/merkle_distributor/instructions/setClawbackReceiver.d.ts +48 -0
  33. package/dist/generated_clients/merkle_distributor/instructions/setClawbackReceiver.js +76 -0
  34. package/dist/generated_clients/merkle_distributor/instructions/setEnableSlot.d.ts +47 -0
  35. package/dist/generated_clients/merkle_distributor/instructions/setEnableSlot.js +73 -0
  36. package/dist/generated_clients/merkle_distributor/programs/index.d.ts +8 -0
  37. package/dist/generated_clients/merkle_distributor/programs/index.js +8 -0
  38. package/dist/generated_clients/merkle_distributor/programs/merkleDistributor.d.ts +50 -0
  39. package/dist/generated_clients/merkle_distributor/programs/merkleDistributor.js +67 -0
  40. package/dist/generated_clients/merkle_distributor/shared/index.d.ts +49 -0
  41. package/dist/generated_clients/merkle_distributor/shared/index.js +86 -0
  42. package/dist/index.d.ts +5 -0
  43. package/dist/index.js +5 -0
  44. package/dist/programs/MerkleDistributorProgram.d.ts +101 -0
  45. package/dist/programs/MerkleDistributorProgram.js +296 -0
  46. package/dist/services/NosService.js +5 -0
  47. package/eslint.config.js +1 -3
  48. package/package.json +13 -12
  49. package/scripts/generate-clients.ts +11 -0
@@ -0,0 +1,296 @@
1
+ import { BaseProgram } from './BaseProgram.js';
2
+ import { parseBase64RpcAccount, address, } from 'gill';
3
+ import { NosanaError, ErrorCodes } from '../index.js';
4
+ import * as programClient from '../generated_clients/merkle_distributor/index.js';
5
+ import { convertBigIntToNumber } from '../utils/index.js';
6
+ import { findAssociatedTokenPda, TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
7
+ import bs58 from 'bs58';
8
+ import { SYSTEM_PROGRAM_ADDRESS } from 'gill/programs';
9
+ /**
10
+ * Claim target enum for merkle distributor.
11
+ * Determines which address receives the claimed tokens.
12
+ */
13
+ export var ClaimTarget;
14
+ (function (ClaimTarget) {
15
+ ClaimTarget["YES"] = "YES";
16
+ ClaimTarget["NO"] = "NO";
17
+ })(ClaimTarget || (ClaimTarget = {}));
18
+ /**
19
+ * Allowed addresses for receiving claimed tokens from merkle distributor.
20
+ * The `to` account must be the ATA of one of these addresses.
21
+ */
22
+ export const ALLOWED_RECEIVE_ADDRESSES = {
23
+ [ClaimTarget.YES]: address('YessuvqUauj9yW4B3eERcyRLWmQtWpFc2ERKmaedmCE'),
24
+ [ClaimTarget.NO]: address('NopXntmRdXhYNkoZaNTMUMShJ3aVG5RvwpiyPdd4bMh'),
25
+ };
26
+ /**
27
+ * Error thrown when a claim status account is not found
28
+ */
29
+ export class ClaimStatusNotFoundError extends Error {
30
+ constructor(address) {
31
+ super(`Claim status account not found at address ${address}`);
32
+ this.name = 'ClaimStatusNotFoundError';
33
+ }
34
+ }
35
+ export class MerkleDistributorProgram extends BaseProgram {
36
+ constructor(sdk) {
37
+ super(sdk);
38
+ this.client = programClient;
39
+ }
40
+ getProgramId() {
41
+ return this.sdk.config.programs.merkleDistributorAddress;
42
+ }
43
+ /**
44
+ * Derive the ClaimStatus PDA address for a given distributor and optional claimant.
45
+ * If claimant is not provided, uses the wallet's address.
46
+ *
47
+ * @param distributor The address of the merkle distributor
48
+ * @param claimant Optional claimant address. If not provided, uses the wallet's address.
49
+ * @returns The ClaimStatus PDA address
50
+ * @throws Error if wallet is not set and claimant is not provided
51
+ */
52
+ async getClaimStatusPda(distributor, claimant) {
53
+ let claimantAddress;
54
+ if (claimant) {
55
+ claimantAddress = claimant;
56
+ }
57
+ else {
58
+ if (!this.sdk.wallet) {
59
+ throw new Error('Wallet not set. Please set a wallet or provide a claimant address.');
60
+ }
61
+ claimantAddress = this.sdk.wallet.address;
62
+ }
63
+ return await this.sdk.solana.pda(['ClaimStatus', claimantAddress, distributor], this.getProgramId());
64
+ }
65
+ /**
66
+ * Fetch a merkle distributor account by address
67
+ */
68
+ async get(addr) {
69
+ try {
70
+ const distributorAccount = await this.client.fetchMerkleDistributor(this.sdk.solana.rpc, addr);
71
+ const distributor = this.transformMerkleDistributorAccount(distributorAccount);
72
+ return distributor;
73
+ }
74
+ catch (err) {
75
+ this.sdk.logger.error(`Failed to fetch merkle distributor ${err}`);
76
+ throw err;
77
+ }
78
+ }
79
+ /**
80
+ * Fetch all merkle distributor accounts
81
+ */
82
+ async all() {
83
+ try {
84
+ const getProgramAccountsResponse = await this.sdk.solana.rpc
85
+ .getProgramAccounts(this.getProgramId(), {
86
+ encoding: 'base64',
87
+ filters: [
88
+ {
89
+ memcmp: {
90
+ offset: BigInt(0),
91
+ bytes: bs58.encode(Buffer.from(this.client.MERKLE_DISTRIBUTOR_DISCRIMINATOR)),
92
+ encoding: 'base58',
93
+ },
94
+ },
95
+ ],
96
+ })
97
+ .send();
98
+ const distributors = getProgramAccountsResponse
99
+ .map((result) => {
100
+ try {
101
+ const distributorAccount = programClient.decodeMerkleDistributor(parseBase64RpcAccount(result.pubkey, result.account));
102
+ return this.transformMerkleDistributorAccount(distributorAccount);
103
+ }
104
+ catch (err) {
105
+ this.sdk.logger.error(`Failed to decode merkle distributor ${err}`);
106
+ return null;
107
+ }
108
+ })
109
+ .filter((account) => account !== null);
110
+ return distributors;
111
+ }
112
+ catch (err) {
113
+ this.sdk.logger.error(`Failed to fetch all merkle distributors ${err}`);
114
+ throw err;
115
+ }
116
+ }
117
+ /**
118
+ * Fetch a claim status account by address
119
+ */
120
+ async getClaimStatus(addr) {
121
+ try {
122
+ const maybeClaimStatus = await this.client.fetchMaybeClaimStatus(this.sdk.solana.rpc, addr);
123
+ // If account doesn't exist, throw a specific error
124
+ if (!maybeClaimStatus.exists) {
125
+ throw new ClaimStatusNotFoundError(addr);
126
+ }
127
+ // Transform and return the claim status
128
+ return this.transformClaimStatusAccount(maybeClaimStatus);
129
+ }
130
+ catch (err) {
131
+ this.sdk.logger.error(`Failed to fetch claim status ${err}`);
132
+ throw err;
133
+ }
134
+ }
135
+ /**
136
+ * Fetch claim status for a specific distributor and optional claimant.
137
+ * Derives the ClaimStatus PDA using the claimant address (or wallet's address if not provided) and the distributor address.
138
+ *
139
+ * @param distributor The address of the merkle distributor
140
+ * @param claimant Optional claimant address. If not provided, uses the wallet's address.
141
+ * @returns The claim status if it exists, null otherwise
142
+ * @throws Error if wallet is not set and claimant is not provided
143
+ */
144
+ async getClaimStatusForDistributor(distributor, claimant) {
145
+ try {
146
+ // Derive ClaimStatus PDA
147
+ const claimStatusPda = await this.getClaimStatusPda(distributor, claimant);
148
+ // Reuse getClaimStatus to fetch and transform the claim status
149
+ // If the account doesn't exist, it will throw, so we catch and return null
150
+ return await this.getClaimStatus(claimStatusPda);
151
+ }
152
+ catch (err) {
153
+ // If the account doesn't exist, return null instead of throwing
154
+ // Check if it's the specific ClaimStatusNotFoundError from getClaimStatus
155
+ if (err instanceof ClaimStatusNotFoundError) {
156
+ return null;
157
+ }
158
+ // For other errors, log and rethrow
159
+ this.sdk.logger.error(`Failed to fetch claim status ${err}`);
160
+ throw err;
161
+ }
162
+ }
163
+ /**
164
+ * Fetch all claim status accounts
165
+ * TODO: add filter for claimant and distributor
166
+ */
167
+ async allClaimStatus() {
168
+ try {
169
+ const getProgramAccountsResponse = await this.sdk.solana.rpc
170
+ .getProgramAccounts(this.getProgramId(), {
171
+ encoding: 'base64',
172
+ filters: [
173
+ {
174
+ memcmp: {
175
+ offset: BigInt(0),
176
+ bytes: bs58.encode(Buffer.from(this.client.CLAIM_STATUS_DISCRIMINATOR)),
177
+ encoding: 'base58',
178
+ },
179
+ },
180
+ ],
181
+ })
182
+ .send();
183
+ const claimStatuses = getProgramAccountsResponse
184
+ .map((result) => {
185
+ try {
186
+ const claimStatusAccount = programClient.decodeClaimStatus(parseBase64RpcAccount(result.pubkey, result.account));
187
+ return this.transformClaimStatusAccount(claimStatusAccount);
188
+ }
189
+ catch (err) {
190
+ this.sdk.logger.error(`Failed to decode claim status ${err}`);
191
+ return null;
192
+ }
193
+ })
194
+ .filter((account) => account !== null);
195
+ return claimStatuses;
196
+ }
197
+ catch (err) {
198
+ this.sdk.logger.error(`Failed to fetch all claim statuses ${err}`);
199
+ throw err;
200
+ }
201
+ }
202
+ /**
203
+ * Transform merkle distributor account to include address and convert BigInt to numbers
204
+ */
205
+ transformMerkleDistributorAccount(distributorAccount) {
206
+ const {
207
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
208
+ discriminator: _, root, buffer0, buffer1, buffer2, ...distributorAccountData } = distributorAccount.data;
209
+ const converted = convertBigIntToNumber(distributorAccountData);
210
+ return {
211
+ address: distributorAccount.address,
212
+ ...converted,
213
+ root: bs58.encode(Buffer.from(root)),
214
+ buffer0: bs58.encode(Buffer.from(buffer0)),
215
+ buffer1: bs58.encode(Buffer.from(buffer1)),
216
+ buffer2: bs58.encode(Buffer.from(buffer2)),
217
+ };
218
+ }
219
+ /**
220
+ * Transform claim status account to include address and convert BigInt to numbers
221
+ */
222
+ transformClaimStatusAccount(claimStatusAccount) {
223
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
224
+ const { discriminator: _, ...claimStatusAccountData } = claimStatusAccount.data;
225
+ return {
226
+ address: claimStatusAccount.address,
227
+ ...convertBigIntToNumber(claimStatusAccountData),
228
+ };
229
+ }
230
+ /**
231
+ * Claim tokens from a merkle distributor.
232
+ * This function creates a new ClaimStatus account and claims the tokens in a single instruction.
233
+ *
234
+ * @param params Parameters for claiming tokens
235
+ * @param params.claimant Optional claimant signer. If not provided, uses the wallet.
236
+ * @returns The newClaim instruction
237
+ * @throws NosanaError if tokens have already been claimed
238
+ * @throws Error if wallet is not set and claimant is not provided
239
+ */
240
+ async claim(params) {
241
+ // Determine claimant signer and address
242
+ let claimantSigner;
243
+ let claimantAddress;
244
+ if (params.claimant) {
245
+ claimantSigner = params.claimant;
246
+ claimantAddress = params.claimant.address;
247
+ }
248
+ else {
249
+ if (!this.sdk.wallet) {
250
+ throw new Error('Wallet not set. Please set a wallet or provide a claimant signer.');
251
+ }
252
+ claimantSigner = this.sdk.wallet;
253
+ claimantAddress = this.sdk.wallet.address;
254
+ }
255
+ try {
256
+ // Get the distributor account to find mint and tokenVault
257
+ const distributorAccount = await this.client.fetchMerkleDistributor(this.sdk.solana.rpc, params.distributor);
258
+ // Derive ClaimStatus PDA using the claimant address
259
+ const claimStatusPda = await this.getClaimStatusPda(params.distributor, claimantAddress);
260
+ // Check if ClaimStatus account exists
261
+ const maybeClaimStatus = await this.client.fetchMaybeClaimStatus(this.sdk.solana.rpc, claimStatusPda);
262
+ // If ClaimStatus already exists, throw an error (already claimed)
263
+ if (maybeClaimStatus.exists) {
264
+ throw new NosanaError('Tokens have already been claimed from this distributor', ErrorCodes.VALIDATION_ERROR);
265
+ }
266
+ // Get the target address for receiving tokens (YES or NO)
267
+ const targetAddress = ALLOWED_RECEIVE_ADDRESSES[params.target];
268
+ // Find the ATA of the target address (where tokens will be sent)
269
+ const [targetAta] = await findAssociatedTokenPda({
270
+ mint: distributorAccount.data.mint,
271
+ owner: targetAddress,
272
+ tokenProgram: TOKEN_PROGRAM_ADDRESS,
273
+ });
274
+ // Create newClaim instruction which creates the account and claims the tokens
275
+ // Note: tokens go to the ATA of the target address (YES or NO), not the claimant's ATA
276
+ // The claimant in the instruction is the claimant signer (or wallet if not provided)
277
+ const newClaimInstruction = programClient.getNewClaimInstruction({
278
+ distributor: params.distributor,
279
+ claimStatus: claimStatusPda,
280
+ from: distributorAccount.data.tokenVault,
281
+ to: targetAta, // ATA of YES or NO address, not claimant's ATA
282
+ claimant: claimantSigner, // Claimant signer (or wallet if not provided)
283
+ tokenProgram: TOKEN_PROGRAM_ADDRESS,
284
+ systemProgram: SYSTEM_PROGRAM_ADDRESS,
285
+ amountUnlocked: params.amountUnlocked,
286
+ amountLocked: params.amountLocked,
287
+ proof: params.proof,
288
+ }, { programAddress: this.getProgramId() });
289
+ return newClaimInstruction;
290
+ }
291
+ catch (err) {
292
+ this.sdk.logger.error(`Failed to create claim instructions: ${err}`);
293
+ throw err;
294
+ }
295
+ }
296
+ }
@@ -1,6 +1,8 @@
1
1
  import { address } from 'gill';
2
2
  import { NosanaError, ErrorCodes } from '../errors/NosanaError.js';
3
3
  import { TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
4
+ // Standard SPL token account size
5
+ const TOKEN_ACCOUNT_SIZE = 165;
4
6
  // Offset of mint address in token account data structure
5
7
  const MINT_OFFSET = 0;
6
8
  export class NosService {
@@ -31,6 +33,9 @@ export class NosService {
31
33
  .getProgramAccounts(TOKEN_PROGRAM_ADDRESS, {
32
34
  encoding: 'jsonParsed',
33
35
  filters: [
36
+ {
37
+ dataSize: BigInt(TOKEN_ACCOUNT_SIZE),
38
+ },
34
39
  {
35
40
  memcmp: {
36
41
  offset: BigInt(MINT_OFFSET),
package/eslint.config.js CHANGED
@@ -40,9 +40,7 @@ export default [
40
40
  // More lenient rules for test files
41
41
  files: ['tests/**/*.ts', '**/*.test.ts', '**/*.spec.ts'],
42
42
  rules: {
43
- '@typescript-eslint/no-explicit-any': 'off',
44
- '@typescript-eslint/no-unused-vars': 'warn',
45
- 'no-console': 'off',
43
+ '@typescript-eslint/no-explicit-any': 'off'
46
44
  },
47
45
  },
48
46
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nosana/kit",
3
- "version": "1.0.2",
3
+ "version": "1.0.7",
4
4
  "description": "Nosana KIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -13,8 +13,8 @@
13
13
  "generate-clients": "npx tsx scripts/generate-clients.ts",
14
14
  "lint": "eslint .",
15
15
  "lint:fix": "eslint . --fix",
16
- "format": "prettier --write \"**/*.ts\"",
17
- "prettier": "prettier --check \"**/*.ts\"",
16
+ "format": "prettier --check \"**/*.ts\"",
17
+ "format:fix": "prettier --write \"**/*.ts\"",
18
18
  "publish:public": "npm run build && npm publish --access public"
19
19
  },
20
20
  "keywords": [
@@ -27,12 +27,13 @@
27
27
  "author": "Nosana",
28
28
  "license": "MIT",
29
29
  "dependencies": {
30
- "@solana-program/token": "^0.5.1",
31
- "axios": "^1.6.0",
32
- "bs58": "^6.0.0",
33
- "buffer": "^6.0.3",
34
- "form-data": "^4.0.0",
35
- "gill": "^0.9.0"
30
+ "@solana-program/token": "0.5.1",
31
+ "@solana/sysvars": "5.0.0",
32
+ "axios": "1.6.0",
33
+ "bs58": "6.0.0",
34
+ "buffer": "6.0.3",
35
+ "form-data": "4.0.0",
36
+ "gill": "0.12.0"
36
37
  },
37
38
  "devDependencies": {
38
39
  "@codama/nodes-from-anchor": "^1.2.0",
@@ -40,13 +41,13 @@
40
41
  "@types/node": "^20.11.24",
41
42
  "@typescript-eslint/eslint-plugin": "^7.1.0",
42
43
  "@typescript-eslint/parser": "^7.1.0",
44
+ "@vitest/coverage-v8": "^1.6.0",
43
45
  "codama": "^1.3.0",
44
46
  "eslint": "^8.57.0",
45
- "vitest": "^1.6.0",
46
- "@vitest/coverage-v8": "^1.6.0",
47
47
  "prettier": "^3.2.5",
48
48
  "ts-node": "^10.9.2",
49
- "typescript": "^5.3.3"
49
+ "typescript": "^5.3.3",
50
+ "vitest": "^1.6.0"
50
51
  },
51
52
  "engines": {
52
53
  "node": ">=20.18.0"
@@ -3,6 +3,7 @@ import { rootNodeFromAnchor, AnchorIdl } from '@codama/nodes-from-anchor';
3
3
  import { renderVisitor as renderJavaScriptVisitor } from '@codama/renderers-js';
4
4
  import anchorJobsIdl from '../../spl/target/idl/nosana_jobs.json';
5
5
  import anchorStakingIdl from '../../spl/target/idl/nosana_staking.json';
6
+ import merkleDistributorIdl from '../src/idl/merkle_distributor.json';
6
7
  import path from 'path';
7
8
  import { fileURLToPath } from 'url';
8
9
 
@@ -20,3 +21,13 @@ const codamaStaking = createFromRoot(rootNodeFromAnchor(anchorStakingIdl as Anch
20
21
  codamaStaking.accept(
21
22
  renderJavaScriptVisitor(path.join(__dirname, '..', 'src', 'generated_clients', 'staking'))
22
23
  );
24
+
25
+ const codamaMerkleDistributor = createFromRoot(
26
+ rootNodeFromAnchor(merkleDistributorIdl as AnchorIdl)
27
+ );
28
+
29
+ codamaMerkleDistributor.accept(
30
+ renderJavaScriptVisitor(
31
+ path.join(__dirname, '..', 'src', 'generated_clients', 'merkle_distributor')
32
+ )
33
+ );