UNPKG

@holographxyz/cli

Version:
171 lines (170 loc) 6.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create2address = exports.deploymentConfigHash = exports.decodeDeploymentConfigInput = exports.decodeDeploymentConfig = exports.deploymentFlags = exports.deploymentProcesses = exports.DeploymentType = exports.web3 = void 0; const tslib_1 = require("tslib"); const core_1 = require("@oclif/core"); const networks_1 = require("@holographxyz/networks"); const ethers_1 = require("ethers"); const web3_1 = tslib_1.__importDefault(require("web3")); const bytecodes_1 = require("./bytecodes"); const utils_1 = require("./utils"); const validation_1 = require("./validation"); exports.web3 = new web3_1.default(); var DeploymentType; (function (DeploymentType) { DeploymentType["deployedTx"] = "deployedTx"; DeploymentType["deploymentConfig"] = "deploymentConfig"; DeploymentType["createConfig"] = "createConfig"; })(DeploymentType = exports.DeploymentType || (exports.DeploymentType = {})); exports.deploymentProcesses = [ { name: 'Extract deployment configuration from existing transaction', value: DeploymentType[DeploymentType.deployedTx], short: 'existing deployment', }, { name: 'Use existing deployment configuration', value: DeploymentType[DeploymentType.deploymentConfig], short: 'existing deployment config', }, { name: 'Create deployment configuration', value: DeploymentType[DeploymentType.createConfig], short: 'create deployment config', }, ]; exports.deploymentFlags = { tx: core_1.Flags.string({ description: 'The hash of transaction that deployed the original contract', parse: validation_1.validateTransactionHash, multiple: false, required: false, }), txNetwork: core_1.Flags.string({ description: 'The network on which the transaction was executed', parse: validation_1.validateNetwork, options: networks_1.supportedShortNetworks, multiple: false, required: false, }), targetNetwork: core_1.Flags.string({ description: 'The network on which the contract will be executed', parse: validation_1.validateNetwork, options: networks_1.supportedShortNetworks, multiple: false, required: false, }), deploymentType: core_1.Flags.string({ description: 'The type of deployment to use', multiple: false, options: Object.values(DeploymentType), required: false, }), deploymentConfig: core_1.Flags.string({ description: 'The config file to use', parse: validation_1.validateNonEmptyString, multiple: false, required: false, }), }; const decodeDeploymentConfig = function (input) { const decodedConfig = exports.web3.eth.abi.decodeParameters([ { components: [ { internalType: 'bytes32', name: 'contractType', type: 'bytes32', }, { internalType: 'uint32', name: 'chainType', type: 'uint32', }, { internalType: 'bytes32', name: 'salt', type: 'bytes32', }, { internalType: 'bytes', name: 'byteCode', type: 'bytes', }, { internalType: 'bytes', name: 'initCode', type: 'bytes', }, ], internalType: 'struct DeploymentConfig', name: 'config', type: 'tuple', }, { components: [ { internalType: 'bytes32', name: 'r', type: 'bytes32', }, { internalType: 'bytes32', name: 's', type: 'bytes32', }, { internalType: 'uint8', name: 'v', type: 'uint8', }, ], internalType: 'struct Verification', name: 'signature', type: 'tuple', }, { internalType: 'address', name: 'signer', type: 'address', }, ], input); return { config: { contractType: decodedConfig.config.contractType, chainType: decodedConfig.config.chainType, salt: decodedConfig.config.salt, byteCode: decodedConfig.config.byteCode, initCode: decodedConfig.config.initCode, }, signature: { r: decodedConfig.signature.r, s: decodedConfig.signature.s, v: decodedConfig.signature.v, }, signer: decodedConfig.signer, }; }; exports.decodeDeploymentConfig = decodeDeploymentConfig; const decodeDeploymentConfigInput = function (input) { return (0, exports.decodeDeploymentConfig)('0x' + input.slice(10)); }; exports.decodeDeploymentConfigInput = decodeDeploymentConfigInput; const deploymentConfigHash = function (deploymentConfig) { const configHash = (0, utils_1.sha3)(('0x' + (0, utils_1.remove0x)(ethers_1.BigNumber.from(deploymentConfig.config.contractType).toHexString()).padStart(64, '0') + (0, utils_1.remove0x)(ethers_1.BigNumber.from(deploymentConfig.config.chainType).toHexString()).padStart(8, '0') + (0, utils_1.remove0x)(ethers_1.BigNumber.from(deploymentConfig.config.salt).toHexString()).padStart(64, '0') + (0, utils_1.remove0x)((0, utils_1.sha3)(deploymentConfig.config.byteCode)) + (0, utils_1.remove0x)((0, utils_1.sha3)(deploymentConfig.config.initCode)) + (0, utils_1.remove0x)(ethers_1.BigNumber.from(deploymentConfig.signer).toHexString()).padStart(40, '0')).toLowerCase()); return configHash; }; exports.deploymentConfigHash = deploymentConfigHash; const create2address = function (deploymentConfig, factoryAddress) { const configHash = (0, exports.deploymentConfigHash)(deploymentConfig); const futureAddress = '0x' + (0, utils_1.sha3)('0xff' + (0, utils_1.remove0x)(factoryAddress) + (0, utils_1.remove0x)(configHash) + (0, utils_1.remove0x)((0, utils_1.sha3)(bytecodes_1.bytecodes[bytecodes_1.BytecodeType.Holographer]))).slice(26); return futureAddress; }; exports.create2address = create2address;