@quicknode/sdk 2.1.3 → 2.2.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/cjs/index.js CHANGED
@@ -5,7 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var viem = require('viem');
6
6
  var zod = require('zod');
7
7
  var chains = require('viem/chains');
8
- var fetch = require('cross-fetch');
8
+ var fetch$1 = require('cross-fetch');
9
+ var web3_js = require('@solana/web3.js');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
@@ -28,7 +29,8 @@ function _interopNamespace(e) {
28
29
  }
29
30
 
30
31
  var viem__namespace = /*#__PURE__*/_interopNamespace(viem);
31
- var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
32
+ var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch$1);
33
+ var web3_js__namespace = /*#__PURE__*/_interopNamespace(web3_js);
32
34
 
33
35
  class QNInputValidationError extends Error {
34
36
  constructor({ messages, zodError, }) {
@@ -329,9 +331,9 @@ function setupGlobalFetch() {
329
331
  // Required for viem to work in node
330
332
  if (!globalThis.fetch) {
331
333
  globalThis.fetch = fetch__default["default"];
332
- globalThis.Headers = fetch.Headers;
333
- globalThis.Request = fetch.Request;
334
- globalThis.Response = fetch.Response;
334
+ globalThis.Headers = fetch$1.Headers;
335
+ globalThis.Request = fetch$1.Request;
336
+ globalThis.Response = fetch$1.Response;
335
337
  }
336
338
  }
337
339
 
@@ -353,13 +355,113 @@ class Core {
353
355
  }
354
356
  }
355
357
 
358
+ // eslint-disable-next-line @nx/enforce-module-boundaries
359
+ class Solana {
360
+ constructor({ endpointUrl }) {
361
+ this.endpointUrl = endpointUrl;
362
+ this.connection = new web3_js.Connection(endpointUrl);
363
+ }
364
+ /**
365
+ * Sends a transaction with a dynamically generated priority fee based on the current network conditions and compute units needed by the transaction.
366
+ */
367
+ async sendSmartTransaction(args) {
368
+ const { transaction, keyPair, feeLevel = 'medium', sendTransactionOptions = {}, } = args;
369
+ const smartTransaction = await this.prepareSmartTransaction({
370
+ transaction,
371
+ payerPublicKey: keyPair.publicKey,
372
+ feeLevel,
373
+ });
374
+ smartTransaction.sign(keyPair);
375
+ const hash = await this.connection.sendRawTransaction(transaction.serialize(), { skipPreflight: true, ...sendTransactionOptions });
376
+ return hash;
377
+ }
378
+ /**
379
+ * Prepares a transaction to be sent with a dynamically generated priority fee based
380
+ * on the current network conditions. It adds a `setComputeUnitPrice` instruction to the transaction
381
+ * and simulates the transaction to estimate the number of compute units it will consume.
382
+ * The returned transaction still needs to be signed and sent to the network.
383
+ */
384
+ async prepareSmartTransaction(args) {
385
+ const { transaction, payerPublicKey, feeLevel = 'medium' } = args;
386
+ // Need to fetch this ahead of time and add to transaction so it's in the transaction instructions
387
+ // for the simulation
388
+ const computeUnitPriceInstruction = await this.createDynamicPriorityFeeInstruction(feeLevel);
389
+ const allInstructions = [
390
+ ...transaction.instructions,
391
+ computeUnitPriceInstruction,
392
+ ];
393
+ // eslint-disable-next-line prefer-const
394
+ let [units, recentBlockhash] = await Promise.all([
395
+ this.getSimulationUnits(this.connection, allInstructions, payerPublicKey),
396
+ this.connection.getLatestBlockhash(),
397
+ ]);
398
+ transaction.add(computeUnitPriceInstruction);
399
+ if (units) {
400
+ units = Math.ceil(units * 1.05); // margin of error
401
+ transaction.add(web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units }));
402
+ }
403
+ transaction.recentBlockhash = recentBlockhash.blockhash;
404
+ return transaction;
405
+ }
406
+ // Get the priority fee averages based on fee data from the latest blocks
407
+ async fetchEstimatePriorityFees(args = {}) {
408
+ const payload = {
409
+ method: 'qn_estimatePriorityFees',
410
+ params: args,
411
+ id: 1,
412
+ jsonrpc: '2.0',
413
+ };
414
+ const response = await fetch(this.endpointUrl, {
415
+ method: 'POST',
416
+ headers: {
417
+ 'Content-Type': 'application/json',
418
+ },
419
+ body: JSON.stringify(payload),
420
+ });
421
+ if (!response.ok) {
422
+ if (response.status === 404) {
423
+ throw new Error(`The RPC method qn_estimatePriorityFees was not found on your endpoint! Your endpoint likely does not have the Priority Fee API add-on installed. Please visit https://marketplace.quicknode.com/add-on/solana-priority-fee to install the Priority Fee API and use this method to send your transactions with priority fees calculated with real-time data.`);
424
+ }
425
+ throw new Error('Failed to fetch priority fee estimates');
426
+ }
427
+ const data = await response.json();
428
+ return data;
429
+ }
430
+ async createDynamicPriorityFeeInstruction(feeType = 'medium') {
431
+ const { result } = await this.fetchEstimatePriorityFees({});
432
+ const priorityFee = result.per_compute_unit[feeType];
433
+ const priorityFeeInstruction = web3_js.ComputeBudgetProgram.setComputeUnitPrice({
434
+ microLamports: priorityFee,
435
+ });
436
+ return priorityFeeInstruction;
437
+ }
438
+ async getSimulationUnits(connection, instructions, publicKey) {
439
+ const testVersionedTxn = new web3_js.VersionedTransaction(new web3_js.TransactionMessage({
440
+ instructions: instructions,
441
+ payerKey: publicKey,
442
+ recentBlockhash: web3_js.PublicKey.default.toString(), // just a placeholder
443
+ }).compileToV0Message());
444
+ const simulation = await connection.simulateTransaction(testVersionedTxn, {
445
+ replaceRecentBlockhash: true,
446
+ sigVerify: false,
447
+ });
448
+ if (simulation.value.err) {
449
+ return undefined;
450
+ }
451
+ return simulation.value.unitsConsumed;
452
+ }
453
+ }
454
+
356
455
  const QuickNode = {
357
456
  Core: Core,
457
+ Solana: Solana,
358
458
  };
359
459
 
360
460
  exports.viem = viem__namespace;
461
+ exports.solanaWeb3 = web3_js__namespace;
361
462
  exports.Core = Core;
362
463
  exports.QNChainNotSupported = QNChainNotSupported;
363
464
  exports.QNInputValidationError = QNInputValidationError;
364
465
  exports.QNInvalidEndpointUrl = QNInvalidEndpointUrl;
466
+ exports.Solana = Solana;
365
467
  exports["default"] = QuickNode;
@@ -1,7 +1,9 @@
1
1
  import { Core } from '../core/core.js';
2
+ import { Solana } from '../solana/solana.js';
2
3
 
3
4
  const QuickNode = {
4
5
  Core: Core,
6
+ Solana: Solana,
5
7
  };
6
8
 
7
9
  export { QuickNode as default };
package/esm/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import QuickNode from './client/client.js';
2
2
  export { default } from './client/client.js';
3
3
  export { Core } from './core/core.js';
4
+ export { Solana } from './solana/solana.js';
4
5
  import * as viem from 'viem';
5
6
  export { viem };
7
+ import * as web3_js from '@solana/web3.js';
8
+ export { web3_js as solanaWeb3 };
6
9
  export { QNInputValidationError } from './lib/errors/QNInputValidationError.js';
7
10
  export { QNInvalidEndpointUrl } from './lib/errors/QNInvalidEnpointUrl.js';
8
11
  export { QNChainNotSupported } from './lib/errors/QNChainNotSupported.js';
@@ -0,0 +1,74 @@
1
+ import * as _solana_web3_js from '@solana/web3.js';
2
+ import { Transaction, PublicKey, Keypair, SendOptions, Connection } from '@solana/web3.js';
3
+
4
+ type PercentileRangeUnion = '0' | '5' | '10' | '15' | '20' | '25' | '30' | '35' | '40' | '45' | '50' | '55' | '60' | '65' | '70' | '75' | '80' | '85' | '90' | '95' | '100';
5
+ type PriorityFeeLevels = 'low' | 'medium' | 'high' | 'extreme';
6
+ interface PriorityFeeRequestPayload {
7
+ method: string;
8
+ params: {
9
+ last_n_blocks?: number;
10
+ account?: string;
11
+ };
12
+ id: number;
13
+ jsonrpc: string;
14
+ }
15
+ interface PriorityFeeEstimates {
16
+ extreme: number;
17
+ high: number;
18
+ low: number;
19
+ medium: number;
20
+ percentiles: {
21
+ [key in PercentileRangeUnion]: number;
22
+ };
23
+ }
24
+ interface PriorityFeeResponseData {
25
+ jsonrpc: string;
26
+ result: {
27
+ context: {
28
+ slot: number;
29
+ };
30
+ per_compute_unit: PriorityFeeEstimates;
31
+ per_transaction: PriorityFeeEstimates;
32
+ };
33
+ id: number;
34
+ }
35
+ interface EstimatePriorityFeesParams {
36
+ last_n_blocks?: number;
37
+ account?: string;
38
+ }
39
+ interface SolanaClientArgs {
40
+ endpointUrl: string;
41
+ }
42
+ interface SmartTransactionBaseArgs {
43
+ transaction: Transaction;
44
+ feeLevel?: PriorityFeeLevels;
45
+ }
46
+ interface PrepareSmartTransactionArgs extends SmartTransactionBaseArgs {
47
+ payerPublicKey: PublicKey;
48
+ }
49
+ interface SendSmartTransactionArgs extends SmartTransactionBaseArgs {
50
+ keyPair: Keypair;
51
+ sendTransactionOptions?: SendOptions;
52
+ }
53
+
54
+ declare class Solana {
55
+ readonly endpointUrl: string;
56
+ readonly connection: Connection;
57
+ constructor({ endpointUrl }: SolanaClientArgs);
58
+ /**
59
+ * Sends a transaction with a dynamically generated priority fee based on the current network conditions and compute units needed by the transaction.
60
+ */
61
+ sendSmartTransaction(args: SendSmartTransactionArgs): Promise<string>;
62
+ /**
63
+ * Prepares a transaction to be sent with a dynamically generated priority fee based
64
+ * on the current network conditions. It adds a `setComputeUnitPrice` instruction to the transaction
65
+ * and simulates the transaction to estimate the number of compute units it will consume.
66
+ * The returned transaction still needs to be signed and sent to the network.
67
+ */
68
+ prepareSmartTransaction(args: PrepareSmartTransactionArgs): Promise<_solana_web3_js.Transaction>;
69
+ fetchEstimatePriorityFees(args?: EstimatePriorityFeesParams): Promise<PriorityFeeResponseData>;
70
+ private createDynamicPriorityFeeInstruction;
71
+ private getSimulationUnits;
72
+ }
73
+
74
+ export { EstimatePriorityFeesParams, PrepareSmartTransactionArgs, PriorityFeeEstimates, PriorityFeeLevels, PriorityFeeRequestPayload, PriorityFeeResponseData, SendSmartTransactionArgs, SmartTransactionBaseArgs, SolanaClientArgs, Solana as default };
@@ -0,0 +1,2 @@
1
+ import { Solana } from './solana.js';
2
+ export { Solana as default } from './solana.js';
@@ -0,0 +1,100 @@
1
+ import { Connection, ComputeBudgetProgram, VersionedTransaction, TransactionMessage, PublicKey } from '@solana/web3.js';
2
+
3
+ // eslint-disable-next-line @nx/enforce-module-boundaries
4
+ class Solana {
5
+ constructor({ endpointUrl }) {
6
+ this.endpointUrl = endpointUrl;
7
+ this.connection = new Connection(endpointUrl);
8
+ }
9
+ /**
10
+ * Sends a transaction with a dynamically generated priority fee based on the current network conditions and compute units needed by the transaction.
11
+ */
12
+ async sendSmartTransaction(args) {
13
+ const { transaction, keyPair, feeLevel = 'medium', sendTransactionOptions = {}, } = args;
14
+ const smartTransaction = await this.prepareSmartTransaction({
15
+ transaction,
16
+ payerPublicKey: keyPair.publicKey,
17
+ feeLevel,
18
+ });
19
+ smartTransaction.sign(keyPair);
20
+ const hash = await this.connection.sendRawTransaction(transaction.serialize(), { skipPreflight: true, ...sendTransactionOptions });
21
+ return hash;
22
+ }
23
+ /**
24
+ * Prepares a transaction to be sent with a dynamically generated priority fee based
25
+ * on the current network conditions. It adds a `setComputeUnitPrice` instruction to the transaction
26
+ * and simulates the transaction to estimate the number of compute units it will consume.
27
+ * The returned transaction still needs to be signed and sent to the network.
28
+ */
29
+ async prepareSmartTransaction(args) {
30
+ const { transaction, payerPublicKey, feeLevel = 'medium' } = args;
31
+ // Need to fetch this ahead of time and add to transaction so it's in the transaction instructions
32
+ // for the simulation
33
+ const computeUnitPriceInstruction = await this.createDynamicPriorityFeeInstruction(feeLevel);
34
+ const allInstructions = [
35
+ ...transaction.instructions,
36
+ computeUnitPriceInstruction,
37
+ ];
38
+ // eslint-disable-next-line prefer-const
39
+ let [units, recentBlockhash] = await Promise.all([
40
+ this.getSimulationUnits(this.connection, allInstructions, payerPublicKey),
41
+ this.connection.getLatestBlockhash(),
42
+ ]);
43
+ transaction.add(computeUnitPriceInstruction);
44
+ if (units) {
45
+ units = Math.ceil(units * 1.05); // margin of error
46
+ transaction.add(ComputeBudgetProgram.setComputeUnitLimit({ units }));
47
+ }
48
+ transaction.recentBlockhash = recentBlockhash.blockhash;
49
+ return transaction;
50
+ }
51
+ // Get the priority fee averages based on fee data from the latest blocks
52
+ async fetchEstimatePriorityFees(args = {}) {
53
+ const payload = {
54
+ method: 'qn_estimatePriorityFees',
55
+ params: args,
56
+ id: 1,
57
+ jsonrpc: '2.0',
58
+ };
59
+ const response = await fetch(this.endpointUrl, {
60
+ method: 'POST',
61
+ headers: {
62
+ 'Content-Type': 'application/json',
63
+ },
64
+ body: JSON.stringify(payload),
65
+ });
66
+ if (!response.ok) {
67
+ if (response.status === 404) {
68
+ throw new Error(`The RPC method qn_estimatePriorityFees was not found on your endpoint! Your endpoint likely does not have the Priority Fee API add-on installed. Please visit https://marketplace.quicknode.com/add-on/solana-priority-fee to install the Priority Fee API and use this method to send your transactions with priority fees calculated with real-time data.`);
69
+ }
70
+ throw new Error('Failed to fetch priority fee estimates');
71
+ }
72
+ const data = await response.json();
73
+ return data;
74
+ }
75
+ async createDynamicPriorityFeeInstruction(feeType = 'medium') {
76
+ const { result } = await this.fetchEstimatePriorityFees({});
77
+ const priorityFee = result.per_compute_unit[feeType];
78
+ const priorityFeeInstruction = ComputeBudgetProgram.setComputeUnitPrice({
79
+ microLamports: priorityFee,
80
+ });
81
+ return priorityFeeInstruction;
82
+ }
83
+ async getSimulationUnits(connection, instructions, publicKey) {
84
+ const testVersionedTxn = new VersionedTransaction(new TransactionMessage({
85
+ instructions: instructions,
86
+ payerKey: publicKey,
87
+ recentBlockhash: PublicKey.default.toString(), // just a placeholder
88
+ }).compileToV0Message());
89
+ const simulation = await connection.simulateTransaction(testVersionedTxn, {
90
+ replaceRecentBlockhash: true,
91
+ sigVerify: false,
92
+ });
93
+ if (simulation.value.err) {
94
+ return undefined;
95
+ }
96
+ return simulation.value.unitsConsumed;
97
+ }
98
+ }
99
+
100
+ export { Solana };
package/index.d.ts CHANGED
@@ -2,6 +2,9 @@ import { Chain, PublicClient } from 'viem';
2
2
  import * as viem from 'viem';
3
3
  export { viem };
4
4
  import { z, ZodError } from 'zod';
5
+ import * as _solana_web3_js from '@solana/web3.js';
6
+ import { Transaction, PublicKey, Keypair, SendOptions, Connection } from '@solana/web3.js';
7
+ export { _solana_web3_js as solanaWeb3 };
5
8
 
6
9
  type SimplifyType<T> = T extends object ? {
7
10
  [K in keyof T]: SimplifyType<T[K]>;
@@ -557,8 +560,79 @@ declare class Core {
557
560
  constructor({ endpointUrl, chain, config }: CoreArguments);
558
561
  }
559
562
 
563
+ type PercentileRangeUnion = '0' | '5' | '10' | '15' | '20' | '25' | '30' | '35' | '40' | '45' | '50' | '55' | '60' | '65' | '70' | '75' | '80' | '85' | '90' | '95' | '100';
564
+ type PriorityFeeLevels = 'low' | 'medium' | 'high' | 'extreme';
565
+ interface PriorityFeeRequestPayload {
566
+ method: string;
567
+ params: {
568
+ last_n_blocks?: number;
569
+ account?: string;
570
+ };
571
+ id: number;
572
+ jsonrpc: string;
573
+ }
574
+ interface PriorityFeeEstimates {
575
+ extreme: number;
576
+ high: number;
577
+ low: number;
578
+ medium: number;
579
+ percentiles: {
580
+ [key in PercentileRangeUnion]: number;
581
+ };
582
+ }
583
+ interface PriorityFeeResponseData {
584
+ jsonrpc: string;
585
+ result: {
586
+ context: {
587
+ slot: number;
588
+ };
589
+ per_compute_unit: PriorityFeeEstimates;
590
+ per_transaction: PriorityFeeEstimates;
591
+ };
592
+ id: number;
593
+ }
594
+ interface EstimatePriorityFeesParams {
595
+ last_n_blocks?: number;
596
+ account?: string;
597
+ }
598
+ interface SolanaClientArgs {
599
+ endpointUrl: string;
600
+ }
601
+ interface SmartTransactionBaseArgs {
602
+ transaction: Transaction;
603
+ feeLevel?: PriorityFeeLevels;
604
+ }
605
+ interface PrepareSmartTransactionArgs extends SmartTransactionBaseArgs {
606
+ payerPublicKey: PublicKey;
607
+ }
608
+ interface SendSmartTransactionArgs extends SmartTransactionBaseArgs {
609
+ keyPair: Keypair;
610
+ sendTransactionOptions?: SendOptions;
611
+ }
612
+
613
+ declare class Solana {
614
+ readonly endpointUrl: string;
615
+ readonly connection: Connection;
616
+ constructor({ endpointUrl }: SolanaClientArgs);
617
+ /**
618
+ * Sends a transaction with a dynamically generated priority fee based on the current network conditions and compute units needed by the transaction.
619
+ */
620
+ sendSmartTransaction(args: SendSmartTransactionArgs): Promise<string>;
621
+ /**
622
+ * Prepares a transaction to be sent with a dynamically generated priority fee based
623
+ * on the current network conditions. It adds a `setComputeUnitPrice` instruction to the transaction
624
+ * and simulates the transaction to estimate the number of compute units it will consume.
625
+ * The returned transaction still needs to be signed and sent to the network.
626
+ */
627
+ prepareSmartTransaction(args: PrepareSmartTransactionArgs): Promise<_solana_web3_js.Transaction>;
628
+ fetchEstimatePriorityFees(args?: EstimatePriorityFeesParams): Promise<PriorityFeeResponseData>;
629
+ private createDynamicPriorityFeeInstruction;
630
+ private getSimulationUnits;
631
+ }
632
+
560
633
  declare const QuickNode: {
561
634
  Core: typeof Core;
635
+ Solana: typeof Solana;
562
636
  };
563
637
 
564
638
  declare class QNInputValidationError extends Error {
@@ -579,4 +653,4 @@ declare class QNChainNotSupported extends Error {
579
653
  constructor(endpointUrl: string);
580
654
  }
581
655
 
582
- export { Core, CoreArguments, QNChainNotSupported, QNCoreClient, QNCoreClientConfig, QNFetchNFTCollectionDetailsInput, QNFetchNFTCollectionDetailsResult, QNFetchNFTInput, QNFetchNFTResult, QNFetchNFTsByCollectionInput, QNFetchNFTsByCollectionResult, QNGetTokenMetadataByCAInput, QNGetTokenMetadataByCAResult, QNGetTokenMetadataBySymbolInput, QNGetTokenMetadataBySymbolResult, QNGetTransactionsByAddressInput, QNGetTransactionsByAddressResult, QNGetTransfersByNFTInput, QNGetTransfersByNFTResult, QNGetWalletTokenBalanceInput, QNGetWalletTokenBalanceResult, QNGetWalletTokenTransactionsInput, QNGetWalletTokenTransactionsResult, QNInputValidationError, QNInvalidEndpointUrl, QNVerifyNFTsOwnerInput, QNVerifyNFTsOwnerResult, QuickNode as default };
656
+ export { Core, CoreArguments, EstimatePriorityFeesParams, PrepareSmartTransactionArgs, PriorityFeeEstimates, PriorityFeeLevels, PriorityFeeRequestPayload, PriorityFeeResponseData, QNChainNotSupported, QNCoreClient, QNCoreClientConfig, QNFetchNFTCollectionDetailsInput, QNFetchNFTCollectionDetailsResult, QNFetchNFTInput, QNFetchNFTResult, QNFetchNFTsByCollectionInput, QNFetchNFTsByCollectionResult, QNGetTokenMetadataByCAInput, QNGetTokenMetadataByCAResult, QNGetTokenMetadataBySymbolInput, QNGetTokenMetadataBySymbolResult, QNGetTransactionsByAddressInput, QNGetTransactionsByAddressResult, QNGetTransfersByNFTInput, QNGetTransfersByNFTResult, QNGetWalletTokenBalanceInput, QNGetWalletTokenBalanceResult, QNGetWalletTokenTransactionsInput, QNGetWalletTokenTransactionsResult, QNInputValidationError, QNInvalidEndpointUrl, QNVerifyNFTsOwnerInput, QNVerifyNFTsOwnerResult, SendSmartTransactionArgs, SmartTransactionBaseArgs, Solana, SolanaClientArgs, QuickNode as default };
package/package.json CHANGED
@@ -6,12 +6,13 @@
6
6
  "directory": "packages/libs/sdk"
7
7
  },
8
8
  "license": "MIT",
9
- "version": "2.1.3",
9
+ "version": "2.2.1",
10
10
  "main": "./cjs/index.js",
11
11
  "module": "./esm/index.js",
12
12
  "types": "./index.d.ts",
13
13
  "sideEffects": false,
14
14
  "dependencies": {
15
+ "@solana/web3.js": "^1.91",
15
16
  "cross-fetch": "^3.1.6",
16
17
  "tslib": "^2.5.3",
17
18
  "viem": "^1.20.0",
@@ -40,12 +41,20 @@
40
41
  "import": "./esm/core/index.js",
41
42
  "default": "./cjs/core/index.js",
42
43
  "types": "./esm/core/index.d.ts"
44
+ },
45
+ "./solana": {
46
+ "import": "./esm/solana/index.js",
47
+ "default": "./cjs/solana/index.js",
48
+ "types": "./esm/solana/index.d.ts"
43
49
  }
44
50
  },
45
51
  "typesVersions": {
46
52
  "*": {
47
53
  "core": [
48
54
  "./esm/core/index.d.ts"
55
+ ],
56
+ "solana": [
57
+ "./esm/solana/index.d.ts"
49
58
  ]
50
59
  }
51
60
  }