Votes.behavior.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. const { constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  2. const { MAX_UINT256, ZERO_ADDRESS } = constants;
  3. const { fromRpcSig } = require('ethereumjs-util');
  4. const ethSigUtil = require('eth-sig-util');
  5. const Wallet = require('ethereumjs-wallet').default;
  6. const { EIP712Domain, domainSeparator } = require('../../helpers/eip712');
  7. const { web3 } = require('hardhat');
  8. const Delegation = [
  9. { name: 'delegatee', type: 'address' },
  10. { name: 'nonce', type: 'uint256' },
  11. { name: 'expiry', type: 'uint256' },
  12. ];
  13. const version = '1';
  14. function shouldBehaveLikeVotes () {
  15. describe('run votes workflow', function () {
  16. it('initial nonce is 0', async function () {
  17. expect(await this.votes.nonces(this.account1)).to.be.bignumber.equal('0');
  18. });
  19. it('domain separator', async function () {
  20. expect(
  21. await this.votes.DOMAIN_SEPARATOR(),
  22. ).to.equal(
  23. await domainSeparator(this.name, version, this.chainId, this.votes.address),
  24. );
  25. });
  26. describe('delegation with signature', function () {
  27. const delegator = Wallet.generate();
  28. const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
  29. const nonce = 0;
  30. const buildData = (chainId, verifyingContract, name, message) => ({
  31. data: {
  32. primaryType: 'Delegation',
  33. types: { EIP712Domain, Delegation },
  34. domain: { name, version, chainId, verifyingContract },
  35. message,
  36. },
  37. });
  38. beforeEach(async function () {
  39. await this.votes.mint(delegatorAddress, this.token0);
  40. });
  41. it('accept signed delegation', async function () {
  42. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  43. delegator.getPrivateKey(),
  44. buildData(this.chainId, this.votes.address, this.name, {
  45. delegatee: delegatorAddress,
  46. nonce,
  47. expiry: MAX_UINT256,
  48. }),
  49. ));
  50. expect(await this.votes.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
  51. const { receipt } = await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  52. expectEvent(receipt, 'DelegateChanged', {
  53. delegator: delegatorAddress,
  54. fromDelegate: ZERO_ADDRESS,
  55. toDelegate: delegatorAddress,
  56. });
  57. expectEvent(receipt, 'DelegateVotesChanged', {
  58. delegate: delegatorAddress,
  59. previousBalance: '0',
  60. newBalance: '1',
  61. });
  62. expect(await this.votes.delegates(delegatorAddress)).to.be.equal(delegatorAddress);
  63. expect(await this.votes.getVotes(delegatorAddress)).to.be.bignumber.equal('1');
  64. expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  65. await time.advanceBlock();
  66. expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber)).to.be.bignumber.equal('1');
  67. });
  68. it('rejects reused signature', async function () {
  69. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  70. delegator.getPrivateKey(),
  71. buildData(this.chainId, this.votes.address, this.name, {
  72. delegatee: delegatorAddress,
  73. nonce,
  74. expiry: MAX_UINT256,
  75. }),
  76. ));
  77. await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  78. await expectRevert(
  79. this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
  80. 'Votes: invalid nonce',
  81. );
  82. });
  83. it('rejects bad delegatee', async function () {
  84. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  85. delegator.getPrivateKey(),
  86. buildData(this.chainId, this.votes.address, this.name, {
  87. delegatee: delegatorAddress,
  88. nonce,
  89. expiry: MAX_UINT256,
  90. }),
  91. ));
  92. const receipt = await this.votes.delegateBySig(this.account1Delegatee, nonce, MAX_UINT256, v, r, s);
  93. const { args } = receipt.logs.find(({ event }) => event === 'DelegateChanged');
  94. expect(args.delegator).to.not.be.equal(delegatorAddress);
  95. expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
  96. expect(args.toDelegate).to.be.equal(this.account1Delegatee);
  97. });
  98. it('rejects bad nonce', async function () {
  99. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  100. delegator.getPrivateKey(),
  101. buildData(this.chainId, this.votes.address, this.name, {
  102. delegatee: delegatorAddress,
  103. nonce,
  104. expiry: MAX_UINT256,
  105. }),
  106. ));
  107. await expectRevert(
  108. this.votes.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
  109. 'Votes: invalid nonce',
  110. );
  111. });
  112. it('rejects expired permit', async function () {
  113. const expiry = (await time.latest()) - time.duration.weeks(1);
  114. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  115. delegator.getPrivateKey(),
  116. buildData(this.chainId, this.votes.address, this.name, {
  117. delegatee: delegatorAddress,
  118. nonce,
  119. expiry,
  120. }),
  121. ));
  122. await expectRevert(
  123. this.votes.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),
  124. 'Votes: signature expired',
  125. );
  126. });
  127. });
  128. describe('set delegation', function () {
  129. describe('call', function () {
  130. it('delegation with tokens', async function () {
  131. await this.votes.mint(this.account1, this.token0);
  132. expect(await this.votes.delegates(this.account1)).to.be.equal(ZERO_ADDRESS);
  133. const { receipt } = await this.votes.delegate(this.account1, { from: this.account1 });
  134. expectEvent(receipt, 'DelegateChanged', {
  135. delegator: this.account1,
  136. fromDelegate: ZERO_ADDRESS,
  137. toDelegate: this.account1,
  138. });
  139. expectEvent(receipt, 'DelegateVotesChanged', {
  140. delegate: this.account1,
  141. previousBalance: '0',
  142. newBalance: '1',
  143. });
  144. expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
  145. expect(await this.votes.getVotes(this.account1)).to.be.bignumber.equal('1');
  146. expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  147. await time.advanceBlock();
  148. expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber)).to.be.bignumber.equal('1');
  149. });
  150. it('delegation without tokens', async function () {
  151. expect(await this.votes.delegates(this.account1)).to.be.equal(ZERO_ADDRESS);
  152. const { receipt } = await this.votes.delegate(this.account1, { from: this.account1 });
  153. expectEvent(receipt, 'DelegateChanged', {
  154. delegator: this.account1,
  155. fromDelegate: ZERO_ADDRESS,
  156. toDelegate: this.account1,
  157. });
  158. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  159. expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
  160. });
  161. });
  162. });
  163. describe('change delegation', function () {
  164. beforeEach(async function () {
  165. await this.votes.mint(this.account1, this.token0);
  166. await this.votes.delegate(this.account1, { from: this.account1 });
  167. });
  168. it('call', async function () {
  169. expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1);
  170. const { receipt } = await this.votes.delegate(this.account1Delegatee, { from: this.account1 });
  171. expectEvent(receipt, 'DelegateChanged', {
  172. delegator: this.account1,
  173. fromDelegate: this.account1,
  174. toDelegate: this.account1Delegatee,
  175. });
  176. expectEvent(receipt, 'DelegateVotesChanged', {
  177. delegate: this.account1,
  178. previousBalance: '1',
  179. newBalance: '0',
  180. });
  181. expectEvent(receipt, 'DelegateVotesChanged', {
  182. delegate: this.account1Delegatee,
  183. previousBalance: '0',
  184. newBalance: '1',
  185. });
  186. const prevBlock = receipt.blockNumber - 1;
  187. expect(await this.votes.delegates(this.account1)).to.be.equal(this.account1Delegatee);
  188. expect(await this.votes.getVotes(this.account1)).to.be.bignumber.equal('0');
  189. expect(await this.votes.getVotes(this.account1Delegatee)).to.be.bignumber.equal('1');
  190. expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber - 1)).to.be.bignumber.equal('1');
  191. expect(await this.votes.getPastVotes(this.account1Delegatee, prevBlock)).to.be.bignumber.equal('0');
  192. await time.advanceBlock();
  193. expect(await this.votes.getPastVotes(this.account1, receipt.blockNumber)).to.be.bignumber.equal('0');
  194. expect(await this.votes.getPastVotes(this.account1Delegatee, receipt.blockNumber)).to.be.bignumber.equal('1');
  195. });
  196. });
  197. describe('getPastTotalSupply', function () {
  198. beforeEach(async function () {
  199. await this.votes.delegate(this.account1, { from: this.account1 });
  200. });
  201. it('reverts if block number >= current block', async function () {
  202. await expectRevert(
  203. this.votes.getPastTotalSupply(5e10),
  204. 'block not yet mined',
  205. );
  206. });
  207. it('returns 0 if there are no checkpoints', async function () {
  208. expect(await this.votes.getPastTotalSupply(0)).to.be.bignumber.equal('0');
  209. });
  210. it('returns the latest block if >= last checkpoint block', async function () {
  211. const t1 = await this.votes.mint(this.account1, this.token0);
  212. await time.advanceBlock();
  213. await time.advanceBlock();
  214. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  215. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  216. });
  217. it('returns zero if < first checkpoint block', async function () {
  218. await time.advanceBlock();
  219. const t2 = await this.votes.mint(this.account1, this.token1);
  220. await time.advanceBlock();
  221. await time.advanceBlock();
  222. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  223. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  224. });
  225. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  226. const t1 = await this.votes.mint(this.account1, this.token1);
  227. await time.advanceBlock();
  228. await time.advanceBlock();
  229. const t2 = await this.votes.burn(this.account1, this.token1);
  230. await time.advanceBlock();
  231. await time.advanceBlock();
  232. const t3 = await this.votes.mint(this.account1, this.token2);
  233. await time.advanceBlock();
  234. await time.advanceBlock();
  235. const t4 = await this.votes.burn(this.account1, this.token2);
  236. await time.advanceBlock();
  237. await time.advanceBlock();
  238. const t5 = await this.votes.mint(this.account1, this.token3);
  239. await time.advanceBlock();
  240. await time.advanceBlock();
  241. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  242. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber)).to.be.bignumber.equal('1');
  243. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  244. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber)).to.be.bignumber.equal('0');
  245. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
  246. expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber)).to.be.bignumber.equal('1');
  247. expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  248. expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber)).to.be.bignumber.equal('0');
  249. expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
  250. expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber)).to.be.bignumber.equal('1');
  251. expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  252. });
  253. });
  254. // The following tests are a adaptation of
  255. // https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
  256. describe('Compound test suite', function () {
  257. beforeEach(async function () {
  258. await this.votes.mint(this.account1, this.token0);
  259. await this.votes.mint(this.account1, this.token1);
  260. await this.votes.mint(this.account1, this.token2);
  261. await this.votes.mint(this.account1, this.token3);
  262. });
  263. describe('getPastVotes', function () {
  264. it('reverts if block number >= current block', async function () {
  265. await expectRevert(
  266. this.votes.getPastVotes(this.account2, 5e10),
  267. 'block not yet mined',
  268. );
  269. });
  270. it('returns 0 if there are no checkpoints', async function () {
  271. expect(await this.votes.getPastVotes(this.account2, 0)).to.be.bignumber.equal('0');
  272. });
  273. it('returns the latest block if >= last checkpoint block', async function () {
  274. const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
  275. await time.advanceBlock();
  276. await time.advanceBlock();
  277. const latest = await this.votes.getVotes(this.account2);
  278. const nextBlock = t1.receipt.blockNumber + 1;
  279. expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber)).to.be.bignumber.equal(latest);
  280. expect(await this.votes.getPastVotes(this.account2, nextBlock)).to.be.bignumber.equal(latest);
  281. });
  282. it('returns zero if < first checkpoint block', async function () {
  283. await time.advanceBlock();
  284. const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
  285. await time.advanceBlock();
  286. await time.advanceBlock();
  287. expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  288. });
  289. });
  290. });
  291. });
  292. }
  293. module.exports = {
  294. shouldBehaveLikeVotes,
  295. };