Skip to content

Commit

Permalink
feat!: introduce Base and StaticToken and make priceUSD in Token requ…
Browse files Browse the repository at this point in the history
…ired (#153)

* fix: baseToken wont contain price information

* fix: add StaticToken and update BaseToken types

* fix: chainId should have type ChainId

---------

Co-authored-by: Daniela Chybisova <iron.melmel@gmail.com>
  • Loading branch information
SebastianBoehler and melianessa committed Apr 25, 2023
1 parent 6aa9253 commit 85b9ab1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 63 deletions.
17 changes: 11 additions & 6 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,22 @@ export enum ChainId {
LNAT = 59140,
}

export interface Token {
export interface BaseToken {
chainId: ChainId
address: string
}
export interface StaticToken extends BaseToken {
symbol: string
decimals: number
chainId: number
name: string
coinKey?: CoinKey
priceUSD?: string
logoURI?: string
}

export interface Token extends StaticToken {
priceUSD: string
}

export interface TokenAmount extends Token {
amount: string
blockNumber?: number
Expand All @@ -200,7 +205,7 @@ export interface Coin {
logoURI: string
verified: boolean
chains: {
[ChainId: string]: Token
[ChainId: string]: StaticToken
}
}

Expand All @@ -216,9 +221,9 @@ export interface ExchangeDefinition {
export interface BridgeDefinition {
tool: BridgeTool
fromChainId: number
fromToken: Token
fromToken: BaseToken
toChainId: number
toToken: Token
toToken: BaseToken
maximumTransfer: string
minimumTransfer: string
swapFeeRate: string
Expand Down
17 changes: 10 additions & 7 deletions src/coins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId, Coin, CoinKey, Token } from './base'
import { StaticToken, ChainId, Coin, CoinKey, BaseToken } from './base'

type BasicToken = {
address: string
Expand Down Expand Up @@ -1343,7 +1343,7 @@ export const defaultCoins: Array<Coin> = basicCoins.map((coin) => {
})

// Wrapped version of gas on chain
export const wrappedTokens: { [ChainId: string]: Token } = {
export const wrappedTokens: { [ChainId: string]: StaticToken } = {
[ChainId.ETH]: {
// https://ww7.etherscan.io/token/0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
address: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
Expand Down Expand Up @@ -1659,7 +1659,10 @@ export const findDefaultCoin = (coinKey: CoinKey): Coin => {
}
return coin
}
export const findDefaultToken = (coinKey: CoinKey, chainId: ChainId): Token => {
export const findDefaultToken = (
coinKey: CoinKey,
chainId: ChainId
): StaticToken => {
const coin = findDefaultCoin(coinKey)
const token = coin.chains[chainId]
if (!token) {
Expand All @@ -1668,7 +1671,7 @@ export const findDefaultToken = (coinKey: CoinKey, chainId: ChainId): Token => {
return token
}

export const findWrappedGasOnChain = (chainId: ChainId): Token => {
export const findWrappedGasOnChain = (chainId: ChainId): StaticToken => {
const token = wrappedTokens[chainId]
if (!token) {
throw new Error(`Wrapped Gas Token not defined for chain ${chainId}.`)
Expand All @@ -1679,11 +1682,11 @@ export const findWrappedGasOnChain = (chainId: ChainId): Token => {
export const findTokenByChainIdAndAddress = (
chainId: number,
tokenAddress: string
): Token | null => {
let token: Token | null = null
): StaticToken | null => {
let token: StaticToken | null = null

defaultCoins.forEach((coin) => {
Object.values(coin.chains).forEach((coinToken: Token) => {
Object.values(coin.chains).forEach((coinToken: StaticToken) => {
if (coinToken.chainId === chainId && coinToken.address === tokenAddress) {
token = coinToken
}
Expand Down

0 comments on commit 85b9ab1

Please sign in to comment.