2020-07-29 13:42:51 +00:00
|
|
|
import axios from "axios";
|
|
|
|
import * as bluebird from "bluebird";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as chalk from 'chalk';
|
2020-08-19 20:59:05 +00:00
|
|
|
import * as config from "../config";
|
2020-09-23 13:47:24 +00:00
|
|
|
import { ActionInterface, CheckStepInterface } from "../generic/interface";
|
|
|
|
import { Binance } from "../generic/blockchains";
|
2021-01-23 00:31:03 +00:00
|
|
|
import { readDirSync } from "../generic/filesystem";
|
2021-05-17 15:02:45 +00:00
|
|
|
import { readJsonFile, writeJsonFile } from "../generic/json";
|
2021-01-29 06:45:43 +00:00
|
|
|
import { TokenItem, Pair, createTokensList, writeToFileWithUpdate } from "../generic/tokenlists";
|
2020-07-29 13:42:51 +00:00
|
|
|
import {
|
|
|
|
getChainAssetLogoPath,
|
2021-01-23 00:04:56 +00:00
|
|
|
getChainAssetsPath,
|
|
|
|
getChainDenylistPath,
|
2021-05-17 15:02:45 +00:00
|
|
|
getChainAssetInfoPath,
|
2021-01-23 00:04:56 +00:00
|
|
|
getChainTokenlistPath
|
2020-09-23 13:47:24 +00:00
|
|
|
} from "../generic/repo-structure";
|
2021-01-23 00:04:56 +00:00
|
|
|
import { CoinType } from "@trustwallet/wallet-core";
|
|
|
|
import { toSatoshis } from "../generic/numbers";
|
2021-01-25 14:23:03 +00:00
|
|
|
import { assetIdSymbol, logoURI, tokenType } from "../generic/asset";
|
2021-01-23 00:04:56 +00:00
|
|
|
import { TokenType } from "../generic/tokentype";
|
2021-05-17 15:02:45 +00:00
|
|
|
import { explorerUrl } from "../generic/asset-infos";
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2020-09-18 14:39:31 +00:00
|
|
|
const binanceChain = "binance";
|
2020-08-19 20:59:05 +00:00
|
|
|
const binanceUrlTokenAssets = config.binanceUrlTokenAssets;
|
2020-09-18 14:39:31 +00:00
|
|
|
let cachedAssets = [];
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2021-05-17 15:02:45 +00:00
|
|
|
export class BinanceTokenInfo {
|
|
|
|
asset: string
|
|
|
|
name: string
|
|
|
|
assetImg: string
|
|
|
|
mappedAsset: string
|
|
|
|
decimals: number
|
|
|
|
}
|
|
|
|
|
|
|
|
async function retrieveBep2AssetList(): Promise<BinanceTokenInfo[]> {
|
2020-10-14 01:20:42 +00:00
|
|
|
console.log(`Retrieving token asset infos from: ${binanceUrlTokenAssets}`);
|
2021-05-15 03:41:13 +00:00
|
|
|
const { assetInfoList } = await axios.get(binanceUrlTokenAssets)
|
|
|
|
.then(r => r.data)
|
|
|
|
.catch(function (error) {
|
|
|
|
console.log(JSON.stringify(error))
|
|
|
|
});
|
2020-10-14 01:20:42 +00:00
|
|
|
console.log(`Retrieved ${assetInfoList.length} token asset infos`);
|
2020-07-29 13:42:51 +00:00
|
|
|
return assetInfoList
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:39:31 +00:00
|
|
|
async function retrieveAssets(): Promise<unknown[]> {
|
2020-08-06 19:36:42 +00:00
|
|
|
// cache results because of rate limit, used more than once
|
|
|
|
if (cachedAssets.length == 0) {
|
2020-10-14 01:20:42 +00:00
|
|
|
console.log(`Retrieving token infos`);
|
|
|
|
const bep2assets = await axios.get(`${config.binanceDexURL}/v1/tokens?limit=1000`);
|
|
|
|
const bep8assets = await axios.get(`${config.binanceDexURL}/v1/mini/tokens?limit=1000`);
|
2020-08-06 19:36:42 +00:00
|
|
|
cachedAssets = bep2assets.data.concat(bep8assets.data);
|
|
|
|
}
|
2020-10-14 01:20:42 +00:00
|
|
|
console.log(`Using ${cachedAssets.length} assets`);
|
2020-08-06 19:36:42 +00:00
|
|
|
return cachedAssets;
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
export async function retrieveAssetSymbols(): Promise<string[]> {
|
2020-08-06 19:36:42 +00:00
|
|
|
const assets = await retrieveAssets();
|
|
|
|
const symbols = assets.map(({ symbol }) => symbol);
|
2020-08-06 19:17:38 +00:00
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:02:45 +00:00
|
|
|
async function fetchImage(url) {
|
2020-07-29 13:42:51 +00:00
|
|
|
return axios.get(url, { responseType: "stream" })
|
|
|
|
.then(r => r.data)
|
|
|
|
.catch(err => {
|
|
|
|
throw `Error fetchImage: ${url} ${err.message}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return: array with images to fetch; {asset, assetImg}
|
2021-05-17 15:02:45 +00:00
|
|
|
export function findImagesToFetch(assetInfoList: BinanceTokenInfo[], denylist: string[]): BinanceTokenInfo[] {
|
|
|
|
const toFetch: BinanceTokenInfo[] = [];
|
2020-07-29 13:42:51 +00:00
|
|
|
console.log(`Checking for asset images to be fetched`);
|
2021-05-17 15:02:45 +00:00
|
|
|
assetInfoList.forEach((tokenInfo) => {
|
|
|
|
process.stdout.write(`.${tokenInfo.asset} `);
|
|
|
|
if (tokenInfo.assetImg) {
|
|
|
|
if (denylist.indexOf(tokenInfo.asset) != -1) {
|
2020-07-29 13:42:51 +00:00
|
|
|
console.log();
|
2021-05-17 15:02:45 +00:00
|
|
|
console.log(`${tokenInfo.asset} is denylisted`);
|
2020-07-29 13:42:51 +00:00
|
|
|
} else {
|
2021-05-17 15:02:45 +00:00
|
|
|
const imagePath = getChainAssetLogoPath(binanceChain, tokenInfo.asset);
|
2020-07-29 13:42:51 +00:00
|
|
|
if (!fs.existsSync(imagePath)) {
|
2021-05-17 15:02:45 +00:00
|
|
|
console.log(chalk.red(`Missing image: ${tokenInfo.asset}`));
|
|
|
|
toFetch.push(tokenInfo);
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log();
|
|
|
|
console.log(`${toFetch.length} asset image(s) to be fetched`);
|
|
|
|
return toFetch;
|
|
|
|
}
|
|
|
|
|
2021-05-17 15:02:45 +00:00
|
|
|
async function createInfoJson(tokenInfo: BinanceTokenInfo): Promise<void> {
|
|
|
|
//console.log(tokenInfo);
|
|
|
|
const info = {
|
|
|
|
name: tokenInfo.name,
|
|
|
|
type: "BEP2",
|
|
|
|
symbol: tokenInfo.mappedAsset,
|
|
|
|
decimals: tokenInfo.decimals,
|
|
|
|
website: '',
|
|
|
|
description: '-',
|
|
|
|
explorer: explorerUrl(binanceChain, tokenInfo.asset),
|
|
|
|
status: 'active',
|
|
|
|
id: tokenInfo.asset
|
|
|
|
};
|
|
|
|
const infoPath = getChainAssetInfoPath(binanceChain, tokenInfo.asset);
|
|
|
|
writeJsonFile(infoPath, info);
|
|
|
|
}
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2021-05-17 15:02:45 +00:00
|
|
|
async function fetchMissingImages(toFetch: BinanceTokenInfo[]): Promise<string[]> {
|
2020-07-29 13:42:51 +00:00
|
|
|
console.log(`Attempting to fetch ${toFetch.length} asset image(s)`);
|
2020-09-18 14:39:31 +00:00
|
|
|
const fetchedAssets: string[] = [];
|
2021-05-17 15:02:45 +00:00
|
|
|
await bluebird.each(toFetch, async (tokenInfo) => {
|
|
|
|
if (tokenInfo && tokenInfo.asset && tokenInfo.assetImg) {
|
|
|
|
const imagePath = getChainAssetLogoPath(binanceChain, tokenInfo.asset);
|
2020-07-29 13:42:51 +00:00
|
|
|
fs.mkdir(path.dirname(imagePath), err => {
|
|
|
|
if (err && err.code != `EEXIST`) throw err;
|
|
|
|
});
|
2021-05-17 15:02:45 +00:00
|
|
|
const buffer = await fetchImage(tokenInfo.assetImg);
|
|
|
|
await buffer.pipe(fs.createWriteStream(imagePath));
|
|
|
|
await createInfoJson(tokenInfo);
|
|
|
|
fetchedAssets.push(tokenInfo.asset)
|
|
|
|
console.log(`Token ${tokenInfo.asset} ${tokenInfo.mappedAsset}: Fetched image, created info.json (${tokenInfo.assetImg})`)
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log();
|
|
|
|
return fetchedAssets;
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
export class BinanceAction implements ActionInterface {
|
|
|
|
getName(): string { return "Binance chain"; }
|
|
|
|
|
2020-08-10 08:56:41 +00:00
|
|
|
getSanityChecks(): CheckStepInterface[] {
|
2020-08-06 19:17:38 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
getName: () => { return "Binance chain; assets must exist on chain"},
|
|
|
|
check: async () => {
|
2020-09-18 14:39:31 +00:00
|
|
|
const errors = [];
|
2020-08-06 19:17:38 +00:00
|
|
|
const tokenSymbols = await retrieveAssetSymbols();
|
|
|
|
const assets = readDirSync(getChainAssetsPath(Binance));
|
|
|
|
assets.forEach(asset => {
|
|
|
|
if (!(tokenSymbols.indexOf(asset) >= 0)) {
|
2020-09-16 12:52:10 +00:00
|
|
|
errors.push(`Asset ${asset} missing on chain`);
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log(` ${assets.length} assets checked.`);
|
2020-09-16 12:52:10 +00:00
|
|
|
return [errors, []];
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-02-01 15:45:55 +00:00
|
|
|
async updateAuto(): Promise<void> {
|
2020-08-06 19:17:38 +00:00
|
|
|
// retrieve missing token images; BEP2 (bep8 not supported)
|
|
|
|
const bep2InfoList = await retrieveBep2AssetList();
|
2021-05-15 03:41:13 +00:00
|
|
|
if (bep2InfoList.length < 5) {
|
|
|
|
console.log(`ERROR: No Binance token info is returned! ${bep2InfoList.length}`);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 14:39:31 +00:00
|
|
|
const denylist: string[] = readJsonFile(getChainDenylistPath(binanceChain)) as string[];
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2020-08-18 06:50:32 +00:00
|
|
|
const toFetch = findImagesToFetch(bep2InfoList, denylist);
|
2020-08-06 19:17:38 +00:00
|
|
|
const fetchedAssets = await fetchMissingImages(toFetch);
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
if (fetchedAssets.length > 0) {
|
|
|
|
console.log(`Fetched ${fetchedAssets.length} asset(s):`);
|
|
|
|
fetchedAssets.forEach(asset => console.log(` ${asset}`));
|
|
|
|
}
|
2021-01-23 00:04:56 +00:00
|
|
|
|
|
|
|
// binance chain list
|
2021-01-25 14:23:03 +00:00
|
|
|
const tokenList = await generateBinanceTokensList();
|
2021-05-15 03:41:13 +00:00
|
|
|
if (tokenList.length < 5) {
|
|
|
|
console.log(`ERROR: no token pair info available from Binance DEX! ${tokenList.length}`);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-29 06:45:43 +00:00
|
|
|
const list = createTokensList("BNB", tokenList,
|
2021-01-25 14:23:03 +00:00
|
|
|
"2020-10-03T12:37:57.000+00:00", // use constants here to prevent changing time every time
|
|
|
|
0, 1, 0);
|
2021-05-07 16:00:07 +00:00
|
|
|
if (tokenList.length > 0) {
|
|
|
|
writeToFileWithUpdate(getChainTokenlistPath(Binance), list);
|
|
|
|
}
|
2021-01-23 00:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BinanceMarket {
|
|
|
|
base_asset_symbol: string
|
|
|
|
quote_asset_symbol: string
|
|
|
|
lot_size: string
|
|
|
|
tick_size: string
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:31:03 +00:00
|
|
|
async function generateBinanceTokensList(): Promise<TokenItem[]> {
|
2021-01-23 00:04:56 +00:00
|
|
|
const decimals = CoinType.decimals(CoinType.binance)
|
|
|
|
const BNBSymbol = CoinType.symbol(CoinType.binance)
|
2021-05-15 03:41:13 +00:00
|
|
|
const markets: BinanceMarket[] = await axios.get(`${config.binanceDexURL}/v1/markets?limit=10000`)
|
|
|
|
.then(r => r.data)
|
|
|
|
.catch(function (error) {
|
|
|
|
console.log(JSON.stringify(error))
|
|
|
|
});
|
|
|
|
if (markets.length < 5) {
|
|
|
|
console.log(`ERROR: No markets info is returned from Binance DEX! ${markets.length}`);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const tokens = await axios.get(`${config.binanceDexURL}/v1/tokens?limit=10000`)
|
|
|
|
.then(r => r.data)
|
|
|
|
.catch(function (error) {
|
|
|
|
console.log(JSON.stringify(error))
|
|
|
|
});
|
|
|
|
if (tokens.length < 5) {
|
|
|
|
console.log(`ERROR: No tokens info is returned from Binance DEX! ${tokens.length}`);
|
|
|
|
return [];
|
|
|
|
}
|
2021-01-23 00:04:56 +00:00
|
|
|
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(
|
2021-01-25 14:23:03 +00:00
|
|
|
assetIdSymbol(market.base_asset_symbol, BNBSymbol, CoinType.binance),
|
2021-01-23 00:04:56 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2021-01-23 00:31:03 +00:00
|
|
|
const list = <string[]>Array.from(pairsList.values())
|
|
|
|
return <TokenItem[]>list.map(item => {
|
2021-01-23 00:04:56 +00:00
|
|
|
const token = tokensMap[item]
|
|
|
|
return new TokenItem (
|
2021-01-25 14:23:03 +00:00
|
|
|
assetIdSymbol(token.symbol, BNBSymbol, CoinType.binance),
|
|
|
|
tokenType(token.symbol, BNBSymbol, TokenType.BEP2),
|
2021-01-23 00:04:56 +00:00
|
|
|
token.symbol,
|
|
|
|
token.name,
|
|
|
|
token.original_symbol,
|
|
|
|
decimals,
|
2021-01-25 14:23:03 +00:00
|
|
|
logoURI(token.symbol, 'binance', BNBSymbol),
|
2021-01-23 00:04:56 +00:00
|
|
|
pairsMap[token.symbol] || []
|
|
|
|
)
|
2021-01-23 00:31:03 +00:00
|
|
|
});
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|