123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
- const { MAX_UINT256, ZERO_ADDRESS } = constants;
- const { fromRpcSig } = require('ethereumjs-util');
- const ethSigUtil = require('eth-sig-util');
- const Wallet = require('ethereumjs-wallet').default;
- const { EIP712Domain, domainSeparator } = require('../../helpers/eip712');
- const { web3 } = require('hardhat');
- const Delegation = [
- { name: 'delegatee', type: 'address' },
- { name: 'nonce', type: 'uint256' },
- { name: 'expiry', type: 'uint256' },
- ];
- const version = '1';
- 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(accounts[0])).to.be.bignumber.equal('0');
- });
- it('domain separator', async function () {
- expect(
- await this.votes.DOMAIN_SEPARATOR(),
- ).to.equal(
- await domainSeparator(this.name, version, this.chainId, this.votes.address),
- );
- });
- describe('delegation', function () {
- const token = tokens[0];
- it('delegation without tokens', async function () {
- expect(await this.votes.delegates(accounts[1])).to.be.equal(ZERO_ADDRESS);
- const { receipt } = await this.votes.delegate(accounts[1], { from: accounts[1] });
- expectEvent(receipt, 'DelegateChanged', {
- delegator: accounts[1],
- fromDelegate: ZERO_ADDRESS,
- toDelegate: accounts[1],
- });
- expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
- expect(await this.votes.delegates(accounts[1])).to.be.equal(accounts[1]);
- });
- it('delegation with tokens', async function () {
- await this.votes.mint(accounts[1], token);
- const weight = getWeight(token);
- expect(await this.votes.delegates(accounts[1])).to.be.equal(ZERO_ADDRESS);
- const { receipt } = await this.votes.delegate(accounts[1], { from: accounts[1] });
- expectEvent(receipt, 'DelegateChanged', {
- delegator: accounts[1],
- fromDelegate: ZERO_ADDRESS,
- toDelegate: accounts[1],
- });
- expectEvent(receipt, 'DelegateVotesChanged', {
- 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: accounts[2],
- previousBalance: '0',
- newBalance: weight,
- });
- 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(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(accounts[1], { from: accounts[1] });
- });
- it('reverts if block number >= current block', async function () {
- await expectRevert(
- this.votes.getPastTotalSupply(5e10),
- 'block not yet mined',
- );
- });
- it('returns 0 if there are no checkpoints', async function () {
- expect(await this.votes.getPastTotalSupply(0)).to.be.bignumber.equal('0');
- });
- 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();
- const weight = tokens.map(getWeight);
- 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 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(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(accounts[2], 5e10),
- 'block not yet mined',
- );
- });
- it('returns 0 if there are no checkpoints', async function () {
- 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 tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
- await time.advanceBlock();
- await time.advanceBlock();
- 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 tx = await this.votes.delegate(accounts[2], { from: accounts[1] });
- await time.advanceBlock();
- await time.advanceBlock();
- expect(await this.votes.getPastVotes(accounts[2], tx.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
- });
- });
- });
- });
- }
- module.exports = {
- shouldBehaveLikeVotes,
- };
|