2020-08-06 19:17:38 +00:00
|
|
|
import * as fs from "fs"
|
|
|
|
import * as path from "path"
|
|
|
|
const Web3 = require('web3')
|
|
|
|
const web3 = new Web3('ws://localhost:8546');
|
|
|
|
|
|
|
|
export const logoName = `logo`
|
|
|
|
export const infoName = `info`
|
|
|
|
|
|
|
|
export const logoExtension = "png"
|
|
|
|
export const jsonExtension = "json"
|
|
|
|
|
2020-08-18 06:50:32 +00:00
|
|
|
const allowList = `allowlist.${jsonExtension}`
|
|
|
|
const denyList = `denylist.${jsonExtension}`
|
2020-08-06 19:17:38 +00:00
|
|
|
|
|
|
|
export const logo = `${logoName}.${logoExtension}`
|
|
|
|
export const info = `${infoName}.${jsonExtension}`
|
|
|
|
|
|
|
|
export const root = './'
|
|
|
|
export const chainsFolderPath = path.join(process.cwd(), '/blockchains')
|
|
|
|
export const getChainLogoPath = (chain: string): string => `${chainsFolderPath}/${chain}/info/${logo}`
|
|
|
|
export const getChainInfoPath = (chain: string): string => `${chainsFolderPath}/${chain}/info/${info}`
|
|
|
|
export const getChainAssetsPath = (chain: string): string => `${chainsFolderPath}/${chain}/assets`
|
|
|
|
export const getChainPath = (chain: string): string => `${chainsFolderPath}/${chain}`
|
|
|
|
|
|
|
|
export const getChainAssetPath = (chain: string, address: string) => `${getChainAssetsPath(chain)}/${address}`
|
|
|
|
export const getAllChainsList = (): string[] => readDirSync(chainsFolderPath)
|
|
|
|
export const getChainAssetLogoPath = (chain: string, address: string) => `${getChainAssetsPath(chain)}/${address}/${logo}`
|
2020-08-18 06:50:32 +00:00
|
|
|
export const getChainAllowlistPath = (chain: string): string => `${chainsFolderPath}/${chain}/${allowList}`
|
|
|
|
export const getChainDenylistPath = (chain: string): string => `${chainsFolderPath}/${chain}/${denyList}`
|
|
|
|
export const getChainAllowlist = (chain: string): string[] => {
|
|
|
|
if (isChainAllowlistExistSync(chain)) {
|
|
|
|
return JSON.parse(readFileSync(getChainAllowlistPath(chain)))
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
2020-08-18 06:50:32 +00:00
|
|
|
export const getChainDenylist = (chain: string): string[] => {
|
|
|
|
if (isChainDenylistExistSync(chain)) {
|
|
|
|
return JSON.parse(readFileSync(getChainDenylistPath(chain)))
|
2020-08-06 19:17:38 +00:00
|
|
|
}
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
export const getRootDirFilesList = (): string[] => readDirSync(root)
|
|
|
|
|
|
|
|
export const readDirSync = (path: string): string[] => fs.readdirSync(path)
|
|
|
|
export const makeDirSync = (path: string) => fs.mkdirSync(path)
|
|
|
|
export const isPathExistsSync = (path: string): boolean => fs.existsSync(path)
|
|
|
|
export const isDirContainLogo = (path: string): boolean => fs.existsSync(`${path}/${logo}`)
|
2020-08-18 06:50:32 +00:00
|
|
|
export const isChainAllowlistExistSync = (chain: string): boolean => isPathExistsSync(getChainAllowlistPath(chain))
|
|
|
|
export const isChainDenylistExistSync = (chain: string): boolean => isPathExistsSync(getChainDenylistPath(chain))
|
2020-08-06 19:17:38 +00:00
|
|
|
export const isChainInfoExistSync = (chain: string): boolean => isPathExistsSync(getChainInfoPath(chain))
|
|
|
|
export const readFileSync = (path: string) => fs.readFileSync(path, 'utf8')
|
|
|
|
|
|
|
|
export const isChecksum = (address: string): boolean => web3.utils.checkAddressChecksum(address)
|
|
|
|
export const toChecksum = (address: string): string => web3.utils.toChecksumAddress(address)
|
|
|
|
|
|
|
|
export const isEthereumAddress = (address: string): boolean => {
|
|
|
|
return web3.utils.isAddress(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 isPathDirEmpty = (path: string): boolean => {
|
|
|
|
try {
|
|
|
|
if (isPathDir(path)) {
|
|
|
|
return fs.readdirSync(path).length == 0
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Error isPathDirEmpty`, error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const removeDir = (path: string) => {
|
|
|
|
fs.rmdirSync(path, {recursive: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|