@snapshot-labs/snapshot.js 0.3.9 → 0.3.13

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
@@ -1,6 +1,8 @@
1
+ import Client712 from './sign';
1
2
  import Client from './client';
2
3
  declare const _default: {
3
4
  Client: typeof Client;
5
+ Client712: typeof Client712;
4
6
  schemas: {
5
7
  space: {
6
8
  title: string;
@@ -311,6 +313,7 @@ declare const _default: {
311
313
  validations: {
312
314
  basic: typeof import("./validations/basic").default;
313
315
  aave: typeof import("./validations/aave").default;
316
+ nouns: typeof import("./validations/nouns").default;
314
317
  };
315
318
  getHash: typeof import("./sign/utils").getHash;
316
319
  verify: typeof import("./sign/utils").verify;
@@ -1,6 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var fetch = require('cross-fetch');
4
+ var providers$1 = require('@ethersproject/providers');
5
+ var wallet = require('@ethersproject/wallet');
4
6
  var bytes = require('@ethersproject/bytes');
5
7
  var abi = require('@ethersproject/abi');
6
8
  var contracts = require('@ethersproject/contracts');
@@ -9,8 +11,6 @@ var jsonToGraphqlQuery = require('json-to-graphql-query');
9
11
  var Ajv = require('ajv');
10
12
  var addFormats = require('ajv-formats');
11
13
  var set = require('lodash.set');
12
- var providers$1 = require('@ethersproject/providers');
13
- var wallet = require('@ethersproject/wallet');
14
14
 
15
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
16
 
@@ -106,6 +106,314 @@ function __spread() {
106
106
  return ar;
107
107
  }
108
108
 
109
+ var spaceTypes = {
110
+ Space: [
111
+ { name: 'from', type: 'address' },
112
+ { name: 'space', type: 'string' },
113
+ { name: 'timestamp', type: 'uint64' },
114
+ { name: 'settings', type: 'string' }
115
+ ]
116
+ };
117
+ var proposalTypes = {
118
+ Proposal: [
119
+ { name: 'from', type: 'address' },
120
+ { name: 'space', type: 'string' },
121
+ { name: 'timestamp', type: 'uint64' },
122
+ { name: 'type', type: 'string' },
123
+ { name: 'title', type: 'string' },
124
+ { name: 'body', type: 'string' },
125
+ { name: 'choices', type: 'string[]' },
126
+ { name: 'start', type: 'uint64' },
127
+ { name: 'end', type: 'uint64' },
128
+ { name: 'snapshot', type: 'uint64' },
129
+ { name: 'network', type: 'string' },
130
+ { name: 'strategies', type: 'string' },
131
+ { name: 'plugins', type: 'string' },
132
+ { name: 'metadata', type: 'string' }
133
+ ]
134
+ };
135
+ var cancelProposalTypes = {
136
+ CancelProposal: [
137
+ { name: 'from', type: 'address' },
138
+ { name: 'space', type: 'string' },
139
+ { name: 'timestamp', type: 'uint64' },
140
+ { name: 'proposal', type: 'string' }
141
+ ]
142
+ };
143
+ var cancelProposal2Types = {
144
+ CancelProposal: [
145
+ { name: 'from', type: 'address' },
146
+ { name: 'space', type: 'string' },
147
+ { name: 'timestamp', type: 'uint64' },
148
+ { name: 'proposal', type: 'bytes32' }
149
+ ]
150
+ };
151
+ var voteTypes = {
152
+ Vote: [
153
+ { name: 'from', type: 'address' },
154
+ { name: 'space', type: 'string' },
155
+ { name: 'timestamp', type: 'uint64' },
156
+ { name: 'proposal', type: 'string' },
157
+ { name: 'choice', type: 'uint32' },
158
+ { name: 'metadata', type: 'string' }
159
+ ]
160
+ };
161
+ var voteArrayTypes = {
162
+ Vote: [
163
+ { name: 'from', type: 'address' },
164
+ { name: 'space', type: 'string' },
165
+ { name: 'timestamp', type: 'uint64' },
166
+ { name: 'proposal', type: 'string' },
167
+ { name: 'choice', type: 'uint32[]' },
168
+ { name: 'metadata', type: 'string' }
169
+ ]
170
+ };
171
+ var voteStringTypes = {
172
+ Vote: [
173
+ { name: 'from', type: 'address' },
174
+ { name: 'space', type: 'string' },
175
+ { name: 'timestamp', type: 'uint64' },
176
+ { name: 'proposal', type: 'string' },
177
+ { name: 'choice', type: 'string' },
178
+ { name: 'metadata', type: 'string' }
179
+ ]
180
+ };
181
+ var vote2Types = {
182
+ Vote: [
183
+ { name: 'from', type: 'address' },
184
+ { name: 'space', type: 'string' },
185
+ { name: 'timestamp', type: 'uint64' },
186
+ { name: 'proposal', type: 'bytes32' },
187
+ { name: 'choice', type: 'uint32' },
188
+ { name: 'metadata', type: 'string' }
189
+ ]
190
+ };
191
+ var voteArray2Types = {
192
+ Vote: [
193
+ { name: 'from', type: 'address' },
194
+ { name: 'space', type: 'string' },
195
+ { name: 'timestamp', type: 'uint64' },
196
+ { name: 'proposal', type: 'bytes32' },
197
+ { name: 'choice', type: 'uint32[]' },
198
+ { name: 'metadata', type: 'string' }
199
+ ]
200
+ };
201
+ var voteString2Types = {
202
+ Vote: [
203
+ { name: 'from', type: 'address' },
204
+ { name: 'space', type: 'string' },
205
+ { name: 'timestamp', type: 'uint64' },
206
+ { name: 'proposal', type: 'bytes32' },
207
+ { name: 'choice', type: 'string' },
208
+ { name: 'metadata', type: 'string' }
209
+ ]
210
+ };
211
+ var followTypes = {
212
+ Follow: [
213
+ { name: 'from', type: 'address' },
214
+ { name: 'space', type: 'string' }
215
+ ]
216
+ };
217
+ var unfollowTypes = {
218
+ Unfollow: [
219
+ { name: 'from', type: 'address' },
220
+ { name: 'space', type: 'string' }
221
+ ]
222
+ };
223
+ var subscribeTypes = {
224
+ Subscribe: [
225
+ { name: 'from', type: 'address' },
226
+ { name: 'space', type: 'string' }
227
+ ]
228
+ };
229
+ var unsubscribeTypes = {
230
+ Unsubscribe: [
231
+ { name: 'from', type: 'address' },
232
+ { name: 'space', type: 'string' }
233
+ ]
234
+ };
235
+ var aliasTypes = {
236
+ Alias: [
237
+ { name: 'from', type: 'address' },
238
+ { name: 'alias', type: 'address' }
239
+ ]
240
+ };
241
+
242
+ var hubs = [
243
+ "https://hub.snapshot.org",
244
+ "https://testnet.snapshot.org"
245
+ ];
246
+
247
+ var NAME = 'snapshot';
248
+ var VERSION = '0.1.4';
249
+ var domain = {
250
+ name: NAME,
251
+ version: VERSION
252
+ // chainId: 1
253
+ };
254
+ var Client = /** @class */ (function () {
255
+ function Client(address) {
256
+ if (address === void 0) { address = hubs[0]; }
257
+ this.address = address;
258
+ }
259
+ Client.prototype.sign = function (web3, address, message, types) {
260
+ return __awaiter(this, void 0, void 0, function () {
261
+ var signer, data, sig;
262
+ return __generator(this, function (_a) {
263
+ switch (_a.label) {
264
+ case 0:
265
+ if (web3 instanceof wallet.Wallet)
266
+ signer = web3;
267
+ if (web3 instanceof providers$1.Web3Provider)
268
+ signer = web3.getSigner();
269
+ if (!message.from)
270
+ message.from = address;
271
+ if (!message.timestamp)
272
+ message.timestamp = parseInt((Date.now() / 1e3).toFixed());
273
+ data = { domain: domain, types: types, message: message };
274
+ return [4 /*yield*/, signer._signTypedData(domain, data.types, message)];
275
+ case 1:
276
+ sig = _a.sent();
277
+ console.log('Sign', { address: address, sig: sig, data: data });
278
+ return [4 /*yield*/, this.send({ address: address, sig: sig, data: data })];
279
+ case 2: return [2 /*return*/, _a.sent()];
280
+ }
281
+ });
282
+ });
283
+ };
284
+ Client.prototype.send = function (envelop) {
285
+ return __awaiter(this, void 0, void 0, function () {
286
+ var url, init;
287
+ return __generator(this, function (_a) {
288
+ url = this.address + "/api/msg";
289
+ init = {
290
+ method: 'POST',
291
+ headers: {
292
+ Accept: 'application/json',
293
+ 'Content-Type': 'application/json'
294
+ },
295
+ body: JSON.stringify(envelop)
296
+ };
297
+ return [2 /*return*/, new Promise(function (resolve, reject) {
298
+ fetch__default['default'](url, init)
299
+ .then(function (res) {
300
+ if (res.ok)
301
+ return resolve(res.json());
302
+ throw res;
303
+ })
304
+ .catch(function (e) { return e.json().then(function (json) { return reject(json); }); });
305
+ })];
306
+ });
307
+ });
308
+ };
309
+ Client.prototype.space = function (web3, address, message) {
310
+ return __awaiter(this, void 0, void 0, function () {
311
+ return __generator(this, function (_a) {
312
+ switch (_a.label) {
313
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, spaceTypes)];
314
+ case 1: return [2 /*return*/, _a.sent()];
315
+ }
316
+ });
317
+ });
318
+ };
319
+ Client.prototype.proposal = function (web3, address, message) {
320
+ return __awaiter(this, void 0, void 0, function () {
321
+ return __generator(this, function (_a) {
322
+ switch (_a.label) {
323
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, proposalTypes)];
324
+ case 1: return [2 /*return*/, _a.sent()];
325
+ }
326
+ });
327
+ });
328
+ };
329
+ Client.prototype.cancelProposal = function (web3, address, message) {
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ var type2;
332
+ return __generator(this, function (_a) {
333
+ switch (_a.label) {
334
+ case 0:
335
+ type2 = message.proposal.startsWith('0x');
336
+ return [4 /*yield*/, this.sign(web3, address, message, type2 ? cancelProposal2Types : cancelProposalTypes)];
337
+ case 1: return [2 /*return*/, _a.sent()];
338
+ }
339
+ });
340
+ });
341
+ };
342
+ Client.prototype.vote = function (web3, address, message) {
343
+ return __awaiter(this, void 0, void 0, function () {
344
+ var type2, type;
345
+ return __generator(this, function (_a) {
346
+ switch (_a.label) {
347
+ case 0:
348
+ type2 = message.proposal.startsWith('0x');
349
+ type = type2 ? vote2Types : voteTypes;
350
+ if (['approval', 'ranked-choice'].includes(message.type))
351
+ type = type2 ? voteArray2Types : voteArrayTypes;
352
+ if (['quadratic', 'weighted'].includes(message.type)) {
353
+ type = type2 ? voteString2Types : voteStringTypes;
354
+ message.choice = JSON.stringify(message.choice);
355
+ }
356
+ // @ts-ignore
357
+ delete message.type;
358
+ return [4 /*yield*/, this.sign(web3, address, message, type)];
359
+ case 1: return [2 /*return*/, _a.sent()];
360
+ }
361
+ });
362
+ });
363
+ };
364
+ Client.prototype.follow = function (web3, address, message) {
365
+ return __awaiter(this, void 0, void 0, function () {
366
+ return __generator(this, function (_a) {
367
+ switch (_a.label) {
368
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, followTypes)];
369
+ case 1: return [2 /*return*/, _a.sent()];
370
+ }
371
+ });
372
+ });
373
+ };
374
+ Client.prototype.unfollow = function (web3, address, message) {
375
+ return __awaiter(this, void 0, void 0, function () {
376
+ return __generator(this, function (_a) {
377
+ switch (_a.label) {
378
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, unfollowTypes)];
379
+ case 1: return [2 /*return*/, _a.sent()];
380
+ }
381
+ });
382
+ });
383
+ };
384
+ Client.prototype.subscribe = function (web3, address, message) {
385
+ return __awaiter(this, void 0, void 0, function () {
386
+ return __generator(this, function (_a) {
387
+ switch (_a.label) {
388
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, subscribeTypes)];
389
+ case 1: return [2 /*return*/, _a.sent()];
390
+ }
391
+ });
392
+ });
393
+ };
394
+ Client.prototype.unsubscribe = function (web3, address, message) {
395
+ return __awaiter(this, void 0, void 0, function () {
396
+ return __generator(this, function (_a) {
397
+ switch (_a.label) {
398
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, unsubscribeTypes)];
399
+ case 1: return [2 /*return*/, _a.sent()];
400
+ }
401
+ });
402
+ });
403
+ };
404
+ Client.prototype.alias = function (web3, address, message) {
405
+ return __awaiter(this, void 0, void 0, function () {
406
+ return __generator(this, function (_a) {
407
+ switch (_a.label) {
408
+ case 0: return [4 /*yield*/, this.sign(web3, address, message, aliasTypes)];
409
+ case 1: return [2 /*return*/, _a.sent()];
410
+ }
411
+ });
412
+ });
413
+ };
414
+ return Client;
415
+ }());
416
+
109
417
  function signMessage(web3, msg, address) {
110
418
  return __awaiter(this, void 0, void 0, function () {
111
419
  return __generator(this, function (_a) {
@@ -138,14 +446,9 @@ function getBlockNumber(provider) {
138
446
  });
139
447
  }
140
448
 
141
- var hubs = [
142
- "https://hub.snapshot.org",
143
- "https://testnet.snapshot.org"
144
- ];
145
-
146
449
  var version = "0.1.3";
147
450
 
148
- var Client = /** @class */ (function () {
451
+ var Client$1 = /** @class */ (function () {
149
452
  function Client(address) {
150
453
  if (address === void 0) { address = hubs[0]; }
151
454
  this.address = address;
@@ -814,6 +1117,18 @@ var networks = {
814
1117
  "wss://eth-kovan.ws.alchemyapi.io/v2/QCsM2iU0bQ49eGDmZ7-Y--Wpu0lVWXSO"
815
1118
  ],
816
1119
  explorer: "https://kovan.etherscan.io"
1120
+ },
1121
+ "44": {
1122
+ key: "44",
1123
+ name: "Crab Network",
1124
+ shortName: "Crab",
1125
+ chainId: 44,
1126
+ network: "mainnet",
1127
+ multicall: "0x4617D470F847Ce166019d19a7944049ebB017400",
1128
+ rpc: [
1129
+ "https://crab-rpc.darwinia.network"
1130
+ ],
1131
+ explorer: "https://crab.subscan.io/"
817
1132
  },
818
1133
  "50": {
819
1134
  key: "50",
@@ -1417,9 +1732,56 @@ function validate$1(author, space, proposal, options) {
1417
1732
  });
1418
1733
  }
1419
1734
 
1735
+ /**
1736
+ * Nouns Space Validation proposal validation uses:
1737
+ *
1738
+ * The current validation implementation mutates the "strategies" field of the space
1739
+ * to be able to use proposition power instead of voting power for "nouns-rfp-power".
1740
+ *
1741
+ */
1742
+ function validate$2(author, space, proposal, options) {
1743
+ var _a, _b;
1744
+ return __awaiter(this, void 0, void 0, function () {
1745
+ var onlyMembers, minScore, members, strategies, nounsRFPStrategyIndex, scores, totalScore;
1746
+ return __generator(this, function (_c) {
1747
+ switch (_c.label) {
1748
+ case 0:
1749
+ onlyMembers = options.onlyMembers || ((_a = space.filters) === null || _a === void 0 ? void 0 : _a.onlyMembers);
1750
+ minScore = options.minScore || ((_b = space.filters) === null || _b === void 0 ? void 0 : _b.minScore);
1751
+ members = (space.members || []).map(function (address) { return address.toLowerCase(); });
1752
+ strategies = __spread(space.strategies);
1753
+ nounsRFPStrategyIndex = strategies.findIndex(function (_a) {
1754
+ var name = _a.name;
1755
+ return name === 'nouns-rfp-power';
1756
+ });
1757
+ // Use the proposition power instead of the voting power
1758
+ if (nounsRFPStrategyIndex >= 0) {
1759
+ strategies[nounsRFPStrategyIndex].params.powerType = 'proposition';
1760
+ }
1761
+ if (members.includes(author.toLowerCase()))
1762
+ return [2 /*return*/, true];
1763
+ if (onlyMembers)
1764
+ return [2 /*return*/, false];
1765
+ if (!minScore) return [3 /*break*/, 2];
1766
+ return [4 /*yield*/, getScores(space.id || space.key, strategies, space.network, [author])];
1767
+ case 1:
1768
+ scores = _c.sent();
1769
+ totalScore = scores
1770
+ .map(function (score) { return Object.values(score).reduce(function (a, b) { return a + b; }, 0); })
1771
+ .reduce(function (a, b) { return a + b; }, 0);
1772
+ if (totalScore < minScore)
1773
+ return [2 /*return*/, false];
1774
+ _c.label = 2;
1775
+ case 2: return [2 /*return*/, true];
1776
+ }
1777
+ });
1778
+ });
1779
+ }
1780
+
1420
1781
  var validations = {
1421
1782
  basic: validate,
1422
- aave: validate$1
1783
+ aave: validate$1,
1784
+ nouns: validate$2
1423
1785
  };
1424
1786
 
1425
1787
  function verifyDefault(address, sig, hash, provider) {
@@ -1594,6 +1956,8 @@ var ApprovalVoting = /** @class */ (function () {
1594
1956
  };
1595
1957
  ApprovalVoting.prototype.getChoiceString = function () {
1596
1958
  var _this = this;
1959
+ if (!this.selected)
1960
+ return '';
1597
1961
  return this.proposal.choices
1598
1962
  .filter(function (choice, i) { return _this.selected.includes(i + 1); })
1599
1963
  .join(', ');
@@ -2059,7 +2423,8 @@ var utils = {
2059
2423
  };
2060
2424
 
2061
2425
  var index = {
2062
- Client: Client,
2426
+ Client: Client$1,
2427
+ Client712: Client,
2063
2428
  schemas: schemas,
2064
2429
  utils: utils
2065
2430
  };