Fix cmc script.ts (#2736)

This commit is contained in:
Nick Kozlov 2020-07-14 14:40:42 +03:00 committed by GitHub
parent ce34e54b4f
commit 3f1529273d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,10 +5,11 @@ const chalk = require('chalk')
const fs = require("fs") const fs = require("fs")
const path = require('path') const path = require('path')
const constants = require('bip44-constants') const constants = require('bip44-constants')
import { import {
readFileSync, readFileSync,
getChainAssetLogoPath, getChainAssetLogoPath,
isPathExistsSync, isPathExistsSync,
getChainName,
makeDirSync, makeDirSync,
getChainAssetPath, getChainAssetPath,
ethSidechains, ethSidechains,
@ -16,6 +17,7 @@ import {
getChainWhitelist, getChainWhitelist,
} from "../../src/test/helpers"; } from "../../src/test/helpers";
import { TickerType, mapTiker, PlatformType } from "../../src/test/models"; import { TickerType, mapTiker, PlatformType } from "../../src/test/models";
import { CoinType } from "@trustwallet/types";
// Steps required to run this: // Steps required to run this:
// 1. (Optional) CMC API key already setup, use yours if needed. Install script deps "npm i" if hasn't been run before. // 1. (Optional) CMC API key already setup, use yours if needed. Install script deps "npm i" if hasn't been run before.
@ -54,6 +56,7 @@ const custom: mapTiker[] = [
] ]
const allContracts: mapTiker[] = [] // Temp storage for mapped assets const allContracts: mapTiker[] = [] // Temp storage for mapped assets
let bip44Constants = {}
let bnbOwnerToSymbol = {} // e.g: bnb1tawge8u97slduhhtumm03l4xl4c46dwv5m9yzk: WISH-2D5 let bnbOwnerToSymbol = {} // e.g: bnb1tawge8u97slduhhtumm03l4xl4c46dwv5m9yzk: WISH-2D5
let bnbOriginalSymbolToSymbol = {} // e.g: WISH: WISH-2D5 let bnbOriginalSymbolToSymbol = {} // e.g: WISH: WISH-2D5
@ -65,7 +68,7 @@ async function run() {
// setBIP44Constants() // setBIP44Constants()
log(`Found ${totalCrypto} on CMC`, chalk.yellowBright) log(`Found ${totalCrypto} on CMC`, chalk.yellowBright)
await BluebirbPromise.mapSeries(coins, processCoin) await BluebirbPromise.mapSeries(coins, processCoin)
addCustom() addCustom()
printContracts() printContracts()
} catch (error) { } catch (error) {
@ -78,97 +81,98 @@ async function processCoin(coin) {
const platformType: PlatformType = platform == null ? "" : platform.name const platformType: PlatformType = platform == null ? "" : platform.name
log(`${symbol}:${platformType}`) log(`${symbol}:${platformType}`)
// await BluebirbPromise.mapSeries(types, async type => { // await BluebirbPromise.mapSeries(types, async type => {
switch (platformType) { switch (platformType) {
case PlatformType.Ethereum: case PlatformType.Ethereum:
// log(`Ticker ${name}(${symbol}) is a token with address ${address} and CMC id ${id}`) // log(`Ticker ${name}(${symbol}) is a token with address ${address} and CMC id ${id}`)
if (platform.token_address) { if (platform.token_address) {
try { try {
const checksum = toChecksum(platform.token_address) const checksum = toChecksum(platform.token_address)
if (!isAddressInBlackList("ethereum", checksum)) { if (!isAddressInBlackList("ethereum", checksum)) {
log(`Added ${checksum}`) log(`Added ${checksum}`)
addToContractsList({ addToContractsList({
coin: 60, coin: 60,
type: typeToken, type: typeToken,
token_id: checksum, token_id: checksum,
id id
}) })
}
} catch (error) {
console.log(`Etheruem platform error`, error)
break
} }
// await getImageIfMissing(getChainName(CoinType.ethereum), checksum, id)
} catch (error) {
console.log(`Etheruem platform error`, error)
break
} }
}
break
case PlatformType.Binance:
if (symbol === "BNB") {
break break
case PlatformType.Binance: }
if (symbol === "BNB") { const ownerAddress = platform.token_address.trim()
break log(`Symbol ${symbol}:${ownerAddress}:${id}`)
} if (ownerAddress && (ownerAddress in bnbOwnerToSymbol)) {
const ownerAddress = platform.token_address.trim() log(`Added ${bnbOwnerToSymbol[ownerAddress]}`)
log(`Symbol ${symbol}:${ownerAddress}:${id}`) addToContractsList({
if (ownerAddress && (ownerAddress in bnbOwnerToSymbol)) { coin: 714,
log(`Added ${bnbOwnerToSymbol[ownerAddress]}`) type: typeToken,
addToContractsList({ token_id: bnbOwnerToSymbol[ownerAddress],
coin: 714, id
type: typeToken, })
token_id: bnbOwnerToSymbol[ownerAddress],
id
})
break
}
if (symbol in bnbOriginalSymbolToSymbol) {
log(`Added Binance ${bnbOriginalSymbolToSymbol[symbol]}`)
addToContractsList({
coin: 714,
type: typeToken,
token_id: bnbOriginalSymbolToSymbol[symbol].trim(),
id
})
break
}
break break
case PlatformType.TRON: }
if (symbol === "TRX") {
break
}
const tokenAddr = platform.token_address.trim()
log(`tron: ${tokenAddr}`)
addToContractsList({
coin: 195,
type: typeToken,
token_id: tokenAddr,
id
})
break
// case PlatformType.VeChain:
// if (symbol === "VET") {
// break
// }
// const addr = platform.token_address.trim() if (symbol in bnbOriginalSymbolToSymbol) {
// log(`vechain: ${tokenAddr}`) log(`Added Binance ${bnbOriginalSymbolToSymbol[symbol]}`)
// addToContractsList({ addToContractsList({
// coin: 0, coin: 714,
// type: typeCoin, type: typeToken,
// token_id: addr, token_id: bnbOriginalSymbolToSymbol[symbol].trim(),
// id id
// }) })
// break
default:
const coinIndex = getSlip44Index(symbol, name)
if (coinIndex >= 0) {
log(`Ticker ${name}(${symbol}) is a coin with id ${coinIndex}`)
addToContractsList({
coin: coinIndex,
type: typeCoin,
id
})
} else {
log(`Coin ${coinIndex} ${name}(${symbol}) not listed in slip44`)
}
break break
} }
break
case PlatformType.TRON:
if (symbol === "TRX") {
break
}
const tokenAddr = platform.token_address.trim()
log(`tron: ${tokenAddr}`)
addToContractsList({
coin: 195,
type: typeToken,
token_id: tokenAddr,
id
})
break
// case PlatformType.VeChain:
// if (symbol === "VET") {
// break
// }
// const addr = platform.token_address.trim()
// log(`vechain: ${tokenAddr}`)
// addToContractsList({
// coin: 0,
// type: typeCoin,
// token_id: addr,
// id
// })
// break
default:
const coinIndex = getSlip44Index(symbol, name)
if (coinIndex >= 0) {
log(`Ticker ${name}(${symbol}) is a coin with id ${coinIndex}`)
addToContractsList({
coin: coinIndex,
type: typeCoin,
id
})
} else {
log(`Coin ${coinIndex} ${name}(${symbol}) not listed in slip44`)
}
break
}
// }) // })
} }
@ -181,7 +185,7 @@ async function mapChainsAssetsLists() {
ethSidechains.forEach(chain => { ethSidechains.forEach(chain => {
Object.assign(mappedChainsWhitelistAssets, {[chain]: {}}) Object.assign(mappedChainsWhitelistAssets, {[chain]: {}})
Object.assign(mappedChainsBlacklistAssets, {[chain]: {}}) Object.assign(mappedChainsBlacklistAssets, {[chain]: {}})
getChainWhitelist(chain).forEach(addr => { getChainWhitelist(chain).forEach(addr => {
Object.assign(mappedChainsWhitelistAssets[chain], {[addr]: ""}) Object.assign(mappedChainsWhitelistAssets[chain], {[addr]: ""})
}) })
@ -241,12 +245,12 @@ async function getImageIfMissing(chain: string, address: string, id: string) {
const logoPath = getChainAssetLogoPath(chain, String(address)) const logoPath = getChainAssetLogoPath(chain, String(address))
if (!isPathExistsSync(logoPath) && !isAddressInBlackList(chain, address)) { if (!isPathExistsSync(logoPath) && !isAddressInBlackList(chain, address)) {
const imageStream = await fetchImage(getImageURL(id)) const imageStream = await fetchImage(getImageURL(id))
if (imageStream) { if (imageStream) {
const logoFolderPath = getChainAssetPath(chain, address) const logoFolderPath = getChainAssetPath(chain, address)
if(!isPathExistsSync(logoFolderPath)) { if(!isPathExistsSync(logoFolderPath)) {
makeDirSync(logoFolderPath) makeDirSync(logoFolderPath)
} }
imageStream.pipe(fs.createWriteStream(logoPath)) imageStream.pipe(fs.createWriteStream(logoPath))
log(`Image saved to: ${logoPath}`, chalk.green) log(`Image saved to: ${logoPath}`, chalk.green)
} }
@ -286,7 +290,7 @@ function exit(code?: number) {
function getTotalActiveCryptocurrencies() { function getTotalActiveCryptocurrencies() {
return axios.get(`${CMC_LATEST_BASE_URL}CMC_PRO_API_KEY=${CMC_PRO_API_KEY}`).then((res) => res.data.data.active_cryptocurrencies).catch(e => { return axios.get(`${CMC_LATEST_BASE_URL}CMC_PRO_API_KEY=${CMC_PRO_API_KEY}`).then((res) => res.data.data.active_cryptocurrencies).catch(e => {
throw `Error getTotalActiveCryptocurrencies ${e.message}` throw `Error getTotalActiveCryptocurrencies ${e.message}`
}) })
} }
@ -301,7 +305,7 @@ async function setBinanceTokens () {
acm[token.original_symbol] = token.symbol acm[token.original_symbol] = token.symbol
return acm return acm
}, {}) }, {})
}).catch(error => {throw Error(`Error fetching Binance markets : ${error.message}`)}) }).catch(error => {throw Error(`Error fetching Binance markets : ${error.message}`)})
} }
function readBEP2() { function readBEP2() {
@ -312,8 +316,8 @@ function readBEP2() {
} }
async function getTickers() { async function getTickers() {
const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?&limit=3500&CMC_PRO_API_KEY=${CMC_PRO_API_KEY}` const url = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?&limit=3500&CMC_PRO_API_KEY=${CMC_PRO_API_KEY}`
return axios.get(url).then(res => res.data.data).catch(e => {throw `Error getTickers ${e.message}`}) return axios.get(url).then(res => res.data.data).catch(e => {throw `Error getTickers ${e.message}`})
} }
function log(string, cb?) { function log(string, cb?) {