@snapshot-labs/snapshot.js 0.9.0 → 0.9.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.
@@ -319,7 +319,7 @@ var livenet = {
319
319
  sequencer: "https://seq.snapshot.org"
320
320
  };
321
321
  var testnet = {
322
- hub: "https://testnet.snapshot.org",
322
+ hub: "https://testnet.hub.snapshot.org",
323
323
  sequencer: "https://testnet.seq.snapshot.org"
324
324
  };
325
325
  var local = {
@@ -3430,6 +3430,7 @@ var SNAPSHOT_SUBGRAPH_URL = delegationSubgraphs;
3430
3430
  var ENS_RESOLVER_ABI = [
3431
3431
  'function text(bytes32 node, string calldata key) external view returns (string memory)'
3432
3432
  ];
3433
+ var EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
3433
3434
  var scoreApiHeaders = {
3434
3435
  Accept: 'application/json',
3435
3436
  'Content-Type': 'application/json'
@@ -3657,10 +3658,30 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3657
3658
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3658
3659
  if (options === void 0) { options = {}; }
3659
3660
  return __awaiter(this, void 0, void 0, function () {
3660
- var url, params, res, obj, e_3;
3661
+ var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3661
3662
  return __generator(this, function (_a) {
3662
3663
  switch (_a.label) {
3663
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
+ }
3664
3685
  url = new URL(scoreApiUrl);
3665
3686
  url.pathname = '/api/scores';
3666
3687
  scoreApiUrl = url.toString();
@@ -3703,7 +3724,7 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3703
3724
  }
3704
3725
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3705
3726
  return __awaiter(this, void 0, void 0, function () {
3706
- var init, res, json, e_4;
3727
+ var invalidStrategy, init, res, json, e_4;
3707
3728
  return __generator(this, function (_a) {
3708
3729
  switch (_a.label) {
3709
3730
  case 0:
@@ -3711,6 +3732,19 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3711
3732
  options = {};
3712
3733
  if (!options.url)
3713
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
+ }
3714
3748
  init = {
3715
3749
  method: 'POST',
3716
3750
  headers: scoreApiHeaders,
@@ -3758,6 +3792,15 @@ function validate(validation, author, space, network, snapshot, params, options)
3758
3792
  return __generator(this, function (_a) {
3759
3793
  switch (_a.label) {
3760
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
+ }
3761
3804
  if (!options)
3762
3805
  options = {};
3763
3806
  if (!options.url)
@@ -3973,6 +4016,19 @@ function getNumberWithOrdinal(n) {
3973
4016
  var s = ['th', 'st', 'nd', 'rd'], v = n % 100;
3974
4017
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
3975
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
+ }
3976
4032
  var utils = {
3977
4033
  call: call,
3978
4034
  multicall: multicall,
@@ -310,7 +310,7 @@ var livenet = {
310
310
  sequencer: "https://seq.snapshot.org"
311
311
  };
312
312
  var testnet = {
313
- hub: "https://testnet.snapshot.org",
313
+ hub: "https://testnet.hub.snapshot.org",
314
314
  sequencer: "https://testnet.seq.snapshot.org"
315
315
  };
316
316
  var local = {
@@ -3421,6 +3421,7 @@ var SNAPSHOT_SUBGRAPH_URL = delegationSubgraphs;
3421
3421
  var ENS_RESOLVER_ABI = [
3422
3422
  'function text(bytes32 node, string calldata key) external view returns (string memory)'
3423
3423
  ];
3424
+ var EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
3424
3425
  var scoreApiHeaders = {
3425
3426
  Accept: 'application/json',
3426
3427
  'Content-Type': 'application/json'
@@ -3648,10 +3649,30 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3648
3649
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3649
3650
  if (options === void 0) { options = {}; }
3650
3651
  return __awaiter(this, void 0, void 0, function () {
3651
- var url, params, res, obj, e_3;
3652
+ var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3652
3653
  return __generator(this, function (_a) {
3653
3654
  switch (_a.label) {
3654
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
+ }
3655
3676
  url = new URL(scoreApiUrl);
3656
3677
  url.pathname = '/api/scores';
3657
3678
  scoreApiUrl = url.toString();
@@ -3694,7 +3715,7 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3694
3715
  }
3695
3716
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3696
3717
  return __awaiter(this, void 0, void 0, function () {
3697
- var init, res, json, e_4;
3718
+ var invalidStrategy, init, res, json, e_4;
3698
3719
  return __generator(this, function (_a) {
3699
3720
  switch (_a.label) {
3700
3721
  case 0:
@@ -3702,6 +3723,19 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3702
3723
  options = {};
3703
3724
  if (!options.url)
3704
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
+ }
3705
3739
  init = {
3706
3740
  method: 'POST',
3707
3741
  headers: scoreApiHeaders,
@@ -3749,6 +3783,15 @@ function validate(validation, author, space, network, snapshot, params, options)
3749
3783
  return __generator(this, function (_a) {
3750
3784
  switch (_a.label) {
3751
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
+ }
3752
3795
  if (!options)
3753
3796
  options = {};
3754
3797
  if (!options.url)
@@ -3964,6 +4007,19 @@ function getNumberWithOrdinal(n) {
3964
4007
  var s = ['th', 'st', 'nd', 'rd'], v = n % 100;
3965
4008
  return n + (s[(v - 20) % 10] || s[v] || s[0]);
3966
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
+ }
3967
4023
  var utils = {
3968
4024
  call: call,
3969
4025
  multicall: multicall,