proposal-32/test/RelayerRegistry.js
2023-10-23 13:48:36 +00:00

176 lines
8.7 KiB
JavaScript

const { time, loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { expect, assert } = require("chai");
const { ethers, network } = require("hardhat");
const { relayerRegistry } = require("torn-token");
describe("Registry update", function () {
async function deployAndExecuteFixture() {
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 network.provider.send("hardhat_setBalance", [
bigStakerAddr,
"0x116663015358483537"
]);
const stakerSigner = await ethers.getImpersonatedSigner(bigStakerAddr);
const governanceAddr = "0x5efda50f22d34F262c29268506C5Fa42cB56A1Ce";
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 relayerRegistryProxyAddr = "0x58E8dCC13BE9780fC42E8723D8EaD4CF46943dF2";
const relayerRegistryContract = await ethers.getContractAt("RelayerRegistry", relayerRegistryProxyAddr, governanceSigner);
return { relayerRegistryProxyAddr, deployedRegistryAddr, relayerRegistryContract, governanceSigner };
}
async function unregisterRelayer(ensNameOrAddress){
const { relayerRegistryContract } = await loadFixture(deployAndExecuteFixture);
const relayerAddr = ethers.isAddress(ensNameOrAddress) ? ensNameOrAddress : resolveAddr(ensNameOrAddress);
await relayerRegistryContract.unregisterRelayer(relayerAddr);
}
async function resolveAddr(ensName){
const ensNode = ethers.namehash(ensName);
const ensAddr = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
const registryContract = await ethers.getContractAt(require("./abi/ensRegistry.abi.json"), ensAddr);
const resolverAddr = await registryContract.resolver(ensNode);
const resolverContract = await ethers.getContractAt(require("./abi/ensResolver.abi.json"), resolverAddr);
return await resolverContract.addr(ensNode);
}
async function getOldRelayerRegistryContractFixture(){
const relayerRegistryProxyAddr = "0x58E8dCC13BE9780fC42E8723D8EaD4CF46943dF2";
return await ethers.getContractAt(require("./abi/relayerRegistryOld.abi.json"), relayerRegistryProxyAddr);
}
it("Implementation address should be updated", async function () {
await loadFixture(getOldRelayerRegistryContractFixture);
const { relayerRegistryProxyAddr, deployedRegistryAddr } = await loadFixture(deployAndExecuteFixture);
const implementation = await ethers.provider.getStorage(relayerRegistryProxyAddr, "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc");
const [implementationAddr] = new ethers.AbiCoder().decode(["address"], implementation);
expect(implementationAddr).to.equal(deployedRegistryAddr)
});
describe("Unregister relayer", function(){
it("Uregister relayer function should work", async function(){
const testRelayerAddr = await resolveAddr("first-relayer.eth");
const relayerRegistryOldContract = await loadFixture(getOldRelayerRegistryContractFixture);
let isRelayer = await relayerRegistryOldContract.isRelayer(testRelayerAddr);
let isRelayerRegistered = await relayerRegistryOldContract.isRelayerRegistered(testRelayerAddr, testRelayerAddr);
expect(isRelayer).to.equal(true);
expect(isRelayerRegistered).to.equal(true);
const { relayerRegistryContract } = await loadFixture(deployAndExecuteFixture);
await unregisterRelayer(testRelayerAddr);
isRelayer = await relayerRegistryContract.isRelayer(testRelayerAddr);
isRelayerRegistered = await relayerRegistryContract.isRelayerRegistered(testRelayerAddr, testRelayerAddr);
expect(isRelayer).to.equal(false);
expect(isRelayerRegistered).to.equal(false);
})
it("Unregistered relayer should have zero balance", async function(){
const testRelayerAddr = await resolveAddr("first-relayer.eth");
const { relayerRegistryContract } = await loadFixture(deployAndExecuteFixture);
await unregisterRelayer(testRelayerAddr);
const relayerBalance = await relayerRegistryContract.getRelayerBalance(testRelayerAddr);
expect(relayerBalance).to.equal(0);
})
it("Tornado router address should be valid", async function(){
const relayerRegistryOldContract = await loadFixture(getOldRelayerRegistryContractFixture);
const oldRegistryRouterAddress = await relayerRegistryOldContract.tornadoRouter();
const { relayerRegistryContract } = await loadFixture(deployAndExecuteFixture);
expect(await relayerRegistryContract.tornadoRouter()).to.equal(oldRegistryRouterAddress);
})
it("Aggregator contract data for unregistered relayers should be valid", async function(){
const aggregatorAddr = "0xE8F47A78A6D52D317D0D2FFFac56739fE14D1b49";
const aggregatorContract = await ethers.getContractAt(require("./abi/aggregator.abi.json"), aggregatorAddr);
const notRelayer = "🦋️-effect.eth";
const testRelayer = "first-relayer.eth";
const callAggr = async () => await aggregatorContract.relayersData([notRelayer, testRelayer].map(ethers.namehash), ["mainnet-tornado", "bsc-tornado"]);
const oldData = await callAggr();
await loadFixture(deployAndExecuteFixture);
await unregisterRelayer(testRelayer);
const newData = await callAggr();
assert.deepEqual(oldData[0], newData[0]);
expect(oldData[1][1]).to.greaterThan(500n * 10n ** 18n);
expect(newData[1][1]).to.equal(0);
expect(oldData[1][2]).to.equal(true);
expect(newData[1][2]).to.equal(false);
})
it("Cheating relayers should be unregistered", async function(){
const cheatingRelayers = await Promise.all([
"available-reliable-relayer.eth",
"0xtornadocash.eth",
"0xtorn365.eth",
"tornrelayers.eth",
"moon-relayer.eth"
].map(resolveAddr));
const relayerRegistryOldContract = await loadFixture(getOldRelayerRegistryContractFixture);
let areRegistered = await Promise.all(cheatingRelayers.map(r => relayerRegistryOldContract.isRelayer(r)));
let balances = await Promise.all(cheatingRelayers.map(r => relayerRegistryOldContract.getRelayerBalance(r)));
expect(areRegistered).satisfy(v => v.every(v => v === true));
expect(balances).satisfy(v => v.every(v => v >= 0n));
const { relayerRegistryContract } = await loadFixture(deployAndExecuteFixture);
areRegistered = await Promise.all(cheatingRelayers.map(r => relayerRegistryContract.isRelayer(r)));
balances = await Promise.all(cheatingRelayers.map(r => relayerRegistryContract.getRelayerBalance(r)));
expect(areRegistered).satisfy(v => v.every(v => v === false));
expect(balances).satisfy(v => v.every(v => v === 0n));
})
})
it("Unregistered relayers can register again", async function(){
const { relayerRegistryContract, governanceSigner } = await loadFixture(deployAndExecuteFixture);
const relayerEns = "moon-relayer.eth";
const unregisteredRelayer = await resolveAddr(relayerEns);
const tornContract = await ethers.getContractAt(require("./abi/torn.abi.json"), "0x77777FeDdddFfC19Ff86DB637967013e6C6A116C", governanceSigner);
const toStake = 2000n * 10n ** 18n;
await tornContract.transfer(unregisteredRelayer, toStake);
const relayerSigner = await ethers.getImpersonatedSigner(unregisteredRelayer);
await relayerRegistryContract.connect(relayerSigner).register(relayerEns, toStake, []);
const isRelayerRegistered = await relayerRegistryContract.isRelayerRegistered(unregisteredRelayer, unregisteredRelayer);
const relayerBalance = await relayerRegistryContract.getRelayerBalance(unregisteredRelayer);
expect(isRelayerRegistered).to.be.equal(true);
expect(relayerBalance).to.be.equal(toStake);
})
});