Votes.behavior.js 14 KB

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