@snapshot-labs/snapshot.js 0.7.3 → 0.8.0-beta.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.
@@ -1,4 +1,4 @@
1
- import fetch from 'cross-fetch';
1
+ import { ofetch } from 'ofetch';
2
2
  import { getAddress, isAddress } from '@ethersproject/address';
3
3
  import { Interface } from '@ethersproject/abi';
4
4
  import { Contract } from '@ethersproject/contracts';
@@ -362,29 +362,35 @@ var Client = /** @class */ (function () {
362
362
  });
363
363
  };
364
364
  Client.prototype.send = function (envelop) {
365
+ var _a, _b;
365
366
  return __awaiter(this, void 0, void 0, function () {
366
- var address, init;
367
- return __generator(this, function (_a) {
368
- address = this.address;
369
- if (envelop.sig === '0x' && this.options.relayerURL)
370
- address = this.options.relayerURL;
371
- init = {
372
- method: 'POST',
373
- headers: {
374
- Accept: 'application/json',
375
- 'Content-Type': 'application/json'
376
- },
377
- body: JSON.stringify(envelop)
378
- };
379
- return [2 /*return*/, new Promise(function (resolve, reject) {
380
- fetch(address, init)
381
- .then(function (res) {
382
- if (res.ok)
383
- return resolve(res.json());
384
- throw res;
385
- })
386
- .catch(function (e) { return e.json().then(function (json) { return reject(json); }); });
387
- })];
367
+ var address, init, e_1, isSequencerError;
368
+ return __generator(this, function (_c) {
369
+ switch (_c.label) {
370
+ case 0:
371
+ address = this.address;
372
+ if (envelop.sig === '0x' && this.options.relayerURL)
373
+ address = this.options.relayerURL;
374
+ init = {
375
+ method: 'POST',
376
+ headers: {
377
+ Accept: 'application/json',
378
+ 'Content-Type': 'application/json'
379
+ },
380
+ timeout: this.options.timeout || 20e3,
381
+ body: envelop
382
+ };
383
+ _c.label = 1;
384
+ case 1:
385
+ _c.trys.push([1, 3, , 4]);
386
+ return [4 /*yield*/, ofetch(address, init)];
387
+ case 2: return [2 /*return*/, _c.sent()];
388
+ case 3:
389
+ e_1 = _c.sent();
390
+ isSequencerError = ((_a = e_1.data) === null || _a === void 0 ? void 0 : _a.hasOwnProperty('error')) && ((_b = e_1.data) === null || _b === void 0 ? void 0 : _b.hasOwnProperty('error_description'));
391
+ return [2 /*return*/, Promise.reject(isSequencerError ? e_1.data : e_1)];
392
+ case 4: return [2 /*return*/];
393
+ }
388
394
  });
389
395
  });
390
396
  };
@@ -4586,32 +4592,50 @@ function multicall(network, provider, abi, calls, options) {
4586
4592
  });
4587
4593
  }
4588
4594
  function subgraphRequest(url, query, options) {
4589
- if (options === void 0) { options = {}; }
4595
+ var _a;
4590
4596
  return __awaiter(this, void 0, void 0, function () {
4591
- var res, responseData, data;
4592
- return __generator(this, function (_a) {
4593
- switch (_a.label) {
4594
- case 0: return [4 /*yield*/, fetch(url, {
4597
+ var init, body, e_3;
4598
+ return __generator(this, function (_b) {
4599
+ switch (_b.label) {
4600
+ case 0:
4601
+ _b.trys.push([0, 2, , 3]);
4602
+ init = {
4595
4603
  method: 'POST',
4596
4604
  headers: __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers),
4597
- body: JSON.stringify({ query: jsonToGraphQLQuery({ query: query }) })
4598
- })];
4605
+ timeout: (options === null || options === void 0 ? void 0 : options.timeout) || 20e3,
4606
+ body: { query: jsonToGraphQLQuery({ query: query }) }
4607
+ };
4608
+ return [4 /*yield*/, ofetch(url, init)];
4599
4609
  case 1:
4600
- res = _a.sent();
4601
- return [4 /*yield*/, res.text()];
4602
- case 2:
4603
- responseData = _a.sent();
4604
- try {
4605
- responseData = JSON.parse(responseData);
4610
+ body = _b.sent();
4611
+ if (typeof body === 'string') {
4612
+ return [2 /*return*/, Promise.reject({
4613
+ errors: [
4614
+ {
4615
+ message: 'Body is not a JSON object',
4616
+ extensions: { code: 'INVALID_JSON' }
4617
+ }
4618
+ ]
4619
+ })];
4606
4620
  }
4607
- catch (e) {
4608
- throw new Error("Errors found in subgraphRequest: URL: " + url + ", Status: " + res.status + ", Response: " + responseData.substring(0, 400));
4609
- }
4610
- if (responseData.errors) {
4611
- throw new Error("Errors found in subgraphRequest: URL: " + url + ", Status: " + res.status + ", Response: " + JSON.stringify(responseData.errors).substring(0, 400));
4621
+ if (body.errors) {
4622
+ return [2 /*return*/, Promise.reject(body)];
4612
4623
  }
4613
- data = responseData.data;
4614
- return [2 /*return*/, data || {}];
4624
+ return [2 /*return*/, body.data];
4625
+ case 2:
4626
+ e_3 = _b.sent();
4627
+ return [2 /*return*/, Promise.reject(((_a = e_3.data) === null || _a === void 0 ? void 0 : _a.errors) ? e_3.data
4628
+ : {
4629
+ errors: [
4630
+ {
4631
+ message: e_3.statusText || e_3.toString(),
4632
+ extensions: {
4633
+ code: e_3.status || 0
4634
+ }
4635
+ }
4636
+ ]
4637
+ })];
4638
+ case 3: return [2 /*return*/];
4615
4639
  }
4616
4640
  });
4617
4641
  });
@@ -4636,20 +4660,39 @@ function getUrl(uri, gateway) {
4636
4660
  function getJSON(uri, options) {
4637
4661
  if (options === void 0) { options = {}; }
4638
4662
  return __awaiter(this, void 0, void 0, function () {
4639
- var url;
4663
+ var url, body;
4640
4664
  return __generator(this, function (_a) {
4641
- url = getUrl(uri, options.gateways);
4642
- return [2 /*return*/, fetch(url).then(function (res) { return res.json(); })];
4665
+ switch (_a.label) {
4666
+ case 0:
4667
+ url = getUrl(uri, options.gateways) || '';
4668
+ return [4 /*yield*/, ofetch(url, {
4669
+ timeout: options.timeout || 30e3,
4670
+ headers: __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers)
4671
+ })];
4672
+ case 1:
4673
+ body = _a.sent();
4674
+ return [2 /*return*/, typeof body === 'string' ? JSON.parse(body) : body];
4675
+ }
4643
4676
  });
4644
4677
  });
4645
4678
  }
4646
- function ipfsGet(gateway, ipfsHash, protocolType) {
4679
+ function ipfsGet(gateway, ipfsHash, protocolType, options) {
4647
4680
  if (protocolType === void 0) { protocolType = 'ipfs'; }
4681
+ if (options === void 0) { options = {}; }
4648
4682
  return __awaiter(this, void 0, void 0, function () {
4649
- var url;
4683
+ var url, body;
4650
4684
  return __generator(this, function (_a) {
4651
- url = "https://" + gateway + "/" + protocolType + "/" + ipfsHash;
4652
- return [2 /*return*/, fetch(url).then(function (res) { return res.json(); })];
4685
+ switch (_a.label) {
4686
+ case 0:
4687
+ url = "https://" + gateway + "/" + protocolType + "/" + ipfsHash;
4688
+ return [4 /*yield*/, ofetch(url, {
4689
+ timeout: options.timeout || 20e3,
4690
+ headers: __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, options.headers)
4691
+ })];
4692
+ case 1:
4693
+ body = _a.sent();
4694
+ return [2 /*return*/, typeof body === 'string' ? JSON.parse(body) : body];
4695
+ }
4653
4696
  });
4654
4697
  });
4655
4698
  }
@@ -4675,17 +4718,18 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
4675
4718
  if (snapshot === void 0) { snapshot = 'latest'; }
4676
4719
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
4677
4720
  if (options === void 0) { options = {}; }
4721
+ var _a;
4678
4722
  return __awaiter(this, void 0, void 0, function () {
4679
- var url, params, res, obj, e_3;
4680
- return __generator(this, function (_a) {
4681
- switch (_a.label) {
4723
+ var url, params, body, e_4;
4724
+ return __generator(this, function (_b) {
4725
+ switch (_b.label) {
4682
4726
  case 0:
4683
4727
  url = new URL(scoreApiUrl);
4684
- url.pathname = '/api/scores';
4728
+ url.pathname = options.pathname || '/api/scores';
4685
4729
  scoreApiUrl = url.toString();
4686
- _a.label = 1;
4730
+ _b.label = 1;
4687
4731
  case 1:
4688
- _a.trys.push([1, 4, , 5]);
4732
+ _b.trys.push([1, 3, , 4]);
4689
4733
  params = {
4690
4734
  space: space,
4691
4735
  network: network,
@@ -4693,47 +4737,43 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
4693
4737
  strategies: strategies,
4694
4738
  addresses: addresses
4695
4739
  };
4696
- return [4 /*yield*/, fetch(scoreApiUrl, {
4740
+ return [4 /*yield*/, ofetch(scoreApiUrl, {
4697
4741
  method: 'POST',
4698
4742
  headers: scoreApiHeaders,
4699
- body: JSON.stringify({ params: params })
4743
+ timeout: options.timeout || 60e3,
4744
+ body: { params: params }
4700
4745
  })];
4701
4746
  case 2:
4702
- res = _a.sent();
4703
- return [4 /*yield*/, res.json()];
4704
- case 3:
4705
- obj = _a.sent();
4706
- if (obj.error) {
4707
- return [2 /*return*/, Promise.reject(obj.error)];
4708
- }
4747
+ body = _b.sent();
4709
4748
  return [2 /*return*/, options.returnValue === 'all'
4710
- ? obj.result
4711
- : obj.result[options.returnValue || 'scores']];
4712
- case 4:
4713
- e_3 = _a.sent();
4714
- if (e_3.errno) {
4715
- return [2 /*return*/, Promise.reject({ code: e_3.errno, message: e_3.toString(), data: '' })];
4716
- }
4717
- return [2 /*return*/, Promise.reject(e_3)];
4718
- case 5: return [2 /*return*/];
4749
+ ? body.result
4750
+ : body.result[options.returnValue || 'scores']];
4751
+ case 3:
4752
+ e_4 = _b.sent();
4753
+ return [2 /*return*/, Promise.reject(((_a = e_4.data) === null || _a === void 0 ? void 0 : _a.error) || {
4754
+ code: e_4.status || 0,
4755
+ message: e_4.statusText || e_4.toString(),
4756
+ data: e_4.data || ''
4757
+ })];
4758
+ case 4: return [2 /*return*/];
4719
4759
  }
4720
4760
  });
4721
4761
  });
4722
4762
  }
4723
4763
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
4764
+ if (options === void 0) { options = {}; }
4765
+ var _a;
4724
4766
  return __awaiter(this, void 0, void 0, function () {
4725
- var init, res, json, e_4;
4726
- return __generator(this, function (_a) {
4727
- switch (_a.label) {
4767
+ var url, init, body, e_5;
4768
+ return __generator(this, function (_b) {
4769
+ switch (_b.label) {
4728
4770
  case 0:
4729
- if (!options)
4730
- options = {};
4731
- if (!options.url)
4732
- options.url = 'https://score.snapshot.org';
4771
+ url = options.url || 'https://score.snapshot.org';
4733
4772
  init = {
4734
4773
  method: 'POST',
4735
4774
  headers: scoreApiHeaders,
4736
- body: JSON.stringify({
4775
+ timeout: options.timeout || 60e3,
4776
+ body: {
4737
4777
  jsonrpc: '2.0',
4738
4778
  method: 'get_vp',
4739
4779
  params: {
@@ -4744,47 +4784,41 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4744
4784
  space: space,
4745
4785
  delegation: delegation
4746
4786
  }
4747
- })
4787
+ }
4748
4788
  };
4749
- _a.label = 1;
4789
+ _b.label = 1;
4750
4790
  case 1:
4751
- _a.trys.push([1, 4, , 5]);
4752
- return [4 /*yield*/, fetch(options.url, init)];
4791
+ _b.trys.push([1, 3, , 4]);
4792
+ return [4 /*yield*/, ofetch(url, init)];
4753
4793
  case 2:
4754
- res = _a.sent();
4755
- return [4 /*yield*/, res.json()];
4794
+ body = _b.sent();
4795
+ return [2 /*return*/, body.result];
4756
4796
  case 3:
4757
- json = _a.sent();
4758
- if (json.error)
4759
- return [2 /*return*/, Promise.reject(json.error)];
4760
- if (json.result)
4761
- return [2 /*return*/, json.result];
4762
- return [3 /*break*/, 5];
4763
- case 4:
4764
- e_4 = _a.sent();
4765
- if (e_4.errno) {
4766
- return [2 /*return*/, Promise.reject({ code: e_4.errno, message: e_4.toString(), data: '' })];
4767
- }
4768
- return [2 /*return*/, Promise.reject(e_4)];
4769
- case 5: return [2 /*return*/];
4797
+ e_5 = _b.sent();
4798
+ return [2 /*return*/, Promise.reject(((_a = e_5.data) === null || _a === void 0 ? void 0 : _a.error) || {
4799
+ code: e_5.status || 0,
4800
+ message: e_5.statusText || e_5.toString(),
4801
+ data: e_5.data || ''
4802
+ })];
4803
+ case 4: return [2 /*return*/];
4770
4804
  }
4771
4805
  });
4772
4806
  });
4773
4807
  }
4774
4808
  function validate(validation, author, space, network, snapshot, params, options) {
4809
+ if (options === void 0) { options = {}; }
4810
+ var _a;
4775
4811
  return __awaiter(this, void 0, void 0, function () {
4776
- var init, res, json, e_5;
4777
- return __generator(this, function (_a) {
4778
- switch (_a.label) {
4812
+ var url, init, body, e_6;
4813
+ return __generator(this, function (_b) {
4814
+ switch (_b.label) {
4779
4815
  case 0:
4780
- if (!options)
4781
- options = {};
4782
- if (!options.url)
4783
- options.url = 'https://score.snapshot.org';
4816
+ url = options.url || 'https://score.snapshot.org';
4784
4817
  init = {
4785
4818
  method: 'POST',
4786
4819
  headers: scoreApiHeaders,
4787
- body: JSON.stringify({
4820
+ timeout: options.timeout || 30e3,
4821
+ body: {
4788
4822
  jsonrpc: '2.0',
4789
4823
  method: 'validate',
4790
4824
  params: {
@@ -4795,27 +4829,23 @@ function validate(validation, author, space, network, snapshot, params, options)
4795
4829
  snapshot: snapshot,
4796
4830
  params: params
4797
4831
  }
4798
- })
4832
+ }
4799
4833
  };
4800
- _a.label = 1;
4834
+ _b.label = 1;
4801
4835
  case 1:
4802
- _a.trys.push([1, 4, , 5]);
4803
- return [4 /*yield*/, fetch(options.url, init)];
4836
+ _b.trys.push([1, 3, , 4]);
4837
+ return [4 /*yield*/, ofetch(url, init)];
4804
4838
  case 2:
4805
- res = _a.sent();
4806
- return [4 /*yield*/, res.json()];
4839
+ body = _b.sent();
4840
+ return [2 /*return*/, body.result];
4807
4841
  case 3:
4808
- json = _a.sent();
4809
- if (json.error)
4810
- return [2 /*return*/, Promise.reject(json.error)];
4811
- return [2 /*return*/, json.result];
4812
- case 4:
4813
- e_5 = _a.sent();
4814
- if (e_5.errno) {
4815
- return [2 /*return*/, Promise.reject({ code: e_5.errno, message: e_5.toString(), data: '' })];
4816
- }
4817
- return [2 /*return*/, Promise.reject(e_5)];
4818
- case 5: return [2 /*return*/];
4842
+ e_6 = _b.sent();
4843
+ return [2 /*return*/, Promise.reject(((_a = e_6.data) === null || _a === void 0 ? void 0 : _a.error) || {
4844
+ code: e_6.status || 0,
4845
+ message: e_6.statusText || e_6.toString(),
4846
+ data: e_6.data || ''
4847
+ })];
4848
+ case 4: return [2 /*return*/];
4819
4849
  }
4820
4850
  });
4821
4851
  });
@@ -4851,7 +4881,7 @@ function getSpaceUri(id, network, options) {
4851
4881
  if (network === void 0) { network = '1'; }
4852
4882
  if (options === void 0) { options = {}; }
4853
4883
  return __awaiter(this, void 0, void 0, function () {
4854
- var e_6;
4884
+ var e_7;
4855
4885
  return __generator(this, function (_a) {
4856
4886
  switch (_a.label) {
4857
4887
  case 0:
@@ -4859,8 +4889,8 @@ function getSpaceUri(id, network, options) {
4859
4889
  return [4 /*yield*/, getEnsTextRecord(id, 'snapshot', network, options)];
4860
4890
  case 1: return [2 /*return*/, _a.sent()];
4861
4891
  case 2:
4862
- e_6 = _a.sent();
4863
- console.log(e_6);
4892
+ e_7 = _a.sent();
4893
+ console.log(e_7);
4864
4894
  return [2 /*return*/, null];
4865
4895
  case 3: return [2 /*return*/];
4866
4896
  }