Votes.behavior.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 { getDomain, domainType, domainSeparator } = require('../../helpers/eip712');
  7. const Delegation = [
  8. { name: 'delegatee', type: 'address' },
  9. { name: 'nonce', type: 'uint256' },
  10. { name: 'expiry', type: 'uint256' },
  11. ];
  12. function shouldBehaveLikeVotes() {
  13. describe('run votes workflow', function () {
  14. it('initial nonce is 0', async function () {
  15. expect(await this.votes.nonces(this.account1)).to.be.bignumber.equal('0');
  16. });
  17. it('domain separator', async function () {
  18. expect(await this.votes.DOMAIN_SEPARATOR()).to.equal(domainSeparator(await getDomain(this.votes)));
  19. });
  20. describe('delegation with signature', function () {
  21. const delegator = Wallet.generate();
  22. const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
  23. const nonce = 0;
  24. const buildAndSignData = async (contract, message, pk) => {
  25. const data = await getDomain(contract).then(domain => ({
  26. primaryType: 'Delegation',
  27. types: { EIP712Domain: domainType(domain), Delegation },
  28. domain,
  29. message,
  30. }));
  31. return fromRpcSig(ethSigUtil.signTypedMessage(pk, { data }));
  32. };
  33. beforeEach(async function () {
  34. await this.votes.$_mint(delegatorAddress, this.NFT0);
  35. });
  36. it('accept signed delegation', async function () {
  37. const { v, r, s } = await buildAndSignData(
  38. this.votes,
  39. {
  40. delegatee: delegatorAddress,
  41. nonce,
  42. expiry: MAX_UINT256,
  43. },
  44. delegator.getPrivateKey(),
  45. );
  46. expect(await this.votes.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
  47. const { receipt } = await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  48. expectEvent(receipt, 'DelegateChanged', {
  49. delegator: delegatorAddress,
  50. fromDelegate: ZERO_ADDRESS,
  51. toDelegate: delegatorAddress,
  52. });
  53. expectEvent(receipt, 'DelegateVotesChanged', {
  54. delegate: delegatorAddress,
  55. previousBalance: '0',
  56. newBalance: '1',
  57. });
  58. expect(await this.votes.delegates(delegatorAddress)).to.be.equal(delegatorAddress);
  59. expect(await this.votes.getVotes(delegatorAddress)).to.be.bignumber.equal('1');
  60. expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  61. await time.advanceBlock();
  62. expect(await this.votes.getPastVotes(delegatorAddress, receipt.blockNumber)).to.be.bignumber.equal('1');
  63. });
  64. it('rejects reused signature', async function () {
  65. const { v, r, s } = await buildAndSignData(
  66. this.votes,
  67. {
  68. delegatee: delegatorAddress,
  69. nonce,
  70. expiry: MAX_UINT256,
  71. },
  72. delegator.getPrivateKey(),
  73. );
  74. await this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  75. await expectRevert(
  76. this.votes.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
  77. 'Votes: invalid nonce',
  78. );
  79. });
  80. it('rejects bad delegatee', async function () {
  81. const { v, r, s } = await buildAndSignData(
  82. this.votes,
  83. {
  84. delegatee: delegatorAddress,
  85. nonce,
  86. expiry: MAX_UINT256,
  87. },
  88. delegator.getPrivateKey(),
  89. );
  90. const receipt = await this.votes.delegateBySig(this.account1Delegatee, nonce, MAX_UINT256, v, r, s);
  91. const { args } = receipt.logs.find(({ event }) => event === 'DelegateChanged');
  92. expect(args.delegator).to.not.be.equal(delegatorAddress);
  93. expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
  94. expect(args.toDelegate).to.be.equal(this.account1Delegatee);
  95. });
  96. it('rejects bad nonce', async function () {
  97. const { v, r, s } = await buildAndSignData(
  98. this.votes,
  99. {
  100. delegatee: delegatorAddress,
  101. nonce,
  102. expiry: MAX_UINT256,
  103. },
  104. delegator.getPrivateKey(),
  105. );
  106. await expectRevert(
  107. this.votes.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
  108. 'Votes: invalid nonce',
  109. );
  110. });
  111. it('rejects expired permit', async function () {
  112. const expiry = (await time.latest()) - time.duration.weeks(1);
  113. const { v, r, s } = await buildAndSignData(
  114. this.votes,
  115. {
  116. delegatee: delegatorAddress,
  117. nonce,
  118. expiry,
  119. },
  120. delegator.getPrivateKey(),
  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.NFT0);
  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.NFT0);
  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(this.votes.getPastTotalSupply(5e10), 'block not yet mined');
  203. });
  204. it('returns 0 if there are no checkpoints', async function () {
  205. expect(await this.votes.getPastTotalSupply(0)).to.be.bignumber.equal('0');
  206. });
  207. it('returns the latest block if >= last checkpoint block', async function () {
  208. const t1 = await this.votes.$_mint(this.account1, this.NFT0);
  209. await time.advanceBlock();
  210. await time.advanceBlock();
  211. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  212. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  213. });
  214. it('returns zero if < first checkpoint block', async function () {
  215. await time.advanceBlock();
  216. const t2 = await this.votes.$_mint(this.account1, this.NFT1);
  217. await time.advanceBlock();
  218. await time.advanceBlock();
  219. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  220. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  221. });
  222. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  223. const t1 = await this.votes.$_mint(this.account1, this.NFT1);
  224. await time.advanceBlock();
  225. await time.advanceBlock();
  226. const t2 = await this.votes.$_burn(this.NFT1);
  227. await time.advanceBlock();
  228. await time.advanceBlock();
  229. const t3 = await this.votes.$_mint(this.account1, this.NFT2);
  230. await time.advanceBlock();
  231. await time.advanceBlock();
  232. const t4 = await this.votes.$_burn(this.NFT2);
  233. await time.advanceBlock();
  234. await time.advanceBlock();
  235. const t5 = await this.votes.$_mint(this.account1, this.NFT3);
  236. await time.advanceBlock();
  237. await time.advanceBlock();
  238. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  239. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber)).to.be.bignumber.equal('1');
  240. expect(await this.votes.getPastTotalSupply(t1.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  241. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber)).to.be.bignumber.equal('0');
  242. expect(await this.votes.getPastTotalSupply(t2.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
  243. expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber)).to.be.bignumber.equal('1');
  244. expect(await this.votes.getPastTotalSupply(t3.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  245. expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber)).to.be.bignumber.equal('0');
  246. expect(await this.votes.getPastTotalSupply(t4.receipt.blockNumber + 1)).to.be.bignumber.equal('0');
  247. expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber)).to.be.bignumber.equal('1');
  248. expect(await this.votes.getPastTotalSupply(t5.receipt.blockNumber + 1)).to.be.bignumber.equal('1');
  249. });
  250. });
  251. // The following tests are a adaptation of
  252. // https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
  253. describe('Compound test suite', function () {
  254. beforeEach(async function () {
  255. await this.votes.$_mint(this.account1, this.NFT0);
  256. await this.votes.$_mint(this.account1, this.NFT1);
  257. await this.votes.$_mint(this.account1, this.NFT2);
  258. await this.votes.$_mint(this.account1, this.NFT3);
  259. });
  260. describe('getPastVotes', function () {
  261. it('reverts if block number >= current block', async function () {
  262. await expectRevert(this.votes.getPastVotes(this.account2, 5e10), 'block not yet mined');
  263. });
  264. it('returns 0 if there are no checkpoints', async function () {
  265. expect(await this.votes.getPastVotes(this.account2, 0)).to.be.bignumber.equal('0');
  266. });
  267. it('returns the latest block if >= last checkpoint block', async function () {
  268. const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
  269. await time.advanceBlock();
  270. await time.advanceBlock();
  271. const latest = await this.votes.getVotes(this.account2);
  272. const nextBlock = t1.receipt.blockNumber + 1;
  273. expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber)).to.be.bignumber.equal(latest);
  274. expect(await this.votes.getPastVotes(this.account2, nextBlock)).to.be.bignumber.equal(latest);
  275. });
  276. it('returns zero if < first checkpoint block', async function () {
  277. await time.advanceBlock();
  278. const t1 = await this.votes.delegate(this.account2, { from: this.account1 });
  279. await time.advanceBlock();
  280. await time.advanceBlock();
  281. expect(await this.votes.getPastVotes(this.account2, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  282. });
  283. });
  284. });
  285. });
  286. }
  287. module.exports = {
  288. shouldBehaveLikeVotes,
  289. };