Skip to content

Commit

Permalink
Add JSON schema validation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddav authored Jun 2, 2022
1 parent 9b5eded commit 449958d
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 59 deletions.
Binary file removed .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions .github/workflows/format_token_list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check format token list

on: pull_request

defaults:
run:
shell: bash

jobs:
check-token-list:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "14"
- name: Install dependencies
shell: bash
run: yarn install --frozen-lockfile --verbose
- name: Validate JSON file
run: yarn validate
- name: Discord notification (failure)
if: failure()
uses: Ilshidur/action-discord@master
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
with:
args: "Error token list format"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
.DS_Store
.DS_Store
dist
build
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
"version": "1.0.0",
"main": "src/index.ts",
"repository": "https://github.com/AngleProtocol/angle-token-list.git",
"author": "greedythib <[email protected]>",
"author": "greedythib <[email protected]>",
"license": "MIT",
"dependencies": {
"@angleprotocol/sdk": "^1.0.63",
"ts-node": "^10.7.0"
"joi": "^17.6.0"
},
"scripts": {
"validate": "ts-node src/index.ts"
},
"devDependencies": {
"@types/node": "^17.0.38",
"ts-node": "^10.7.0",
"typescript": "^4.7.2"
}
}
Binary file removed src/.DS_Store
Binary file not shown.
Binary file removed src/assets/.DS_Store
Binary file not shown.
47 changes: 0 additions & 47 deletions src/chains/mainnet.ts

This file was deleted.

18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ChainId } from "@angleprotocol/sdk";
import { TokenInfo, TokenInfoListType, TokenList } from "./types";
import { token_list as mainnet_token_list } from "./chains/mainnet";
import { TokenList, joiSchema } from "./types";
import erc20s from "../ERC20_LIST.json";
import { inspect } from "util";

export const TOKEN_LIST: TokenList = {
[ChainId.MAINNET]: mainnet_token_list,
};
export const TOKEN_LIST: TokenList[] = erc20s;

const result = joiSchema.validate(TOKEN_LIST);
if (result.error) {
console.log(inspect(result.error, { showHidden: false, depth: null, colors: true }));
throw new Error("wrong JSON format");
} else {
console.log("all good");
}
22 changes: 19 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChainId } from "@angleprotocol/sdk";
import Joi from "joi";

export interface TokenInfo {
readonly name: string;
readonly address: string;
readonly decimals: number;
readonly symbol: string;
readonly isSanToken?: boolean;
Expand All @@ -13,10 +13,26 @@ export interface TokenInfo {
readonly tags?: string[];
}

const tokenInfo = Joi.object().keys({
name: Joi.string(),
decimals: Joi.number(),
symbol: Joi.string(),
useInSwap: Joi.bool(),
isSanToken: Joi.bool(),
hasPermit: Joi.bool(),
permitVersion: Joi.string(),
logoURI: Joi.string()
});

export const joiSchema = Joi.array().items({
mainnet: Joi.object().pattern(/^/, tokenInfo),
rinkeby: Joi.object().pattern(/^/, tokenInfo)
});

export interface TokenInfoListType {
[symbol: string]: TokenInfo;
[address: string]: TokenInfo;
}

export interface TokenList {
[chainId: number]: TokenInfoListType;
[chainName: number]: TokenInfoListType;
}
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "build"
},
"include": ["src"]
}
50 changes: 50 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,35 @@
"@ethersproject/properties" "^5.6.0"
"@ethersproject/strings" "^5.6.0"

"@hapi/hoek@^9.0.0":
version "9.3.0"
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.3.0.tgz#8368869dcb735be2e7f5cb7647de78e167a251fb"
integrity sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==

"@hapi/topo@^5.0.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012"
integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==
dependencies:
"@hapi/hoek" "^9.0.0"

"@sideway/address@^4.1.3":
version "4.1.4"
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0"
integrity sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==
dependencies:
"@hapi/hoek" "^9.0.0"

"@sideway/formula@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c"
integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg==

"@sideway/pinpoint@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df"
integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ==

"@tsconfig/node10@^1.0.7":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.8.tgz#c1e4e80d6f964fbecb3359c43bd48b40f7cadad9"
Expand Down Expand Up @@ -394,6 +423,11 @@
lodash "^4.17.15"
ts-essentials "^7.0.1"

"@types/node@^17.0.38":
version "17.0.38"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.38.tgz#f8bb07c371ccb1903f3752872c89f44006132947"
integrity sha512-5jY9RhV7c0Z4Jy09G+NIDTsCZ5G0L5n+Z+p+Y7t5VJHM30bgwzSjVtlcBxqAj+6L/swIlvtOSzr8rBk/aNyV2g==

"@types/prettier@^2.1.1":
version "2.4.4"
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.4.tgz#5d9b63132df54d8909fce1c3f8ca260fdd693e17"
Expand Down Expand Up @@ -743,6 +777,17 @@ inherits@2, inherits@^2.0.3, inherits@^2.0.4:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

joi@^17.6.0:
version "17.6.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-17.6.0.tgz#0bb54f2f006c09a96e75ce687957bd04290054b2"
integrity sha512-OX5dG6DTbcr/kbMFj0KGYxuew69HPcAE3K/sZpEV2nP6e/j/C0HV+HNiBPCASxdx5T7DMoa0s8UeHWMnb6n2zw==
dependencies:
"@hapi/hoek" "^9.0.0"
"@hapi/topo" "^5.0.0"
"@sideway/address" "^4.1.3"
"@sideway/formula" "^3.0.0"
"@sideway/pinpoint" "^2.0.0"

[email protected], js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
Expand Down Expand Up @@ -932,6 +977,11 @@ typechain@^6.0.5:
ts-command-line-args "^2.2.0"
ts-essentials "^7.0.1"

typescript@^4.7.2:
version "4.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.2.tgz#1f9aa2ceb9af87cca227813b4310fff0b51593c4"
integrity sha512-Mamb1iX2FDUpcTRzltPxgWMKy3fhg0TN378ylbktPGPK/99KbDtMQ4W1hwgsbPAsG3a0xKa1vmw4VKZQbkvz5A==

typical@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/typical/-/typical-4.0.0.tgz#cbeaff3b9d7ae1e2bbfaf5a4e6f11eccfde94fc4"
Expand Down

0 comments on commit 449958d

Please sign in to comment.