[Internal] Adjust binance tokenlist generation, fix array types (#5357)

* Move binance tokenlist generation to BinanceAction.

* Fix array types in tokenlists.ts

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2021-01-23 01:04:56 +01:00 committed by GitHub
parent 9f740c31eb
commit 0f1909110d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 121 deletions

View File

@ -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);
}

View File

@ -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<void> {
// 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);
}

View File

@ -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: