@solarity/hardhat-migrate
Version:
The simplest way to deploy smart contracts
92 lines • 3.11 kB
JavaScript
import { id, hexlify, Interface, ConstructorFragment, } from "ethers";
import { isBytes } from "@ethersproject/bytes";
import { getChainId } from "./network.js";
import { deepCopy, toJSON } from "./common.js";
import { MigrateError } from "./MigrateError.js";
import { UNKNOWN_CONTRACT_NAME } from "../../constants.js";
export async function fillParameters(parameters) {
if (parameters.chainId === undefined) {
parameters.chainId = await getChainId();
}
if (parameters.value === undefined) {
parameters.value = 0;
}
return parameters;
}
export function getInterfaceOnlyWithConstructor(fragments) {
let abi;
if (typeof fragments === "string") {
abi = JSON.parse(fragments);
}
else {
abi = fragments;
}
const fragment = abi.find((a) => a.type === "constructor");
if (fragment !== undefined) {
return Interface.from([fragment]);
}
return new Interface([ConstructorFragment.from("constructor()")]);
}
export function bytecodeHash(bytecode) {
return id(bytecodeToString(bytecode));
}
export function createKeyDeploymentFieldsHash(keyTxFields) {
const obj = {
name: keyTxFields.name,
data: keyTxFields.data,
from: keyTxFields.from,
chainId: keyTxFields.chainId,
value: keyTxFields.value,
};
return id(toJSON(obj));
}
export function createKeyTxFieldsHash(keyTxFields) {
const obj = {
data: keyTxFields.data,
from: keyTxFields.from,
chainId: keyTxFields.chainId,
to: keyTxFields.to,
value: keyTxFields.value,
name: keyTxFields.name,
};
return id(toJSON(obj));
}
export function bytecodeToString(bytecode) {
let bytecodeHex;
if (typeof bytecode === "string") {
bytecodeHex = bytecode;
}
else if (isBytes(bytecode)) {
bytecodeHex = hexlify(bytecode);
}
else {
throw new MigrateError(`Invalid bytecode: ${bytecode}`);
}
// Make sure it is 0x prefixed
if (bytecodeHex.substring(0, 2) !== "0x") {
bytecodeHex = "0x" + bytecodeHex;
}
return bytecodeHex;
}
export function getMethodString(contractName, methodName, methodFragment = {}, args = []) {
if (contractName === UNKNOWN_CONTRACT_NAME) {
contractName = "Unidentified Contract";
}
const copyOfArgs = deepCopy(args);
if (methodFragment.inputs.length + 1 === copyOfArgs.length) {
copyOfArgs.pop();
}
if (methodFragment.inputs === undefined) {
return `${contractName}.${methodName}()`;
}
const argsString = methodFragment.inputs.map((input, i) => `${input.name}:${copyOfArgs[i]}`).join(", ");
const methodSting = `${contractName}.${methodName}(${argsString})`;
if (methodSting.length > 60) {
const shortenMethodString = `${contractName}.${methodName}(${copyOfArgs.length} arguments)`;
if (shortenMethodString.length > 60) {
return `${contractName.split(":").pop()}.${methodName}(${copyOfArgs.length} arguments)`;
}
}
return methodSting;
}
//# sourceMappingURL=migration.js.map