ethers.js/lib.esm/contract/factory.js

71 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
import { Interface } from "../abi/index.js";
import { getCreateAddress } from "../address/index.js";
2022-11-09 10:57:02 +03:00
import { concat, defineProperties, getBytes, hexlify, assert, assertArgument } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { BaseContract, copyOverrides, resolveArgs } from "./contract.js";
// A = Arguments to the constructor
// I = Interface of deployed contracts
export class ContractFactory {
interface;
bytecode;
runner;
constructor(abi, bytecode, runner) {
const iface = Interface.from(abi);
// Dereference Solidity bytecode objects and allow a missing `0x`-prefix
if (bytecode instanceof Uint8Array) {
2022-09-16 05:58:45 +03:00
bytecode = hexlify(getBytes(bytecode));
2022-09-05 23:57:11 +03:00
}
else {
if (typeof (bytecode) === "object") {
bytecode = bytecode.object;
}
2023-02-19 06:18:42 +03:00
if (!bytecode.startsWith("0x")) {
2022-09-05 23:57:11 +03:00
bytecode = "0x" + bytecode;
}
2022-09-16 05:58:45 +03:00
bytecode = hexlify(getBytes(bytecode));
2022-09-05 23:57:11 +03:00
}
defineProperties(this, {
bytecode, interface: iface, runner: (runner || null)
});
}
async getDeployTransaction(...args) {
let overrides = {};
const fragment = this.interface.deploy;
if (fragment.inputs.length + 1 === args.length) {
overrides = await copyOverrides(args.pop());
}
if (fragment.inputs.length !== args.length) {
throw new Error("incorrect number of arguments to constructor");
}
const resolvedArgs = await resolveArgs(this.runner, fragment.inputs, args);
const data = concat([this.bytecode, this.interface.encodeDeploy(resolvedArgs)]);
return Object.assign({}, overrides, { data });
}
async deploy(...args) {
const tx = await this.getDeployTransaction(...args);
2022-11-09 10:57:02 +03:00
assert(this.runner && typeof (this.runner.sendTransaction) === "function", "factory runner does not support sending transactions", "UNSUPPORTED_OPERATION", {
operation: "sendTransaction"
});
2022-09-05 23:57:11 +03:00
const sentTx = await this.runner.sendTransaction(tx);
const address = getCreateAddress(sentTx);
return new BaseContract(address, this.interface, this.runner, sentTx);
}
connect(runner) {
return new ContractFactory(this.interface, this.bytecode, runner);
}
static fromSolidity(output, runner) {
2022-11-09 10:57:02 +03:00
assertArgument(output != null, "bad compiler output", "output", output);
2022-09-05 23:57:11 +03:00
if (typeof (output) === "string") {
output = JSON.parse(output);
}
const abi = output.abi;
let bytecode = "";
if (output.bytecode) {
bytecode = output.bytecode;
}
else if (output.evm && output.evm.bytecode) {
bytecode = output.evm.bytecode;
}
return new this(abi, bytecode, runner);
}
}
//# sourceMappingURL=factory.js.map