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-08-06 19:17:38 +00:00
|
|
|
import { ActionInterface, CheckStepInterface } from "./interface";
|
|
|
|
import { getChainAssetsPath } from "../common/repo-structure";
|
|
|
|
import { Binance } from "../common/blockchains";
|
|
|
|
import { readDirSync } from "../common/filesystem";
|
2020-07-29 13:42:51 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
getChainAssetLogoPath,
|
2020-08-18 06:50:32 +00:00
|
|
|
getChainDenylistPath
|
2020-07-29 13:42:51 +00:00
|
|
|
} from "../common/repo-structure";
|
|
|
|
|
|
|
|
const binanceChain = "binance"
|
2020-08-19 20:59:05 +00:00
|
|
|
const binanceUrlTokens2 = config.binanceUrlTokens2;
|
|
|
|
const binanceUrlTokens8 = config.binanceUrlTokens8;
|
|
|
|
const binanceUrlTokenAssets = config.binanceUrlTokenAssets;
|
2020-08-06 19:36:42 +00:00
|
|
|
var cachedAssets = [];
|
2020-07-29 13:42:51 +00:00
|
|
|
|
2020-08-06 19:17:38 +00:00
|
|
|
async function retrieveBep2AssetList(): Promise<any[]> {
|
|
|
|
console.log(` Retrieving token asset infos from: ${binanceUrlTokenAssets}`);
|
|
|
|
const { assetInfoList } = await axios.get(binanceUrlTokenAssets).then(r => r.data);
|
|
|
|
console.log(` Retrieved ${assetInfoList.length} token asset infos`);
|
2020-07-29 13:42:51 +00:00
|
|
|
return assetInfoList
|
|
|
|
}
|
|
|
|
|
2020-08-06 19:36:42 +00:00
|
|
|
async function retrieveAssets(): Promise<any[]> {
|
|
|
|
// cache results because of rate limit, used more than once
|
|
|
|
if (cachedAssets.length == 0) {
|
|
|
|
console.log(` Retrieving token infos (${binanceUrlTokens2}, ${binanceUrlTokens8})`);
|
|
|
|
const bep2assets = await axios.get(binanceUrlTokens2);
|
|
|
|
const bep8assets = await axios.get(binanceUrlTokens8);
|
|
|
|
cachedAssets = bep2assets.data.concat(bep8assets.data);
|
|
|
|
}
|
|
|
|
console.log(` Using ${cachedAssets.length} assets`);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:42:51 +00:00
|
|
|
function fetchImage(url) {
|
|
|
|
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}
|
2020-08-18 06:50:32 +00:00
|
|
|
export function findImagesToFetch(assetInfoList: any, denylist: string[]): any[] {
|
2020-07-29 13:42:51 +00:00
|
|
|
let toFetch: any[] = [];
|
|
|
|
console.log(`Checking for asset images to be fetched`);
|
|
|
|
assetInfoList.forEach(({asset, assetImg}) => {
|
|
|
|
process.stdout.write(`.${asset} `);
|
|
|
|
if (assetImg) {
|
2020-08-18 06:50:32 +00:00
|
|
|
if (denylist.indexOf(asset) != -1) {
|
2020-07-29 13:42:51 +00:00
|
|
|
console.log();
|
2020-08-18 06:50:32 +00:00
|
|
|
console.log(`${asset} is denylisted`);
|
2020-07-29 13:42:51 +00:00
|
|
|
} else {
|
|
|
|
const imagePath = getChainAssetLogoPath(binanceChain, asset);
|
|
|
|
if (!fs.existsSync(imagePath)) {
|
|
|
|
console.log(chalk.red(`Missing image: ${asset}`));
|
|
|
|
toFetch.push({asset, assetImg});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log();
|
|
|
|
console.log(`${toFetch.length} asset image(s) to be fetched`);
|
|
|
|
return toFetch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function fetchMissingImages(toFetch: any[]): Promise<string[]> {
|
|
|
|
console.log(`Attempting to fetch ${toFetch.length} asset image(s)`);
|
|
|
|
let fetchedAssets: string[] = [];
|
|
|
|
await bluebird.each(toFetch, async ({ asset, assetImg }) => {
|
|
|
|
if (assetImg) {
|
|
|
|
const imagePath = getChainAssetLogoPath(binanceChain, asset);
|
|
|
|
fs.mkdir(path.dirname(imagePath), err => {
|
|
|
|
if (err && err.code != `EEXIST`) throw err;
|
|
|
|
});
|
|
|
|
await fetchImage(assetImg).then(buffer => {
|
|
|
|
buffer.pipe(fs.createWriteStream(imagePath));
|
|
|
|
fetchedAssets.push(asset)
|
|
|
|
console.log(`Fetched image ${asset} ${imagePath} from ${assetImg}`)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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 () => {
|
|
|
|
var error: string = "";
|
|
|
|
const tokenSymbols = await retrieveAssetSymbols();
|
|
|
|
const assets = readDirSync(getChainAssetsPath(Binance));
|
|
|
|
assets.forEach(asset => {
|
|
|
|
if (!(tokenSymbols.indexOf(asset) >= 0)) {
|
|
|
|
error += `Asset ${asset} missing on chain\n`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
console.log(` ${assets.length} assets checked.`);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2020-08-10 08:56:41 +00:00
|
|
|
|
|
|
|
getConsistencyChecks = null;
|
|
|
|
|
|
|
|
sanityFix = null;
|
|
|
|
|
|
|
|
consistencyFix = null;
|
2020-08-06 19:17:38 +00:00
|
|
|
|
|
|
|
async update(): Promise<void> {
|
|
|
|
// retrieve missing token images; BEP2 (bep8 not supported)
|
|
|
|
const bep2InfoList = await retrieveBep2AssetList();
|
2020-08-18 06:50:32 +00:00
|
|
|
const denylist: string[] = require(getChainDenylistPath(binanceChain));
|
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}`));
|
|
|
|
}
|
2020-07-29 13:42:51 +00:00
|
|
|
}
|
|
|
|
}
|