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

Add TypeScript and general refactor #1

Merged
merged 38 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f61fb55
WIP - Adding TS support and combining chains into single exported array
andreogle Apr 5, 2023
9895549
Remove extra empty line
andreogle Apr 5, 2023
d3f12b2
Cleaning up. Add validation script
andreogle Apr 6, 2023
19abb00
Cleaning up scripts
andreogle Apr 6, 2023
39bb422
More cleaning up scripts
andreogle Apr 6, 2023
25399fb
Remove silent logger
andreogle Apr 6, 2023
120799b
Add workflows and separate tsconfigs
andreogle Apr 6, 2023
92d5caa
Fix validate-providers script
andreogle Apr 6, 2023
7efb8fe
Only install deps if cache isn't hit
andreogle Apr 6, 2023
cdcc5a5
Add prepublishOnly step to validate chains before publish
andreogle Apr 6, 2023
9031aa0
Fix TS error
andreogle Apr 6, 2023
37b89dd
Rename script to generate-chains
andreogle Apr 6, 2023
86c19a8
Add validate script and run it as part of CI
andreogle Apr 11, 2023
8360e08
Run prettier before saving generated chains
andreogle Apr 11, 2023
2bcefa7
Use sync fs functions rather than async functions
andreogle Apr 11, 2023
52aad08
Drop valid boolean and exit with status 1 instead
andreogle Apr 11, 2023
1c61348
Rename zod schemas. Refactor getEnvVariables
andreogle Apr 11, 2023
0694aec
Use existing prettier config for generated chains
andreogle Apr 11, 2023
59b774d
Refactor writing of env variables
andreogle Apr 11, 2023
f0d6522
Add comment about chokidar options
andreogle Apr 11, 2023
b4e4002
Ensure all CI steps have passed
andreogle Apr 11, 2023
3c5712a
Add husky pre-push hook
andreogle Apr 11, 2023
f66e835
Simplify TypeScript config
andreogle Apr 11, 2023
b36848c
Validate chains only on pre-push
andreogle Apr 11, 2023
0419a45
Always return chainId as a string. Add explanatory comment
andreogle Apr 11, 2023
a591f4f
Always run yarn install in CI
andreogle Apr 11, 2023
b5c9423
Fix script name
andreogle Apr 11, 2023
b1f3b63
Update README
andreogle Apr 12, 2023
a9e4065
Add table of contents to README
andreogle Apr 12, 2023
4d48567
Fix code snippet
andreogle Apr 12, 2023
968c623
Update README.md
andreogle Apr 14, 2023
e8f3e69
Update README.md
andreogle Apr 14, 2023
cf0827b
Rename validate:providers to providers:ping. Remove from validations
andreogle Apr 14, 2023
526f168
Validate JSON file names and remove rename script
andreogle Apr 14, 2023
cecf19e
Validate the JSON content, rather than the already generated chain
andreogle Apr 14, 2023
547cbb5
Remove stale comment
andreogle Apr 14, 2023
6d744b7
Update comment with correct script name
andreogle Apr 14, 2023
e180b83
Merge branch 'main' into feature/typescript-exports
bbenligiray Apr 14, 2023
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
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: monthly
time: "03:00"
open-pull-requests-limit: 10
reviewers:
- andreogle
57 changes: 57 additions & 0 deletions .github/workflows/continuous-build.yml
andreogle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Continuous Build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
documentation:
runs-on: ubuntu-latest
steps:
- name: Clone @api3/chains
uses: actions/checkout@v3

- name: Check hyperlinks
uses: gaurav-nelson/github-action-markdown-link-check@v1

lint-test:
runs-on: ubuntu-latest
steps:
- name: Clone @api3/chains
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'

- name: Get yarn cache directory path
id: deps-cache-dir-path
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- uses: actions/cache@v3
id: deps-cache
with:
path: ${{ steps.deps-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Install Dependencies
run: yarn install --frozen-lockfile

- name: Run Validations
run: yarn validate

required-checks-passed:
name: All required checks passed
runs-on: ubuntu-latest
needs: [documentation, lint-test]
steps:
- run: exit 0

5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules

.env
*.env
dist/
node_modules/
5 changes: 5 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn validate:chains
yarn lint
14 changes: 14 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# NOTE: Keep in sync with .gitignore

# Dependencies
/node_modules

# Build files
/coverage
/dist

# Logs
*.log

# IDEs
.vscode
22 changes: 22 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"bracketSpacing": true,
"printWidth": 120,
"singleQuote": true,
"trailingComma": "es5",
"useTabs": false,
"overrides": [
{
"files": "*.ts",
"options": {
"parser": "typescript"
}
},
{
"files": "*.md",
"options": {
"parser": "markdown",
"proseWrap": "always"
}
}
]
}
191 changes: 186 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
# @api3/chains

> A single source of truth for chain-related information
> The single source of truth for chain-related information

# Notes
- [Notes](#notes)
- [General](#general)
- [Deployment](#deployment)
- [hardhat-etherscan](#hardhat-etherscan)
- [API](#api)
- [CHAINS](#chains)
- [getChainByAlias](#getchainbyalias)
- [hardhatConfigNetworks](#hardhatconfignetworks)
- [hardhatEtherscan](#hardhatetherscan)
- [getEnvVariables](#getenvvariables)
- [Types](#types)
- [Scripts](#scripts)
- [chains:generate](#chainsgenerate)
- [chains:rename](#chainsrename)
- [env:write](#envwrite)
- [Development](#development)
- [Validation](#validation)
- [Building](#building)

## General
## Notes

### General

- `gnosis-testnet` provider may return an invalid gas price.

## Deployment
### Deployment

- `metis-goerli-testnet` and `metis` do not support deterministic deployment.

## hardhat-etherscan
### hardhat-etherscan

- `hardhat-etherscan` requires us to use a dummy API key with Blockscout block explorer APIs.
We use `"DUMMY_VALUE"` but it could have been anything else.
Expand All @@ -24,3 +43,165 @@
- `gnosis-testnet` block explorer contract verification API does not work.

- `rsk` and `rsk-testnet` block explorers do not have contract verification APIs.

## API

The following variables/functions are exported from this package

### CHAINS

The single source of truth for the list of supported chains. A static array of `Chain` objects.

```ts
import { CHAINS } from '@api3/chains';
console.log(CHAINS);
/*
[
{
name: 'Arbitrum testnet',
alias: 'arbitrum-goerli-testnet',
id: '421613',
...
},
...
]
*/
```

### getChainByAlias

Returns a single `Chain` record found by it's alias. Throws an error if the `Chain` is not found.

```ts
import { getChainByAlias } from '@api3/chains';
console.log(getChainByAlias('ethereum'));
/*
{
"name": "Ethereum",
"alias": "ethereum",
"id": "1",
...
}
*/
```

### hardhatConfigNetworks

Returns an object where the key is each chain's alias and the value is an object that can be used with [hardhat](https://hardhat.org).
andreogle marked this conversation as resolved.
Show resolved Hide resolved

```ts
import { hardhatConfigNetworks } from '@api3/chains';
console.log(hardhatConfigNetworks());
/*
{
"arbitrum-goerli-testnet": {
accounts: { mnemonic: '' },
chainId: '421613',
url: 'https://...',
},
...
}
*/
```

### hardhatEtherscan

Returns an object that can be used with Etherscan.
andreogle marked this conversation as resolved.
Show resolved Hide resolved

```ts
import { hardhatEtherscan } from '@api3/chains';
console.log(hardhatEtherscan())
/*
{
apiKey: {
'arbitrumGoerli': { ... }
},
customChains: [
...
]
}
*/
```

### getEnvVariables

Returns an array of expected environment variable names for chains that have an API key required for the explorer.

```ts
import { getEnvVariables } from '@api3/chains';
console.log(getEnvVariables());
/*
[
'MNEMONIC',
'ETHERSCAN_API_KEY_arbitrum-goerli-testnet',
...
]
*/
```

### Types

Types are also exported and can be found in `src/types.ts`. Types are generated from [zod](https://github.com/colinhacks/zod) schemas. These schemas are also used to validate each chain.

## Scripts

The following utility scripts are available

### chains:generate

Generates the latest `CHAINS` array and outputs the file to `src/generated/chains.ts`

```sh
yarn chains:generate
```

### chains:rename

Renames each JSON file using the `alias` as the filename.

```sh
yarn chains:rename
```

### env:write

Generates the default content for a `.env` file using the output of `getEnvVariables` as the keys.

```sh
yarn env:write --path .env
```

## Development

This project works by combining the various JSON files defined in the `chains/` directory into a single generated TypeScript file. This file is then validated to ensure that each chain description conforms to specific requirements. The TypeScript file is generated by running any of these commands

```sh
yarn chains:generate

# Alternatively, watch the chains/ directory and regenerate on file change
yarn dev
```

### Validation

Validations can be run with the following commands.

```sh
# Validate each chain JSON file conforms to the zod schemas
yarn validate:chains

# Validate each chain's provider URL is correct and working
yarn validate:providers

# Run all validations synchronously
yarn validate
```

## Building

The TypeScript project can be compiled by running the following command. This regenerates the latest CHAINS array from the JSON files first, before running `tsc`. Files are output in the `dist/` directory

```sh
yarn build
```

37 changes: 31 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@
"name": "@api3/chains",
"license": "MIT",
"version": "1.4.0",
"author": "API3 DAO",
"private": false,
"main": "src/index",
"main": "./dist/index.js",
"repository": "[email protected]:api3dao/chains",
"bugs": {
"url": "https://github.com/api3dao/chains/issues"
},
"files": [
"chains",
"dist",
"src"
],
"scripts": {
"check-provider": "node scripts/check-provider",
"build": "yarn clean && yarn chains:generate && tsc -p tsconfig.build.json",
"clean": "rimraf ./dist",
"dev": "ts-node scripts/generate-chains.ts --watch",
"chains:generate": "ts-node scripts/generate-chains.ts",
"chains:rename": "ts-node scripts/rename-chain-file.ts",
"env:write": "ts-node scripts/write-env-variables.ts",
"lint": "exit 0",
"prepare": "husky install",
"prepublishOnly": "yarn validate:chains",
andreogle marked this conversation as resolved.
Show resolved Hide resolved
"prettier": "prettier --write \"./**/*.{js,md,json}\"",
"rename-chain-files": "node scripts/rename-chain-files"
"validate": "yarn validate:chains && yarn validate:providers",
"validate:chains": "ts-node scripts/validate-chains.ts",
"validate:providers": "ts-node scripts/validate-providers.ts"
},
"dependencies": {
"zod": "^3.21.4"
},
"devDependencies": {
"ethers": "^6.0.8",
"prettier": "^2.8.4"
"@types/node": "^18.15.11",
"@types/prettier": "^2.7.2",
"chokidar": "^3.5.3",
"ethers": "^6.2.3",
"husky": "^8.0.3",
"prettier": "^2.8.4",
"rimraf": "^4.4.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.3"
}
}
Loading