@snapshot-labs/snapshot.js 0.8.3 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -80,6 +80,7 @@ declare const _default: {
80
80
  };
81
81
  network: {
82
82
  type: string;
83
+ snapshotNetwork: boolean;
83
84
  title: string;
84
85
  minLength: number;
85
86
  maxLength: number;
@@ -116,6 +117,7 @@ declare const _default: {
116
117
  type: string;
117
118
  maxLength: number;
118
119
  title: string;
120
+ snapshotNetwork: boolean;
119
121
  };
120
122
  params: {
121
123
  type: string;
@@ -337,6 +339,7 @@ declare const _default: {
337
339
  };
338
340
  network: {
339
341
  type: string;
342
+ snapshotNetwork: boolean;
340
343
  title: string;
341
344
  maxLength: number;
342
345
  };
@@ -554,6 +557,7 @@ declare const _default: {
554
557
  network: {
555
558
  title: string;
556
559
  type: string;
560
+ snapshotNetwork: boolean;
557
561
  };
558
562
  multisend: {
559
563
  title: string;
@@ -76,6 +76,7 @@ declare const _default: {
76
76
  };
77
77
  network: {
78
78
  type: string;
79
+ snapshotNetwork: boolean;
79
80
  title: string;
80
81
  minLength: number;
81
82
  maxLength: number;
@@ -112,6 +113,7 @@ declare const _default: {
112
113
  type: string;
113
114
  maxLength: number;
114
115
  title: string;
116
+ snapshotNetwork: boolean;
115
117
  };
116
118
  params: {
117
119
  type: string;
@@ -333,6 +335,7 @@ declare const _default: {
333
335
  };
334
336
  network: {
335
337
  type: string;
338
+ snapshotNetwork: boolean;
336
339
  title: string;
337
340
  maxLength: number;
338
341
  };
@@ -550,6 +553,7 @@ declare const _default: {
550
553
  network: {
551
554
  title: string;
552
555
  type: string;
556
+ snapshotNetwork: boolean;
553
557
  };
554
558
  multisend: {
555
559
  title: string;
@@ -650,6 +650,7 @@ var definitions = {
650
650
  },
651
651
  network: {
652
652
  type: "string",
653
+ snapshotNetwork: true,
653
654
  title: "network",
654
655
  minLength: 1,
655
656
  maxLength: 32
@@ -685,7 +686,8 @@ var definitions = {
685
686
  network: {
686
687
  type: "string",
687
688
  maxLength: 12,
688
- title: "network"
689
+ title: "network",
690
+ snapshotNetwork: true
689
691
  },
690
692
  params: {
691
693
  type: "object",
@@ -941,6 +943,7 @@ var definitions = {
941
943
  },
942
944
  network: {
943
945
  type: "string",
946
+ snapshotNetwork: true,
944
947
  title: "Network",
945
948
  maxLength: 12
946
949
  }
@@ -1269,7 +1272,8 @@ var definitions$6 = {
1269
1272
  properties: {
1270
1273
  network: {
1271
1274
  title: "Network",
1272
- type: "string"
1275
+ type: "string",
1276
+ snapshotNetwork: true
1273
1277
  },
1274
1278
  multisend: {
1275
1279
  title: "Multisend contract address",
@@ -3426,11 +3430,17 @@ var SNAPSHOT_SUBGRAPH_URL = delegationSubgraphs;
3426
3430
  var ENS_RESOLVER_ABI = [
3427
3431
  'function text(bytes32 node, string calldata key) external view returns (string memory)'
3428
3432
  ];
3433
+ var EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
3429
3434
  var scoreApiHeaders = {
3430
3435
  Accept: 'application/json',
3431
3436
  'Content-Type': 'application/json'
3432
3437
  };
3433
- var ajv = new Ajv__default['default']({ allErrors: true, allowUnionTypes: true, $data: true });
3438
+ var ajv = new Ajv__default['default']({
3439
+ allErrors: true,
3440
+ allowUnionTypes: true,
3441
+ $data: true,
3442
+ passContext: true
3443
+ });
3434
3444
  // @ts-ignore
3435
3445
  addFormats__default['default'](ajv);
3436
3446
  ajv.addFormat('address', {
@@ -3459,6 +3469,24 @@ ajv.addFormat('ethValue', {
3459
3469
  }
3460
3470
  }
3461
3471
  });
3472
+ var networksIds = Object.keys(networks);
3473
+ var mainnetNetworkIds = Object.keys(networks).filter(function (id) { return !networks[id].testnet; });
3474
+ var testnetNetworkIds = Object.keys(networks).filter(function (id) { return networks[id].testnet; });
3475
+ ajv.addKeyword({
3476
+ keyword: 'snapshotNetwork',
3477
+ validate: function (schema, data) {
3478
+ // @ts-ignore
3479
+ var snapshotEnv = this.snapshotEnv || 'default';
3480
+ if (snapshotEnv === 'mainnet')
3481
+ return mainnetNetworkIds.includes(data);
3482
+ if (snapshotEnv === 'testnet')
3483
+ return testnetNetworkIds.includes(data);
3484
+ return networksIds.includes(data);
3485
+ },
3486
+ error: {
3487
+ message: 'must be a valid network used by snapshot'
3488
+ }
3489
+ });
3462
3490
  // Custom URL format to allow empty string values
3463
3491
  // https://github.com/snapshot-labs/snapshot.js/pull/541/files
3464
3492
  ajv.addFormat('customUrl', {
@@ -3630,10 +3658,30 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3630
3658
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3631
3659
  if (options === void 0) { options = {}; }
3632
3660
  return __awaiter(this, void 0, void 0, function () {
3633
- var url, params, res, obj, e_3;
3661
+ var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3634
3662
  return __generator(this, function (_a) {
3635
3663
  switch (_a.label) {
3636
3664
  case 0:
3665
+ if (!Array.isArray(addresses)) {
3666
+ return [2 /*return*/, inputError('addresses should be an array of addresses')];
3667
+ }
3668
+ if (addresses.length === 0) {
3669
+ return [2 /*return*/, inputError('addresses can not be empty')];
3670
+ }
3671
+ invalidAddress = addresses.find(function (address) { return !isValidAddress(address); });
3672
+ if (invalidAddress) {
3673
+ return [2 /*return*/, inputError("Invalid address: " + invalidAddress)];
3674
+ }
3675
+ if (!isValidNetwork(network)) {
3676
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3677
+ }
3678
+ invalidStrategy = strategies.find(function (strategy) { return strategy.network && !isValidNetwork(strategy.network); });
3679
+ if (invalidStrategy) {
3680
+ return [2 /*return*/, inputError("Invalid network (" + invalidStrategy.network + ") in strategy " + invalidStrategy.name)];
3681
+ }
3682
+ if (!isValidSnapshot(snapshot, network)) {
3683
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3684
+ }
3637
3685
  url = new URL(scoreApiUrl);
3638
3686
  url.pathname = '/api/scores';
3639
3687
  scoreApiUrl = url.toString();
@@ -3676,7 +3724,7 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3676
3724
  }
3677
3725
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3678
3726
  return __awaiter(this, void 0, void 0, function () {
3679
- var init, res, json, e_4;
3727
+ var invalidStrategy, init, res, json, e_4;
3680
3728
  return __generator(this, function (_a) {
3681
3729
  switch (_a.label) {
3682
3730
  case 0:
@@ -3684,6 +3732,19 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3684
3732
  options = {};
3685
3733
  if (!options.url)
3686
3734
  options.url = 'https://score.snapshot.org';
3735
+ if (!isValidAddress(address)) {
3736
+ return [2 /*return*/, inputError("Invalid voter address: " + address)];
3737
+ }
3738
+ if (!isValidNetwork(network)) {
3739
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3740
+ }
3741
+ invalidStrategy = strategies.find(function (strategy) { return strategy.network && !isValidNetwork(strategy.network); });
3742
+ if (invalidStrategy) {
3743
+ return [2 /*return*/, inputError("Invalid network (" + invalidStrategy.network + ") in strategy " + invalidStrategy.name)];
3744
+ }
3745
+ if (!isValidSnapshot(snapshot, network)) {
3746
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3747
+ }
3687
3748
  init = {
3688
3749
  method: 'POST',
3689
3750
  headers: scoreApiHeaders,
@@ -3731,6 +3792,15 @@ function validate(validation, author, space, network, snapshot, params, options)
3731
3792
  return __generator(this, function (_a) {
3732
3793
  switch (_a.label) {
3733
3794
  case 0:
3795
+ if (!isValidAddress(author)) {
3796
+ return [2 /*return*/, inputError("Invalid author: " + author)];
3797
+ }
3798
+ if (!isValidNetwork(network)) {
3799
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3800
+ }
3801
+ if (!isValidSnapshot(snapshot, network)) {
3802
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3803
+ }
3734
3804
  if (!options)
3735
3805
  options = {};
3736
3806
  if (!options.url)
@@ -3774,9 +3844,12 @@ function validate(validation, author, space, network, snapshot, params, options)
3774
3844
  });
3775
3845
  });
3776
3846
  }
3777
- function validateSchema(schema, data) {
3847
+ function validateSchema(schema, data, options) {
3848
+ if (options === void 0) { options = {
3849
+ snapshotEnv: 'default'
3850
+ }; }
3778
3851
  var ajvValidate = ajv.compile(schema);
3779
- var valid = ajvValidate(data);
3852
+ var valid = ajvValidate.call(options, data);
3780
3853
  return valid ? valid : ajvValidate.errors;
3781
3854
  }
3782
3855
  function getEnsTextRecord(ens, record, network, options) {
@@ -3943,6 +4016,19 @@ function getNumberWithOrdinal(n) {
3943
4016
  var s = ['th', 'st', 'nd', 'rd'], v = n % 100;
3944
4017
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
3945
4018
  }
4019
+ function isValidNetwork(network) {
4020
+ return !!networks[network];
4021
+ }
4022
+ function isValidAddress(address$1) {
4023
+ return address.isAddress(address$1) && address$1 !== EMPTY_ADDRESS;
4024
+ }
4025
+ function isValidSnapshot(snapshot, network) {
4026
+ return (snapshot === 'latest' ||
4027
+ (typeof snapshot === 'number' && snapshot >= networks[network].start));
4028
+ }
4029
+ function inputError(message) {
4030
+ return Promise.reject(new Error(message));
4031
+ }
3946
4032
  var utils = {
3947
4033
  call: call,
3948
4034
  multicall: multicall,
@@ -641,6 +641,7 @@ var definitions = {
641
641
  },
642
642
  network: {
643
643
  type: "string",
644
+ snapshotNetwork: true,
644
645
  title: "network",
645
646
  minLength: 1,
646
647
  maxLength: 32
@@ -676,7 +677,8 @@ var definitions = {
676
677
  network: {
677
678
  type: "string",
678
679
  maxLength: 12,
679
- title: "network"
680
+ title: "network",
681
+ snapshotNetwork: true
680
682
  },
681
683
  params: {
682
684
  type: "object",
@@ -932,6 +934,7 @@ var definitions = {
932
934
  },
933
935
  network: {
934
936
  type: "string",
937
+ snapshotNetwork: true,
935
938
  title: "Network",
936
939
  maxLength: 12
937
940
  }
@@ -1260,7 +1263,8 @@ var definitions$6 = {
1260
1263
  properties: {
1261
1264
  network: {
1262
1265
  title: "Network",
1263
- type: "string"
1266
+ type: "string",
1267
+ snapshotNetwork: true
1264
1268
  },
1265
1269
  multisend: {
1266
1270
  title: "Multisend contract address",
@@ -3417,11 +3421,17 @@ var SNAPSHOT_SUBGRAPH_URL = delegationSubgraphs;
3417
3421
  var ENS_RESOLVER_ABI = [
3418
3422
  'function text(bytes32 node, string calldata key) external view returns (string memory)'
3419
3423
  ];
3424
+ var EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
3420
3425
  var scoreApiHeaders = {
3421
3426
  Accept: 'application/json',
3422
3427
  'Content-Type': 'application/json'
3423
3428
  };
3424
- var ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
3429
+ var ajv = new Ajv({
3430
+ allErrors: true,
3431
+ allowUnionTypes: true,
3432
+ $data: true,
3433
+ passContext: true
3434
+ });
3425
3435
  // @ts-ignore
3426
3436
  addFormats(ajv);
3427
3437
  ajv.addFormat('address', {
@@ -3450,6 +3460,24 @@ ajv.addFormat('ethValue', {
3450
3460
  }
3451
3461
  }
3452
3462
  });
3463
+ var networksIds = Object.keys(networks);
3464
+ var mainnetNetworkIds = Object.keys(networks).filter(function (id) { return !networks[id].testnet; });
3465
+ var testnetNetworkIds = Object.keys(networks).filter(function (id) { return networks[id].testnet; });
3466
+ ajv.addKeyword({
3467
+ keyword: 'snapshotNetwork',
3468
+ validate: function (schema, data) {
3469
+ // @ts-ignore
3470
+ var snapshotEnv = this.snapshotEnv || 'default';
3471
+ if (snapshotEnv === 'mainnet')
3472
+ return mainnetNetworkIds.includes(data);
3473
+ if (snapshotEnv === 'testnet')
3474
+ return testnetNetworkIds.includes(data);
3475
+ return networksIds.includes(data);
3476
+ },
3477
+ error: {
3478
+ message: 'must be a valid network used by snapshot'
3479
+ }
3480
+ });
3453
3481
  // Custom URL format to allow empty string values
3454
3482
  // https://github.com/snapshot-labs/snapshot.js/pull/541/files
3455
3483
  ajv.addFormat('customUrl', {
@@ -3621,10 +3649,30 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3621
3649
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3622
3650
  if (options === void 0) { options = {}; }
3623
3651
  return __awaiter(this, void 0, void 0, function () {
3624
- var url, params, res, obj, e_3;
3652
+ var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3625
3653
  return __generator(this, function (_a) {
3626
3654
  switch (_a.label) {
3627
3655
  case 0:
3656
+ if (!Array.isArray(addresses)) {
3657
+ return [2 /*return*/, inputError('addresses should be an array of addresses')];
3658
+ }
3659
+ if (addresses.length === 0) {
3660
+ return [2 /*return*/, inputError('addresses can not be empty')];
3661
+ }
3662
+ invalidAddress = addresses.find(function (address) { return !isValidAddress(address); });
3663
+ if (invalidAddress) {
3664
+ return [2 /*return*/, inputError("Invalid address: " + invalidAddress)];
3665
+ }
3666
+ if (!isValidNetwork(network)) {
3667
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3668
+ }
3669
+ invalidStrategy = strategies.find(function (strategy) { return strategy.network && !isValidNetwork(strategy.network); });
3670
+ if (invalidStrategy) {
3671
+ return [2 /*return*/, inputError("Invalid network (" + invalidStrategy.network + ") in strategy " + invalidStrategy.name)];
3672
+ }
3673
+ if (!isValidSnapshot(snapshot, network)) {
3674
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3675
+ }
3628
3676
  url = new URL(scoreApiUrl);
3629
3677
  url.pathname = '/api/scores';
3630
3678
  scoreApiUrl = url.toString();
@@ -3667,7 +3715,7 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3667
3715
  }
3668
3716
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3669
3717
  return __awaiter(this, void 0, void 0, function () {
3670
- var init, res, json, e_4;
3718
+ var invalidStrategy, init, res, json, e_4;
3671
3719
  return __generator(this, function (_a) {
3672
3720
  switch (_a.label) {
3673
3721
  case 0:
@@ -3675,6 +3723,19 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3675
3723
  options = {};
3676
3724
  if (!options.url)
3677
3725
  options.url = 'https://score.snapshot.org';
3726
+ if (!isValidAddress(address)) {
3727
+ return [2 /*return*/, inputError("Invalid voter address: " + address)];
3728
+ }
3729
+ if (!isValidNetwork(network)) {
3730
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3731
+ }
3732
+ invalidStrategy = strategies.find(function (strategy) { return strategy.network && !isValidNetwork(strategy.network); });
3733
+ if (invalidStrategy) {
3734
+ return [2 /*return*/, inputError("Invalid network (" + invalidStrategy.network + ") in strategy " + invalidStrategy.name)];
3735
+ }
3736
+ if (!isValidSnapshot(snapshot, network)) {
3737
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3738
+ }
3678
3739
  init = {
3679
3740
  method: 'POST',
3680
3741
  headers: scoreApiHeaders,
@@ -3722,6 +3783,15 @@ function validate(validation, author, space, network, snapshot, params, options)
3722
3783
  return __generator(this, function (_a) {
3723
3784
  switch (_a.label) {
3724
3785
  case 0:
3786
+ if (!isValidAddress(author)) {
3787
+ return [2 /*return*/, inputError("Invalid author: " + author)];
3788
+ }
3789
+ if (!isValidNetwork(network)) {
3790
+ return [2 /*return*/, inputError("Invalid network: " + network)];
3791
+ }
3792
+ if (!isValidSnapshot(snapshot, network)) {
3793
+ return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3794
+ }
3725
3795
  if (!options)
3726
3796
  options = {};
3727
3797
  if (!options.url)
@@ -3765,9 +3835,12 @@ function validate(validation, author, space, network, snapshot, params, options)
3765
3835
  });
3766
3836
  });
3767
3837
  }
3768
- function validateSchema(schema, data) {
3838
+ function validateSchema(schema, data, options) {
3839
+ if (options === void 0) { options = {
3840
+ snapshotEnv: 'default'
3841
+ }; }
3769
3842
  var ajvValidate = ajv.compile(schema);
3770
- var valid = ajvValidate(data);
3843
+ var valid = ajvValidate.call(options, data);
3771
3844
  return valid ? valid : ajvValidate.errors;
3772
3845
  }
3773
3846
  function getEnsTextRecord(ens, record, network, options) {
@@ -3934,6 +4007,19 @@ function getNumberWithOrdinal(n) {
3934
4007
  var s = ['th', 'st', 'nd', 'rd'], v = n % 100;
3935
4008
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
3936
4009
  }
4010
+ function isValidNetwork(network) {
4011
+ return !!networks[network];
4012
+ }
4013
+ function isValidAddress(address) {
4014
+ return isAddress(address) && address !== EMPTY_ADDRESS;
4015
+ }
4016
+ function isValidSnapshot(snapshot, network) {
4017
+ return (snapshot === 'latest' ||
4018
+ (typeof snapshot === 'number' && snapshot >= networks[network].start));
4019
+ }
4020
+ function inputError(message) {
4021
+ return Promise.reject(new Error(message));
4022
+ }
3937
4023
  var utils = {
3938
4024
  call: call,
3939
4025
  multicall: multicall,