Skip to content

Commit

Permalink
feat: changes to support non EVM chains (#59)
Browse files Browse the repository at this point in the history
* feat: initial changes to support non EVM chains

* fix(typealias): added type alias => EVMChain = Chain

* fix: added legacy supprotedChains export

* feat: added chainType to the _Chain object
  • Loading branch information
H3xept authored May 12, 2022
1 parent 6ab8e9e commit a8ae8a6
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 50 deletions.
Binary file added src/assets/icons/chains/solana.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export enum CoinKey {
VLX = 'VLX',
GLMR = 'GLMR',
METIS = 'METIS',
SOL = 'SOL',

// Stable coins
USDT = 'USDT',
Expand Down
18 changes: 18 additions & 0 deletions src/chains/Chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ChainKey, CoinKey } from '../base'

export enum ChainType {
EVM = 'EVM',
Solana = 'SOLANA',
}

export interface _Chain {
key: ChainKey
chainType: ChainType
name: string
coin: CoinKey
id: number
mainnet: boolean
logoURI?: string
// faucetUrls is DEPRECATED - will be removed in the next breaking release
faucetUrls?: string[]
}
30 changes: 30 additions & 0 deletions src/chains/EVMChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BigNumber } from 'ethers'
import { _Chain } from './Chain'

export interface EVMChain extends _Chain {
// tokenlistUrl is DEPRECATED - will be removed in the next breaking release
tokenlistUrl?: string
metamask: AddEthereumChainParameter
multicallAddress?: string
}

export interface AddEthereumChainParameter {
chainId: string
blockExplorerUrls: string[]
chainName: string
nativeCurrency: {
name: string
symbol: string
decimals: number
}
rpcUrls: string[]
}

export const prefixChainId = (chainId: number): string => {
return '0x' + BigNumber.from(chainId)._hex.split('0x')[1].replace(/\b0+/g, '')
}

// This type alias is required to avoid breaking
// changes with the new non EVM support types release
// This will be removed in the future in favour of _Chain
export type Chain = EVMChain
3 changes: 3 additions & 0 deletions src/chains/SolanaChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { _Chain } from './Chain'

export type SolanaChain = _Chain
27 changes: 27 additions & 0 deletions src/chains/chain.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ChainKey } from '../base'
import { Chain } from './EVMChain'
import { EVMChain } from './EVMChain'
import { SolanaChain } from './SolanaChain'
import { supportedEVMChains, supportedSolanaChains } from './supported.chains'

const supportedChains: Chain[] = [
// This will be added in the future
// ...supportedSolanaChains,
...supportedEVMChains,
]

export const getChainByKey = (chainKey: ChainKey): Chain => {
const chain = supportedChains.find((c) => c.key === chainKey)
if (!chain) {
throw new Error('Invalid chainKey')
}
return chain
}

export const getChainById = (chainId: number): Chain => {
const chain = supportedChains.find((c) => c.id === chainId)
if (!chain) {
throw new Error('Invalid chainId')
}
return chain
}
5 changes: 5 additions & 0 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './Chain'
export * from './EVMChain'
export * from './SolanaChain'
export * from './supported.chains'
export * from './chain.utils'
Loading

0 comments on commit a8ae8a6

Please sign in to comment.