@snapshot-labs/snapshot.js 0.11.22 → 0.11.23

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.
@@ -3647,6 +3647,51 @@ var scoreApiHeaders = {
3647
3647
  Accept: 'application/json',
3648
3648
  'Content-Type': 'application/json'
3649
3649
  };
3650
+ var DEFAULT_SCORE_API_URL = 'https://score.snapshot.org';
3651
+ function formatScoreAPIUrl(url, options) {
3652
+ if (url === void 0) { url = DEFAULT_SCORE_API_URL; }
3653
+ if (options === void 0) { options = {
3654
+ path: ''
3655
+ }; }
3656
+ var scoreURL = new URL(url);
3657
+ if (options.path)
3658
+ scoreURL.pathname = options.path;
3659
+ var apiKey = scoreURL.searchParams.get('apiKey');
3660
+ var headers = __assign({}, scoreApiHeaders);
3661
+ if (apiKey) {
3662
+ scoreURL.searchParams.delete('apiKey');
3663
+ headers = __assign(__assign({}, scoreApiHeaders), { 'X-API-KEY': apiKey });
3664
+ }
3665
+ return {
3666
+ url: scoreURL.toString(),
3667
+ headers: headers
3668
+ };
3669
+ }
3670
+ function parseScoreAPIResponse(res) {
3671
+ return __awaiter(this, void 0, void 0, function () {
3672
+ var data;
3673
+ return __generator(this, function (_a) {
3674
+ switch (_a.label) {
3675
+ case 0: return [4 /*yield*/, res.text()];
3676
+ case 1:
3677
+ data = _a.sent();
3678
+ try {
3679
+ data = JSON.parse(data);
3680
+ }
3681
+ catch (e) {
3682
+ return [2 /*return*/, Promise.reject({
3683
+ code: res.status || 500,
3684
+ message: 'Failed to parse response from score API',
3685
+ data: data
3686
+ })];
3687
+ }
3688
+ if (data.error)
3689
+ return [2 /*return*/, Promise.reject(data.error)];
3690
+ return [2 /*return*/, data];
3691
+ }
3692
+ });
3693
+ });
3694
+ }
3650
3695
  var ajv = new Ajv__default['default']({
3651
3696
  allErrors: true,
3652
3697
  allowUnionTypes: true,
@@ -3905,12 +3950,12 @@ function sendTransaction(web3, contractAddress, abi, action, params, overrides)
3905
3950
  }
3906
3951
  function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl, options) {
3907
3952
  if (snapshot === void 0) { snapshot = 'latest'; }
3908
- if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3953
+ if (scoreApiUrl === void 0) { scoreApiUrl = DEFAULT_SCORE_API_URL; }
3909
3954
  if (options === void 0) { options = {}; }
3910
3955
  return __awaiter(this, void 0, void 0, function () {
3911
- var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3912
- return __generator(this, function (_a) {
3913
- switch (_a.label) {
3956
+ var invalidAddress, invalidStrategy, urlObject, _a, url, headers, params, res, response, e_3;
3957
+ return __generator(this, function (_b) {
3958
+ switch (_b.label) {
3914
3959
  case 0:
3915
3960
  if (!Array.isArray(addresses)) {
3916
3961
  return [2 /*return*/, inputError('addresses should be an array of addresses')];
@@ -3932,12 +3977,14 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3932
3977
  if (!isValidSnapshot(snapshot, network)) {
3933
3978
  return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3934
3979
  }
3935
- url = new URL(scoreApiUrl);
3936
- url.pathname = '/api/scores';
3937
- scoreApiUrl = url.toString();
3938
- _a.label = 1;
3980
+ urlObject = new URL(scoreApiUrl);
3981
+ urlObject.pathname = '/api/scores';
3982
+ _a = formatScoreAPIUrl(scoreApiUrl, {
3983
+ path: '/api/scores'
3984
+ }), url = _a.url, headers = _a.headers;
3985
+ _b.label = 1;
3939
3986
  case 1:
3940
- _a.trys.push([1, 4, , 5]);
3987
+ _b.trys.push([1, 4, , 5]);
3941
3988
  params = {
3942
3989
  space: space,
3943
3990
  network: network,
@@ -3945,24 +3992,21 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3945
3992
  strategies: strategies,
3946
3993
  addresses: addresses
3947
3994
  };
3948
- return [4 /*yield*/, fetch__default['default'](scoreApiUrl, {
3995
+ return [4 /*yield*/, fetch__default['default'](url, {
3949
3996
  method: 'POST',
3950
- headers: scoreApiHeaders,
3997
+ headers: headers,
3951
3998
  body: JSON.stringify({ params: params })
3952
3999
  })];
3953
4000
  case 2:
3954
- res = _a.sent();
3955
- return [4 /*yield*/, res.json()];
4001
+ res = _b.sent();
4002
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
3956
4003
  case 3:
3957
- obj = _a.sent();
3958
- if (obj.error) {
3959
- return [2 /*return*/, Promise.reject(obj.error)];
3960
- }
4004
+ response = _b.sent();
3961
4005
  return [2 /*return*/, options.returnValue === 'all'
3962
- ? obj.result
3963
- : obj.result[options.returnValue || 'scores']];
4006
+ ? response.result
4007
+ : response.result[options.returnValue || 'scores']];
3964
4008
  case 4:
3965
- e_3 = _a.sent();
4009
+ e_3 = _b.sent();
3966
4010
  if (e_3.errno) {
3967
4011
  return [2 /*return*/, Promise.reject({ code: e_3.errno, message: e_3.toString(), data: '' })];
3968
4012
  }
@@ -3974,14 +4018,11 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3974
4018
  }
3975
4019
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3976
4020
  return __awaiter(this, void 0, void 0, function () {
3977
- var invalidStrategy, init, res, json, e_4;
3978
- return __generator(this, function (_a) {
3979
- switch (_a.label) {
4021
+ var _a, url, headers, invalidStrategy, init, res, response, e_4;
4022
+ return __generator(this, function (_b) {
4023
+ switch (_b.label) {
3980
4024
  case 0:
3981
- if (!options)
3982
- options = {};
3983
- if (!options.url)
3984
- options.url = 'https://score.snapshot.org';
4025
+ _a = formatScoreAPIUrl(options === null || options === void 0 ? void 0 : options.url), url = _a.url, headers = _a.headers;
3985
4026
  if (!isValidAddress(address)) {
3986
4027
  return [2 /*return*/, inputError("Invalid voter address: " + address)];
3987
4028
  }
@@ -3997,7 +4038,7 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3997
4038
  }
3998
4039
  init = {
3999
4040
  method: 'POST',
4000
- headers: scoreApiHeaders,
4041
+ headers: headers,
4001
4042
  body: JSON.stringify({
4002
4043
  jsonrpc: '2.0',
4003
4044
  method: 'get_vp',
@@ -4011,22 +4052,18 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4011
4052
  }
4012
4053
  })
4013
4054
  };
4014
- _a.label = 1;
4055
+ _b.label = 1;
4015
4056
  case 1:
4016
- _a.trys.push([1, 4, , 5]);
4017
- return [4 /*yield*/, fetch__default['default'](options.url, init)];
4057
+ _b.trys.push([1, 4, , 5]);
4058
+ return [4 /*yield*/, fetch__default['default'](url, init)];
4018
4059
  case 2:
4019
- res = _a.sent();
4020
- return [4 /*yield*/, res.json()];
4060
+ res = _b.sent();
4061
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
4021
4062
  case 3:
4022
- json = _a.sent();
4023
- if (json.error)
4024
- return [2 /*return*/, Promise.reject(json.error)];
4025
- if (json.result)
4026
- return [2 /*return*/, json.result];
4027
- return [3 /*break*/, 5];
4063
+ response = _b.sent();
4064
+ return [2 /*return*/, response.result];
4028
4065
  case 4:
4029
- e_4 = _a.sent();
4066
+ e_4 = _b.sent();
4030
4067
  if (e_4.errno) {
4031
4068
  return [2 /*return*/, Promise.reject({ code: e_4.errno, message: e_4.toString(), data: '' })];
4032
4069
  }
@@ -4038,9 +4075,9 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4038
4075
  }
4039
4076
  function validate(validation, author, space, network, snapshot, params, options) {
4040
4077
  return __awaiter(this, void 0, void 0, function () {
4041
- var init, res, json, e_5;
4042
- return __generator(this, function (_a) {
4043
- switch (_a.label) {
4078
+ var _a, url, headers, init, res, response, e_5;
4079
+ return __generator(this, function (_b) {
4080
+ switch (_b.label) {
4044
4081
  case 0:
4045
4082
  if (!isValidAddress(author)) {
4046
4083
  return [2 /*return*/, inputError("Invalid author: " + author)];
@@ -4053,11 +4090,10 @@ function validate(validation, author, space, network, snapshot, params, options)
4053
4090
  }
4054
4091
  if (!options)
4055
4092
  options = {};
4056
- if (!options.url)
4057
- options.url = 'https://score.snapshot.org';
4093
+ _a = formatScoreAPIUrl(options.url), url = _a.url, headers = _a.headers;
4058
4094
  init = {
4059
4095
  method: 'POST',
4060
- headers: scoreApiHeaders,
4096
+ headers: headers,
4061
4097
  body: JSON.stringify({
4062
4098
  jsonrpc: '2.0',
4063
4099
  method: 'validate',
@@ -4071,20 +4107,18 @@ function validate(validation, author, space, network, snapshot, params, options)
4071
4107
  }
4072
4108
  })
4073
4109
  };
4074
- _a.label = 1;
4110
+ _b.label = 1;
4075
4111
  case 1:
4076
- _a.trys.push([1, 4, , 5]);
4077
- return [4 /*yield*/, fetch__default['default'](options.url, init)];
4112
+ _b.trys.push([1, 4, , 5]);
4113
+ return [4 /*yield*/, fetch__default['default'](url, init)];
4078
4114
  case 2:
4079
- res = _a.sent();
4080
- return [4 /*yield*/, res.json()];
4115
+ res = _b.sent();
4116
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
4081
4117
  case 3:
4082
- json = _a.sent();
4083
- if (json.error)
4084
- return [2 /*return*/, Promise.reject(json.error)];
4085
- return [2 /*return*/, json.result];
4118
+ response = _b.sent();
4119
+ return [2 /*return*/, response.result];
4086
4120
  case 4:
4087
- e_5 = _a.sent();
4121
+ e_5 = _b.sent();
4088
4122
  if (e_5.errno) {
4089
4123
  return [2 /*return*/, Promise.reject({ code: e_5.errno, message: e_5.toString(), data: '' })];
4090
4124
  }
@@ -3637,6 +3637,51 @@ var scoreApiHeaders = {
3637
3637
  Accept: 'application/json',
3638
3638
  'Content-Type': 'application/json'
3639
3639
  };
3640
+ var DEFAULT_SCORE_API_URL = 'https://score.snapshot.org';
3641
+ function formatScoreAPIUrl(url, options) {
3642
+ if (url === void 0) { url = DEFAULT_SCORE_API_URL; }
3643
+ if (options === void 0) { options = {
3644
+ path: ''
3645
+ }; }
3646
+ var scoreURL = new URL(url);
3647
+ if (options.path)
3648
+ scoreURL.pathname = options.path;
3649
+ var apiKey = scoreURL.searchParams.get('apiKey');
3650
+ var headers = __assign({}, scoreApiHeaders);
3651
+ if (apiKey) {
3652
+ scoreURL.searchParams.delete('apiKey');
3653
+ headers = __assign(__assign({}, scoreApiHeaders), { 'X-API-KEY': apiKey });
3654
+ }
3655
+ return {
3656
+ url: scoreURL.toString(),
3657
+ headers: headers
3658
+ };
3659
+ }
3660
+ function parseScoreAPIResponse(res) {
3661
+ return __awaiter(this, void 0, void 0, function () {
3662
+ var data;
3663
+ return __generator(this, function (_a) {
3664
+ switch (_a.label) {
3665
+ case 0: return [4 /*yield*/, res.text()];
3666
+ case 1:
3667
+ data = _a.sent();
3668
+ try {
3669
+ data = JSON.parse(data);
3670
+ }
3671
+ catch (e) {
3672
+ return [2 /*return*/, Promise.reject({
3673
+ code: res.status || 500,
3674
+ message: 'Failed to parse response from score API',
3675
+ data: data
3676
+ })];
3677
+ }
3678
+ if (data.error)
3679
+ return [2 /*return*/, Promise.reject(data.error)];
3680
+ return [2 /*return*/, data];
3681
+ }
3682
+ });
3683
+ });
3684
+ }
3640
3685
  var ajv = new Ajv({
3641
3686
  allErrors: true,
3642
3687
  allowUnionTypes: true,
@@ -3895,12 +3940,12 @@ function sendTransaction(web3, contractAddress, abi, action, params, overrides)
3895
3940
  }
3896
3941
  function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl, options) {
3897
3942
  if (snapshot === void 0) { snapshot = 'latest'; }
3898
- if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
3943
+ if (scoreApiUrl === void 0) { scoreApiUrl = DEFAULT_SCORE_API_URL; }
3899
3944
  if (options === void 0) { options = {}; }
3900
3945
  return __awaiter(this, void 0, void 0, function () {
3901
- var invalidAddress, invalidStrategy, url, params, res, obj, e_3;
3902
- return __generator(this, function (_a) {
3903
- switch (_a.label) {
3946
+ var invalidAddress, invalidStrategy, urlObject, _a, url, headers, params, res, response, e_3;
3947
+ return __generator(this, function (_b) {
3948
+ switch (_b.label) {
3904
3949
  case 0:
3905
3950
  if (!Array.isArray(addresses)) {
3906
3951
  return [2 /*return*/, inputError('addresses should be an array of addresses')];
@@ -3922,12 +3967,14 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3922
3967
  if (!isValidSnapshot(snapshot, network)) {
3923
3968
  return [2 /*return*/, inputError("Snapshot (" + snapshot + ") must be 'latest' or greater than network start block (" + networks[network].start + ")")];
3924
3969
  }
3925
- url = new URL(scoreApiUrl);
3926
- url.pathname = '/api/scores';
3927
- scoreApiUrl = url.toString();
3928
- _a.label = 1;
3970
+ urlObject = new URL(scoreApiUrl);
3971
+ urlObject.pathname = '/api/scores';
3972
+ _a = formatScoreAPIUrl(scoreApiUrl, {
3973
+ path: '/api/scores'
3974
+ }), url = _a.url, headers = _a.headers;
3975
+ _b.label = 1;
3929
3976
  case 1:
3930
- _a.trys.push([1, 4, , 5]);
3977
+ _b.trys.push([1, 4, , 5]);
3931
3978
  params = {
3932
3979
  space: space,
3933
3980
  network: network,
@@ -3935,24 +3982,21 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3935
3982
  strategies: strategies,
3936
3983
  addresses: addresses
3937
3984
  };
3938
- return [4 /*yield*/, fetch(scoreApiUrl, {
3985
+ return [4 /*yield*/, fetch(url, {
3939
3986
  method: 'POST',
3940
- headers: scoreApiHeaders,
3987
+ headers: headers,
3941
3988
  body: JSON.stringify({ params: params })
3942
3989
  })];
3943
3990
  case 2:
3944
- res = _a.sent();
3945
- return [4 /*yield*/, res.json()];
3991
+ res = _b.sent();
3992
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
3946
3993
  case 3:
3947
- obj = _a.sent();
3948
- if (obj.error) {
3949
- return [2 /*return*/, Promise.reject(obj.error)];
3950
- }
3994
+ response = _b.sent();
3951
3995
  return [2 /*return*/, options.returnValue === 'all'
3952
- ? obj.result
3953
- : obj.result[options.returnValue || 'scores']];
3996
+ ? response.result
3997
+ : response.result[options.returnValue || 'scores']];
3954
3998
  case 4:
3955
- e_3 = _a.sent();
3999
+ e_3 = _b.sent();
3956
4000
  if (e_3.errno) {
3957
4001
  return [2 /*return*/, Promise.reject({ code: e_3.errno, message: e_3.toString(), data: '' })];
3958
4002
  }
@@ -3964,14 +4008,11 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
3964
4008
  }
3965
4009
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
3966
4010
  return __awaiter(this, void 0, void 0, function () {
3967
- var invalidStrategy, init, res, json, e_4;
3968
- return __generator(this, function (_a) {
3969
- switch (_a.label) {
4011
+ var _a, url, headers, invalidStrategy, init, res, response, e_4;
4012
+ return __generator(this, function (_b) {
4013
+ switch (_b.label) {
3970
4014
  case 0:
3971
- if (!options)
3972
- options = {};
3973
- if (!options.url)
3974
- options.url = 'https://score.snapshot.org';
4015
+ _a = formatScoreAPIUrl(options === null || options === void 0 ? void 0 : options.url), url = _a.url, headers = _a.headers;
3975
4016
  if (!isValidAddress(address)) {
3976
4017
  return [2 /*return*/, inputError("Invalid voter address: " + address)];
3977
4018
  }
@@ -3987,7 +4028,7 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
3987
4028
  }
3988
4029
  init = {
3989
4030
  method: 'POST',
3990
- headers: scoreApiHeaders,
4031
+ headers: headers,
3991
4032
  body: JSON.stringify({
3992
4033
  jsonrpc: '2.0',
3993
4034
  method: 'get_vp',
@@ -4001,22 +4042,18 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4001
4042
  }
4002
4043
  })
4003
4044
  };
4004
- _a.label = 1;
4045
+ _b.label = 1;
4005
4046
  case 1:
4006
- _a.trys.push([1, 4, , 5]);
4007
- return [4 /*yield*/, fetch(options.url, init)];
4047
+ _b.trys.push([1, 4, , 5]);
4048
+ return [4 /*yield*/, fetch(url, init)];
4008
4049
  case 2:
4009
- res = _a.sent();
4010
- return [4 /*yield*/, res.json()];
4050
+ res = _b.sent();
4051
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
4011
4052
  case 3:
4012
- json = _a.sent();
4013
- if (json.error)
4014
- return [2 /*return*/, Promise.reject(json.error)];
4015
- if (json.result)
4016
- return [2 /*return*/, json.result];
4017
- return [3 /*break*/, 5];
4053
+ response = _b.sent();
4054
+ return [2 /*return*/, response.result];
4018
4055
  case 4:
4019
- e_4 = _a.sent();
4056
+ e_4 = _b.sent();
4020
4057
  if (e_4.errno) {
4021
4058
  return [2 /*return*/, Promise.reject({ code: e_4.errno, message: e_4.toString(), data: '' })];
4022
4059
  }
@@ -4028,9 +4065,9 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4028
4065
  }
4029
4066
  function validate(validation, author, space, network, snapshot, params, options) {
4030
4067
  return __awaiter(this, void 0, void 0, function () {
4031
- var init, res, json, e_5;
4032
- return __generator(this, function (_a) {
4033
- switch (_a.label) {
4068
+ var _a, url, headers, init, res, response, e_5;
4069
+ return __generator(this, function (_b) {
4070
+ switch (_b.label) {
4034
4071
  case 0:
4035
4072
  if (!isValidAddress(author)) {
4036
4073
  return [2 /*return*/, inputError("Invalid author: " + author)];
@@ -4043,11 +4080,10 @@ function validate(validation, author, space, network, snapshot, params, options)
4043
4080
  }
4044
4081
  if (!options)
4045
4082
  options = {};
4046
- if (!options.url)
4047
- options.url = 'https://score.snapshot.org';
4083
+ _a = formatScoreAPIUrl(options.url), url = _a.url, headers = _a.headers;
4048
4084
  init = {
4049
4085
  method: 'POST',
4050
- headers: scoreApiHeaders,
4086
+ headers: headers,
4051
4087
  body: JSON.stringify({
4052
4088
  jsonrpc: '2.0',
4053
4089
  method: 'validate',
@@ -4061,20 +4097,18 @@ function validate(validation, author, space, network, snapshot, params, options)
4061
4097
  }
4062
4098
  })
4063
4099
  };
4064
- _a.label = 1;
4100
+ _b.label = 1;
4065
4101
  case 1:
4066
- _a.trys.push([1, 4, , 5]);
4067
- return [4 /*yield*/, fetch(options.url, init)];
4102
+ _b.trys.push([1, 4, , 5]);
4103
+ return [4 /*yield*/, fetch(url, init)];
4068
4104
  case 2:
4069
- res = _a.sent();
4070
- return [4 /*yield*/, res.json()];
4105
+ res = _b.sent();
4106
+ return [4 /*yield*/, parseScoreAPIResponse(res)];
4071
4107
  case 3:
4072
- json = _a.sent();
4073
- if (json.error)
4074
- return [2 /*return*/, Promise.reject(json.error)];
4075
- return [2 /*return*/, json.result];
4108
+ response = _b.sent();
4109
+ return [2 /*return*/, response.result];
4076
4110
  case 4:
4077
- e_5 = _a.sent();
4111
+ e_5 = _b.sent();
4078
4112
  if (e_5.errno) {
4079
4113
  return [2 /*return*/, Promise.reject({ code: e_5.errno, message: e_5.toString(), data: '' })];
4080
4114
  }