Skip to content

Commit

Permalink
feat(permit): add permit info json schema (#294)
Browse files Browse the repository at this point in the history
* chore: move coingecko and donwloadImages to the `scripts` folder

* feat: add permitInfo json schema

* feat: add permit info schema unittests
  • Loading branch information
alfetopito authored Oct 23, 2023
1 parent 79b3f29 commit 1c8409a
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 5 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "1.0.0",
"description": "",
"type": "module",
"main": "src/coingecko.js",
"main": "src/scripts/coingecko.js",
"scripts": {
"build": "npm run public && npm run coingecko",
"coingecko": "node src/coingecko.js",
"downloadImages": "node --experimental-json-modules src/downloadImages",
"coingecko": "node src/scripts/coingecko.js",
"downloadImages": "node --experimental-json-modules src/scripts/downloadImages",
"public": "copyfiles src/public/*.json build/lists -f",
"workflowHelper": "python3 src/scripts/workflow_helper.py",
"validate": "ajv -s node_modules/@uniswap/token-lists/dist/tokenlist.schema.json -d src/public/CowSwap.json -c ajv-formats --errors text"
"validate": "ajv -s node_modules/@uniswap/token-lists/dist/tokenlist.schema.json -d src/public/CowSwap.json -c ajv-formats --errors text",
"test": "node --test"
},
"license": "(MIT OR Apache-2.0)",
"dependencies": {
Expand Down
42 changes: 42 additions & 0 deletions src/permitInfo/permitInfo.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$id": "https://cow.fi/schemas/token-list/v0.0.1.json",
"$schema": "http://json-schema.org/draft-07/schema",
"title": "PermitInfo",
"description": "Permit info for tokens",
"type": "object",
"patternProperties": {
"^0x[a-fA-F0-9]{40}$": {
"oneOf": [
{
"type": "object",
"title": "PermitInfo",
"description": "Individual permit info when a token is known to be permittable",
"properties": {
"version": {
"type": "string",
"description": "Optional version number, as a string",
"pattern": "^\\d+$"
},
"type": {
"type": "string",
"description": "Type of permit",
"enum": [
"eip-2612",
"dai-like"
]
}
},
"required": [
"type"
]
},
{
"type": "boolean",
"description": "When a token is known to not be permittable",
"const": false
}
]
}
},
"additionalProperties": false
}
161 changes: 161 additions & 0 deletions src/permitInfo/permitInfo.schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import {describe, it} from 'node:test'
import assert from 'node:assert'
import Ajv from 'ajv'

import schema from './permitInfo.schema.json' assert {type: 'json'}


describe('The permitInfo schema', () => {
it('should be valid', () => {
const ajv = new Ajv()

assert.strictEqual(ajv.validateSchema(schema), true)
})
})

describe('Valid PermitInfo data', () => {

it('should be valid with `false` value', () => {
const data = {
'0x0000000000000000000000000000000000000000': false
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(ajv.errors, null)
assert.strictEqual(result, true)
})

it('should be valid with `eip-2612` and no version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'eip-2612'
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(ajv.errors, null)
assert.strictEqual(result, true)
})

it('should be valid with `eip-2612` and version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'eip-2612',
version: '1',
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(ajv.errors, null)
assert.strictEqual(result, true)
})

it('should be valid with `dai-like` and no version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'dai-like'
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(ajv.errors, null)
assert.strictEqual(result, true)
})

it('should be valid with `dai-like` and version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'dai-like',
version: "2"
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(ajv.errors, null)
assert.strictEqual(result, true)
})
})

describe('Invalid PermitInfo data', () => {
it('should be invalid with number version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'eip-2612',
version: 1,
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(result, false)
assert.notEqual(ajv.errors, null)
})

it('should be invalid with non integer version', () => {
const data = {
'0x0000000000000000000000000000000000000000': {
type: 'eip-2612',
version: '1.1'
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(result, false)
assert.notEqual(ajv.errors, null)
})

it('should be invalid with non address key', () => {
const data = {
'not an address': false
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)


assert.strictEqual(result, false)
assert.notEqual(ajv.errors, null)
})

it('should be invalid with `true` value', () => {

const data = {
'0x0000000000000000000000000000000000000000': true
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(result, false)
assert.notEqual(ajv.errors, null)
})

it('should be invalid with non existent type', () => {

const data = {
'0x0000000000000000000000000000000000000000': {
type: 'non-existent'
}
}

const ajv = new Ajv()
const result = ajv.validate(schema, data)

assert.strictEqual(result, false)
assert.notEqual(ajv.errors, null)
})

})
File renamed without changes.
2 changes: 1 addition & 1 deletion src/downloadImages.js → src/scripts/downloadImages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cowSwapList from './public/CowSwap.json' assert { type: "json" }
import cowSwapList from '../public/CowSwap.json' assert { type: "json" }
import fetch from 'node-fetch';
import {createWriteStream} from 'fs'
import path, {dirname} from 'path'
Expand Down

0 comments on commit 1c8409a

Please sign in to comment.