Votes.behavior.js 14 KB

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