2019-11-08 18:55:41 +00:00
import * as fs from "fs"
2020-01-14 23:02:14 +00:00
import * as path from "path"
2020-01-21 09:20:02 +00:00
import { ValidatorModel } from "./models" ;
2019-11-08 18:55:41 +00:00
const axios = require ( 'axios' )
const Web3 = require ( 'web3' )
const web3 = new Web3 ( 'ws://localhost:8546' ) ;
import { CoinTypeUtils , CoinType } from "@trustwallet/types" ;
2019-11-19 20:22:38 +00:00
const sizeOf = require ( "image-size" ) ;
2020-03-20 20:48:55 +00:00
const { execSync } = require ( 'child_process' ) ;
2019-11-08 18:55:41 +00:00
2020-02-04 08:54:44 +00:00
export const getChainName = ( id : CoinType ) : string = > CoinTypeUtils . id ( id ) // 60 => ethereum
2019-11-08 18:55:41 +00:00
export const Binance = getChainName ( CoinType . binance )
2020-01-04 23:20:58 +00:00
export const Classic = getChainName ( CoinType . classic )
2019-11-08 18:55:41 +00:00
export const Cosmos = getChainName ( CoinType . cosmos )
export const Ethereum = getChainName ( CoinType . ethereum )
2020-01-04 23:20:58 +00:00
export const GoChain = getChainName ( CoinType . gochain )
2019-11-08 18:55:41 +00:00
export const IoTeX = getChainName ( CoinType . iotex )
export const POA = getChainName ( CoinType . poa )
2020-01-04 23:20:58 +00:00
export const Tezos = getChainName ( CoinType . tezos )
export const ThunderCore = getChainName ( CoinType . thundertoken )
2020-01-11 04:24:29 +00:00
export const Terra = getChainName ( CoinType . terra )
2019-11-08 18:55:41 +00:00
export const TomoChain = getChainName ( CoinType . tomochain )
2020-01-04 23:20:58 +00:00
export const Tron = getChainName ( CoinType . tron )
2019-11-08 18:55:41 +00:00
export const Wanchain = getChainName ( CoinType . wanchain )
2020-01-04 23:20:58 +00:00
export const Waves = getChainName ( CoinType . waves )
2019-11-08 18:55:41 +00:00
2020-01-17 08:31:42 +00:00
export const ethSidechains = [ Ethereum , Classic , POA , TomoChain , GoChain , Wanchain , ThunderCore ]
2020-03-20 20:48:55 +00:00
export const logoName = ` logo `
export const infoName = ` info `
export const logoExtension = "png"
export const jsonExtension = "json"
const whiteList = ` whitelist. ${ jsonExtension } `
const blackList = ` blacklist. ${ jsonExtension } `
export const logo = ` ${ logoName } . ${ logoExtension } `
export const info = ` ${ infoName } . ${ jsonExtension } `
2019-11-08 18:55:41 +00:00
2020-01-14 23:02:14 +00:00
export const root = './'
2019-11-08 18:55:41 +00:00
export const chainsFolderPath = './blockchains'
2020-01-14 23:02:14 +00:00
export const pricingFolderPath = './pricing'
2020-01-16 21:10:59 +00:00
export const getChainLogoPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } /info/ ${ logo } `
2020-01-18 22:21:09 +00:00
export const getChainInfoPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } /info/ ${ info } `
2019-11-19 20:22:38 +00:00
export const getChainAssetsPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } /assets `
export const minLogoWidth = 64
export const minLogoHeight = 64
export const maxLogoWidth = 512
export const maxLogoHeight = 512
2019-11-08 18:55:41 +00:00
2020-02-04 08:54:44 +00:00
export const getChainAssetPath = ( chain : string , address : string ) = > ` ${ getChainAssetsPath ( chain ) } / ${ address } `
2020-01-16 21:10:59 +00:00
export const getChainAssetLogoPath = ( chain : string , address : string ) = > ` ${ getChainAssetsPath ( chain ) } / ${ address } / ${ logo } `
2020-03-20 20:48:55 +00:00
export const getChainAssetFilesList = ( chain : string , address : string ) = > readDirSync ( getChainAssetPath ( chain , address ) )
2020-01-16 21:10:59 +00:00
export const getChainValidatorsPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } /validators `
export const getChainValidatorsAssets = ( chain : string ) : string [ ] = > readDirSync ( getChainValidatorsAssetsPath ( chain ) )
2020-03-20 20:48:55 +00:00
export const getChainValidatorsListPath = ( chain : string ) : string = > ` ${ ( getChainValidatorsPath ( chain ) ) } /list. ${ jsonExtension } `
2020-01-16 21:10:59 +00:00
export const getChainValidatorsAssetsPath = ( chain : string ) : string = > ` ${ getChainValidatorsPath ( chain ) } /assets `
export const getChainValidatorAssetLogoPath = ( chain : string , asset : string ) : string = > ` ${ getChainValidatorsAssetsPath ( chain ) } / ${ asset } / ${ logo } `
export const getChainWhitelistPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } / ${ whiteList } `
export const getChainBlacklistPath = ( chain : string ) : string = > ` ${ chainsFolderPath } / ${ chain } / ${ blackList } `
2020-02-21 22:01:03 +00:00
export const getChainWhitelist = ( chain : string ) : string [ ] = > {
if ( isChainWhitelistExistSync ( chain ) ) {
return JSON . parse ( readFileSync ( getChainWhitelistPath ( chain ) ) )
}
return [ ]
}
export const getChainBlacklist = ( chain : string ) : string [ ] = > {
if ( isChainBlacklistExistSync ( chain ) ) {
return JSON . parse ( readFileSync ( getChainBlacklistPath ( chain ) ) )
}
return [ ]
}
2019-11-08 18:55:41 +00:00
2020-01-18 22:21:09 +00:00
export const readDirSync = ( path : string ) : string [ ] = > fs . readdirSync ( path )
2020-02-04 08:54:44 +00:00
export const makeDirSync = ( path : string ) = > fs . mkdirSync ( path )
2020-01-16 21:10:59 +00:00
export const isPathExistsSync = ( path : string ) : boolean = > fs . existsSync ( path )
export const isChainWhitelistExistSync = ( chain : string ) : boolean = > isPathExistsSync ( getChainWhitelistPath ( chain ) )
export const isChainBlacklistExistSync = ( chain : string ) : boolean = > isPathExistsSync ( getChainBlacklistPath ( chain ) )
2020-01-18 22:21:09 +00:00
export const isChainInfoExistSync = ( chain : string ) : boolean = > isPathExistsSync ( getChainInfoPath ( chain ) )
2020-01-16 21:10:59 +00:00
export const readFileSync = ( path : string ) = > fs . readFileSync ( path , 'utf8' )
export const writeFileSync = ( path : string , str : string ) = > fs . writeFileSync ( path , str )
2019-11-08 18:55:41 +00:00
2020-01-16 21:10:59 +00:00
export const isLowerCase = ( str : string ) : boolean = > str . toLowerCase ( ) === str
export const isUpperCase = ( str : string ) : boolean = > str . toUpperCase ( ) === str
export const isChecksum = ( address : string ) : boolean = > web3 . utils . checkAddressChecksum ( address )
export const toChecksum = ( address : string ) : string = > web3 . utils . toChecksumAddress ( address )
2019-11-08 18:55:41 +00:00
export const getBinanceBEP2Symbols = async ( ) = > axios . get ( ` https://dex-atlantic.binance.org/api/v1/tokens?limit=1000 ` ) . then ( res = > res . data . map ( ( { symbol } ) = > symbol ) )
2020-03-20 20:48:55 +00:00
export const getFileName = ( fileName : string ) : string = > path . basename ( fileName , path . extname ( fileName ) )
export const getFileExt = ( name : string ) : string = > name . slice ( ( Math . max ( 0 , name . lastIndexOf ( "." ) ) || Infinity ) + 1 )
2020-01-16 21:10:59 +00:00
export const isTRC10 = ( string : string ) : boolean = > ( /^\d+$/ . test ( string ) )
2019-11-08 18:55:41 +00:00
export const isTRC20 = address = > {
return address . length == 34 &&
address . startsWith ( "T" ) &&
isLowerCase ( address ) == false &&
isUpperCase ( address ) == false
}
2020-03-20 22:16:45 +00:00
export const isPathDir = ( path : string ) : boolean = > {
try {
return fs . lstatSync ( path ) . isDirectory ( )
} catch ( e ) {
console . log ( ` Path: ${ path } is not a directory with error: ${ e . message } ` )
return false
}
}
export const makeDirIfDoestExist = async ( dirPath : string , dirName : string ) = > {
const path = ` ${ dirPath } / ${ dirName } `
await fs . mkdir ( path , { recursive : true } , ( err ) = > {
if ( err ) {
console . error ( ` Error creating dir at path ${ path } with result ${ err } ` )
} else {
console . log ( ` Created direcotry at ${ path } ` )
}
} )
}
2019-11-08 18:55:41 +00:00
export const sortDesc = arr = > arr . sort ( ( a , b ) = > a - b )
export const getUnique = arr = > Array . from ( new Set ( arr ) )
export const mapList = arr = > {
return arr . reduce ( ( acm , val ) = > {
acm [ val ] = ""
return acm
} , { } )
}
2019-11-19 20:22:38 +00:00
export const getImageDimentions = ( path : string ) = > sizeOf ( path )
export const isLogoOK = ( path : string ) : [ boolean , string ] = > {
const { width , height } = getImageDimentions ( path )
if ( ( ( width >= minLogoWidth && width <= maxLogoWidth ) && ( height >= minLogoHeight && height <= maxLogoHeight ) ) ) {
return [ true , '' ]
} else {
return [ false , ` Image at path ${ path } must have dimensions: min: ${ minLogoWidth } x ${ minLogoHeight } and max: ${ maxLogoWidth } x ${ maxLogoHeight } insted ${ width } x ${ height } ` ]
}
2019-11-08 18:55:41 +00:00
}
2019-11-19 20:22:38 +00:00
export const calculateAspectRatioFit = ( srcWidth : number , srcHeight : number , maxWidth : number , maxHeight : number ) = > {
const ratio = Math . min ( maxWidth / srcWidth , maxHeight / srcHeight )
return { width : Math.round ( srcWidth * ratio ) , height : Math.round ( srcHeight * ratio ) }
}
2020-01-14 23:02:14 +00:00
export const findFiles = ( base : string , ext : string , files : string [ ] = [ ] , result : string [ ] = [ ] ) = > {
files = fs . readdirSync ( base ) || files
result = result || result
files . forEach (
function ( file ) {
var newbase = path . join ( base , file )
if ( fs . statSync ( newbase ) . isDirectory ( ) ) {
result = findFiles ( newbase , ext , fs . readdirSync ( newbase ) , result )
} else {
if ( file . substr ( - 1 * ( ext . length + 1 ) ) == '.' + ext ) {
result . push ( newbase )
}
}
}
)
return result
}
export const isValidJSON = ( path : string ) = > {
let rawdata = fs . readFileSync ( path , 'utf8' )
try {
JSON . parse ( rawdata )
return true
} catch {
return false
}
2020-01-21 09:20:02 +00:00
}
2020-03-20 20:48:55 +00:00
export function getMoveCommandFromTo ( oldName : string , newName : string ) : string {
return ` git mv ${ oldName } ${ newName } -temp && git mv ${ newName } -temp ${ newName } `
}
export function execRename ( path : string , command : string ) {
execSync ( ` cd ${ path } && ${ command } ` , { encoding : "utf-8" } )
}
2020-01-21 09:20:02 +00:00
export const isValidatorHasAllKeys = ( val : ValidatorModel ) : boolean = > {
return typeof val . id === "string"
&& typeof val . name === "string"
&& typeof val . description === "string"
&& typeof val . website === "string"
}
2020-02-20 06:02:05 +00:00
export const rootDirAllowedFiles = [
".github" ,
"blockchains" ,
"dapps" ,
"media" ,
"node_modules" ,
"script" ,
"src" ,
".gitignore" ,
"azure-pipelines.yml" ,
"jest.config.js" ,
"LICENSE" ,
"package-lock.json" ,
"package.json" ,
"README.md" ,
".git" ,
"pricing"
]
export const assetFolderAllowedFiles = [
2020-03-20 20:48:55 +00:00
logo ,
info
2020-02-20 06:02:05 +00:00
]