draft-ERC20Votes.test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* eslint-disable */
  2. const { BN, constants, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const { MAX_UINT256, ZERO_ADDRESS, ZERO_BYTES32 } = constants;
  5. const { fromRpcSig } = require('ethereumjs-util');
  6. const ethSigUtil = require('eth-sig-util');
  7. const Wallet = require('ethereumjs-wallet').default;
  8. const { promisify } = require('util');
  9. const queue = promisify(setImmediate);
  10. const ERC20VotesMock = artifacts.require('ERC20VotesMock');
  11. const { EIP712Domain, domainSeparator } = require('../../../helpers/eip712');
  12. const Delegation = [
  13. { name: 'delegatee', type: 'address' },
  14. { name: 'nonce', type: 'uint256' },
  15. { name: 'expiry', type: 'uint256' },
  16. ];
  17. async function countPendingTransactions() {
  18. return parseInt(
  19. await network.provider.send('eth_getBlockTransactionCountByNumber', ['pending'])
  20. );
  21. }
  22. async function batchInBlock (txs) {
  23. try {
  24. // disable auto-mining
  25. await network.provider.send('evm_setAutomine', [false]);
  26. // send all transactions
  27. const promises = txs.map(fn => fn());
  28. // wait for node to have all pending transactions
  29. while (txs.length > await countPendingTransactions()) {
  30. await queue();
  31. }
  32. // mine one block
  33. await network.provider.send('evm_mine');
  34. // fetch receipts
  35. const receipts = await Promise.all(promises);
  36. // Sanity check, all tx should be in the same block
  37. const minedBlocks = new Set(receipts.map(({ receipt }) => receipt.blockNumber));
  38. expect(minedBlocks.size).to.equal(1);
  39. return receipts;
  40. } finally {
  41. // enable auto-mining
  42. await network.provider.send('evm_setAutomine', [true]);
  43. }
  44. }
  45. contract('ERC20Votes', function (accounts) {
  46. const [ holder, recipient, holderDelegatee, recipientDelegatee, other1, other2 ] = accounts;
  47. const name = 'My Token';
  48. const symbol = 'MTKN';
  49. const version = '1';
  50. const supply = new BN('10000000000000000000000000');
  51. beforeEach(async function () {
  52. this.token = await ERC20VotesMock.new(name, symbol, holder, supply);
  53. // We get the chain id from the contract because Ganache (used for coverage) does not return the same chain id
  54. // from within the EVM as from the JSON RPC interface.
  55. // See https://github.com/trufflesuite/ganache-core/issues/515
  56. this.chainId = await this.token.getChainId();
  57. });
  58. it('initial nonce is 0', async function () {
  59. expect(await this.token.nonces(holder)).to.be.bignumber.equal('0');
  60. });
  61. it('domain separator', async function () {
  62. expect(
  63. await this.token.DOMAIN_SEPARATOR(),
  64. ).to.equal(
  65. await domainSeparator(name, version, this.chainId, this.token.address),
  66. );
  67. });
  68. it('minting restriction', async function () {
  69. const amount = new BN('2').pow(new BN('224'));
  70. await expectRevert(
  71. ERC20VotesMock.new(name, symbol, holder, amount),
  72. 'ERC20Votes: total supply exceeds 2**224',
  73. );
  74. });
  75. describe('set delegation', function () {
  76. describe('call', function () {
  77. it('delegation with balance', async function () {
  78. expect(await this.token.delegates(holder)).to.be.equal(ZERO_ADDRESS);
  79. const { receipt } = await this.token.delegate(holder, { from: holder });
  80. expectEvent(receipt, 'DelegateChanged', {
  81. delegator: holder,
  82. fromDelegate: ZERO_ADDRESS,
  83. toDelegate: holder,
  84. });
  85. expectEvent(receipt, 'DelegateVotesChanged', {
  86. delegate: holder,
  87. previousBalance: '0',
  88. newBalance: supply,
  89. });
  90. expect(await this.token.delegates(holder)).to.be.equal(holder);
  91. expect(await this.token.getCurrentVotes(holder)).to.be.bignumber.equal(supply);
  92. expect(await this.token.getPriorVotes(holder, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  93. await time.advanceBlock();
  94. expect(await this.token.getPriorVotes(holder, receipt.blockNumber)).to.be.bignumber.equal(supply);
  95. });
  96. it('delegation without balance', async function () {
  97. expect(await this.token.delegates(recipient)).to.be.equal(ZERO_ADDRESS);
  98. const { receipt } = await this.token.delegate(recipient, { from: recipient });
  99. expectEvent(receipt, 'DelegateChanged', {
  100. delegator: recipient,
  101. fromDelegate: ZERO_ADDRESS,
  102. toDelegate: recipient,
  103. });
  104. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  105. expect(await this.token.delegates(recipient)).to.be.equal(recipient);
  106. });
  107. });
  108. describe('with signature', function () {
  109. const delegator = Wallet.generate();
  110. const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
  111. const nonce = 0;
  112. const buildData = (chainId, verifyingContract, message) => ({ data: {
  113. primaryType: 'Delegation',
  114. types: { EIP712Domain, Delegation },
  115. domain: { name, version, chainId, verifyingContract },
  116. message,
  117. }});
  118. beforeEach(async function () {
  119. await this.token.transfer(delegatorAddress, supply, { from: holder });
  120. });
  121. it('accept signed delegation', async function () {
  122. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  123. delegator.getPrivateKey(),
  124. buildData(this.chainId, this.token.address, {
  125. delegatee: delegatorAddress,
  126. nonce,
  127. expiry: MAX_UINT256,
  128. }),
  129. ));
  130. expect(await this.token.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
  131. const { receipt } = await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  132. expectEvent(receipt, 'DelegateChanged', {
  133. delegator: delegatorAddress,
  134. fromDelegate: ZERO_ADDRESS,
  135. toDelegate: delegatorAddress,
  136. });
  137. expectEvent(receipt, 'DelegateVotesChanged', {
  138. delegate: delegatorAddress,
  139. previousBalance: '0',
  140. newBalance: supply,
  141. });
  142. expect(await this.token.delegates(delegatorAddress)).to.be.equal(delegatorAddress);
  143. expect(await this.token.getCurrentVotes(delegatorAddress)).to.be.bignumber.equal(supply);
  144. expect(await this.token.getPriorVotes(delegatorAddress, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  145. await time.advanceBlock();
  146. expect(await this.token.getPriorVotes(delegatorAddress, receipt.blockNumber)).to.be.bignumber.equal(supply);
  147. });
  148. it('rejects reused signature', async function () {
  149. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  150. delegator.getPrivateKey(),
  151. buildData(this.chainId, this.token.address, {
  152. delegatee: delegatorAddress,
  153. nonce,
  154. expiry: MAX_UINT256,
  155. }),
  156. ));
  157. await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  158. await expectRevert(
  159. this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
  160. 'ERC20Votes::delegateBySig: invalid nonce',
  161. );
  162. });
  163. it('rejects bad delegatee', async function () {
  164. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  165. delegator.getPrivateKey(),
  166. buildData(this.chainId, this.token.address, {
  167. delegatee: delegatorAddress,
  168. nonce,
  169. expiry: MAX_UINT256,
  170. }),
  171. ));
  172. const { logs } = await this.token.delegateBySig(holderDelegatee, nonce, MAX_UINT256, v, r, s);
  173. const { args } = logs.find(({ event }) => event == 'DelegateChanged');
  174. expect(args.delegator).to.not.be.equal(delegatorAddress);
  175. expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
  176. expect(args.toDelegate).to.be.equal(holderDelegatee);
  177. });
  178. it('rejects bad nonce', async function () {
  179. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  180. delegator.getPrivateKey(),
  181. buildData(this.chainId, this.token.address, {
  182. delegatee: delegatorAddress,
  183. nonce,
  184. expiry: MAX_UINT256,
  185. }),
  186. ));
  187. await expectRevert(
  188. this.token.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
  189. 'ERC20Votes::delegateBySig: invalid nonce',
  190. );
  191. });
  192. it('rejects expired permit', async function () {
  193. const expiry = (await time.latest()) - time.duration.weeks(1);
  194. const { v, r, s } = fromRpcSig(ethSigUtil.signTypedMessage(
  195. delegator.getPrivateKey(),
  196. buildData(this.chainId, this.token.address, {
  197. delegatee: delegatorAddress,
  198. nonce,
  199. expiry,
  200. }),
  201. ));
  202. await expectRevert(
  203. this.token.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),
  204. 'ERC20Votes::delegateBySig: signature expired',
  205. );
  206. });
  207. });
  208. });
  209. describe('change delegation', function () {
  210. beforeEach(async function () {
  211. await this.token.delegate(holder, { from: holder });
  212. });
  213. it('call', async function () {
  214. expect(await this.token.delegates(holder)).to.be.equal(holder);
  215. const { receipt } = await this.token.delegate(holderDelegatee, { from: holder });
  216. expectEvent(receipt, 'DelegateChanged', {
  217. delegator: holder,
  218. fromDelegate: holder,
  219. toDelegate: holderDelegatee,
  220. });
  221. expectEvent(receipt, 'DelegateVotesChanged', {
  222. delegate: holder,
  223. previousBalance: supply,
  224. newBalance: '0',
  225. });
  226. expectEvent(receipt, 'DelegateVotesChanged', {
  227. delegate: holderDelegatee,
  228. previousBalance: '0',
  229. newBalance: supply,
  230. });
  231. expect(await this.token.delegates(holder)).to.be.equal(holderDelegatee);
  232. expect(await this.token.getCurrentVotes(holder)).to.be.bignumber.equal('0');
  233. expect(await this.token.getCurrentVotes(holderDelegatee)).to.be.bignumber.equal(supply);
  234. expect(await this.token.getPriorVotes(holder, receipt.blockNumber - 1)).to.be.bignumber.equal(supply);
  235. expect(await this.token.getPriorVotes(holderDelegatee, receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  236. await time.advanceBlock();
  237. expect(await this.token.getPriorVotes(holder, receipt.blockNumber)).to.be.bignumber.equal('0');
  238. expect(await this.token.getPriorVotes(holderDelegatee, receipt.blockNumber)).to.be.bignumber.equal(supply);
  239. });
  240. });
  241. describe('transfers', function () {
  242. it('no delegation', async function () {
  243. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  244. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  245. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  246. this.holderVotes = '0';
  247. this.recipientVotes = '0';
  248. });
  249. it('sender delegation', async function () {
  250. await this.token.delegate(holder, { from: holder });
  251. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  252. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  253. expectEvent(receipt, 'DelegateVotesChanged', { delegate: holder, previousBalance: supply, newBalance: supply.subn(1) });
  254. this.holderVotes = supply.subn(1);
  255. this.recipientVotes = '0';
  256. });
  257. it('receiver delegation', async function () {
  258. await this.token.delegate(recipient, { from: recipient });
  259. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  260. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  261. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousBalance: '0', newBalance: '1' });
  262. this.holderVotes = '0';
  263. this.recipientVotes = '1';
  264. });
  265. it('full delegation', async function () {
  266. await this.token.delegate(holder, { from: holder });
  267. await this.token.delegate(recipient, { from: recipient });
  268. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  269. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  270. expectEvent(receipt, 'DelegateVotesChanged', { delegate: holder, previousBalance: supply, newBalance: supply.subn(1) });
  271. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousBalance: '0', newBalance: '1' });
  272. this.holderVotes = supply.subn(1);
  273. this.recipientVotes = '1';
  274. });
  275. afterEach(async function () {
  276. expect(await this.token.getCurrentVotes(holder)).to.be.bignumber.equal(this.holderVotes);
  277. expect(await this.token.getCurrentVotes(recipient)).to.be.bignumber.equal(this.recipientVotes);
  278. // need to advance 2 blocks to see the effect of a transfer on "getPriorVotes"
  279. const blockNumber = await time.latestBlock();
  280. await time.advanceBlock();
  281. expect(await this.token.getPriorVotes(holder, blockNumber)).to.be.bignumber.equal(this.holderVotes);
  282. expect(await this.token.getPriorVotes(recipient, blockNumber)).to.be.bignumber.equal(this.recipientVotes);
  283. });
  284. });
  285. // The following tests are a adaptation of https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
  286. describe('Compound test suite', function () {
  287. describe('balanceOf', function () {
  288. it('grants to initial account', async function () {
  289. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('10000000000000000000000000');
  290. });
  291. });
  292. describe('numCheckpoints', function () {
  293. it('returns the number of checkpoints for a delegate', async function () {
  294. await this.token.transfer(recipient, '100', { from: holder }); //give an account a few tokens for readability
  295. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  296. const t1 = await this.token.delegate(other1, { from: recipient });
  297. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  298. const t2 = await this.token.transfer(other2, 10, { from: recipient });
  299. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  300. const t3 = await this.token.transfer(other2, 10, { from: recipient });
  301. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('3');
  302. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  303. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('4');
  304. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([ t1.receipt.blockNumber.toString(), '100' ]);
  305. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([ t2.receipt.blockNumber.toString(), '90' ]);
  306. expect(await this.token.checkpoints(other1, 2)).to.be.deep.equal([ t3.receipt.blockNumber.toString(), '80' ]);
  307. expect(await this.token.checkpoints(other1, 3)).to.be.deep.equal([ t4.receipt.blockNumber.toString(), '100' ]);
  308. await time.advanceBlock();
  309. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber)).to.be.bignumber.equal('100');
  310. expect(await this.token.getPriorVotes(other1, t2.receipt.blockNumber)).to.be.bignumber.equal('90');
  311. expect(await this.token.getPriorVotes(other1, t3.receipt.blockNumber)).to.be.bignumber.equal('80');
  312. expect(await this.token.getPriorVotes(other1, t4.receipt.blockNumber)).to.be.bignumber.equal('100');
  313. });
  314. it('does not add more than one checkpoint in a block', async function () {
  315. await this.token.transfer(recipient, '100', { from: holder });
  316. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  317. const [ t1, t2, t3 ] = await batchInBlock([
  318. () => this.token.delegate(other1, { from: recipient, gas: 100000 }),
  319. () => this.token.transfer(other2, 10, { from: recipient, gas: 100000 }),
  320. () => this.token.transfer(other2, 10, { from: recipient, gas: 100000 }),
  321. ]);
  322. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  323. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([ t1.receipt.blockNumber.toString(), '80' ]);
  324. // expectReve(await this.token.checkpoints(other1, 1)).to.be.deep.equal([ '0', '0' ]); // Reverts due to array overflow check
  325. // expect(await this.token.checkpoints(other1, 2)).to.be.deep.equal([ '0', '0' ]); // Reverts due to array overflow check
  326. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  327. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  328. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([ t4.receipt.blockNumber.toString(), '100' ]);
  329. });
  330. });
  331. describe('getPriorVotes', function () {
  332. it('reverts if block number >= current block', async function () {
  333. await expectRevert(
  334. this.token.getPriorVotes(other1, 5e10),
  335. 'ERC20Votes::getPriorVotes: not yet determined',
  336. );
  337. });
  338. it('returns 0 if there are no checkpoints', async function () {
  339. expect(await this.token.getPriorVotes(other1, 0)).to.be.bignumber.equal('0');
  340. });
  341. it('returns the latest block if >= last checkpoint block', async function () {
  342. const t1 = await this.token.delegate(other1, { from: holder });
  343. await time.advanceBlock();
  344. await time.advanceBlock();
  345. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber)).to.be.bignumber.equal('10000000000000000000000000');
  346. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber + 1)).to.be.bignumber.equal('10000000000000000000000000');
  347. });
  348. it('returns zero if < first checkpoint block', async function () {
  349. await time.advanceBlock();
  350. const t1 = await this.token.delegate(other1, { from: holder });
  351. await time.advanceBlock();
  352. await time.advanceBlock();
  353. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  354. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber + 1)).to.be.bignumber.equal('10000000000000000000000000');
  355. });
  356. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  357. const t1 = await this.token.delegate(other1, { from: holder });
  358. await time.advanceBlock();
  359. await time.advanceBlock();
  360. const t2 = await this.token.transfer(other2, 10, { from: holder });
  361. await time.advanceBlock();
  362. await time.advanceBlock();
  363. const t3 = await this.token.transfer(other2, 10, { from: holder });
  364. await time.advanceBlock();
  365. await time.advanceBlock();
  366. const t4 = await this.token.transfer(holder, 20, { from: other2 });
  367. await time.advanceBlock();
  368. await time.advanceBlock();
  369. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber - 1)).to.be.bignumber.equal('0');
  370. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber)).to.be.bignumber.equal('10000000000000000000000000');
  371. expect(await this.token.getPriorVotes(other1, t1.receipt.blockNumber + 1)).to.be.bignumber.equal('10000000000000000000000000');
  372. expect(await this.token.getPriorVotes(other1, t2.receipt.blockNumber)).to.be.bignumber.equal('9999999999999999999999990');
  373. expect(await this.token.getPriorVotes(other1, t2.receipt.blockNumber + 1)).to.be.bignumber.equal('9999999999999999999999990');
  374. expect(await this.token.getPriorVotes(other1, t3.receipt.blockNumber)).to.be.bignumber.equal('9999999999999999999999980');
  375. expect(await this.token.getPriorVotes(other1, t3.receipt.blockNumber + 1)).to.be.bignumber.equal('9999999999999999999999980');
  376. expect(await this.token.getPriorVotes(other1, t4.receipt.blockNumber)).to.be.bignumber.equal('10000000000000000000000000');
  377. expect(await this.token.getPriorVotes(other1, t4.receipt.blockNumber + 1)).to.be.bignumber.equal('10000000000000000000000000');
  378. });
  379. });
  380. });
  381. });