2020-10-14 01:20:42 +00:00
|
|
|
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";
|
2020-10-23 04:36:39 +00:00
|
|
|
import { TokenType } from "./tokentype";
|
2020-10-23 04:10:51 +00:00
|
|
|
|
2020-10-14 01:20:42 +00:00
|
|
|
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;
|
2020-10-23 04:10:51 +00:00
|
|
|
type: string;
|
2020-10-14 01:20:42 +00:00
|
|
|
address: string;
|
|
|
|
name: string;
|
|
|
|
symbol: string;
|
|
|
|
decimals: number;
|
|
|
|
logoURI: string;
|
|
|
|
pairs: [Pair];
|
|
|
|
|
2020-10-23 04:10:51 +00:00
|
|
|
constructor(asset: string, type: string, address: string, name: string, symbol: string, decimals: number, logoURI: string, pairs: [Pair]) {
|
2020-10-14 01:20:42 +00:00
|
|
|
this.asset = asset
|
2020-10-23 04:10:51 +00:00
|
|
|
this.type = type
|
2020-10-14 01:20:42 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-10-23 04:36:39 +00:00
|
|
|
function tokenType(symbol: string): string {
|
|
|
|
if (symbol == BNBSymbol) {
|
|
|
|
return TokenType.COIN
|
|
|
|
}
|
|
|
|
return TokenType.BEP2
|
|
|
|
}
|
2020-10-23 04:10:51 +00:00
|
|
|
const list = <[string]>Array.from(pairsList.values())
|
2020-10-14 01:20:42 +00:00
|
|
|
return <[TokenItem]>list.map(item => {
|
2020-10-23 04:10:51 +00:00
|
|
|
const token = tokensMap[item]
|
2020-10-14 01:20:42 +00:00
|
|
|
return new TokenItem (
|
|
|
|
asset(token.symbol),
|
2020-10-23 04:36:39 +00:00
|
|
|
tokenType(token.symbol),
|
2020-10-14 01:20:42 +00:00
|
|
|
token.symbol,
|
|
|
|
token.name,
|
2020-10-23 04:36:39 +00:00
|
|
|
token.original_symbol,
|
2020-10-14 01:20:42 +00:00
|
|
|
decimals,
|
|
|
|
logoURI(token.symbol),
|
|
|
|
pairsMap[token.symbol] || []
|
|
|
|
)
|
|
|
|
}).sort((n1,n2) => (n2.pairs || []).length - (n1.pairs || []).length);
|
|
|
|
}
|