UNPKG

@holographxyz/cli

Version:
158 lines (157 loc) 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.utf8ToBytes32 = exports.generateRandomSalt = exports.toLong18 = exports.toShort18 = exports.toShort18Str = exports.isStringAValidURL = exports.randomASCII = exports.storageSlot = exports.functionSignature = exports.sha3 = exports.toAscii = exports.remove0x = exports.generateInitCode = exports.zeroAddress = exports.rgbToHex = exports.NETWORK_COLORS = exports.networkToChainId = exports.webSocketConfig = exports.getSecondsLeft = exports.sleep = exports.capitalize = exports.randomNumber = exports.web3 = void 0; const tslib_1 = require("tslib"); const web3_1 = tslib_1.__importDefault(require("web3")); const bignumber_1 = require("@ethersproject/bignumber"); const units_1 = require("@ethersproject/units"); exports.web3 = new web3_1.default(); function randomNumber(min, max) { return Math.floor(Math.random() * (max - min)) + min; } exports.randomNumber = randomNumber; function capitalize(input) { return input.charAt(0).toUpperCase() + input.slice(1); } exports.capitalize = capitalize; // eslint-disable-next-line no-promise-executor-return const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); exports.sleep = sleep; const getSecondsLeft = (timestamp) => { return Math.round((timestamp - Date.now()) / 1000); }; exports.getSecondsLeft = getSecondsLeft; exports.webSocketConfig = { reconnect: { auto: false, // delay: 7000, // ms onTimeout: false, // maxAttempts: }, timeout: 1000 * 15, clientConfig: { maxReceivedFrameSize: 10000000000, maxReceivedMessageSize: 10000000000, keepalive: true, keepaliveInterval: 1000, dropConnectionOnKeepaliveTimeout: true, keepaliveGracePeriod: 4000, // ms }, }; exports.networkToChainId = { ethereum: 1, ethereumTestnetRinkeby: 4, ethereumTestnetGoerli: 5, polygon: 89, polygonTestnet: 80001, avalanche: 43114, avalancheTestnet: 43113, binanceSmartChain: 56, binanceSmartChainTestnet: 97, }; exports.NETWORK_COLORS = { localhost: '##83EEFF', localhost2: '#ff0000', avalancheTestnet: '#ff0000', avalanche: '#ff0000', binanceSmartChain: '#f0b90b', binanceSmartChainTestnet: '#f0b90b', polygonTestnet: '##B026FF ', polygon: '#B026FF ', ethereumTestnetRinkeby: '##83EEFF', ethereumTestnetGoerli: '#83EEFF', etheterum: '##83EEFF', }; const rgbToHex = (rgb) => { const hex = Number(rgb).toString(16); return hex.length === 1 ? `0${hex}` : hex; }; exports.rgbToHex = rgbToHex; exports.zeroAddress = '0x' + '00'.repeat(20); function generateInitCode(vars, vals) { return exports.web3.eth.abi.encodeParameters(vars, vals); } exports.generateInitCode = generateInitCode; function remove0x(input) { if (input.startsWith('0x')) { return input.slice(2); } return input; } exports.remove0x = remove0x; function toAscii(input) { input = remove0x(input.trim().toLowerCase()); if (input.length % 2 !== 0) { input = '0' + input; } const arr = [...input]; let output = ''; for (let i = 0, l = input.length; i < l; i += 2) { const chunk = arr[i] + arr[i + 1]; if (chunk !== '00') { output += String.fromCharCode(Number.parseInt(chunk, 16)); } } return output; } exports.toAscii = toAscii; function sha3(input) { // handle empty bytes issue if (input === undefined || input === '' || input === '0x') { return '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; } return exports.web3.utils.keccak256(input); } exports.sha3 = sha3; function functionSignature(input) { return sha3(input).slice(0, 10); } exports.functionSignature = functionSignature; function storageSlot(input) { return ('0x' + remove0x(exports.web3.utils.toHex(exports.web3.utils.toBN(exports.web3.utils.keccak256(input)).sub(exports.web3.utils.toBN(1)))).padStart(64, '0')); } exports.storageSlot = storageSlot; function randomASCII(bytes) { let text = ''; for (let i = 0; i < bytes; i++) { text += (32 + Math.floor(Math.random() * 94)).toString(16).padStart(2, '0'); } return Buffer.from(text, 'hex').toString(); } exports.randomASCII = randomASCII; function isStringAValidURL(s) { const protocols = ['http:', 'https:', 'wss:']; try { const result = new URL(s); return result.protocol ? protocols.includes(result.protocol) : false; } catch { return false; } } exports.isStringAValidURL = isStringAValidURL; const toShort18Str = (num) => { return (0, units_1.formatUnits)(num, 'ether'); }; exports.toShort18Str = toShort18Str; const toShort18 = (num) => { return bignumber_1.BigNumber.from(num).div(bignumber_1.BigNumber.from('10').pow(18)); }; exports.toShort18 = toShort18; const toLong18 = (num) => { return bignumber_1.BigNumber.from(num).mul(bignumber_1.BigNumber.from('10').pow(18)); }; exports.toLong18 = toLong18; const generateRandomSalt = () => { return '0x' + Date.now().toString(16).padStart(64, '0'); }; exports.generateRandomSalt = generateRandomSalt; const utf8ToBytes32 = (str) => { return ('0x' + [...str] .map(c => c.charCodeAt(0) < 128 ? c.charCodeAt(0).toString(16) : encodeURIComponent(c).replace(/%/g, '').toLowerCase()) .join('') .padStart(64, '0')); }; exports.utf8ToBytes32 = utf8ToBytes32;