Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorjdawson committed May 4, 2021
0 parents commit cee8ddc
Show file tree
Hide file tree
Showing 13 changed files with 3,220 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chains.json
node_modules
8 changes: 8 additions & 0 deletions .prettierrc.json
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"
}
24 changes: 24 additions & 0 deletions README.md
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
5 changes: 5 additions & 0 deletions index.ts
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']))
24 changes: 24 additions & 0 deletions package.json
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"
}
}
55 changes: 55 additions & 0 deletions src/build.ts
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()
Loading

0 comments on commit cee8ddc

Please sign in to comment.