forked from trustwallet/assets
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token list support for pairs on binance chain (trustwallet#4428)
* Add token list support for pairs on binance chain * Update tokenlists.ts * Update tokenlists.ts * Update tokenlists.ts * Fix asset for bnb pair * Update package-lock.json * Add models for token list * Add BinanceMarket
- Loading branch information
Showing
12 changed files
with
2,227 additions
and
76 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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,6 @@ | ||
export function assetID(coin: number, token_id = ``): string { | ||
if (token_id.length > 0) { | ||
return `c${coin}_t${token_id}` | ||
} | ||
return `c${coin}` | ||
} |
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,9 @@ | ||
import BigNumber from "bignumber.js"; | ||
|
||
export function toSatoshis(value: string, decimals: number): string { | ||
return new BigNumber(value).multipliedBy(new BigNumber(10).exponentiatedBy(decimals)).toFixed() | ||
} | ||
|
||
export function fromSatoshis(value: string, decimals: number): string { | ||
return new BigNumber(value).dividedBy(new BigNumber(10).exponentiatedBy(decimals)).toFixed() | ||
} |
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,170 @@ | ||
import { ActionInterface, CheckStepInterface } from "./interface"; | ||
import axios from "axios"; | ||
import { | ||
getChainTokenlistPath | ||
} from "./repo-structure"; | ||
import { Binance } from "./blockchains"; | ||
import { writeFileSync } from "./filesystem"; | ||
import { formatJson } from "./json"; | ||
import { assetID } from "./asset"; | ||
import * as config from "../config"; | ||
import { CoinType } from "@trustwallet/wallet-core"; | ||
import { toSatoshis } from "./numbers"; | ||
|
||
class BinanceMarket { | ||
base_asset_symbol: string | ||
quote_asset_symbol: string | ||
lot_size: string | ||
tick_size: string | ||
} | ||
|
||
class Version { | ||
major: number | ||
minor: number | ||
patch: number | ||
|
||
constructor(major: number, minor: number, patch: number) { | ||
this.major = major | ||
this.minor = minor | ||
this.patch = patch | ||
} | ||
} | ||
|
||
class List { | ||
name: string | ||
logoURI: string | ||
timestamp: string | ||
tokens: [TokenItem] | ||
pairs: [Pair] | ||
version: Version | ||
|
||
constructor(name: string, logoURI: string, timestamp: string, tokens: [TokenItem], version: Version) { | ||
this.name = name | ||
this.logoURI = logoURI | ||
this.timestamp = timestamp; | ||
this.tokens = tokens | ||
this.version = version | ||
} | ||
} | ||
|
||
class TokenItem { | ||
asset: string; | ||
address: string; | ||
name: string; | ||
symbol: string; | ||
decimals: number; | ||
logoURI: string; | ||
pairs: [Pair]; | ||
|
||
constructor(asset: string, address: string, name: string, symbol: string, decimals: number, logoURI: string, pairs: [Pair]) { | ||
this.asset = asset | ||
this.address = address | ||
this.name = name; | ||
this.symbol = symbol | ||
this.decimals = decimals | ||
this.logoURI = logoURI | ||
this.pairs = pairs | ||
} | ||
} | ||
|
||
class Pair { | ||
base: string; | ||
lotSize: string; | ||
tickSize: string; | ||
|
||
constructor(base: string, lotSize: string, tickSize: string) { | ||
this.base = base | ||
this.lotSize = lotSize | ||
this.tickSize = tickSize | ||
} | ||
} | ||
|
||
export class TokenLists implements ActionInterface { | ||
getName(): string { return "TokenLists"; } | ||
|
||
getSanityChecks = null; | ||
|
||
getConsistencyChecks(): CheckStepInterface[] { | ||
const steps: CheckStepInterface[] = []; | ||
return steps; | ||
} | ||
|
||
async consistencyFix(): Promise<void> { | ||
|
||
// binance chain list | ||
const list = await generateBinanceTokensList() | ||
writeFileSync(getChainTokenlistPath(Binance), formatJson(generateTokensList(list))); | ||
|
||
return | ||
} | ||
} | ||
|
||
function generateTokensList(tokens: [TokenItem]): List { | ||
return new List( | ||
"Trust Wallet: BNB", | ||
"https://trustwallet.com/assets/images/favicon.png", | ||
"2020-10-03T12:37:57.000+00:00", | ||
tokens, | ||
new Version(0, 1, 0) | ||
) | ||
} | ||
|
||
async function generateBinanceTokensList(): Promise<[TokenItem]> { | ||
const decimals = CoinType.decimals(CoinType.binance) | ||
const BNBSymbol = CoinType.symbol(CoinType.binance) | ||
const markets: [BinanceMarket] = await axios.get(`${config.binanceDexURL}/v1/markets?limit=10000`).then(r => r.data); | ||
const tokens = await axios.get(`${config.binanceDexURL}/v1/tokens?limit=10000`).then(r => r.data); | ||
const tokensMap = Object.assign({}, ...tokens.map(s => ({[s.symbol]: s}))); | ||
const pairsMap = {} | ||
const pairsList = new Set(); | ||
|
||
markets.forEach(market => { | ||
const key = market.quote_asset_symbol | ||
|
||
function pair(market: BinanceMarket): Pair { | ||
return new Pair( | ||
asset(market.base_asset_symbol), | ||
toSatoshis(market.lot_size, decimals), | ||
toSatoshis(market.tick_size, decimals) | ||
) | ||
} | ||
|
||
if (pairsMap[key]) { | ||
const newList = pairsMap[key] | ||
newList.push(pair(market)) | ||
pairsMap[key] = newList | ||
} else { | ||
pairsMap[key] = [ | ||
pair(market) | ||
] | ||
} | ||
pairsList.add(market.base_asset_symbol) | ||
pairsList.add(market.quote_asset_symbol) | ||
}) | ||
|
||
function logoURI(symbol: string): string { | ||
if (symbol == BNBSymbol) { | ||
return `${config.assetsURL}/blockchains/binance/assets/${symbol}/logo.png` | ||
} | ||
return `${config.assetsURL}/blockchains/binance/assets/${symbol}/logo.png` | ||
} | ||
function asset(symbol: string): string { | ||
if (symbol == BNBSymbol) { | ||
return assetID(CoinType.binance) | ||
} | ||
return assetID(CoinType.binance, symbol) | ||
} | ||
const list = <[TokenItem]>Array.from(pairsList.values()) | ||
return <[TokenItem]>list.map(item => { | ||
const token = tokensMap[item.symbol] | ||
return new TokenItem ( | ||
asset(token.symbol), | ||
token.symbol, | ||
token.name, | ||
token.symbol, | ||
decimals, | ||
logoURI(token.symbol), | ||
pairsMap[token.symbol] || [] | ||
) | ||
}).sort((n1,n2) => (n2.pairs || []).length - (n1.pairs || []).length); | ||
} |
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
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,13 @@ | ||
import { | ||
assetID | ||
} from "../script/generic/asset"; | ||
|
||
describe("Test eth-address helpers", () => { | ||
test(`Test coin`, () => { | ||
expect(assetID(714)).toEqual('c714'); | ||
expect(assetID(714, '')).toEqual('c714'); | ||
}); | ||
test(`Test token`, () => { | ||
expect(assetID(714, 'TWT-8C2')).toEqual('c714_tTWT-8C2'); | ||
}); | ||
}); |
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,18 @@ | ||
import { | ||
toSatoshis, | ||
fromSatoshis | ||
} from "../script/generic/numbers"; | ||
|
||
describe("Test eth-address helpers", () => { | ||
test(`Test to satoshis`, () => { | ||
expect(toSatoshis('43523.423423432112321234', 18)).toEqual('43523423423432112321234'); | ||
expect(toSatoshis('0.123', 3)).toEqual('123'); | ||
expect(toSatoshis('0.00000001', 8)).toEqual('1'); | ||
}); | ||
test(`Test from Satoshis`, () => { | ||
expect(fromSatoshis('123', 3)).toEqual('0.123'); | ||
expect(fromSatoshis('1234', 3)).toEqual('1.234'); | ||
expect(fromSatoshis('43523423423432112321234', 18)).toEqual('43523.423423432112321234'); | ||
expect(fromSatoshis('1', 8)).toEqual('0.00000001'); | ||
}); | ||
}); |