Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Check if network is mainnet or testnet depending on env #936

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.8.3",
"version": "0.9.0",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
5 changes: 4 additions & 1 deletion src/schemas/space.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
},
"network": {
"type": "string",
"snapshotNetwork": true,
"title": "network",
"minLength": 1,
"maxLength": 32
Expand Down Expand Up @@ -114,7 +115,8 @@
"network": {
"type": "string",
"maxLength": 12,
"title": "network"
"title": "network",
"snapshotNetwork": true
},
"params": {
"type": "object",
Expand Down Expand Up @@ -346,6 +348,7 @@
},
"network": {
"type": "string",
"snapshotNetwork": true,
"title": "Network",
"maxLength": 12
}
Expand Down
3 changes: 2 additions & 1 deletion src/schemas/zodiac.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"properties": {
"network": {
"title": "Network",
"type": "string"
"type": "string",
"snapshotNetwork": true
},
"multisend": {
"title": "Multisend contract address",
Expand Down
39 changes: 36 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ const scoreApiHeaders = {
'Content-Type': 'application/json'
};

const ajv = new Ajv({ allErrors: true, allowUnionTypes: true, $data: true });
const ajv = new Ajv({
allErrors: true,
allowUnionTypes: true,
$data: true,
passContext: true
});
// @ts-ignore
addFormats(ajv);

Expand Down Expand Up @@ -68,6 +73,28 @@ ajv.addFormat('ethValue', {
}
});

const networksIds = Object.keys(networks);
const mainnetNetworkIds = Object.keys(networks).filter(
(id) => !networks[id].testnet
);
const testnetNetworkIds = Object.keys(networks).filter(
(id) => networks[id].testnet
);

ajv.addKeyword({
keyword: 'snapshotNetwork',
validate: function (schema, data) {
// @ts-ignore
const snapshotEnv = this.snapshotEnv || 'default';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we default to mainnet ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We now support all networks 🙈 so i thought it is the default case

we can change it later because,
If we make mainnet default, then we need to change the sequencer code before merging this, else demo.snapshot.org will also accept only mainnet networks
Also, UI still displays all networks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So everything works like before, without the 3rd param ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep should work

if (snapshotEnv === 'mainnet') return mainnetNetworkIds.includes(data);
if (snapshotEnv === 'testnet') return testnetNetworkIds.includes(data);
return networksIds.includes(data);
},
error: {
message: 'must be a valid network used by snapshot'
}
});

// Custom URL format to allow empty string values
// https://github.com/snapshot-labs/snapshot.js/pull/541/files
ajv.addFormat('customUrl', {
Expand Down Expand Up @@ -338,9 +365,15 @@ export async function validate(
}
}

export function validateSchema(schema, data) {
export function validateSchema(
schema,
data,
options = {
snapshotEnv: 'default'
}
) {
const ajvValidate = ajv.compile(schema);
const valid = ajvValidate(data);
const valid = ajvValidate.call(options, data);
return valid ? valid : ajvValidate.errors;
}

Expand Down
2 changes: 1 addition & 1 deletion test/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe.each([
{ schemaType: 'alias', schema: schemas.alias, example: alias }
])(`Run validate for all schemas`, ({ schemaType, schema, example }) => {
test(`validating schema ${schemaType} should return true`, () => {
const isValid = validateSchema(schema, example);
const isValid = validateSchema(schema, example, { snapshotEnv: 'mainnet' });
expect(isValid).toBe(true);
});
});