Votes.behavior.js 14 KB

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