@kadena/hardhat-chainweb
Version:
Hardhat plugin for Kadena's Chainweb network
176 lines • 8.27 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployCreate2Factory = exports.getCreate2FactoryAddress = exports.create2Artifacts = void 0;
const ethers_1 = require("ethers");
const combined_json_1 = __importDefault(require("../../build/create2-factory/combined.json"));
const pure_utils_1 = require("../pure-utils");
const utils_1 = require("../utils");
const hardhat_1 = __importStar(require("hardhat"));
exports.create2Artifacts = combined_json_1.default.contracts['contracts/Create2Factory.sol:Create2Factory'];
const networkStem = (0, pure_utils_1.getNetworkStem)(hardhat_1.default.config.defaultChainweb);
const getCreate2FactoryAddress = async (signer, version = 1) => {
const secondaryKey = await deriveSecondaryKey(signer, version);
const factoryAddress = (0, ethers_1.getCreateAddress)({
from: secondaryKey.publicKey,
nonce: 0,
});
return factoryAddress;
};
exports.getCreate2FactoryAddress = getCreate2FactoryAddress;
async function deriveSecondaryKey(signer, version = 1) {
const message = `create deployer key for create2 factory version: ${version}`;
const signature = await signer.signMessage(message);
// Combine signature and label to get deterministic entropy
const hash = (0, ethers_1.keccak256)((0, ethers_1.toUtf8Bytes)(signature));
// Use first 32 bytes (64 hex chars + '0x') as the private key
const derivedPrivateKey = '0x' + hash.slice(2, 66);
const wallet = new ethers_1.Wallet(derivedPrivateKey, hardhat_1.ethers.provider);
return {
publicKey: await wallet.getAddress(),
privateKey: derivedPrivateKey,
};
}
async function fundDeployer(sender, receiver, amountStr) {
const amount = hardhat_1.ethers.parseEther(amountStr); // 1 ether
const receiverAddress = await receiver.getAddress();
const tx = await sender.sendTransaction({
to: receiverAddress,
value: amount,
});
await tx.wait();
const receiverBalance = await hardhat_1.ethers.provider.getBalance(receiverAddress);
if (receiverBalance !== amount) {
throw new Error(`funding deployer failed. receiver balance: ${receiverBalance} is not equal to funding amount: ${amount}`);
}
}
const deployCreate2Factory = async (props) => {
const { signer, version = 1, fundingDeployerWith = '1.0' } = props !== null && props !== void 0 ? props : {};
function isContractDeployed(address) {
return hardhat_1.ethers.provider.getCode(address).then((code) => code !== '0x');
}
let secondaryPrivateKey = undefined;
const getSecondaryWallet = async (signer, version) => {
if (secondaryPrivateKey) {
return new ethers_1.Wallet(secondaryPrivateKey, hardhat_1.ethers.provider);
}
secondaryPrivateKey = (await deriveSecondaryKey(signer, version))
.privateKey;
return new ethers_1.Wallet(secondaryPrivateKey, hardhat_1.ethers.provider);
};
return (0, utils_1.runOverChains)(async (cwId) => {
console.log(`deploying create2 factory on chain ${cwId}`);
const signers = await hardhat_1.ethers.getSigners();
const masterDeployer = !signer
? signers[0]
: signers.find((account) => account.address === signer);
if (!masterDeployer) {
throw new Error(`cant find the account with address ${signer}`);
}
const secondaryKey = await getSecondaryWallet(masterDeployer, version);
const secondaryKeyAddress = await secondaryKey.getAddress();
const factoryAddress = hardhat_1.ethers.getCreateAddress({
from: secondaryKey.address,
nonce: 0,
});
const isDeployed = await isContractDeployed(factoryAddress);
if (isDeployed) {
console.log(`the factory address ${factoryAddress} is already deployed`);
const Factory = await hardhat_1.default.ethers.getContractFactory(exports.create2Artifacts.abi, exports.create2Artifacts.bin);
const create2 = Factory.attach(factoryAddress);
console.log(`the factory address ${factoryAddress} is already deployed on chain ${cwId}`);
return {
contract: create2,
address: factoryAddress,
chain: cwId,
deployer: secondaryKeyAddress,
network: {
chainId: cwId,
name: `${networkStem}${cwId}`,
},
};
}
const nonce = await hardhat_1.ethers.provider.getTransactionCount(secondaryKeyAddress);
if (nonce > 0) {
throw new Error(`This address has already been used for another type of transaction. you need a new address to deploy a create2 factory`);
}
console.log(`the contract will be deploying with address: ${factoryAddress} and the deployer address: ${secondaryKeyAddress}`);
const balance = await hardhat_1.ethers.provider.getBalance(secondaryKeyAddress);
if (balance > BigInt(0)) {
console.log('deployer address:', secondaryKeyAddress);
console.log('balance:', hardhat_1.ethers.formatEther(balance));
}
else {
if (+fundingDeployerWith < 0) {
throw new Error(`the fundingDeployerWith amount must be greater than 0`);
}
console.log('FUNDING DEPLOYER WITH', fundingDeployerWith);
await fundDeployer(masterDeployer, secondaryKey, fundingDeployerWith);
}
/* Deploy the contract */
const factory = await hardhat_1.default.ethers.getContractFactory(combined_json_1.default.contracts['contracts/Create2Factory.sol:Create2Factory']
.abi, combined_json_1.default.contracts['contracts/Create2Factory.sol:Create2Factory']
.bin, secondaryKey);
const contract = await factory.deploy();
const deploymentTx = contract.deploymentTransaction();
if (!deploymentTx) {
throw new Error('Deployment transaction failed');
}
await deploymentTx.wait();
if (factoryAddress !== (await contract.getAddress())) {
throw new Error('Factory address mismatch');
}
console.log(`create2 factory deployed at ${factoryAddress} on chain ${cwId}`);
return {
contract: contract,
address: factoryAddress,
chain: cwId,
deployer: secondaryKeyAddress,
network: {
chainId: cwId,
name: `${networkStem}${cwId}`,
},
};
});
};
exports.deployCreate2Factory = deployCreate2Factory;
// return {
// getCreate2FactoryAddress,
// deployCreate2Factory,
// };
//# sourceMappingURL=deployCreate2Factory.js.map