forked from taylorjdawson/eth-chains
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cee8ddc
Showing
13 changed files
with
3,220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
chains.json | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"semi": false, | ||
"trailingComma": "none", | ||
"singleQuote": true, | ||
"printWidth": 80, | ||
"tabWidth": 2, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
### Usage | ||
```js | ||
import chains, { NetworkId, NetworkName } from 'eth-chains' | ||
|
||
chains.getById(NetworkId.EthereumMainnet) | ||
chains.getByName(NetworkName.EthereumMainnet) | ||
chains.get() // pass in a networkId or a networkName | ||
|
||
console.log(NetworkId.Ethereum.Mainnet) // 1 | ||
console.log(NetworkId.BinanceSmartChain.Mainnet) // 56 | ||
|
||
console.log(NetworkName.Ethereum.Mainnet) // "Ethereum Mainnet" | ||
``` | ||
|
||
|
||
TODO: | ||
- [ ] Add webhook that watches the chains repo and triggers an update to this package whenever that repo gets updated | ||
- [ ] Add check in the deploy script to make sure that the types are correct before publishing | ||
- [ ] Add Tests | ||
- [ ] Once quicktype is added, test with different chains.json objects to make sure it can handle new types | ||
- [ ] Use [quicktype](https://github.com/quicktype/quicktype) to autogen type from json | ||
- [ ] Autogen Chain Id enum | ||
- [x] Autogen Chain Name enum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// import chains, { NetworkName } from './src' | ||
import jsonToTs from 'json-to-ts'; | ||
import chains from './chains.json' | ||
console.log(jsonToTs(chains[0])) | ||
// console.log(chains.get(NetworkName['Ether-1'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "eth-chains", | ||
"version": "0.1.0", | ||
"description": "Helper module for getting Ethereum network info", | ||
"main": "index.ts", | ||
"author": "Taylor Dawson", | ||
"license": "WTFPL", | ||
"private": false, | ||
"scripts": { | ||
"build": "ts-node src/build.ts", | ||
"format": "prettier --write src/" | ||
}, | ||
"dependencies": { | ||
"@types/node": "^14.14.37", | ||
"got": "^11.8.2", | ||
"json-to-ts": "^1.7.0", | ||
"typescript": "^4.2.3" | ||
}, | ||
"devDependencies": { | ||
"@types/prettier": "^2.2.3", | ||
"prettier": "^2.2.1", | ||
"ts-node": "^9.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import got from 'got' | ||
import { promises as fs } from 'fs' | ||
import { inspect } from 'util' | ||
import { format } from 'prettier' | ||
import { Options } from 'prettier' | ||
|
||
import { Chain } from './types' | ||
import { capitalize } from './helpers' | ||
import prettierOptions from '../.prettierrc.json' | ||
|
||
const formatterOptions = { | ||
...(prettierOptions as Options), | ||
parser: 'typescript' | ||
} | ||
|
||
const getEnum = (chainName: string) => { | ||
let enumKey = capitalize(chainName.replace(/\s/g, '')) | ||
// If the key starts with a number or contains ',' or '.' then wrap it in quotes | ||
enumKey = !!chainName.match(/^\d|[\-\.]/) ? `'${enumKey}'` : enumKey | ||
return `${enumKey} = '${chainName}'` | ||
} | ||
|
||
const generateEnumFile = async (chains: Chain[]) => { | ||
fs.writeFile( | ||
'./src/enums.ts', | ||
format( | ||
`export enum NetworkName {${chains | ||
.map(chain => `${getEnum(chain.name)}`) | ||
.join(',\n')}}`, | ||
{ ...(prettierOptions as Options), parser: 'typescript' } | ||
) | ||
) | ||
} | ||
|
||
const generateChainsFile = async () => { | ||
const chains: Chain[] = await got( | ||
'https://chainid.network/chains.json' | ||
).json() | ||
|
||
//await generateEnumFile(chains) | ||
|
||
const chainsJs = chains | ||
.map(chain => `${chain.chainId}: ${inspect(chain)}`) | ||
.join(',\n') | ||
|
||
fs.writeFile( | ||
'./src/chains.ts', | ||
format( | ||
`import { Chains } from "./types"\nexport const chains: Chains = {\n${chainsJs}\n}`, | ||
formatterOptions | ||
) | ||
) | ||
} | ||
|
||
generateChainsFile() |
Oops, something went wrong.