@snapshot-labs/snapshot.js 0.8.3 → 0.9.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.
package/dist/utils.d.ts CHANGED
@@ -31,7 +31,9 @@ export declare function sendTransaction(web3: any, contractAddress: string, abi:
31
31
  export declare function getScores(space: string, strategies: Strategy[], network: string, addresses: string[], snapshot?: number | string, scoreApiUrl?: string, options?: any): Promise<any>;
32
32
  export declare function getVp(address: string, network: string, strategies: Strategy[], snapshot: number | 'latest', space: string, delegation: boolean, options?: Options): Promise<any>;
33
33
  export declare function validate(validation: string, author: string, space: string, network: string, snapshot: number | 'latest', params: any, options: any): Promise<any>;
34
- export declare function validateSchema(schema: any, data: any): true | import("ajv").ErrorObject<string, Record<string, any>, unknown>[] | null | undefined;
34
+ export declare function validateSchema(schema: any, data: any, options?: {
35
+ snapshotEnv: string;
36
+ }): true | import("ajv").ErrorObject<string, Record<string, any>, unknown>[] | null | undefined;
35
37
  export declare function getEnsTextRecord(ens: string, record: string, network?: string, options?: any): Promise<any>;
36
38
  export declare function getSpaceUri(id: string, network?: string, options?: any): Promise<string | null>;
37
39
  export declare function getEnsOwner(ens: string, network?: string, options?: any): Promise<string | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snapshot-labs/snapshot.js",
3
- "version": "0.8.3",
3
+ "version": "0.9.0",
4
4
  "repository": "snapshot-labs/snapshot.js",
5
5
  "license": "MIT",
6
6
  "main": "dist/snapshot.cjs.js",
@@ -79,6 +79,7 @@
79
79
  },
80
80
  "network": {
81
81
  "type": "string",
82
+ "snapshotNetwork": true,
82
83
  "title": "network",
83
84
  "minLength": 1,
84
85
  "maxLength": 32
@@ -114,7 +115,8 @@
114
115
  "network": {
115
116
  "type": "string",
116
117
  "maxLength": 12,
117
- "title": "network"
118
+ "title": "network",
119
+ "snapshotNetwork": true
118
120
  },
119
121
  "params": {
120
122
  "type": "object",
@@ -346,6 +348,7 @@
346
348
  },
347
349
  "network": {
348
350
  "type": "string",
351
+ "snapshotNetwork": true,
349
352
  "title": "Network",
350
353
  "maxLength": 12
351
354
  }
@@ -15,7 +15,8 @@
15
15
  "properties": {
16
16
  "network": {
17
17
  "title": "Network",
18
- "type": "string"
18
+ "type": "string",
19
+ "snapshotNetwork": true
19
20
  },
20
21
  "multisend": {
21
22
  "title": "Multisend contract address",
package/src/utils.ts CHANGED
@@ -37,7 +37,12 @@ const scoreApiHeaders = {
37
37
  'Content-Type': 'application/json'
38
38
  };
39
39
 
40
- const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
40
+ const ajv = new Ajv({
41
+ allErrors: true,
42
+ allowUnionTypes: true,
43
+ $data: true,
44
+ passContext: true
45
+ });
41
46
  // @ts-ignore
42
47
  addFormats(ajv);
43
48
 
@@ -68,6 +73,28 @@ ajv.addFormat('ethValue', {
68
73
  }
69
74
  });
70
75
 
76
+ const networksIds = Object.keys(networks);
77
+ const mainnetNetworkIds = Object.keys(networks).filter(
78
+ (id) => !networks[id].testnet
79
+ );
80
+ const testnetNetworkIds = Object.keys(networks).filter(
81
+ (id) => networks[id].testnet
82
+ );
83
+
84
+ ajv.addKeyword({
85
+ keyword: 'snapshotNetwork',
86
+ validate: function (schema, data) {
87
+ // @ts-ignore
88
+ const snapshotEnv = this.snapshotEnv || 'default';
89
+ if (snapshotEnv === 'mainnet') return mainnetNetworkIds.includes(data);
90
+ if (snapshotEnv === 'testnet') return testnetNetworkIds.includes(data);
91
+ return networksIds.includes(data);
92
+ },
93
+ error: {
94
+ message: 'must be a valid network used by snapshot'
95
+ }
96
+ });
97
+
71
98
  // Custom URL format to allow empty string values
72
99
  // https://github.com/snapshot-labs/snapshot.js/pull/541/files
73
100
  ajv.addFormat('customUrl', {
@@ -338,9 +365,15 @@ export async function validate(
338
365
  }
339
366
  }
340
367
 
341
- export function validateSchema(schema, data) {
368
+ export function validateSchema(
369
+ schema,
370
+ data,
371
+ options = {
372
+ snapshotEnv: 'default'
373
+ }
374
+ ) {
342
375
  const ajvValidate = ajv.compile(schema);
343
- const valid = ajvValidate(data);
376
+ const valid = ajvValidate.call(options, data);
344
377
  return valid ? valid : ajvValidate.errors;
345
378
  }
346
379