[Internal] Tokenlists.json: Common save, type fixes (#5358)

* Move binance tokenlist generation to BinanceAction.

* Fix array types in tokenlists.ts

* Common writeToFile method, sort.

* Type fixes

* Revert tokenlist.json to master

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
This commit is contained in:
Adam R 2021-01-23 01:31:03 +01:00 committed by GitHub
parent 945dfbb8bd
commit 0c98779e0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 10 deletions

View File

@ -6,9 +6,9 @@ import * as chalk from 'chalk';
import * as config from "../config";
import { ActionInterface, CheckStepInterface } from "../generic/interface";
import { Binance } from "../generic/blockchains";
import { readDirSync, writeFileSync } from "../generic/filesystem";
import { readJsonFile, formatJson } from "../generic/json";
import { TokenItem, Pair, generateTokensList } from "../generic/tokenlists";
import { readDirSync } from "../generic/filesystem";
import { readJsonFile } from "../generic/json";
import { TokenItem, Pair, generateTokensList, writeToFile } from "../generic/tokenlists";
import {
getChainAssetLogoPath,
getChainAssetsPath,
@ -140,8 +140,7 @@ export class BinanceAction implements ActionInterface {
// 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.`);
writeToFile(getChainTokenlistPath(Binance), generateTokensList("BNB", list));
}
}
@ -152,7 +151,7 @@ class BinanceMarket {
tick_size: string
}
async function generateBinanceTokensList(): Promise<[TokenItem]> {
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);
@ -203,8 +202,8 @@ async function generateBinanceTokensList(): Promise<[TokenItem]> {
}
return TokenType.BEP2
}
const list = <[string]>Array.from(pairsList.values())
return <[TokenItem]>list.map(item => {
const list = <string[]>Array.from(pairsList.values())
return <TokenItem[]>list.map(item => {
const token = tokensMap[item]
return new TokenItem (
asset(token.symbol),
@ -216,5 +215,5 @@ async function generateBinanceTokensList(): Promise<[TokenItem]> {
logoURI(token.symbol),
pairsMap[token.symbol] || []
)
}).sort((n1,n2) => (n2.pairs || []).length - (n1.pairs || []).length);
});
}

View File

@ -1,3 +1,5 @@
import { writeJsonFile } from "../generic/json";
class Version {
major: number
minor: number
@ -66,7 +68,23 @@ export function generateTokensList(titleCoin: string, tokens: TokenItem[]): List
`Trust Wallet: ${titleCoin}`,
"https://trustwallet.com/assets/images/favicon.png",
"2020-10-03T12:37:57.000+00:00",
tokens,
tokens.sort((t1,t2) => {
const t1pairs = (t1.pairs || []).length;
const t2pairs = (t2.pairs || []).length;
if (t1pairs != t2pairs) { return t2pairs - t1pairs; }
return t1.address.localeCompare(t2.address);
}),
new Version(0, 1, 0)
)
}
function totalPairs(list: List): number {
let c = 0;
list.tokens.forEach(t => c += (t.pairs || []).length);
return c;
}
export function writeToFile(filename: string, list: List): void {
writeJsonFile(filename, list);
console.log(`Tokenlist: list with ${list.tokens.length} tokens and ${totalPairs(list)} pairs written to ${filename}.`);
}