Prepare tests for hardhat-exposed transition (#3930)

Co-authored-by: Francisco <frangio.1@gmail.com>
This commit is contained in:
Hadrien Croubois
2023-01-12 09:51:29 +01:00
committed by GitHub
parent d210847e28
commit 7c6e289782
13 changed files with 369 additions and 418 deletions

View File

@ -17,10 +17,12 @@ const Delegation = [
const version = '1';
function shouldBehaveLikeVotes () {
function shouldBehaveLikeVotes (accounts, tokens, fungible = true) {
const getWeight = token => web3.utils.toBN(fungible ? token : 1);
describe('run votes workflow', function () {
it('initial nonce is 0', async function () {
expect(await this.votes.nonces(this.account1)).to.be.bignumber.equal('0');
expect(await this.votes.nonces(accounts[0])).to.be.bignumber.equal('0');
});
it('domain separator', async function () {
@ -31,207 +33,207 @@ function shouldBehaveLikeVotes () {
);
});
describe('delegation with signature', function () {
const delegator = Wallet.generate();
const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
const nonce = 0;
describe('delegation', function () {
const token = tokens[0];
const buildData = (chainId, verifyingContract, name, message) => ({
data: {
primaryType: 'Delegation',
types: { EIP712Domain, Delegation },
domain: { name, version, chainId, verifyingContract },
message,
},
});
it('delegation without tokens', async function () {
expect(await this.votes.delegates(accounts[1])).to.be.equal(ZERO_ADDRESS);
beforeEach(async function () {
await this.votes.mint(delegatorAddress, this.token0);
});
it('accept signed delegation', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
));
expect(await this.votes.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
const { receipt } = await this.votes.delegate(accounts[1], { from: accounts[1] });
expectEvent(receipt, 'DelegateChanged', {
delegator: delegatorAddress,
delegator: accounts[1],
fromDelegate: ZERO_ADDRESS,
toDelegate: delegatorAddress,
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: delegatorAddress,
previousBalance: '0',
newBalance: '1',
toDelegate: accounts[1],
});
expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
expect(await this.votes.delegates(delegatorAddress)).to.be.equal(delegatorAddress);
expect(await this.votes.getVotes(delegatorAddress)).to.be.bignumber.equal('1');
expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[1]);
});
it('rejects reused signature', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
));
it('delegation with tokens', async function () {
await this.votes.mint(accounts[1], token);
const weight = getWeight(token);
await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
expect(await this.votes.delegates(accounts[1])).to.be.equal(ZERO_ADDRESS);
await expectRevert(
this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
'Votes: invalid nonce',
);
});
it('rejects bad delegatee', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
));
const receipt = await this.votes.delegateBySig(this.account1Delegatee, nonce, MAX_UINT256, v, r, s);
const { args } = receipt.logs.find(({ event }) => event === 'DelegateChanged');
expect(args.delegator).to.not.be.equal(delegatorAddress);
expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
expect(args.toDelegate).to.be.equal(this.account1Delegatee);
});
it('rejects bad nonce', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee: delegatorAddress,
nonce,
expiry: MAX_UINT256,
}),
));
await expectRevert(
this.votes.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
'Votes: invalid nonce',
);
});
it('rejects expired permit', async function () {
const expiry = (await time.latest()) - time.duration.weeks(1);
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee: delegatorAddress,
nonce,
expiry,
}),
));
await expectRevert(
this.votes.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),
'Votes: signature expired',
);
});
});
describe('set delegation', function () {
describe('call', function () {
it('delegation with tokens', async function () {
await this.votes.mint(this.account1, this.token0);
expect(await this.votes.delegates(this.account1)).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegate(this.account1, { from: this.account1 });
expectEvent(receipt, 'DelegateChanged', {
delegator: this.account1,
fromDelegate: ZERO_ADDRESS,
toDelegate: this.account1,
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: this.account1,
previousBalance: '0',
newBalance: '1',
});
expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
expect(await this.votes.getVotes(this.account1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber)).to.be.bignumber.equal('1');
});
it('delegation without tokens', async function () {
expect(await this.votes.delegates(this.account1)).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegate(this.account1, { from: this.account1 });
expectEvent(receipt, 'DelegateChanged', {
delegator: this.account1,
fromDelegate: ZERO_ADDRESS,
toDelegate: this.account1,
});
expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
});
});
});
describe('change delegation', function () {
beforeEach(async function () {
await this.votes.mint(this.account1, this.token0);
await this.votes.delegate(this.account1, { from: this.account1 });
});
it('call', async function () {
expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
const { receipt } = await this.votes.delegate(this.account1Delegatee, { from: this.account1 });
const { receipt } = await this.votes.delegate(accounts[1], { from: accounts[1] });
expectEvent(receipt, 'DelegateChanged', {
delegator: this.account1,
fromDelegate: this.account1,
toDelegate: this.account1Delegatee,
delegator: accounts[1],
fromDelegate: ZERO_ADDRESS,
toDelegate: accounts[1],
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: this.account1,
previousBalance: '1',
delegate: accounts[1],
previousBalance: '0',
newBalance: weight,
});
expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[1]);
expect(await this.votes.getVotes(accounts[1])).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber)).to.be.bignumber.equal(weight);
});
it('delegation update', async function () {
await this.votes.delegate(accounts[1], { from: accounts[1] });
await this.votes.mint(accounts[1], token);
const weight = getWeight(token);
expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[1]);
expect(await this.votes.getVotes(accounts[1])).to.be.bignumber.equal(weight);
expect(await this.votes.getVotes(accounts[2])).to.be.bignumber.equal('0');
const { receipt } = await this.votes.delegate(accounts[2], { from: accounts[1] });
expectEvent(receipt, 'DelegateChanged', {
delegator: accounts[1],
fromDelegate: accounts[1],
toDelegate: accounts[2],
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: accounts[1],
previousBalance: weight,
newBalance: '0',
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: this.account1Delegatee,
delegate: accounts[2],
previousBalance: '0',
newBalance: '1',
newBalance: weight,
});
const prevBlock = receipt.blockNumber - 1;
expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1Delegatee);
expect(await this.votes.getVotes(this.account1)).to.be.bignumber.equal('0');
expect(await this.votes.getVotes(this.account1Delegatee)).to.be.bignumber.equal('1');
expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber - 1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastVotes(this.account1Delegatee, prevBlock)).to.be.bignumber.equal('0');
expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[2]);
expect(await this.votes.getVotes(accounts[1])).to.be.bignumber.equal('0');
expect(await this.votes.getVotes(accounts[2])).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber - 1)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(accounts[2], receipt.blockNumber - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(this.account1Delegatee, receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await this.votes.getPastVotes(accounts[1], receipt.blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], receipt.blockNumber)).to.be.bignumber.equal(weight);
});
describe('with signature', function () {
const delegator = Wallet.generate();
const [delegatee, other] = accounts;
const nonce = 0;
delegator.address = web3.utils.toChecksumAddress(delegator.getAddressString());
const buildData = (chainId, verifyingContract, name, message) => ({
data: {
primaryType: 'Delegation',
types: { EIP712Domain, Delegation },
domain: { name, version, chainId, verifyingContract },
message,
},
});
it('accept signed delegation', async function () {
await this.votes.mint(delegator.address, token);
const weight = getWeight(token);
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
));
expect(await this.votes.delegates(delegator.address)).to.be.equal(ZERO_ADDRESS);
const { receipt } = await this.votes.delegateBySig(delegatee, nonce, MAX_UINT256, v, r, s);
expectEvent(receipt, 'DelegateChanged', {
delegator: delegator.address,
fromDelegate: ZERO_ADDRESS,
toDelegate: delegatee,
});
expectEvent(receipt, 'DelegateVotesChanged', {
delegate: delegatee,
previousBalance: '0',
newBalance: weight,
});
expect(await this.votes.delegates(delegator.address)).to.be.equal(delegatee);
expect(await this.votes.getVotes(delegator.address)).to.be.bignumber.equal('0');
expect(await this.votes.getVotes(delegatee)).to.be.bignumber.equal(weight);
expect(await this.votes.getPastVotes(delegatee, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
await time.advanceBlock();
expect(await this.votes.getPastVotes(delegatee, receipt.blockNumber)).to.be.bignumber.equal(weight);
});
it('rejects reused signature', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
));
await this.votes.delegateBySig(delegatee, nonce, MAX_UINT256, v, r, s);
await expectRevert(
this.votes.delegateBySig(delegatee, nonce, MAX_UINT256, v, r, s),
'Votes: invalid nonce',
);
});
it('rejects bad delegatee', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry: MAX_UINT256,
}),
));
const receipt = await this.votes.delegateBySig(other, nonce, MAX_UINT256, v, r, s);
const { args } = receipt.logs.find(({ event }) => event === 'DelegateChanged');
expect(args.delegator).to.not.be.equal(delegator.address);
expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
expect(args.toDelegate).to.be.equal(other);
});
it('rejects bad nonce', async function () {
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce: nonce + 1,
expiry: MAX_UINT256,
}),
));
await expectRevert(
this.votes.delegateBySig(delegatee, nonce + 1, MAX_UINT256, v, r, s),
'Votes: invalid nonce',
);
});
it('rejects expired permit', async function () {
const expiry = (await time.latest()) - time.duration.weeks(1);
const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
delegator.getPrivateKey(),
buildData(this.chainId, this.votes.address, this.name, {
delegatee,
nonce,
expiry,
}),
));
await expectRevert(
this.votes.delegateBySig(delegatee, nonce, expiry, v, r, s),
'Votes: signature expired',
);
});
});
});
describe('getPastTotalSupply', function () {
beforeEach(async function () {
await this.votes.delegate(this.account1, { from: this.account1 });
await this.votes.delegate(accounts[1], { from: accounts[1] });
});
it('reverts if block number >= current block', async function () {
@ -245,95 +247,77 @@ function shouldBehaveLikeVotes () {
expect(await this.votes.getPastTotalSupply(0)).to.be.bignumber.equal('0');
});
it('returns the latest block if >= last checkpoint block', async function () {
const t1 = await this.votes.mint(this.account1, this.token0);
it('returns the correct checkpointed total supply', async function () {
const blockNumber = Number(await time.latestBlock());
await this.votes.mint(accounts[1], tokens[0]); // mint 0
await time.advanceBlock();
await this.votes.mint(accounts[1], tokens[1]); // mint 1
await time.advanceBlock();
await this.votes.burn(...(fungible ? [accounts[1]] : []), tokens[1]); // burn 1
await time.advanceBlock();
await this.votes.mint(accounts[1], tokens[2]); // mint 2
await time.advanceBlock();
await this.votes.burn(...(fungible ? [accounts[1]] : []), tokens[0]); // burn 0
await time.advanceBlock();
await this.votes.burn(...(fungible ? [accounts[1]] : []), tokens[2]); // burn 2
await time.advanceBlock();
expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
});
const weight = tokens.map(getWeight);
it('returns zero if < first checkpoint block', async function () {
await time.advanceBlock();
const t2 = await this.votes.mint(this.account1, this.token1);
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
});
it('generally returns the voting balance at the appropriate checkpoint', async function () {
const t1 = await this.votes.mint(this.account1, this.token1);
await time.advanceBlock();
await time.advanceBlock();
const t2 = await this.votes.burn(this.account1, this.token1);
await time.advanceBlock();
await time.advanceBlock();
const t3 = await this.votes.mint(this.account1, this.token2);
await time.advanceBlock();
await time.advanceBlock();
const t4 = await this.votes.burn(this.account1, this.token2);
await time.advanceBlock();
await time.advanceBlock();
const t5 = await this.votes.mint(this.account1, this.token3);
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
expect(await this.votes.getPastTotalSupply(blockNumber)).to.be.bignumber.equal('0');
expect(await this.votes.getPastTotalSupply(blockNumber + 1)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 2)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 3)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(blockNumber + 4)).to.be.bignumber.equal(weight[0].add(weight[1]));
expect(await this.votes.getPastTotalSupply(blockNumber + 5)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 6)).to.be.bignumber.equal(weight[0]);
expect(await this.votes.getPastTotalSupply(blockNumber + 7)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(blockNumber + 8)).to.be.bignumber.equal(weight[0].add(weight[2]));
expect(await this.votes.getPastTotalSupply(blockNumber + 9)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(blockNumber + 10)).to.be.bignumber.equal(weight[2]);
expect(await this.votes.getPastTotalSupply(blockNumber + 11)).to.be.bignumber.equal('0');
await expectRevert(this.votes.getPastTotalSupply(blockNumber + 12), 'Votes: block not yet mined');
});
});
// The following tests are a adaptation of
// The following tests are an adaptation of
// https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
describe('Compound test suite', function () {
beforeEach(async function () {
await this.votes.mint(this.account1, this.token0);
await this.votes.mint(this.account1, this.token1);
await this.votes.mint(this.account1, this.token2);
await this.votes.mint(this.account1, this.token3);
await this.votes.mint(accounts[1], tokens[0]);
await this.votes.mint(accounts[1], tokens[1]);
await this.votes.mint(accounts[1], tokens[2]);
});
describe('getPastVotes', function () {
it('reverts if block number >= current block', async function () {
await expectRevert(
this.votes.getPastVotes(this.account2, 5e10),
this.votes.getPastVotes(accounts[2], 5e10),
'block not yet mined',
);
});
it('returns 0 if there are no checkpoints', async function () {
expect(await this.votes.getPastVotes(this.account2, 0)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], 0)).to.be.bignumber.equal('0');
});
it('returns the latest block if >= last checkpoint block', async function () {
const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
const tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
await time.advanceBlock();
await time.advanceBlock();
const latest = await this.votes.getVotes(this.account2);
const nextBlock = t1.receipt.blockNumber + 1;
expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber)).to.be.bignumber.equal(latest);
expect(await this.votes.getPastVotes(this.account2, nextBlock)).to.be.bignumber.equal(latest);
const latest = await this.votes.getVotes(accounts[2]);
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber)).to.be.bignumber.equal(latest);
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber + 1)).to.be.bignumber.equal(latest);
});
it('returns zero if < first checkpoint block', async function () {
await time.advanceBlock();
const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
const tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
await time.advanceBlock();
await time.advanceBlock();
expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
});
});
});