@solana/web3.js 1.46.0 → 1.47.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.46.0",
3
+ "version": "1.47.2",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
package/src/connection.ts CHANGED
@@ -26,7 +26,7 @@ import RpcClient from 'jayson/lib/client/browser';
26
26
 
27
27
  import {AgentManager} from './agent-manager';
28
28
  import {EpochSchedule} from './epoch-schedule';
29
- import {SendTransactionError} from './errors';
29
+ import {SendTransactionError, SolanaJSONRPCError} from './errors';
30
30
  import fetchImpl, {Response} from './fetch-impl';
31
31
  import {NonceAccount} from './nonce-account';
32
32
  import {PublicKey} from './publickey';
@@ -2532,11 +2532,9 @@ export class Connection {
2532
2532
  const unsafeRes = await this._rpcRequest('getBalance', args);
2533
2533
  const res = create(unsafeRes, jsonRpcResultAndContext(number()));
2534
2534
  if ('error' in res) {
2535
- throw new Error(
2536
- 'failed to get balance for ' +
2537
- publicKey.toBase58() +
2538
- ': ' +
2539
- res.error.message,
2535
+ throw new SolanaJSONRPCError(
2536
+ res.error,
2537
+ `failed to get balance for ${publicKey.toBase58()}`,
2540
2538
  );
2541
2539
  }
2542
2540
  return res.result;
@@ -2565,8 +2563,9 @@ export class Connection {
2565
2563
  const unsafeRes = await this._rpcRequest('getBlockTime', [slot]);
2566
2564
  const res = create(unsafeRes, jsonRpcResult(nullable(number())));
2567
2565
  if ('error' in res) {
2568
- throw new Error(
2569
- 'failed to get block time for slot ' + slot + ': ' + res.error.message,
2566
+ throw new SolanaJSONRPCError(
2567
+ res.error,
2568
+ `failed to get block time for slot ${slot}`,
2570
2569
  );
2571
2570
  }
2572
2571
  return res.result;
@@ -2580,8 +2579,9 @@ export class Connection {
2580
2579
  const unsafeRes = await this._rpcRequest('minimumLedgerSlot', []);
2581
2580
  const res = create(unsafeRes, jsonRpcResult(number()));
2582
2581
  if ('error' in res) {
2583
- throw new Error(
2584
- 'failed to get minimum ledger slot: ' + res.error.message,
2582
+ throw new SolanaJSONRPCError(
2583
+ res.error,
2584
+ 'failed to get minimum ledger slot',
2585
2585
  );
2586
2586
  }
2587
2587
  return res.result;
@@ -2594,8 +2594,9 @@ export class Connection {
2594
2594
  const unsafeRes = await this._rpcRequest('getFirstAvailableBlock', []);
2595
2595
  const res = create(unsafeRes, SlotRpcResult);
2596
2596
  if ('error' in res) {
2597
- throw new Error(
2598
- 'failed to get first available block: ' + res.error.message,
2597
+ throw new SolanaJSONRPCError(
2598
+ res.error,
2599
+ 'failed to get first available block',
2599
2600
  );
2600
2601
  }
2601
2602
  return res.result;
@@ -2624,7 +2625,7 @@ export class Connection {
2624
2625
  const unsafeRes = await this._rpcRequest('getSupply', [configArg]);
2625
2626
  const res = create(unsafeRes, GetSupplyRpcResult);
2626
2627
  if ('error' in res) {
2627
- throw new Error('failed to get supply: ' + res.error.message);
2628
+ throw new SolanaJSONRPCError(res.error, 'failed to get supply');
2628
2629
  }
2629
2630
  return res.result;
2630
2631
  }
@@ -2640,7 +2641,7 @@ export class Connection {
2640
2641
  const unsafeRes = await this._rpcRequest('getTokenSupply', args);
2641
2642
  const res = create(unsafeRes, jsonRpcResultAndContext(TokenAmountResult));
2642
2643
  if ('error' in res) {
2643
- throw new Error('failed to get token supply: ' + res.error.message);
2644
+ throw new SolanaJSONRPCError(res.error, 'failed to get token supply');
2644
2645
  }
2645
2646
  return res.result;
2646
2647
  }
@@ -2656,8 +2657,9 @@ export class Connection {
2656
2657
  const unsafeRes = await this._rpcRequest('getTokenAccountBalance', args);
2657
2658
  const res = create(unsafeRes, jsonRpcResultAndContext(TokenAmountResult));
2658
2659
  if ('error' in res) {
2659
- throw new Error(
2660
- 'failed to get token account balance: ' + res.error.message,
2660
+ throw new SolanaJSONRPCError(
2661
+ res.error,
2662
+ 'failed to get token account balance',
2661
2663
  );
2662
2664
  }
2663
2665
  return res.result;
@@ -2690,11 +2692,9 @@ export class Connection {
2690
2692
  const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
2691
2693
  const res = create(unsafeRes, GetTokenAccountsByOwner);
2692
2694
  if ('error' in res) {
2693
- throw new Error(
2694
- 'failed to get token accounts owned by account ' +
2695
- ownerAddress.toBase58() +
2696
- ': ' +
2697
- res.error.message,
2695
+ throw new SolanaJSONRPCError(
2696
+ res.error,
2697
+ `failed to get token accounts owned by account ${ownerAddress.toBase58()}`,
2698
2698
  );
2699
2699
  }
2700
2700
  return res.result;
@@ -2725,11 +2725,9 @@ export class Connection {
2725
2725
  const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
2726
2726
  const res = create(unsafeRes, GetParsedTokenAccountsByOwner);
2727
2727
  if ('error' in res) {
2728
- throw new Error(
2729
- 'failed to get token accounts owned by account ' +
2730
- ownerAddress.toBase58() +
2731
- ': ' +
2732
- res.error.message,
2728
+ throw new SolanaJSONRPCError(
2729
+ res.error,
2730
+ `failed to get token accounts owned by account ${ownerAddress.toBase58()}`,
2733
2731
  );
2734
2732
  }
2735
2733
  return res.result;
@@ -2749,7 +2747,7 @@ export class Connection {
2749
2747
  const unsafeRes = await this._rpcRequest('getLargestAccounts', args);
2750
2748
  const res = create(unsafeRes, GetLargestAccountsRpcResult);
2751
2749
  if ('error' in res) {
2752
- throw new Error('failed to get largest accounts: ' + res.error.message);
2750
+ throw new SolanaJSONRPCError(res.error, 'failed to get largest accounts');
2753
2751
  }
2754
2752
  return res.result;
2755
2753
  }
@@ -2766,8 +2764,9 @@ export class Connection {
2766
2764
  const unsafeRes = await this._rpcRequest('getTokenLargestAccounts', args);
2767
2765
  const res = create(unsafeRes, GetTokenLargestAccountsResult);
2768
2766
  if ('error' in res) {
2769
- throw new Error(
2770
- 'failed to get token largest accounts: ' + res.error.message,
2767
+ throw new SolanaJSONRPCError(
2768
+ res.error,
2769
+ 'failed to get token largest accounts',
2771
2770
  );
2772
2771
  }
2773
2772
  return res.result;
@@ -2794,11 +2793,9 @@ export class Connection {
2794
2793
  jsonRpcResultAndContext(nullable(AccountInfoResult)),
2795
2794
  );
2796
2795
  if ('error' in res) {
2797
- throw new Error(
2798
- 'failed to get info about account ' +
2799
- publicKey.toBase58() +
2800
- ': ' +
2801
- res.error.message,
2796
+ throw new SolanaJSONRPCError(
2797
+ res.error,
2798
+ `failed to get info about account ${publicKey.toBase58()}`,
2802
2799
  );
2803
2800
  }
2804
2801
  return res.result;
@@ -2824,11 +2821,9 @@ export class Connection {
2824
2821
  jsonRpcResultAndContext(nullable(ParsedAccountInfoResult)),
2825
2822
  );
2826
2823
  if ('error' in res) {
2827
- throw new Error(
2828
- 'failed to get info about account ' +
2829
- publicKey.toBase58() +
2830
- ': ' +
2831
- res.error.message,
2824
+ throw new SolanaJSONRPCError(
2825
+ res.error,
2826
+ `failed to get info about account ${publicKey.toBase58()}`,
2832
2827
  );
2833
2828
  }
2834
2829
  return res.result;
@@ -2871,8 +2866,9 @@ export class Connection {
2871
2866
  jsonRpcResultAndContext(array(nullable(AccountInfoResult))),
2872
2867
  );
2873
2868
  if ('error' in res) {
2874
- throw new Error(
2875
- 'failed to get info for accounts ' + keys + ': ' + res.error.message,
2869
+ throw new SolanaJSONRPCError(
2870
+ res.error,
2871
+ `failed to get info for accounts ${keys}`,
2876
2872
  );
2877
2873
  }
2878
2874
  return res.result;
@@ -2915,10 +2911,9 @@ export class Connection {
2915
2911
  const unsafeRes = await this._rpcRequest('getStakeActivation', args);
2916
2912
  const res = create(unsafeRes, jsonRpcResult(StakeActivationResult));
2917
2913
  if ('error' in res) {
2918
- throw new Error(
2919
- `failed to get Stake Activation ${publicKey.toBase58()}: ${
2920
- res.error.message
2921
- }`,
2914
+ throw new SolanaJSONRPCError(
2915
+ res.error,
2916
+ `failed to get Stake Activation ${publicKey.toBase58()}`,
2922
2917
  );
2923
2918
  }
2924
2919
  return res.result;
@@ -2945,11 +2940,9 @@ export class Connection {
2945
2940
  const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
2946
2941
  const res = create(unsafeRes, jsonRpcResult(array(KeyedAccountInfoResult)));
2947
2942
  if ('error' in res) {
2948
- throw new Error(
2949
- 'failed to get accounts owned by program ' +
2950
- programId.toBase58() +
2951
- ': ' +
2952
- res.error.message,
2943
+ throw new SolanaJSONRPCError(
2944
+ res.error,
2945
+ `failed to get accounts owned by program ${programId.toBase58()}`,
2953
2946
  );
2954
2947
  }
2955
2948
  return res.result;
@@ -2983,11 +2976,9 @@ export class Connection {
2983
2976
  jsonRpcResult(array(KeyedParsedAccountInfoResult)),
2984
2977
  );
2985
2978
  if ('error' in res) {
2986
- throw new Error(
2987
- 'failed to get accounts owned by program ' +
2988
- programId.toBase58() +
2989
- ': ' +
2990
- res.error.message,
2979
+ throw new SolanaJSONRPCError(
2980
+ res.error,
2981
+ `failed to get accounts owned by program ${programId.toBase58()}`,
2991
2982
  );
2992
2983
  }
2993
2984
  return res.result;
@@ -3142,7 +3133,7 @@ export class Connection {
3142
3133
  const unsafeRes = await this._rpcRequest('getClusterNodes', []);
3143
3134
  const res = create(unsafeRes, jsonRpcResult(array(ContactInfoResult)));
3144
3135
  if ('error' in res) {
3145
- throw new Error('failed to get cluster nodes: ' + res.error.message);
3136
+ throw new SolanaJSONRPCError(res.error, 'failed to get cluster nodes');
3146
3137
  }
3147
3138
  return res.result;
3148
3139
  }
@@ -3155,7 +3146,7 @@ export class Connection {
3155
3146
  const unsafeRes = await this._rpcRequest('getVoteAccounts', args);
3156
3147
  const res = create(unsafeRes, GetVoteAccounts);
3157
3148
  if ('error' in res) {
3158
- throw new Error('failed to get vote accounts: ' + res.error.message);
3149
+ throw new SolanaJSONRPCError(res.error, 'failed to get vote accounts');
3159
3150
  }
3160
3151
  return res.result;
3161
3152
  }
@@ -3177,7 +3168,7 @@ export class Connection {
3177
3168
  const unsafeRes = await this._rpcRequest('getSlot', args);
3178
3169
  const res = create(unsafeRes, jsonRpcResult(number()));
3179
3170
  if ('error' in res) {
3180
- throw new Error('failed to get slot: ' + res.error.message);
3171
+ throw new SolanaJSONRPCError(res.error, 'failed to get slot');
3181
3172
  }
3182
3173
  return res.result;
3183
3174
  }
@@ -3199,7 +3190,7 @@ export class Connection {
3199
3190
  const unsafeRes = await this._rpcRequest('getSlotLeader', args);
3200
3191
  const res = create(unsafeRes, jsonRpcResult(string()));
3201
3192
  if ('error' in res) {
3202
- throw new Error('failed to get slot leader: ' + res.error.message);
3193
+ throw new SolanaJSONRPCError(res.error, 'failed to get slot leader');
3203
3194
  }
3204
3195
  return res.result;
3205
3196
  }
@@ -3218,7 +3209,7 @@ export class Connection {
3218
3209
  const unsafeRes = await this._rpcRequest('getSlotLeaders', args);
3219
3210
  const res = create(unsafeRes, jsonRpcResult(array(PublicKeyFromString)));
3220
3211
  if ('error' in res) {
3221
- throw new Error('failed to get slot leaders: ' + res.error.message);
3212
+ throw new SolanaJSONRPCError(res.error, 'failed to get slot leaders');
3222
3213
  }
3223
3214
  return res.result;
3224
3215
  }
@@ -3253,7 +3244,7 @@ export class Connection {
3253
3244
  const unsafeRes = await this._rpcRequest('getSignatureStatuses', params);
3254
3245
  const res = create(unsafeRes, GetSignatureStatusesRpcResult);
3255
3246
  if ('error' in res) {
3256
- throw new Error('failed to get signature status: ' + res.error.message);
3247
+ throw new SolanaJSONRPCError(res.error, 'failed to get signature status');
3257
3248
  }
3258
3249
  return res.result;
3259
3250
  }
@@ -3275,7 +3266,10 @@ export class Connection {
3275
3266
  const unsafeRes = await this._rpcRequest('getTransactionCount', args);
3276
3267
  const res = create(unsafeRes, jsonRpcResult(number()));
3277
3268
  if ('error' in res) {
3278
- throw new Error('failed to get transaction count: ' + res.error.message);
3269
+ throw new SolanaJSONRPCError(
3270
+ res.error,
3271
+ 'failed to get transaction count',
3272
+ );
3279
3273
  }
3280
3274
  return res.result;
3281
3275
  }
@@ -3303,7 +3297,7 @@ export class Connection {
3303
3297
  const unsafeRes = await this._rpcRequest('getInflationGovernor', args);
3304
3298
  const res = create(unsafeRes, GetInflationGovernorRpcResult);
3305
3299
  if ('error' in res) {
3306
- throw new Error('failed to get inflation: ' + res.error.message);
3300
+ throw new SolanaJSONRPCError(res.error, 'failed to get inflation');
3307
3301
  }
3308
3302
  return res.result;
3309
3303
  }
@@ -3330,7 +3324,7 @@ export class Connection {
3330
3324
  const unsafeRes = await this._rpcRequest('getInflationReward', args);
3331
3325
  const res = create(unsafeRes, GetInflationRewardResult);
3332
3326
  if ('error' in res) {
3333
- throw new Error('failed to get inflation reward: ' + res.error.message);
3327
+ throw new SolanaJSONRPCError(res.error, 'failed to get inflation reward');
3334
3328
  }
3335
3329
  return res.result;
3336
3330
  }
@@ -3352,7 +3346,7 @@ export class Connection {
3352
3346
  const unsafeRes = await this._rpcRequest('getEpochInfo', args);
3353
3347
  const res = create(unsafeRes, GetEpochInfoRpcResult);
3354
3348
  if ('error' in res) {
3355
- throw new Error('failed to get epoch info: ' + res.error.message);
3349
+ throw new SolanaJSONRPCError(res.error, 'failed to get epoch info');
3356
3350
  }
3357
3351
  return res.result;
3358
3352
  }
@@ -3364,7 +3358,7 @@ export class Connection {
3364
3358
  const unsafeRes = await this._rpcRequest('getEpochSchedule', []);
3365
3359
  const res = create(unsafeRes, GetEpochScheduleRpcResult);
3366
3360
  if ('error' in res) {
3367
- throw new Error('failed to get epoch schedule: ' + res.error.message);
3361
+ throw new SolanaJSONRPCError(res.error, 'failed to get epoch schedule');
3368
3362
  }
3369
3363
  const epochSchedule = res.result;
3370
3364
  return new EpochSchedule(
@@ -3384,7 +3378,7 @@ export class Connection {
3384
3378
  const unsafeRes = await this._rpcRequest('getLeaderSchedule', []);
3385
3379
  const res = create(unsafeRes, GetLeaderScheduleRpcResult);
3386
3380
  if ('error' in res) {
3387
- throw new Error('failed to get leader schedule: ' + res.error.message);
3381
+ throw new SolanaJSONRPCError(res.error, 'failed to get leader schedule');
3388
3382
  }
3389
3383
  return res.result;
3390
3384
  }
@@ -3425,7 +3419,7 @@ export class Connection {
3425
3419
  const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
3426
3420
  const res = create(unsafeRes, GetRecentBlockhashAndContextRpcResult);
3427
3421
  if ('error' in res) {
3428
- throw new Error('failed to get recent blockhash: ' + res.error.message);
3422
+ throw new SolanaJSONRPCError(res.error, 'failed to get recent blockhash');
3429
3423
  }
3430
3424
  return res.result;
3431
3425
  }
@@ -3437,15 +3431,15 @@ export class Connection {
3437
3431
  async getRecentPerformanceSamples(
3438
3432
  limit?: number,
3439
3433
  ): Promise<Array<PerfSample>> {
3440
- const args = this._buildArgs(limit ? [limit] : []);
3441
3434
  const unsafeRes = await this._rpcRequest(
3442
3435
  'getRecentPerformanceSamples',
3443
- args,
3436
+ limit ? [limit] : [],
3444
3437
  );
3445
3438
  const res = create(unsafeRes, GetRecentPerformanceSamplesRpcResult);
3446
3439
  if ('error' in res) {
3447
- throw new Error(
3448
- 'failed to get recent performance samples: ' + res.error.message,
3440
+ throw new SolanaJSONRPCError(
3441
+ res.error,
3442
+ 'failed to get recent performance samples',
3449
3443
  );
3450
3444
  }
3451
3445
 
@@ -3469,7 +3463,7 @@ export class Connection {
3469
3463
 
3470
3464
  const res = create(unsafeRes, GetFeeCalculatorRpcResult);
3471
3465
  if ('error' in res) {
3472
- throw new Error('failed to get fee calculator: ' + res.error.message);
3466
+ throw new SolanaJSONRPCError(res.error, 'failed to get fee calculator');
3473
3467
  }
3474
3468
  const {context, value} = res.result;
3475
3469
  return {
@@ -3491,7 +3485,7 @@ export class Connection {
3491
3485
 
3492
3486
  const res = create(unsafeRes, jsonRpcResultAndContext(nullable(number())));
3493
3487
  if ('error' in res) {
3494
- throw new Error('failed to get slot: ' + res.error.message);
3488
+ throw new SolanaJSONRPCError(res.error, 'failed to get slot');
3495
3489
  }
3496
3490
  if (res.result === null) {
3497
3491
  throw new Error('invalid blockhash');
@@ -3549,7 +3543,7 @@ export class Connection {
3549
3543
  const unsafeRes = await this._rpcRequest('getLatestBlockhash', args);
3550
3544
  const res = create(unsafeRes, GetLatestBlockhashRpcResult);
3551
3545
  if ('error' in res) {
3552
- throw new Error('failed to get latest blockhash: ' + res.error.message);
3546
+ throw new SolanaJSONRPCError(res.error, 'failed to get latest blockhash');
3553
3547
  }
3554
3548
  return res.result;
3555
3549
  }
@@ -3561,7 +3555,7 @@ export class Connection {
3561
3555
  const unsafeRes = await this._rpcRequest('getVersion', []);
3562
3556
  const res = create(unsafeRes, jsonRpcResult(VersionResult));
3563
3557
  if ('error' in res) {
3564
- throw new Error('failed to get version: ' + res.error.message);
3558
+ throw new SolanaJSONRPCError(res.error, 'failed to get version');
3565
3559
  }
3566
3560
  return res.result;
3567
3561
  }
@@ -3573,7 +3567,7 @@ export class Connection {
3573
3567
  const unsafeRes = await this._rpcRequest('getGenesisHash', []);
3574
3568
  const res = create(unsafeRes, jsonRpcResult(string()));
3575
3569
  if ('error' in res) {
3576
- throw new Error('failed to get genesis hash: ' + res.error.message);
3570
+ throw new SolanaJSONRPCError(res.error, 'failed to get genesis hash');
3577
3571
  }
3578
3572
  return res.result;
3579
3573
  }
@@ -3593,7 +3587,7 @@ export class Connection {
3593
3587
  const res = create(unsafeRes, GetBlockRpcResult);
3594
3588
 
3595
3589
  if ('error' in res) {
3596
- throw new Error('failed to get confirmed block: ' + res.error.message);
3590
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3597
3591
  }
3598
3592
 
3599
3593
  const result = res.result;
@@ -3631,8 +3625,9 @@ export class Connection {
3631
3625
  const unsafeRes = await this._rpcRequest('getBlockHeight', args);
3632
3626
  const res = create(unsafeRes, jsonRpcResult(number()));
3633
3627
  if ('error' in res) {
3634
- throw new Error(
3635
- 'failed to get block height information: ' + res.error.message,
3628
+ throw new SolanaJSONRPCError(
3629
+ res.error,
3630
+ 'failed to get block height information',
3636
3631
  );
3637
3632
  }
3638
3633
 
@@ -3660,8 +3655,9 @@ export class Connection {
3660
3655
  const unsafeRes = await this._rpcRequest('getBlockProduction', args);
3661
3656
  const res = create(unsafeRes, BlockProductionResponseStruct);
3662
3657
  if ('error' in res) {
3663
- throw new Error(
3664
- 'failed to get block production information: ' + res.error.message,
3658
+ throw new SolanaJSONRPCError(
3659
+ res.error,
3660
+ 'failed to get block production information',
3665
3661
  );
3666
3662
  }
3667
3663
 
@@ -3682,7 +3678,7 @@ export class Connection {
3682
3678
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3683
3679
  const res = create(unsafeRes, GetTransactionRpcResult);
3684
3680
  if ('error' in res) {
3685
- throw new Error('failed to get transaction: ' + res.error.message);
3681
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3686
3682
  }
3687
3683
 
3688
3684
  const result = res.result;
@@ -3712,7 +3708,7 @@ export class Connection {
3712
3708
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3713
3709
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3714
3710
  if ('error' in res) {
3715
- throw new Error('failed to get transaction: ' + res.error.message);
3711
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3716
3712
  }
3717
3713
  return res.result;
3718
3714
  }
@@ -3740,7 +3736,7 @@ export class Connection {
3740
3736
  const res = unsafeRes.map((unsafeRes: any) => {
3741
3737
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3742
3738
  if ('error' in res) {
3743
- throw new Error('failed to get transactions: ' + res.error.message);
3739
+ throw new SolanaJSONRPCError(res.error, 'failed to get transactions');
3744
3740
  }
3745
3741
  return res.result;
3746
3742
  });
@@ -3768,7 +3764,7 @@ export class Connection {
3768
3764
  const res = unsafeRes.map((unsafeRes: any) => {
3769
3765
  const res = create(unsafeRes, GetTransactionRpcResult);
3770
3766
  if ('error' in res) {
3771
- throw new Error('failed to get transactions: ' + res.error.message);
3767
+ throw new SolanaJSONRPCError(res.error, 'failed to get transactions');
3772
3768
  }
3773
3769
  const result = res.result;
3774
3770
  if (!result) return result;
@@ -3800,7 +3796,7 @@ export class Connection {
3800
3796
  const res = create(unsafeRes, GetConfirmedBlockRpcResult);
3801
3797
 
3802
3798
  if ('error' in res) {
3803
- throw new Error('failed to get confirmed block: ' + res.error.message);
3799
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3804
3800
  }
3805
3801
 
3806
3802
  const result = res.result;
@@ -3851,7 +3847,7 @@ export class Connection {
3851
3847
  const unsafeRes = await this._rpcRequest('getBlocks', args);
3852
3848
  const res = create(unsafeRes, jsonRpcResult(array(number())));
3853
3849
  if ('error' in res) {
3854
- throw new Error('failed to get blocks: ' + res.error.message);
3850
+ throw new SolanaJSONRPCError(res.error, 'failed to get blocks');
3855
3851
  }
3856
3852
  return res.result;
3857
3853
  }
@@ -3875,7 +3871,7 @@ export class Connection {
3875
3871
  const unsafeRes = await this._rpcRequest('getBlock', args);
3876
3872
  const res = create(unsafeRes, GetBlockSignaturesRpcResult);
3877
3873
  if ('error' in res) {
3878
- throw new Error('failed to get block: ' + res.error.message);
3874
+ throw new SolanaJSONRPCError(res.error, 'failed to get block');
3879
3875
  }
3880
3876
  const result = res.result;
3881
3877
  if (!result) {
@@ -3905,7 +3901,7 @@ export class Connection {
3905
3901
  const unsafeRes = await this._rpcRequest('getConfirmedBlock', args);
3906
3902
  const res = create(unsafeRes, GetBlockSignaturesRpcResult);
3907
3903
  if ('error' in res) {
3908
- throw new Error('failed to get confirmed block: ' + res.error.message);
3904
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3909
3905
  }
3910
3906
  const result = res.result;
3911
3907
  if (!result) {
@@ -3927,7 +3923,7 @@ export class Connection {
3927
3923
  const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
3928
3924
  const res = create(unsafeRes, GetTransactionRpcResult);
3929
3925
  if ('error' in res) {
3930
- throw new Error('failed to get transaction: ' + res.error.message);
3926
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3931
3927
  }
3932
3928
 
3933
3929
  const result = res.result;
@@ -3958,8 +3954,9 @@ export class Connection {
3958
3954
  const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
3959
3955
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3960
3956
  if ('error' in res) {
3961
- throw new Error(
3962
- 'failed to get confirmed transaction: ' + res.error.message,
3957
+ throw new SolanaJSONRPCError(
3958
+ res.error,
3959
+ 'failed to get confirmed transaction',
3963
3960
  );
3964
3961
  }
3965
3962
  return res.result;
@@ -3990,8 +3987,9 @@ export class Connection {
3990
3987
  const res = unsafeRes.map((unsafeRes: any) => {
3991
3988
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3992
3989
  if ('error' in res) {
3993
- throw new Error(
3994
- 'failed to get confirmed transactions: ' + res.error.message,
3990
+ throw new SolanaJSONRPCError(
3991
+ res.error,
3992
+ 'failed to get confirmed transactions',
3995
3993
  );
3996
3994
  }
3997
3995
  return res.result;
@@ -4096,8 +4094,9 @@ export class Connection {
4096
4094
  );
4097
4095
  const res = create(unsafeRes, GetConfirmedSignaturesForAddress2RpcResult);
4098
4096
  if ('error' in res) {
4099
- throw new Error(
4100
- 'failed to get confirmed signatures for address: ' + res.error.message,
4097
+ throw new SolanaJSONRPCError(
4098
+ res.error,
4099
+ 'failed to get confirmed signatures for address',
4101
4100
  );
4102
4101
  }
4103
4102
  return res.result;
@@ -4125,8 +4124,9 @@ export class Connection {
4125
4124
  const unsafeRes = await this._rpcRequest('getSignaturesForAddress', args);
4126
4125
  const res = create(unsafeRes, GetSignaturesForAddressRpcResult);
4127
4126
  if ('error' in res) {
4128
- throw new Error(
4129
- 'failed to get signatures for address: ' + res.error.message,
4127
+ throw new SolanaJSONRPCError(
4128
+ res.error,
4129
+ 'failed to get signatures for address',
4130
4130
  );
4131
4131
  }
4132
4132
  return res.result;
@@ -4198,8 +4198,9 @@ export class Connection {
4198
4198
  ]);
4199
4199
  const res = create(unsafeRes, RequestAirdropRpcResult);
4200
4200
  if ('error' in res) {
4201
- throw new Error(
4202
- 'airdrop to ' + to.toBase58() + ' failed: ' + res.error.message,
4201
+ throw new SolanaJSONRPCError(
4202
+ res.error,
4203
+ `airdrop to ${to.toBase58()} failed`,
4203
4204
  );
4204
4205
  }
4205
4206
  return res.result;
@@ -4440,7 +4441,7 @@ export class Connection {
4440
4441
  const preflightCommitment =
4441
4442
  (options && options.preflightCommitment) || this.commitment;
4442
4443
 
4443
- if (options && options.maxRetries) {
4444
+ if (options && options.maxRetries != null) {
4444
4445
  config.maxRetries = options.maxRetries;
4445
4446
  }
4446
4447
  if (options && options.minContextSlot != null) {
package/src/errors.ts CHANGED
@@ -7,3 +7,44 @@ export class SendTransactionError extends Error {
7
7
  this.logs = logs;
8
8
  }
9
9
  }
10
+
11
+ // Keep in sync with client/src/rpc_custom_errors.rs
12
+ // Typescript `enums` thwart tree-shaking. See https://bargsten.org/jsts/enums/
13
+ export const SolanaJSONRPCErrorCode = {
14
+ JSON_RPC_SERVER_ERROR_BLOCK_CLEANED_UP: -32001,
15
+ JSON_RPC_SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE: -32002,
16
+ JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_VERIFICATION_FAILURE: -32003,
17
+ JSON_RPC_SERVER_ERROR_BLOCK_NOT_AVAILABLE: -32004,
18
+ JSON_RPC_SERVER_ERROR_NODE_UNHEALTHY: -32005,
19
+ JSON_RPC_SERVER_ERROR_TRANSACTION_PRECOMPILE_VERIFICATION_FAILURE: -32006,
20
+ JSON_RPC_SERVER_ERROR_SLOT_SKIPPED: -32007,
21
+ JSON_RPC_SERVER_ERROR_NO_SNAPSHOT: -32008,
22
+ JSON_RPC_SERVER_ERROR_LONG_TERM_STORAGE_SLOT_SKIPPED: -32009,
23
+ JSON_RPC_SERVER_ERROR_KEY_EXCLUDED_FROM_SECONDARY_INDEX: -32010,
24
+ JSON_RPC_SERVER_ERROR_TRANSACTION_HISTORY_NOT_AVAILABLE: -32011,
25
+ JSON_RPC_SCAN_ERROR: -32012,
26
+ JSON_RPC_SERVER_ERROR_TRANSACTION_SIGNATURE_LEN_MISMATCH: -32013,
27
+ JSON_RPC_SERVER_ERROR_BLOCK_STATUS_NOT_AVAILABLE_YET: -32014,
28
+ JSON_RPC_SERVER_ERROR_UNSUPPORTED_TRANSACTION_VERSION: -32015,
29
+ JSON_RPC_SERVER_ERROR_MIN_CONTEXT_SLOT_NOT_REACHED: -32016,
30
+ } as const;
31
+ export type SolanaJSONRPCErrorCodeEnum =
32
+ typeof SolanaJSONRPCErrorCode[keyof typeof SolanaJSONRPCErrorCode];
33
+
34
+ export class SolanaJSONRPCError extends Error {
35
+ code: SolanaJSONRPCErrorCodeEnum | unknown;
36
+ data?: any;
37
+ constructor(
38
+ {
39
+ code,
40
+ message,
41
+ data,
42
+ }: Readonly<{code: unknown; message: string; data?: any}>,
43
+ customMessage?: string,
44
+ ) {
45
+ super(customMessage != null ? `${customMessage}: ${message}` : message);
46
+ this.code = code;
47
+ this.data = data;
48
+ this.name = 'SolanaJSONRPCError';
49
+ }
50
+ }