diff --git a/script/blockchain/binance.ts b/script/blockchain/binance.ts index b8a5277b9..1135cd720 100644 --- a/script/blockchain/binance.ts +++ b/script/blockchain/binance.ts @@ -5,15 +5,20 @@ import * as path from "path"; import * as chalk from 'chalk'; import * as config from "../config"; import { ActionInterface, CheckStepInterface } from "../generic/interface"; -import { getChainAssetsPath } from "../generic/repo-structure"; import { Binance } from "../generic/blockchains"; -import { readDirSync } from "../generic/filesystem"; -import { readJsonFile } from "../generic/json"; - +import { readDirSync, writeFileSync } from "../generic/filesystem"; +import { readJsonFile, formatJson } from "../generic/json"; +import { TokenItem, Pair, generateTokensList } from "../generic/tokenlists"; import { getChainAssetLogoPath, - getChainDenylistPath + getChainAssetsPath, + getChainDenylistPath, + getChainTokenlistPath } from "../generic/repo-structure"; +import { CoinType } from "@trustwallet/wallet-core"; +import { toSatoshis } from "../generic/numbers"; +import { assetID } from "../generic/asset"; +import { TokenType } from "../generic/tokentype"; const binanceChain = "binance"; const binanceUrlTokenAssets = config.binanceUrlTokenAssets; @@ -132,5 +137,84 @@ export class BinanceAction implements ActionInterface { console.log(`Fetched ${fetchedAssets.length} asset(s):`); fetchedAssets.forEach(asset => console.log(` ${asset}`)); } + + // binance chain list + const list = await generateBinanceTokensList(); + writeFileSync(getChainTokenlistPath(Binance), formatJson(generateTokensList("BNB", list))); + console.log(`Binance token list: list with ${list.length} tokens generated.`); } } + +class BinanceMarket { + base_asset_symbol: string + quote_asset_symbol: string + lot_size: string + tick_size: string +} + +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) + } + function tokenType(symbol: string): string { + if (symbol == BNBSymbol) { + return TokenType.COIN + } + return TokenType.BEP2 + } + const list = <[string]>Array.from(pairsList.values()) + return <[TokenItem]>list.map(item => { + const token = tokensMap[item] + return new TokenItem ( + asset(token.symbol), + tokenType(token.symbol), + token.symbol, + token.name, + token.original_symbol, + decimals, + logoURI(token.symbol), + pairsMap[token.symbol] || [] + ) + }).sort((n1,n2) => (n2.pairs || []).length - (n1.pairs || []).length); +} diff --git a/script/generic/tokenlists.ts b/script/generic/tokenlists.ts index f35e80857..5561a575d 100644 --- a/script/generic/tokenlists.ts +++ b/script/generic/tokenlists.ts @@ -1,24 +1,3 @@ -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"; -import { TokenType } from "./tokentype"; - -class BinanceMarket { - base_asset_symbol: string - quote_asset_symbol: string - lot_size: string - tick_size: string -} - class Version { major: number minor: number @@ -35,11 +14,11 @@ class List { name: string logoURI: string timestamp: string - tokens: [TokenItem] - pairs: [Pair] + tokens: TokenItem[] + pairs: Pair[] version: Version - constructor(name: string, logoURI: string, timestamp: string, tokens: [TokenItem], version: Version) { + constructor(name: string, logoURI: string, timestamp: string, tokens: TokenItem[], version: Version) { this.name = name this.logoURI = logoURI this.timestamp = timestamp; @@ -48,7 +27,7 @@ class List { } } -class TokenItem { +export class TokenItem { asset: string; type: string; address: string; @@ -56,9 +35,9 @@ class TokenItem { symbol: string; decimals: number; logoURI: string; - pairs: [Pair]; + pairs: Pair[]; - constructor(asset: string, type: string, address: string, name: string, symbol: string, decimals: number, logoURI: string, pairs: [Pair]) { + constructor(asset: string, type: string, address: string, name: string, symbol: string, decimals: number, logoURI: string, pairs: Pair[]) { this.asset = asset this.type = type this.address = address @@ -70,7 +49,7 @@ class TokenItem { } } -class Pair { +export class Pair { base: string; lotSize: string; tickSize: string; @@ -82,96 +61,12 @@ class Pair { } } -export class TokenLists implements ActionInterface { - getName(): string { return "TokenLists"; } - - getSanityChecks = null; - - getConsistencyChecks(): CheckStepInterface[] { - const steps: CheckStepInterface[] = []; - return steps; - } - - async update(): Promise { - // binance chain list - const list = await generateBinanceTokensList(); - writeFileSync(getChainTokenlistPath(Binance), formatJson(generateTokensList(list))); - } -} - -function generateTokensList(tokens: [TokenItem]): List { +export function generateTokensList(titleCoin: string, tokens: TokenItem[]): List { return new List( - "Trust Wallet: BNB", + `Trust Wallet: ${titleCoin}`, "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) - } - function tokenType(symbol: string): string { - if (symbol == BNBSymbol) { - return TokenType.COIN - } - return TokenType.BEP2 - } - const list = <[string]>Array.from(pairsList.values()) - return <[TokenItem]>list.map(item => { - const token = tokensMap[item] - return new TokenItem ( - asset(token.symbol), - tokenType(token.symbol), - token.symbol, - token.name, - token.original_symbol, - decimals, - logoURI(token.symbol), - pairsMap[token.symbol] || [] - ) - }).sort((n1,n2) => (n2.pairs || []).length - (n1.pairs || []).length); -} \ No newline at end of file diff --git a/script/generic/update-all.ts b/script/generic/update-all.ts index 1329e9877..e9c2dd599 100644 --- a/script/generic/update-all.ts +++ b/script/generic/update-all.ts @@ -12,7 +12,6 @@ import { TronAction } from "../blockchain/tron"; import { Validators } from "../generic/validators"; import { WavesAction } from "../blockchain/waves"; import { Allowlists } from "../generic/allowlists"; -import { TokenLists } from "../generic/tokenlists"; import { ActionInterface, CheckStepInterface } from "../generic/interface"; import * as chalk from 'chalk'; import * as bluebird from "bluebird"; @@ -23,7 +22,6 @@ const actionList: ActionInterface[] = [ new EthForks(), new LogoSize(), new Allowlists(), - new TokenLists(), new Validators(), new JsonAction(), // chains: