proposal-34/test/utils.js

178 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-11-03 22:31:24 +03:00
const { ethers, network } = require("hardhat");
const { time } = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const ensAddr = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
const relayerRegistryProxyAddr = "0x58E8dCC13BE9780fC42E8723D8EaD4CF46943dF2";
const tornAddr = "0x77777FeDdddFfC19Ff86DB637967013e6C6A116C";
const governanceAddr = "0x5efda50f22d34F262c29268506C5Fa42cB56A1Ce";
async function getPermitSignature(signer, tokenContract, spender, value, deadline) {
const [nonce, name, version, chainId] = await Promise.all([
tokenContract.nonces(signer.address),
tokenContract.name(),
"1",
tokenContract.chainID(),
]);
const domain = {
name,
version,
chainId,
verifyingContract: tornAddr,
};
const types = {
Permit: [
{
name: "owner",
type: "address",
},
{
name: "spender",
type: "address",
},
{
name: "value",
type: "uint256",
},
{
name: "nonce",
type: "uint256",
},
{
name: "deadline",
type: "uint256",
},
],
};
const values = {
owner: signer.address,
spender,
value,
nonce,
deadline,
};
const signature = await signer.signTypedData(domain, types, values);
return ethers.Signature.from(signature);
}
async function getRegisterRelayerParams(ensDomain, additionalStake = 0, workerAddrs = []) {
const relayerAddr = await resolveAddr(ensDomain);
const toStake = await sendMinimalStakeAmount(relayerAddr, BigInt(additionalStake));
const tornContract = await ethers.getContractAt(require("./abi/torn.abi.json"), tornAddr);
const relayerSigner = await ethers.getSigner(relayerAddr);
const { v, r, s } = await getPermitSignature(
relayerSigner,
tornContract,
relayerRegistryProxyAddr,
toStake,
ethers.MaxUint256,
);
return [ensDomain, toStake, workerAddrs, relayerAddr, ethers.MaxUint256, v, r, s];
}
async function getEnsRegistryContract() {
return await ethers.getContractAt(require("./abi/ensRegistry.abi.json"), ensAddr);
}
async function resolveAddr(ensName) {
if(ethers.isAddress(ensName)) return ensName;
const ensNode = ethers.namehash(ensName);
const registryContract = await getEnsRegistryContract();
const resolverAddr = await registryContract.resolver(ensNode);
const resolverContract = await ethers.getContractAt(require("./abi/ensResolver.abi.json"), resolverAddr);
return await resolverContract.addr(ensNode);
}
async function getRelayerRegistryContract(signer) {
return await ethers.getContractAt("RelayerRegistry", relayerRegistryProxyAddr, signer);
}
async function getOldRelayerRegistryContract() {
return await ethers.getContractAt(require("./abi/relayerRegistryOld.abi.json"), relayerRegistryProxyAddr);
}
async function getManyEth(addr) {
await network.provider.send("hardhat_setBalance", [addr, "0x111166630153555558483537"]);
}
async function sendMinimalStakeAmount(addr, additionalStake = 0n) {
const relayerRegistryContract = await getRelayerRegistryContract();
const minRelayerStakeAmount = await relayerRegistryContract.minStakeAmount();
const governanceSigner = await ethers.getImpersonatedSigner(governanceAddr);
const tornContract = await ethers.getContractAt(require("./abi/torn.abi.json"), tornAddr, governanceSigner);
await tornContract.transfer(addr, minRelayerStakeAmount + additionalStake);
return minRelayerStakeAmount + additionalStake;
}
async function deployAndExecuteProposal() {
const RelayerRegistryFactory = await ethers.getContractFactory("RelayerRegistry");
const constructorArgs = [
"0x77777FeDdddFfC19Ff86DB637967013e6C6A116C",
"0x5efda50f22d34F262c29268506C5Fa42cB56A1Ce",
"0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
"0x5B3f656C80E8ddb9ec01Dd9018815576E9238c29",
"0x5f6c97C6AD7bdd0AE7E0Dd4ca33A4ED3fDabD4D7",
];
const relayerRegistry = await RelayerRegistryFactory.deploy(...constructorArgs);
const deployedRegistryAddr = await relayerRegistry.getAddress();
const proposalFactory = await ethers.getContractFactory("Proposal");
const proposal = await proposalFactory.deploy(deployedRegistryAddr);
const deployedProposalAddr = await proposal.getAddress();
const bigStakerAddr = "0xE4143f6377AEcd7193b9731d1C28815b57C4f5Ab";
await getManyEth(bigStakerAddr);
const stakerSigner = await ethers.getImpersonatedSigner(bigStakerAddr);
const governanceContract = await ethers.getContractAt(
require("./abi/governance.abi.json"),
governanceAddr,
stakerSigner,
);
await governanceContract.propose(deployedProposalAddr, "");
const proposalId = await governanceContract.proposalCount();
await time.increase(60 * 60);
await governanceContract.castVote(proposalId, true);
await time.increase(60 * 60 * 24 * 7 + 60);
await governanceContract.execute(proposalId);
const governanceSigner = await ethers.getImpersonatedSigner(governanceAddr);
const relayerRegistryContract = await ethers.getContractAt(
"RelayerRegistry",
relayerRegistryProxyAddr,
governanceSigner,
);
return { relayerRegistryProxyAddr, deployedRegistryAddr, relayerRegistryContract, governanceSigner, constructorArgs };
}
async function unregisterRelayer(ensNameOrAddress) {
const governanceSigner = await ethers.getImpersonatedSigner(governanceAddr);
const relayerRegistryContract = await getRelayerRegistryContract(governanceSigner);
const relayerAddr = ethers.isAddress(ensNameOrAddress) ? ensNameOrAddress : resolveAddr(ensNameOrAddress);
await relayerRegistryContract.unregisterRelayer(relayerAddr);
}
async function getRelayerBalance(relayerAddr, blockNumber){
const relayerRegistryContract = await getRelayerRegistryContract();
return await relayerRegistryContract.getRelayerBalance(relayerAddr, {blockTag: blockNumber});
}
module.exports = {
sendMinimalStakeAmount,
getEnsRegistryContract,
getOldRelayerRegistryContract,
getRelayerRegistryContract,
getPermitSignature,
resolveAddr,
getManyEth,
unregisterRelayer,
deployAndExecuteProposal,
getRegisterRelayerParams,
getRelayerBalance,
governanceAddr
};