@snapshot-labs/snapshot.js 0.7.2 → 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
  };
@@ -1288,6 +1294,30 @@ var zodiac = {
1288
1294
  definitions: definitions$6
1289
1295
  };
1290
1296
 
1297
+ var $schema$7 = "http://json-schema.org/draft-07/schema#";
1298
+ var $ref$7 = "#/definitions/Alias";
1299
+ var definitions$7 = {
1300
+ Alias: {
1301
+ title: "Alias",
1302
+ type: "object",
1303
+ properties: {
1304
+ alias: {
1305
+ type: "string",
1306
+ format: "address"
1307
+ }
1308
+ },
1309
+ required: [
1310
+ "alias"
1311
+ ],
1312
+ additionalProperties: false
1313
+ }
1314
+ };
1315
+ var alias = {
1316
+ $schema: $schema$7,
1317
+ $ref: $ref$7,
1318
+ definitions: definitions$7
1319
+ };
1320
+
1291
1321
  var schemas = {
1292
1322
  space: space.definitions.Space,
1293
1323
  proposal: proposal.definitions.Proposal,
@@ -1295,7 +1325,8 @@ var schemas = {
1295
1325
  vote: vote.definitions.Vote,
1296
1326
  profile: profile.definitions.Profile,
1297
1327
  statement: statement.definitions.Statement,
1298
- zodiac: zodiac.definitions.Zodiac
1328
+ zodiac: zodiac.definitions.Zodiac,
1329
+ alias: alias.definitions.Alias
1299
1330
  };
1300
1331
 
1301
1332
  var Multicaller = /** @class */ (function () {
@@ -4561,32 +4592,50 @@ function multicall(network, provider, abi, calls, options) {
4561
4592
  });
4562
4593
  }
4563
4594
  function subgraphRequest(url, query, options) {
4564
- if (options === void 0) { options = {}; }
4595
+ var _a;
4565
4596
  return __awaiter(this, void 0, void 0, function () {
4566
- var res, responseData, data;
4567
- return __generator(this, function (_a) {
4568
- switch (_a.label) {
4569
- 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 = {
4570
4603
  method: 'POST',
4571
4604
  headers: __assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, options === null || options === void 0 ? void 0 : options.headers),
4572
- body: JSON.stringify({ query: jsonToGraphQLQuery({ query: query }) })
4573
- })];
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)];
4574
4609
  case 1:
4575
- res = _a.sent();
4576
- return [4 /*yield*/, res.text()];
4577
- case 2:
4578
- responseData = _a.sent();
4579
- try {
4580
- responseData = JSON.parse(responseData);
4581
- }
4582
- catch (e) {
4583
- throw new Error("Errors found in subgraphRequest: URL: " + url + ", Status: " + res.status + ", Response: " + responseData.substring(0, 400));
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
+ })];
4584
4620
  }
4585
- if (responseData.errors) {
4586
- 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)];
4587
4623
  }
4588
- data = responseData.data;
4589
- 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*/];
4590
4639
  }
4591
4640
  });
4592
4641
  });
@@ -4611,20 +4660,39 @@ function getUrl(uri, gateway) {
4611
4660
  function getJSON(uri, options) {
4612
4661
  if (options === void 0) { options = {}; }
4613
4662
  return __awaiter(this, void 0, void 0, function () {
4614
- var url;
4663
+ var url, body;
4615
4664
  return __generator(this, function (_a) {
4616
- url = getUrl(uri, options.gateways);
4617
- 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
+ }
4618
4676
  });
4619
4677
  });
4620
4678
  }
4621
- function ipfsGet(gateway, ipfsHash, protocolType) {
4679
+ function ipfsGet(gateway, ipfsHash, protocolType, options) {
4622
4680
  if (protocolType === void 0) { protocolType = 'ipfs'; }
4681
+ if (options === void 0) { options = {}; }
4623
4682
  return __awaiter(this, void 0, void 0, function () {
4624
- var url;
4683
+ var url, body;
4625
4684
  return __generator(this, function (_a) {
4626
- url = "https://" + gateway + "/" + protocolType + "/" + ipfsHash;
4627
- 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
+ }
4628
4696
  });
4629
4697
  });
4630
4698
  }
@@ -4650,17 +4718,18 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
4650
4718
  if (snapshot === void 0) { snapshot = 'latest'; }
4651
4719
  if (scoreApiUrl === void 0) { scoreApiUrl = 'https://score.snapshot.org'; }
4652
4720
  if (options === void 0) { options = {}; }
4721
+ var _a;
4653
4722
  return __awaiter(this, void 0, void 0, function () {
4654
- var url, params, res, obj, e_3;
4655
- return __generator(this, function (_a) {
4656
- switch (_a.label) {
4723
+ var url, params, body, e_4;
4724
+ return __generator(this, function (_b) {
4725
+ switch (_b.label) {
4657
4726
  case 0:
4658
4727
  url = new URL(scoreApiUrl);
4659
- url.pathname = '/api/scores';
4728
+ url.pathname = options.pathname || '/api/scores';
4660
4729
  scoreApiUrl = url.toString();
4661
- _a.label = 1;
4730
+ _b.label = 1;
4662
4731
  case 1:
4663
- _a.trys.push([1, 4, , 5]);
4732
+ _b.trys.push([1, 3, , 4]);
4664
4733
  params = {
4665
4734
  space: space,
4666
4735
  network: network,
@@ -4668,47 +4737,43 @@ function getScores(space, strategies, network, addresses, snapshot, scoreApiUrl,
4668
4737
  strategies: strategies,
4669
4738
  addresses: addresses
4670
4739
  };
4671
- return [4 /*yield*/, fetch(scoreApiUrl, {
4740
+ return [4 /*yield*/, ofetch(scoreApiUrl, {
4672
4741
  method: 'POST',
4673
4742
  headers: scoreApiHeaders,
4674
- body: JSON.stringify({ params: params })
4743
+ timeout: options.timeout || 60e3,
4744
+ body: { params: params }
4675
4745
  })];
4676
4746
  case 2:
4677
- res = _a.sent();
4678
- return [4 /*yield*/, res.json()];
4679
- case 3:
4680
- obj = _a.sent();
4681
- if (obj.error) {
4682
- return [2 /*return*/, Promise.reject(obj.error)];
4683
- }
4747
+ body = _b.sent();
4684
4748
  return [2 /*return*/, options.returnValue === 'all'
4685
- ? obj.result
4686
- : obj.result[options.returnValue || 'scores']];
4687
- case 4:
4688
- e_3 = _a.sent();
4689
- if (e_3.errno) {
4690
- return [2 /*return*/, Promise.reject({ code: e_3.errno, message: e_3.toString(), data: '' })];
4691
- }
4692
- return [2 /*return*/, Promise.reject(e_3)];
4693
- 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*/];
4694
4759
  }
4695
4760
  });
4696
4761
  });
4697
4762
  }
4698
4763
  function getVp(address, network, strategies, snapshot, space, delegation, options) {
4764
+ if (options === void 0) { options = {}; }
4765
+ var _a;
4699
4766
  return __awaiter(this, void 0, void 0, function () {
4700
- var init, res, json, e_4;
4701
- return __generator(this, function (_a) {
4702
- switch (_a.label) {
4767
+ var url, init, body, e_5;
4768
+ return __generator(this, function (_b) {
4769
+ switch (_b.label) {
4703
4770
  case 0:
4704
- if (!options)
4705
- options = {};
4706
- if (!options.url)
4707
- options.url = 'https://score.snapshot.org';
4771
+ url = options.url || 'https://score.snapshot.org';
4708
4772
  init = {
4709
4773
  method: 'POST',
4710
4774
  headers: scoreApiHeaders,
4711
- body: JSON.stringify({
4775
+ timeout: options.timeout || 60e3,
4776
+ body: {
4712
4777
  jsonrpc: '2.0',
4713
4778
  method: 'get_vp',
4714
4779
  params: {
@@ -4719,47 +4784,41 @@ function getVp(address, network, strategies, snapshot, space, delegation, option
4719
4784
  space: space,
4720
4785
  delegation: delegation
4721
4786
  }
4722
- })
4787
+ }
4723
4788
  };
4724
- _a.label = 1;
4789
+ _b.label = 1;
4725
4790
  case 1:
4726
- _a.trys.push([1, 4, , 5]);
4727
- return [4 /*yield*/, fetch(options.url, init)];
4791
+ _b.trys.push([1, 3, , 4]);
4792
+ return [4 /*yield*/, ofetch(url, init)];
4728
4793
  case 2:
4729
- res = _a.sent();
4730
- return [4 /*yield*/, res.json()];
4794
+ body = _b.sent();
4795
+ return [2 /*return*/, body.result];
4731
4796
  case 3:
4732
- json = _a.sent();
4733
- if (json.error)
4734
- return [2 /*return*/, Promise.reject(json.error)];
4735
- if (json.result)
4736
- return [2 /*return*/, json.result];
4737
- return [3 /*break*/, 5];
4738
- case 4:
4739
- e_4 = _a.sent();
4740
- if (e_4.errno) {
4741
- return [2 /*return*/, Promise.reject({ code: e_4.errno, message: e_4.toString(), data: '' })];
4742
- }
4743
- return [2 /*return*/, Promise.reject(e_4)];
4744
- 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*/];
4745
4804
  }
4746
4805
  });
4747
4806
  });
4748
4807
  }
4749
4808
  function validate(validation, author, space, network, snapshot, params, options) {
4809
+ if (options === void 0) { options = {}; }
4810
+ var _a;
4750
4811
  return __awaiter(this, void 0, void 0, function () {
4751
- var init, res, json, e_5;
4752
- return __generator(this, function (_a) {
4753
- switch (_a.label) {
4812
+ var url, init, body, e_6;
4813
+ return __generator(this, function (_b) {
4814
+ switch (_b.label) {
4754
4815
  case 0:
4755
- if (!options)
4756
- options = {};
4757
- if (!options.url)
4758
- options.url = 'https://score.snapshot.org';
4816
+ url = options.url || 'https://score.snapshot.org';
4759
4817
  init = {
4760
4818
  method: 'POST',
4761
4819
  headers: scoreApiHeaders,
4762
- body: JSON.stringify({
4820
+ timeout: options.timeout || 30e3,
4821
+ body: {
4763
4822
  jsonrpc: '2.0',
4764
4823
  method: 'validate',
4765
4824
  params: {
@@ -4770,27 +4829,23 @@ function validate(validation, author, space, network, snapshot, params, options)
4770
4829
  snapshot: snapshot,
4771
4830
  params: params
4772
4831
  }
4773
- })
4832
+ }
4774
4833
  };
4775
- _a.label = 1;
4834
+ _b.label = 1;
4776
4835
  case 1:
4777
- _a.trys.push([1, 4, , 5]);
4778
- return [4 /*yield*/, fetch(options.url, init)];
4836
+ _b.trys.push([1, 3, , 4]);
4837
+ return [4 /*yield*/, ofetch(url, init)];
4779
4838
  case 2:
4780
- res = _a.sent();
4781
- return [4 /*yield*/, res.json()];
4839
+ body = _b.sent();
4840
+ return [2 /*return*/, body.result];
4782
4841
  case 3:
4783
- json = _a.sent();
4784
- if (json.error)
4785
- return [2 /*return*/, Promise.reject(json.error)];
4786
- return [2 /*return*/, json.result];
4787
- case 4:
4788
- e_5 = _a.sent();
4789
- if (e_5.errno) {
4790
- return [2 /*return*/, Promise.reject({ code: e_5.errno, message: e_5.toString(), data: '' })];
4791
- }
4792
- return [2 /*return*/, Promise.reject(e_5)];
4793
- 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*/];
4794
4849
  }
4795
4850
  });
4796
4851
  });
@@ -4826,7 +4881,7 @@ function getSpaceUri(id, network, options) {
4826
4881
  if (network === void 0) { network = '1'; }
4827
4882
  if (options === void 0) { options = {}; }
4828
4883
  return __awaiter(this, void 0, void 0, function () {
4829
- var e_6;
4884
+ var e_7;
4830
4885
  return __generator(this, function (_a) {
4831
4886
  switch (_a.label) {
4832
4887
  case 0:
@@ -4834,8 +4889,8 @@ function getSpaceUri(id, network, options) {
4834
4889
  return [4 /*yield*/, getEnsTextRecord(id, 'snapshot', network, options)];
4835
4890
  case 1: return [2 /*return*/, _a.sent()];
4836
4891
  case 2:
4837
- e_6 = _a.sent();
4838
- console.log(e_6);
4892
+ e_7 = _a.sent();
4893
+ console.log(e_7);
4839
4894
  return [2 /*return*/, null];
4840
4895
  case 3: return [2 /*return*/];
4841
4896
  }