Skip to content

Commit

Permalink
Merge pull request #14 from taylorjdawson/update/chains
Browse files Browse the repository at this point in the history
[1.0.0]: Chain updates + helpers
  • Loading branch information
taylorjdawson authored Jul 6, 2022
2 parents e898ed7 + 77b9e61 commit 1f229d9
Show file tree
Hide file tree
Showing 11 changed files with 4,314 additions and 654 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
chains.json
node_modules
dist
dist
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[![Publish](https://github.com/taylorjdawson/eth-chains/actions/workflows/main.yml/badge.svg)](https://github.com/taylorjdawson/eth-chains/actions/workflows/main.yml) ![npm](https://img.shields.io/npm/v/eth-chains?logoColor=blue)

# Eth Chains

Helper module for getting Ethereum chains info from [chainid.network](https://chainid.network/).

## Install

```
yarn add eth-chains
```
Expand All @@ -13,32 +15,57 @@ npm install eth-chains
```

## Usage

Import `chains` methods and enums:

```ts
import chains, { ChainId, ChainName } from 'eth-chains'
```

### Easily get most popular chains:

```ts
import { chain } from 'eth-chains'

console.log(chain.ethereum.rinkeby)

console.log(chain.polygon.mumbai)
```

### Chain names and ids via Enums:

```ts
console.log(ChainId.EthereumMainnet) // 1
console.log(ChainId.BinanceSmartChainMainnet) // 56
console.log(ChainName.EthereumMainnet) // "Ethereum Mainnet"
console.log(ChainName.Rinkeby) // "Rinkeby"
```

### Chain by ID:

```ts
chains.getById(ChainId.EthereumMainnet) // { name: "Ethereum Mainnet", ..., "infoURL": "https://ethereum.org" }
// Equivalent
chains.getById(1)
chains.getById(1)
```

### Chain by Name:

```ts
chains.getByName(ChainName.EthereumMainnet) // { name: "Ethereum Mainnet", ..., "infoURL": "https://ethereum.org" }
// Equivalent
chains.getByName('Ethereum Mainnet')
```

### Get all Chains:

```ts
const allChains = chains.all()
// { 1: { name: "Ethereum Mainnet", ..., "infoURL": "https://ethereum.org" }, 2: {...}}
```

### Typescript Types:

```ts
import { Chain, NativeCurrency, Explorer } from 'eth-chains'
const ethereum: Chain = chains.getById(ChainId.EthereumMainnet)
Expand All @@ -48,10 +75,11 @@ ethereum.chain // 'ETH'
---

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 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
- [ ] 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 types from json
- [x] Autogen Chain Id enum
- [x] Autogen Chain Name enum
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export {
ChainName,
NativeCurrency,
Explorer,
Parent
Parent,
chain
} from './src'
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eth-chains",
"version": "0.4.0",
"version": "1.0.0",
"description": "Helper module for getting Ethereum chains info.",
"author": "Taylor Dawson",
"main": "dist/index.js",
Expand All @@ -11,15 +11,13 @@
],
"license": "WTFPL",
"scripts": {
"build": "tsc",
"build": "rm -r dist && tsc",
"generate": "ts-node src/build.ts",
"format": "prettier --write src/",
"prepack": "yarn build"
},
"dependencies": {
"got": "^11.8.2"
},
"devDependencies": {
"got": "^11.8.2",
"@types/node": "^14.14.37",
"@types/prettier": "^2.2.3",
"prettier": "^2.2.1",
Expand Down
9 changes: 6 additions & 3 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Options } from 'prettier'
import { Chain } from './types'
import { capitalize } from './helpers'
import prettierOptions from '../.prettierrc.json'
import { ENUM_KEY_OVERRIDES } from './constants'

const formatterOptions = {
...(prettierOptions as Options),
Expand All @@ -16,15 +17,15 @@ const formatterOptions = {
const getEnumKey = (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
enumKey = !!chainName.match(/^\d|[\-\.\()]/) ? `'${enumKey}'` : enumKey
return enumKey
}

const getBuildEnums = (chains: Chain[]) =>
chains
.reduce(
(enumStrings, chain, index, array) => {
const key = getEnumKey(chain.name)
const key = ENUM_KEY_OVERRIDES[chain.chainId] ?? getEnumKey(chain.name)
const tail = index === array.length - 1 ? ' }' : ', '
enumStrings[0] += `${key} = '${chain.name}'${tail}`
enumStrings[1] += `${key} = ${chain.chainId}${tail}`
Expand All @@ -49,7 +50,9 @@ const generateChainsFile = async () => {
'https://chainid.network/chains.json'
).json()

await generateEnumFile(chains)
await generateEnumFile(chains).catch(() => {
console.log('Error generating enum file')
})

const chainsJs = chains
.map(chain => `${chain.chainId}: ${inspect(chain, { depth: null })}`)
Expand Down
Loading

0 comments on commit 1f229d9

Please sign in to comment.