@solana/web3.js 1.46.0 → 1.47.0

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.0",
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
  }
@@ -3444,8 +3438,9 @@ export class Connection {
3444
3438
  );
3445
3439
  const res = create(unsafeRes, GetRecentPerformanceSamplesRpcResult);
3446
3440
  if ('error' in res) {
3447
- throw new Error(
3448
- 'failed to get recent performance samples: ' + res.error.message,
3441
+ throw new SolanaJSONRPCError(
3442
+ res.error,
3443
+ 'failed to get recent performance samples',
3449
3444
  );
3450
3445
  }
3451
3446
 
@@ -3469,7 +3464,7 @@ export class Connection {
3469
3464
 
3470
3465
  const res = create(unsafeRes, GetFeeCalculatorRpcResult);
3471
3466
  if ('error' in res) {
3472
- throw new Error('failed to get fee calculator: ' + res.error.message);
3467
+ throw new SolanaJSONRPCError(res.error, 'failed to get fee calculator');
3473
3468
  }
3474
3469
  const {context, value} = res.result;
3475
3470
  return {
@@ -3491,7 +3486,7 @@ export class Connection {
3491
3486
 
3492
3487
  const res = create(unsafeRes, jsonRpcResultAndContext(nullable(number())));
3493
3488
  if ('error' in res) {
3494
- throw new Error('failed to get slot: ' + res.error.message);
3489
+ throw new SolanaJSONRPCError(res.error, 'failed to get slot');
3495
3490
  }
3496
3491
  if (res.result === null) {
3497
3492
  throw new Error('invalid blockhash');
@@ -3549,7 +3544,7 @@ export class Connection {
3549
3544
  const unsafeRes = await this._rpcRequest('getLatestBlockhash', args);
3550
3545
  const res = create(unsafeRes, GetLatestBlockhashRpcResult);
3551
3546
  if ('error' in res) {
3552
- throw new Error('failed to get latest blockhash: ' + res.error.message);
3547
+ throw new SolanaJSONRPCError(res.error, 'failed to get latest blockhash');
3553
3548
  }
3554
3549
  return res.result;
3555
3550
  }
@@ -3561,7 +3556,7 @@ export class Connection {
3561
3556
  const unsafeRes = await this._rpcRequest('getVersion', []);
3562
3557
  const res = create(unsafeRes, jsonRpcResult(VersionResult));
3563
3558
  if ('error' in res) {
3564
- throw new Error('failed to get version: ' + res.error.message);
3559
+ throw new SolanaJSONRPCError(res.error, 'failed to get version');
3565
3560
  }
3566
3561
  return res.result;
3567
3562
  }
@@ -3573,7 +3568,7 @@ export class Connection {
3573
3568
  const unsafeRes = await this._rpcRequest('getGenesisHash', []);
3574
3569
  const res = create(unsafeRes, jsonRpcResult(string()));
3575
3570
  if ('error' in res) {
3576
- throw new Error('failed to get genesis hash: ' + res.error.message);
3571
+ throw new SolanaJSONRPCError(res.error, 'failed to get genesis hash');
3577
3572
  }
3578
3573
  return res.result;
3579
3574
  }
@@ -3593,7 +3588,7 @@ export class Connection {
3593
3588
  const res = create(unsafeRes, GetBlockRpcResult);
3594
3589
 
3595
3590
  if ('error' in res) {
3596
- throw new Error('failed to get confirmed block: ' + res.error.message);
3591
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3597
3592
  }
3598
3593
 
3599
3594
  const result = res.result;
@@ -3631,8 +3626,9 @@ export class Connection {
3631
3626
  const unsafeRes = await this._rpcRequest('getBlockHeight', args);
3632
3627
  const res = create(unsafeRes, jsonRpcResult(number()));
3633
3628
  if ('error' in res) {
3634
- throw new Error(
3635
- 'failed to get block height information: ' + res.error.message,
3629
+ throw new SolanaJSONRPCError(
3630
+ res.error,
3631
+ 'failed to get block height information',
3636
3632
  );
3637
3633
  }
3638
3634
 
@@ -3660,8 +3656,9 @@ export class Connection {
3660
3656
  const unsafeRes = await this._rpcRequest('getBlockProduction', args);
3661
3657
  const res = create(unsafeRes, BlockProductionResponseStruct);
3662
3658
  if ('error' in res) {
3663
- throw new Error(
3664
- 'failed to get block production information: ' + res.error.message,
3659
+ throw new SolanaJSONRPCError(
3660
+ res.error,
3661
+ 'failed to get block production information',
3665
3662
  );
3666
3663
  }
3667
3664
 
@@ -3682,7 +3679,7 @@ export class Connection {
3682
3679
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3683
3680
  const res = create(unsafeRes, GetTransactionRpcResult);
3684
3681
  if ('error' in res) {
3685
- throw new Error('failed to get transaction: ' + res.error.message);
3682
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3686
3683
  }
3687
3684
 
3688
3685
  const result = res.result;
@@ -3712,7 +3709,7 @@ export class Connection {
3712
3709
  const unsafeRes = await this._rpcRequest('getTransaction', args);
3713
3710
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3714
3711
  if ('error' in res) {
3715
- throw new Error('failed to get transaction: ' + res.error.message);
3712
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3716
3713
  }
3717
3714
  return res.result;
3718
3715
  }
@@ -3740,7 +3737,7 @@ export class Connection {
3740
3737
  const res = unsafeRes.map((unsafeRes: any) => {
3741
3738
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3742
3739
  if ('error' in res) {
3743
- throw new Error('failed to get transactions: ' + res.error.message);
3740
+ throw new SolanaJSONRPCError(res.error, 'failed to get transactions');
3744
3741
  }
3745
3742
  return res.result;
3746
3743
  });
@@ -3768,7 +3765,7 @@ export class Connection {
3768
3765
  const res = unsafeRes.map((unsafeRes: any) => {
3769
3766
  const res = create(unsafeRes, GetTransactionRpcResult);
3770
3767
  if ('error' in res) {
3771
- throw new Error('failed to get transactions: ' + res.error.message);
3768
+ throw new SolanaJSONRPCError(res.error, 'failed to get transactions');
3772
3769
  }
3773
3770
  const result = res.result;
3774
3771
  if (!result) return result;
@@ -3800,7 +3797,7 @@ export class Connection {
3800
3797
  const res = create(unsafeRes, GetConfirmedBlockRpcResult);
3801
3798
 
3802
3799
  if ('error' in res) {
3803
- throw new Error('failed to get confirmed block: ' + res.error.message);
3800
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3804
3801
  }
3805
3802
 
3806
3803
  const result = res.result;
@@ -3851,7 +3848,7 @@ export class Connection {
3851
3848
  const unsafeRes = await this._rpcRequest('getBlocks', args);
3852
3849
  const res = create(unsafeRes, jsonRpcResult(array(number())));
3853
3850
  if ('error' in res) {
3854
- throw new Error('failed to get blocks: ' + res.error.message);
3851
+ throw new SolanaJSONRPCError(res.error, 'failed to get blocks');
3855
3852
  }
3856
3853
  return res.result;
3857
3854
  }
@@ -3875,7 +3872,7 @@ export class Connection {
3875
3872
  const unsafeRes = await this._rpcRequest('getBlock', args);
3876
3873
  const res = create(unsafeRes, GetBlockSignaturesRpcResult);
3877
3874
  if ('error' in res) {
3878
- throw new Error('failed to get block: ' + res.error.message);
3875
+ throw new SolanaJSONRPCError(res.error, 'failed to get block');
3879
3876
  }
3880
3877
  const result = res.result;
3881
3878
  if (!result) {
@@ -3905,7 +3902,7 @@ export class Connection {
3905
3902
  const unsafeRes = await this._rpcRequest('getConfirmedBlock', args);
3906
3903
  const res = create(unsafeRes, GetBlockSignaturesRpcResult);
3907
3904
  if ('error' in res) {
3908
- throw new Error('failed to get confirmed block: ' + res.error.message);
3905
+ throw new SolanaJSONRPCError(res.error, 'failed to get confirmed block');
3909
3906
  }
3910
3907
  const result = res.result;
3911
3908
  if (!result) {
@@ -3927,7 +3924,7 @@ export class Connection {
3927
3924
  const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
3928
3925
  const res = create(unsafeRes, GetTransactionRpcResult);
3929
3926
  if ('error' in res) {
3930
- throw new Error('failed to get transaction: ' + res.error.message);
3927
+ throw new SolanaJSONRPCError(res.error, 'failed to get transaction');
3931
3928
  }
3932
3929
 
3933
3930
  const result = res.result;
@@ -3958,8 +3955,9 @@ export class Connection {
3958
3955
  const unsafeRes = await this._rpcRequest('getConfirmedTransaction', args);
3959
3956
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3960
3957
  if ('error' in res) {
3961
- throw new Error(
3962
- 'failed to get confirmed transaction: ' + res.error.message,
3958
+ throw new SolanaJSONRPCError(
3959
+ res.error,
3960
+ 'failed to get confirmed transaction',
3963
3961
  );
3964
3962
  }
3965
3963
  return res.result;
@@ -3990,8 +3988,9 @@ export class Connection {
3990
3988
  const res = unsafeRes.map((unsafeRes: any) => {
3991
3989
  const res = create(unsafeRes, GetParsedTransactionRpcResult);
3992
3990
  if ('error' in res) {
3993
- throw new Error(
3994
- 'failed to get confirmed transactions: ' + res.error.message,
3991
+ throw new SolanaJSONRPCError(
3992
+ res.error,
3993
+ 'failed to get confirmed transactions',
3995
3994
  );
3996
3995
  }
3997
3996
  return res.result;
@@ -4096,8 +4095,9 @@ export class Connection {
4096
4095
  );
4097
4096
  const res = create(unsafeRes, GetConfirmedSignaturesForAddress2RpcResult);
4098
4097
  if ('error' in res) {
4099
- throw new Error(
4100
- 'failed to get confirmed signatures for address: ' + res.error.message,
4098
+ throw new SolanaJSONRPCError(
4099
+ res.error,
4100
+ 'failed to get confirmed signatures for address',
4101
4101
  );
4102
4102
  }
4103
4103
  return res.result;
@@ -4125,8 +4125,9 @@ export class Connection {
4125
4125
  const unsafeRes = await this._rpcRequest('getSignaturesForAddress', args);
4126
4126
  const res = create(unsafeRes, GetSignaturesForAddressRpcResult);
4127
4127
  if ('error' in res) {
4128
- throw new Error(
4129
- 'failed to get signatures for address: ' + res.error.message,
4128
+ throw new SolanaJSONRPCError(
4129
+ res.error,
4130
+ 'failed to get signatures for address',
4130
4131
  );
4131
4132
  }
4132
4133
  return res.result;
@@ -4198,8 +4199,9 @@ export class Connection {
4198
4199
  ]);
4199
4200
  const res = create(unsafeRes, RequestAirdropRpcResult);
4200
4201
  if ('error' in res) {
4201
- throw new Error(
4202
- 'airdrop to ' + to.toBase58() + ' failed: ' + res.error.message,
4202
+ throw new SolanaJSONRPCError(
4203
+ res.error,
4204
+ `airdrop to ${to.toBase58()} failed`,
4203
4205
  );
4204
4206
  }
4205
4207
  return res.result;
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
+ }