trustwallet-assets/script/generic/eth-forks.ts
Adam R 3429636c87
[internal] Assets checks: Info.json check for every chain (was for eth forks only). (#4524)
* Assets checks: Info.json check for every chain (was for eth forks only).

* Rename.

* Minor refinement.

* Add one missing TRC20 token explorer.

* Add check for explorerUrl, incl. content.

* Rename tronscan.org to tronscan.io

* Revert "Rename tronscan.org to tronscan.io"

This reverts commit 4de796d7825a7a04a06204040bed7298418aaf33.

* Revert "Add check for explorerUrl, incl. content."

This reverts commit fedcb8b3e611234da07241d0d55d198e38a1b1d0.

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2020-10-20 00:51:47 +02:00

108 lines
4.2 KiB
TypeScript

import { ethForkChains } from "../generic/blockchains";
import {
getChainAssetsPath,
getChainAssetsList,
getChainAssetPath,
getChainAssetInfoPath,
getChainAssetFilesList,
isChainAssetInfoExistSync,
logoName,
logoExtension,
logoFullName,
getChainAssetLogoPath
} from "../generic/repo-structure";
import { formatJsonFile } from "../generic/json";
import {
getFileName,
getFileExt,
gitMove,
readDirSync,
isPathExistsSync,
} from "../generic/filesystem";
import { toChecksum } from "../generic/eth-address";
import { ActionInterface, CheckStepInterface } from "../generic/interface";
import * as bluebird from "bluebird";
async function formatInfos() {
console.log(`Formatting info files...`);
await bluebird.each(ethForkChains, async (chain) => {
let count = 0;
const chainAssets = getChainAssetsList(chain);
await bluebird.each(chainAssets, async (address) => {
if (isChainAssetInfoExistSync(chain, address)) {
const chainAssetInfoPath = getChainAssetInfoPath(chain, address);
formatJsonFile(chainAssetInfoPath);
++count;
}
})
console.log(`Formatted ${count} info files for chain ${chain} (total ${chainAssets.length})`);
})
}
function checkAddressChecksum(assetsFolderPath: string, address: string, chain: string) {
const checksumAddress = toChecksum(address, chain);
if (checksumAddress !== address) {
gitMove(assetsFolderPath, address, checksumAddress);
console.log(`Renamed to checksum format ${checksumAddress}`);
}
}
async function checkAddressChecksums() {
console.log(`Checking for checksum formats ...`);
await bluebird.each(ethForkChains, async (chain) => {
const assetsPath = getChainAssetsPath(chain);
await bluebird.each(readDirSync(assetsPath), async (address) => {
await bluebird.each(getChainAssetFilesList(chain, address), async (file) => {
if (getFileName(file) == logoName && getFileExt(file) !== logoExtension) {
console.log(`Renaming incorrect asset logo extension ${file} ...`);
gitMove(getChainAssetPath(chain, address), file, logoFullName);
}
});
checkAddressChecksum(assetsPath, address, chain);
});
});
}
export class EthForks implements ActionInterface {
getName(): string { return "Ethereum forks"; }
getSanityChecks(): CheckStepInterface[] {
const steps: CheckStepInterface[] = [];
ethForkChains.forEach(chain => {
steps.push(
{
getName: () => { return `Folder structure for chain ${chain} (ethereum fork)`;},
check: async () => {
const errors: string[] = [];
const assetsFolder = getChainAssetsPath(chain);
const assetsList = getChainAssetsList(chain);
console.log(` Found ${assetsList.length} assets for chain ${chain}`);
await bluebird.each(assetsList, async (address) => {
const assetPath = `${assetsFolder}/${address}`;
if (!isPathExistsSync(assetPath)) {
errors.push(`Expect directory at path: ${assetPath}`);
}
const inChecksum = toChecksum(address, chain);
if (address !== inChecksum) {
errors.push(`Expect asset at path ${assetPath} in checksum: '${inChecksum}'`);
}
const assetLogoPath = getChainAssetLogoPath(chain, address);
if (!isPathExistsSync(assetLogoPath)) {
errors.push(`Missing file at path '${assetLogoPath}'`);
}
});
return [errors, []];
}
}
);
});
return steps;
}
async sanityFix(): Promise<void> {
await formatInfos();
await checkAddressChecksums();
}
}