2020-10-19 22:51:47 +00:00
|
|
|
import {
|
|
|
|
allChains,
|
|
|
|
getChainAssetsList,
|
|
|
|
getChainAssetsPath,
|
|
|
|
getChainAssetInfoPath
|
|
|
|
} from "./repo-structure";
|
|
|
|
import {
|
|
|
|
readFileSync,
|
|
|
|
isPathExistsSync
|
|
|
|
} from "./filesystem";
|
|
|
|
import { arrayDiff } from "./types";
|
2020-11-05 22:51:08 +00:00
|
|
|
import { isValidJSON } from "../generic/json";
|
2020-10-19 22:51:47 +00:00
|
|
|
import { ActionInterface, CheckStepInterface } from "../generic/interface";
|
2020-12-07 10:56:44 +00:00
|
|
|
import { CoinType } from "@trustwallet/wallet-core";
|
2020-10-19 22:51:47 +00:00
|
|
|
import * as bluebird from "bluebird";
|
|
|
|
|
2021-02-04 16:47:14 +00:00
|
|
|
const requiredKeys = ["name", "type", "symbol", "decimals", "description", "website", "explorer", "id"];
|
2020-10-19 22:51:47 +00:00
|
|
|
|
2020-11-05 22:51:08 +00:00
|
|
|
function isAssetInfoHasAllKeys(info: unknown, path: string): [boolean, string] {
|
2020-10-19 22:51:47 +00:00
|
|
|
const infoKeys = Object.keys(info);
|
|
|
|
|
|
|
|
const hasAllKeys = requiredKeys.every(k => Object.prototype.hasOwnProperty.call(info, k));
|
|
|
|
|
2021-02-04 16:47:14 +00:00
|
|
|
return [hasAllKeys, `Info at path '${path}' missing next key(s): ${arrayDiff(requiredKeys, infoKeys)}`];
|
|
|
|
}
|
2020-10-19 22:51:47 +00:00
|
|
|
|
2021-02-04 16:47:14 +00:00
|
|
|
function isAssetInfoValid(info: unknown, path: string): [string, string] {
|
|
|
|
const isKeys1CorrectType =
|
|
|
|
typeof info['name'] === "string" && info['name'] !== "" &&
|
|
|
|
typeof info['type'] === "string" && info['type'] !== "" &&
|
|
|
|
typeof info['symbol'] === "string" && info['symbol'] !== "" &&
|
|
|
|
typeof info['decimals'] === "number" && //(info['description'] === "-" || info['decimals'] !== 0) &&
|
|
|
|
typeof info['id'] === "string" && info['id'] !== "";
|
|
|
|
if (!isKeys1CorrectType) {
|
|
|
|
return [`Check keys1 '${info['name']}' '${info['type']}' '${info['symbol']}' '${info['decimals']}' '${info['id']}' ${path}`, ""];
|
|
|
|
}
|
2020-10-19 22:51:47 +00:00
|
|
|
|
2021-02-04 16:47:14 +00:00
|
|
|
const isKeys2CorrectType =
|
|
|
|
typeof info['description'] === "string" && info['description'] !== "" &&
|
|
|
|
// website should be set (exception description='-' marks empty infos)
|
|
|
|
typeof info['website'] === "string" && (info['description'] === "-" || info['website'] !== "") &&
|
|
|
|
typeof info['explorer'] === "string" && info['explorer'] != "";
|
|
|
|
if (!isKeys2CorrectType) {
|
|
|
|
return [`Check keys2 '${info['description']}' '${info['website']}' '${info['explorer']}' ${path}`, ""];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info['description'].length > 500) {
|
|
|
|
const msg = `Description too long, ${info['description'].length}, ${path}`;
|
|
|
|
return ["", msg];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ["", ""];
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:12:30 +00:00
|
|
|
export function explorerUrl(chain: string, contract: string): string {
|
2020-10-19 23:22:00 +00:00
|
|
|
if (contract) {
|
|
|
|
switch (chain.toLowerCase()) {
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.ethereum).toLowerCase():
|
2020-10-19 23:22:00 +00:00
|
|
|
return `https://etherscan.io/token/${contract}`;
|
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.tron).toLowerCase():
|
2020-10-19 23:22:00 +00:00
|
|
|
if (contract.startsWith("10")) {
|
|
|
|
// trc10
|
|
|
|
return `https://tronscan.io/#/token/${contract}`;
|
|
|
|
}
|
|
|
|
// trc20
|
|
|
|
return `https://tronscan.io/#/token20/${contract}`;
|
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.binance).toLowerCase():
|
2020-10-19 23:22:00 +00:00
|
|
|
return `https://explorer.binance.org/asset/${contract}`;
|
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.smartchain).toLowerCase():
|
2021-02-04 16:47:14 +00:00
|
|
|
case "smartchain":
|
2020-10-19 23:22:00 +00:00
|
|
|
return `https://bscscan.com/token/${contract}`;
|
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.neo).toLowerCase():
|
2020-10-22 14:41:12 +00:00
|
|
|
return `https://neo.tokenview.com/en/token/0x${contract}`;
|
2020-10-19 23:22:00 +00:00
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.nuls).toLowerCase():
|
2020-10-19 23:22:00 +00:00
|
|
|
return `https://nulscan.io/token/info?contractAddress=${contract}`;
|
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.wanchain).toLowerCase():
|
2020-10-19 23:22:00 +00:00
|
|
|
return `https://www.wanscan.org/token/${contract}`;
|
2020-12-07 10:56:44 +00:00
|
|
|
|
2021-02-03 22:42:15 +00:00
|
|
|
case CoinType.name(CoinType.solana).toLowerCase():
|
2020-12-07 10:56:44 +00:00
|
|
|
return `https://explorer.solana.com/address/${contract}`;
|
2021-02-03 22:42:15 +00:00
|
|
|
|
|
|
|
case CoinType.name(CoinType.tomochain).toLowerCase():
|
|
|
|
return `https://scan.tomochain.com/address/${contract}`;
|
|
|
|
|
|
|
|
case CoinType.name(CoinType.kava).toLowerCase():
|
|
|
|
return "https://www.mintscan.io/kava";
|
|
|
|
|
|
|
|
case CoinType.name(CoinType.ontology).toLowerCase():
|
|
|
|
return "https://explorer.ont.io";
|
|
|
|
|
|
|
|
case CoinType.name(CoinType.gochain).toLowerCase():
|
|
|
|
return `https://explorer.gochain.io/addr/${contract}`;
|
2021-02-04 16:47:14 +00:00
|
|
|
|
|
|
|
case CoinType.name(CoinType.thundertoken).toLowerCase():
|
|
|
|
case "thundertoken":
|
|
|
|
return `https://scan.thundercore.com/`;
|
2020-10-19 23:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:41:12 +00:00
|
|
|
function explorerUrlAlternatives(chain: string, contract: string, name: string): string[] {
|
|
|
|
const altUrls: string[] = [];
|
|
|
|
if (name) {
|
|
|
|
const nameNorm = name.toLowerCase().replace(' ', '').replace(')', '').replace('(', '');
|
2020-12-07 10:56:44 +00:00
|
|
|
if (chain.toLowerCase() == CoinType.name(CoinType.ethereum)) {
|
2020-10-22 14:41:12 +00:00
|
|
|
altUrls.push(`https://etherscan.io/token/${nameNorm}`);
|
|
|
|
}
|
|
|
|
altUrls.push(`https://explorer.${nameNorm}.io`);
|
|
|
|
altUrls.push(`https://scan.${nameNorm}.io`);
|
|
|
|
}
|
|
|
|
return altUrls;
|
|
|
|
}
|
|
|
|
|
2020-10-19 23:22:00 +00:00
|
|
|
function isAssetInfoOK(chain: string, address: string, errors: string[], warnings: string[]): void {
|
2020-10-19 22:51:47 +00:00
|
|
|
const assetInfoPath = getChainAssetInfoPath(chain, address);
|
|
|
|
if (!isPathExistsSync(assetInfoPath)) {
|
2020-10-19 23:22:00 +00:00
|
|
|
// Info file doesn't exist, no need to check
|
|
|
|
return;
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidJSON(assetInfoPath)) {
|
|
|
|
console.log(`JSON at path: '${assetInfoPath}' is invalid`);
|
2020-10-19 23:22:00 +00:00
|
|
|
errors.push(`JSON at path: '${assetInfoPath}' is invalid`);
|
|
|
|
return;
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:22:00 +00:00
|
|
|
const info = JSON.parse(readFileSync(assetInfoPath));
|
2021-02-04 16:47:14 +00:00
|
|
|
const [hasAllKeys, msg1] = isAssetInfoHasAllKeys(info, assetInfoPath);
|
2020-10-19 22:51:47 +00:00
|
|
|
if (!hasAllKeys) {
|
2021-02-04 16:47:14 +00:00
|
|
|
console.log(msg1);
|
|
|
|
errors.push(msg1);
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 16:47:14 +00:00
|
|
|
const [err2, warn2] = isAssetInfoValid(info, assetInfoPath);
|
|
|
|
if (err2) {
|
|
|
|
errors.push(err2);
|
|
|
|
}
|
|
|
|
if (warn2) {
|
|
|
|
warnings.push(warn2);
|
2021-02-03 22:42:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 23:22:00 +00:00
|
|
|
const hasExplorer = Object.prototype.hasOwnProperty.call(info, 'explorer');
|
|
|
|
if (!hasExplorer) {
|
|
|
|
errors.push(`Missing explorer key`);
|
|
|
|
} else {
|
|
|
|
const explorerActual = info['explorer'];
|
2020-10-22 14:41:12 +00:00
|
|
|
const explorerActualLower = explorerActual.toLowerCase();
|
2020-10-19 23:22:00 +00:00
|
|
|
const explorerExpected = explorerUrl(chain, address);
|
2020-10-22 14:41:12 +00:00
|
|
|
if (explorerActualLower != explorerExpected.toLowerCase() && explorerExpected) {
|
|
|
|
// doesn't match, check for alternatives
|
|
|
|
const explorersAlt = explorerUrlAlternatives(chain, address, info['name']);
|
|
|
|
if (explorersAlt && explorersAlt.length > 0) {
|
|
|
|
let matchCount = 0;
|
|
|
|
explorersAlt.forEach(exp => { if (exp.toLowerCase() == explorerActualLower) { ++matchCount; }});
|
|
|
|
if (matchCount == 0) {
|
2020-10-27 15:09:28 +00:00
|
|
|
// none matches, this is warning/error
|
2020-12-07 10:56:44 +00:00
|
|
|
if (chain.toLowerCase() == CoinType.name(CoinType.ethereum) || chain.toLowerCase() == CoinType.name(CoinType.smartchain)) {
|
2020-10-27 15:09:28 +00:00
|
|
|
errors.push(`Incorrect explorer, ${explorerActual} instead of ${explorerExpected} (${explorersAlt.join(', ')})`);
|
|
|
|
} else {
|
|
|
|
warnings.push(`Unexpected explorer, ${explorerActual} instead of ${explorerExpected} (${explorersAlt.join(', ')})`);
|
|
|
|
}
|
2020-10-22 14:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 23:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AssetInfos implements ActionInterface {
|
|
|
|
getName(): string { return "Asset Infos"; }
|
|
|
|
|
|
|
|
getSanityChecks(): CheckStepInterface[] {
|
|
|
|
const steps: CheckStepInterface[] = [];
|
|
|
|
allChains.forEach(chain => {
|
|
|
|
// only if there is no assets subfolder
|
|
|
|
if (isPathExistsSync(getChainAssetsPath(chain))) {
|
|
|
|
steps.push(
|
|
|
|
{
|
|
|
|
getName: () => { return `Info.json's for chain ${chain}`;},
|
|
|
|
check: async () => {
|
|
|
|
const errors: string[] = [];
|
2020-10-19 23:22:00 +00:00
|
|
|
const warnings: string[] = [];
|
2020-10-19 22:51:47 +00:00
|
|
|
const assetsList = getChainAssetsList(chain);
|
|
|
|
//console.log(` Found ${assetsList.length} assets for chain ${chain}`);
|
|
|
|
await bluebird.each(assetsList, async (address) => {
|
2020-10-19 23:22:00 +00:00
|
|
|
isAssetInfoOK(chain, address, errors, warnings);
|
2020-10-19 22:51:47 +00:00
|
|
|
});
|
2020-10-19 23:22:00 +00:00
|
|
|
return [errors, warnings];
|
2020-10-19 22:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
}
|