const { expect } = require('chai') const { ethers } = require('hardhat') const { BigNumber } = require('@ethersproject/bignumber') const { propose } = require('../../scripts/helper/propose_proposal.js') const config = require('../../config') const { getSignerFromAddress, takeSnapshot, revertSnapshot, advanceTime } = require('../utils') function printBalanceWithDescription(descr, bn) { console.log( `\n ${descr} => ${ bn.div(BigNumber.from(10).pow(18)).toString() + '.' + bn.div(BigNumber.from(10).pow(14)).toString().slice(-4) }`, ) } describe('V3 governance tests', () => { const zero = BigNumber.from(0) const ProposalState = { Pending: 0, Active: 1, Defeated: 2, Timelocked: 3, AwaitingExecution: 4, Executed: 5, Expired: 6, } let periods = { EXECUTION_DELAY: zero, EXECUTION_EXPIRATION: zero, QUORUM_VOTES: zero, PROPOSAL_THRESHOLD: zero, VOTING_DELAY: zero, VOTING_PERIOD: zero, CLOSING_PERIOD: zero, VOTE_EXTEND_TIME: zero, } async function setPeriods(_periods, govc) { _periods.EXECUTION_DELAY = await govc.EXECUTION_DELAY() _periods.EXECUTION_EXPIRATION = await govc.EXECUTION_EXPIRATION() _periods.QUORUM_VOTES = await govc.QUORUM_VOTES() _periods.PROPOSAL_THRESHOLD = await govc.PROPOSAL_THRESHOLD() _periods.VOTING_DELAY = await govc.VOTING_DELAY() _periods.VOTING_PERIOD = await govc.VOTING_PERIOD() _periods.CLOSING_PERIOD = await govc.CLOSING_PERIOD() _periods.VOTE_EXTEND_TIME = await govc.VOTE_EXTEND_TIME() return _periods } let quorumVotes let GovernanceContract let TornToken let provider let tornwhale let proposer let propfacfac let propfac let torn let gov // From other tests let getToken = async (tokenAddress) => { return await ethers.getContractAt('@openzeppelin/contracts/token/ERC20/ERC20.sol:ERC20', tokenAddress) } let minewait = async (time) => { await ethers.provider.send('evm_increaseTime', [time]) await ethers.provider.send('evm_mine', []) } let sendr = async (method, params) => { return await ethers.provider.send(method, params) } let clog = (...x) => { console.log(x) } let pE = (x) => { return ethers.utils.parseEther(`${x}`) } let snapshotId before(async function () { // Pick our signer proposer = (await ethers.getSigners())[2] // Ok get current gov gov = (await ethers.getContractAt('GovernanceStakingUpgrade', config.governance)).connect(proposer) // Impersonate await sendr('hardhat_impersonateAccount', [config.governance]) // Pick whale tornwhale = ethers.provider.getSigner(config.governance) // Connect to above torn = (await getToken(config.TORN)).connect(tornwhale) // Set balance of governance contract await ethers.provider.send('hardhat_setBalance', [proposer.address, pE(10).toHexString()]) // Take gov balance const govbal = await torn.balanceOf(gov.address) // Transfer await torn.transfer(proposer.address, govbal) // Check bal was allocated expect(await torn.balanceOf(proposer.address)).to.equal(govbal) // Connect torn = torn.connect(proposer) periods = await setPeriods(periods, gov) // Factory of the proposal propfacfac = await ethers.getContractFactory('ProposalContractFactory') propfac = await ethers.getContractFactory('PatchProposal') snapshotId = await takeSnapshot() }) afterEach(async () => { await revertSnapshot(snapshotId) snapshotId = await takeSnapshot() }) describe('Patch: Main integrative', () => { it('Should be able to pass all predicates concerning patch update', async () => { // Load these vars const oldVaultAddr = await gov.userVault() const oldGasCompAddr = await gov.gasCompensationVault() const oldStaking = await gov.Staking() const govBalBef = await torn.balanceOf(gov.address) const propBal = await torn.balanceOf(proposer.address) // Start proposing const proposal = await propfac.deploy((await propfacfac.deploy()).address) await torn.approve(gov.address, propBal) await gov.connect(proposer).lockWithApproval(propBal) await gov.propose(proposal.address, 'PATCH') const proposalId = await gov.latestProposalIds(proposer.address) let proposalData = await gov.proposals(proposalId) await minewait(periods.VOTING_DELAY.add(1).toNumber()) await gov.castVote(proposalId, true) await ethers.provider.send('evm_setNextBlockTimestamp', [ proposalData.endTime.add(periods.EXECUTION_DELAY).add(BigNumber.from(1000)).toNumber(), ]) await ethers.provider.send('evm_mine', []) await gov.execute(proposalId) const newVaultAddr = await gov.userVault() const newGasCompAddr = await gov.gasCompensationVault() const newStaking = await gov.Staking() expect(oldGasCompAddr).to.equal(newGasCompAddr) expect(newVaultAddr).to.equal(oldVaultAddr) expect(newStaking) .to.not.equal(oldStaking) .and.to.not.equal('0x0000000000000000000000000000000000000000') }) }) })