hardhat-gasless-deployer
Version:
Hardhat Plugin for deploying contracts using GSN
106 lines • 5.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Deployer = void 0;
const ethers_1 = require("ethers");
const fs_1 = __importDefault(require("fs"));
const plugins_1 = require("hardhat/plugins");
const DeployerABI_json_1 = require("./abi/DeployerABI.json");
const WhitelistPaymasterABI_json_1 = require("./abi/WhitelistPaymasterABI.json");
const constants_1 = require("./constants");
const gsnProvider_1 = require("./gsnProvider");
class Deployer {
constructor(hre, deployer) {
this.env = hre;
this.deployer = deployer;
this.artifacts = {
Factory: { abi: DeployerABI_json_1.abi, bytecode: DeployerABI_json_1.bytecode },
WhitelistPaymaster: {
abi: WhitelistPaymasterABI_json_1.abi,
bytecode: WhitelistPaymasterABI_json_1.bytecode,
},
};
}
async deployAndInitFactoryAndPaymaster(hre) {
if (!(await this.isDeployerInitialized(hre.config.hHGaslessDeployer.network))) {
// deploy factory (deployer contract)
const factory = (await this.deploy(this.artifacts.Factory, [this.env.config.hHGaslessDeployer.forwarder], this.deployer));
this.factoryAddress = await factory.getAddress();
console.log(`Deployer contract has been successfully deployed @ ${this.factoryAddress}`);
// deploy whitelist paymaster
const paymaster = (await this.deploy(this.artifacts.WhitelistPaymaster, [], this.deployer));
this.paymasterAddress = await paymaster.getAddress();
console.log(`Paymaster contract has been successfully deployed @ ${this.paymasterAddress}`);
// init whitelist paymaster
await paymaster.setRelayHub(this.env.config.hHGaslessDeployer.relayerHub);
console.log(`RelayHub (this.env.config.hHGaslessDeployer.relayer_hub_address) has been successfully registerd at paymaster (${this.paymasterAddress})`);
await paymaster.setTrustedForwarder(this.env.config.hHGaslessDeployer.forwarder);
console.log(`Forwarder (this.env.config.hHGaslessDeployer.forwarder) has been successfully added to paymaster (${this.paymasterAddress})`);
const deployerAddress = await this.deployer.getAddress();
await paymaster.whitelistSender(deployerAddress, true);
console.log(`Deployer (deployerAddress) has been successfully whitelisted at paymaster (${this.paymasterAddress})`);
// save deployment
const data = {
gsn: {
factory: this.factoryAddress,
paymaster: this.paymasterAddress,
},
};
await this.saveDeployment(data);
}
else {
const data = await this.getDeployerContracts(hre.config.hHGaslessDeployer.network);
this.factoryAddress = data.factory;
this.paymasterAddress = data.paymaster;
}
}
async deployTargetContract(hre, salt, bytecode) {
const provider = await (0, gsnProvider_1.getGSNProvider)(hre);
const saltHash = hre.ethers.keccak256(salt.toString());
const factory = await hre.ethers.getContractAt(this.artifacts.Factory.abi, this.factoryAddress, provider.gsnSigner);
const txOptions = {
gasPrice: (await provider.gsnProvider.getFeeData()).gasPrice,
};
// TODO: replace bytecodex with bytcode
const tx = await factory.deploy(hre.config.hHGaslessDeployer.value, saltHash, bytecode.data, txOptions);
console.log(`Transaction hash: ${tx.hash}`);
const receipt = await tx.wait();
return {
receipt,
contractAddress: await factory.computeAddress(saltHash, hre.ethers.keccak256(bytecode.data)),
};
}
async isDeployerInitialized(network) {
try {
const jsonData = fs_1.default.readFileSync(constants_1.GASLESS_DEPLOYERS_FILE, "utf8");
const data = JSON.parse(jsonData);
return (data[network].factory != undefined &&
data[network].paymaster != undefined);
}
catch (error) {
return false;
}
}
async saveDeployment(data) {
const jsonData = JSON.stringify(data, null, 2);
fs_1.default.writeFileSync(constants_1.GASLESS_DEPLOYERS_FILE, jsonData, "utf8");
}
async getDeployerContracts(network) {
try {
const jsonData = fs_1.default.readFileSync(constants_1.GASLESS_DEPLOYERS_FILE, "utf8");
const data = JSON.parse(jsonData);
return data[network];
}
catch (error) {
throw new plugins_1.NomicLabsHardhatPluginError(constants_1.PLUGIN_NAME, "Failed to open the deployment files");
}
}
async deploy(contract, deployParams, actor) {
const factory = new ethers_1.ContractFactory(contract.abi, contract.bytecode, actor);
return factory.deploy(...deployParams);
}
}
exports.Deployer = Deployer;
//# sourceMappingURL=deployer.js.map