proposal-37/test/utils.js
2023-11-10 16:30:48 +00:00

180 lines
6.5 KiB
JavaScript

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";
const cheatingRelayers = [
"0x853281B7676DFB66B87e2f26c9cB9D10Ce883F37", // available-reliable-relayer.eth,
"0x0000208a6cC0299dA631C08fE8c2EDe435Ea83B8", // 0xtornadocash.eth,
"0xaaaaD0b504B4CD22348C4Db1071736646Aa314C6", // tornrelayers.eth
"0x36DD7b862746fdD3eDd3577c8411f1B76FDC2Af5", // tornado-crypto-bot-exchange.eth
"0x5007565e69E5c23C278c2e976beff38eF4D27B3d", // official-tornado.eth
"0xa42303EE9B2eC1DB7E2a86Ed6C24AF7E49E9e8B9", // relayer-tornado.eth
"0x18F516dD6D5F46b2875Fd822B994081274be2a8b", // torn69.eth
"0x2ffAc4D796261ba8964d859867592B952b9FC158", // safe-tornado.eth
"0x12D92FeD171F16B3a05ACB1542B40648E7CEd384", // torn-relayers.eth
"0x996ad81FD83eD7A87FD3D03694115dff19db0B3b", // secure-tornado.eth
"0x7853E027F37830790685622cdd8685fF0c8255A2", // tornado-secure.eth
"0xf0D9b969925116074eF43e7887Bcf035Ff1e7B19", // lowfee-relayer.eth
"0xEFa22d23de9f293B11e0c4aC865d7b440647587a", // tornado-relayer.eth
"0x14812AE927e2BA5aA0c0f3C0eA016b3039574242", // pls-im-poor.eth
"0x87BeDf6AD81A2907633Ab68D02c44f0415bc68C1", // tornrelayer.eth
];
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 proposalFactory = await ethers.getContractFactory("Proposal");
const proposal = await proposalFactory.deploy();
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 getRelayerRegistryContract(governanceSigner);
return { relayerRegistryProxyAddr, relayerRegistryContract, governanceSigner };
}
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,
cheatingRelayers,
};