ERC20Votes.test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 } = constants;
  5. const { shouldBehaveLikeVotes } = require('../../../governance/utils/Votes.behavior');
  6. const { fromRpcSig } = require('ethereumjs-util');
  7. const ethSigUtil = require('eth-sig-util');
  8. const Wallet = require('ethereumjs-wallet').default;
  9. const { batchInBlock } = require('../../../helpers/txpool');
  10. const { getDomain, domainType, domainSeparator } = require('../../../helpers/eip712');
  11. const { clock, clockFromReceipt } = require('../../../helpers/time');
  12. const Delegation = [
  13. { name: 'delegatee', type: 'address' },
  14. { name: 'nonce', type: 'uint256' },
  15. { name: 'expiry', type: 'uint256' },
  16. ];
  17. const MODES = {
  18. blocknumber: artifacts.require('$ERC20Votes'),
  19. timestamp: artifacts.require('$ERC20VotesTimestampMock'),
  20. };
  21. contract('ERC20Votes', function (accounts) {
  22. const [holder, recipient, holderDelegatee, other1, other2] = accounts;
  23. const name = 'My Token';
  24. const symbol = 'MTKN';
  25. const version = '1';
  26. const supply = new BN('10000000000000000000000000');
  27. for (const [mode, artifact] of Object.entries(MODES)) {
  28. describe(`vote with ${mode}`, function () {
  29. beforeEach(async function () {
  30. this.token = await artifact.new(name, symbol, name, version);
  31. this.votes = this.token;
  32. });
  33. // includes EIP6372 behavior check
  34. shouldBehaveLikeVotes(accounts, [1, 17, 42], { mode, fungible: true });
  35. it('initial nonce is 0', async function () {
  36. expect(await this.token.nonces(holder)).to.be.bignumber.equal('0');
  37. });
  38. it('domain separator', async function () {
  39. expect(await this.token.DOMAIN_SEPARATOR()).to.equal(await getDomain(this.token).then(domainSeparator));
  40. });
  41. it('minting restriction', async function () {
  42. const amount = new BN('2').pow(new BN('224'));
  43. await expectRevert(this.token.$_mint(holder, amount), 'ERC20Votes: total supply risks overflowing votes');
  44. });
  45. it('recent checkpoints', async function () {
  46. await this.token.delegate(holder, { from: holder });
  47. for (let i = 0; i < 6; i++) {
  48. await this.token.$_mint(holder, 1);
  49. }
  50. const timepoint = await clock[mode]();
  51. expect(await this.token.numCheckpoints(holder)).to.be.bignumber.equal('6');
  52. // recent
  53. expect(await this.token.getPastVotes(holder, timepoint - 1)).to.be.bignumber.equal('5');
  54. // non-recent
  55. expect(await this.token.getPastVotes(holder, timepoint - 6)).to.be.bignumber.equal('0');
  56. });
  57. describe('set delegation', function () {
  58. describe('call', function () {
  59. it('delegation with balance', async function () {
  60. await this.token.$_mint(holder, supply);
  61. expect(await this.token.delegates(holder)).to.be.equal(ZERO_ADDRESS);
  62. const { receipt } = await this.token.delegate(holder, { from: holder });
  63. const timepoint = await clockFromReceipt[mode](receipt);
  64. expectEvent(receipt, 'DelegateChanged', {
  65. delegator: holder,
  66. fromDelegate: ZERO_ADDRESS,
  67. toDelegate: holder,
  68. });
  69. expectEvent(receipt, 'DelegateVotesChanged', {
  70. delegate: holder,
  71. previousBalance: '0',
  72. newBalance: supply,
  73. });
  74. expect(await this.token.delegates(holder)).to.be.equal(holder);
  75. expect(await this.token.getVotes(holder)).to.be.bignumber.equal(supply);
  76. expect(await this.token.getPastVotes(holder, timepoint - 1)).to.be.bignumber.equal('0');
  77. await time.advanceBlock();
  78. expect(await this.token.getPastVotes(holder, timepoint)).to.be.bignumber.equal(supply);
  79. });
  80. it('delegation without balance', async function () {
  81. expect(await this.token.delegates(holder)).to.be.equal(ZERO_ADDRESS);
  82. const { receipt } = await this.token.delegate(holder, { from: holder });
  83. expectEvent(receipt, 'DelegateChanged', {
  84. delegator: holder,
  85. fromDelegate: ZERO_ADDRESS,
  86. toDelegate: holder,
  87. });
  88. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  89. expect(await this.token.delegates(holder)).to.be.equal(holder);
  90. });
  91. });
  92. describe('with signature', function () {
  93. const delegator = Wallet.generate();
  94. const delegatorAddress = web3.utils.toChecksumAddress(delegator.getAddressString());
  95. const nonce = 0;
  96. const buildData = (contract, message) =>
  97. getDomain(contract).then(domain => ({
  98. primaryType: 'Delegation',
  99. types: { EIP712Domain: domainType(domain), Delegation },
  100. domain,
  101. message,
  102. }));
  103. beforeEach(async function () {
  104. await this.token.$_mint(delegatorAddress, supply);
  105. });
  106. it('accept signed delegation', async function () {
  107. const { v, r, s } = await buildData(this.token, {
  108. delegatee: delegatorAddress,
  109. nonce,
  110. expiry: MAX_UINT256,
  111. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  112. expect(await this.token.delegates(delegatorAddress)).to.be.equal(ZERO_ADDRESS);
  113. const { receipt } = await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  114. const timepoint = await clockFromReceipt[mode](receipt);
  115. expectEvent(receipt, 'DelegateChanged', {
  116. delegator: delegatorAddress,
  117. fromDelegate: ZERO_ADDRESS,
  118. toDelegate: delegatorAddress,
  119. });
  120. expectEvent(receipt, 'DelegateVotesChanged', {
  121. delegate: delegatorAddress,
  122. previousBalance: '0',
  123. newBalance: supply,
  124. });
  125. expect(await this.token.delegates(delegatorAddress)).to.be.equal(delegatorAddress);
  126. expect(await this.token.getVotes(delegatorAddress)).to.be.bignumber.equal(supply);
  127. expect(await this.token.getPastVotes(delegatorAddress, timepoint - 1)).to.be.bignumber.equal('0');
  128. await time.advanceBlock();
  129. expect(await this.token.getPastVotes(delegatorAddress, timepoint)).to.be.bignumber.equal(supply);
  130. });
  131. it('rejects reused signature', async function () {
  132. const { v, r, s } = await buildData(this.token, {
  133. delegatee: delegatorAddress,
  134. nonce,
  135. expiry: MAX_UINT256,
  136. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  137. await this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s);
  138. await expectRevert(
  139. this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
  140. 'Votes: invalid nonce',
  141. );
  142. });
  143. it('rejects bad delegatee', async function () {
  144. const { v, r, s } = await buildData(this.token, {
  145. delegatee: delegatorAddress,
  146. nonce,
  147. expiry: MAX_UINT256,
  148. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  149. const receipt = await this.token.delegateBySig(holderDelegatee, nonce, MAX_UINT256, v, r, s);
  150. const { args } = receipt.logs.find(({ event }) => event == 'DelegateChanged');
  151. expect(args.delegator).to.not.be.equal(delegatorAddress);
  152. expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
  153. expect(args.toDelegate).to.be.equal(holderDelegatee);
  154. });
  155. it('rejects bad nonce', async function () {
  156. const { v, r, s } = await buildData(this.token, {
  157. delegatee: delegatorAddress,
  158. nonce,
  159. expiry: MAX_UINT256,
  160. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  161. await expectRevert(
  162. this.token.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
  163. 'Votes: invalid nonce',
  164. );
  165. });
  166. it('rejects expired permit', async function () {
  167. const expiry = (await time.latest()) - time.duration.weeks(1);
  168. const { v, r, s } = await buildData(this.token, {
  169. delegatee: delegatorAddress,
  170. nonce,
  171. expiry,
  172. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  173. await expectRevert(
  174. this.token.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),
  175. 'Votes: signature expired',
  176. );
  177. });
  178. });
  179. });
  180. describe('change delegation', function () {
  181. beforeEach(async function () {
  182. await this.token.$_mint(holder, supply);
  183. await this.token.delegate(holder, { from: holder });
  184. });
  185. it('call', async function () {
  186. expect(await this.token.delegates(holder)).to.be.equal(holder);
  187. const { receipt } = await this.token.delegate(holderDelegatee, { from: holder });
  188. const timepoint = await clockFromReceipt[mode](receipt);
  189. expectEvent(receipt, 'DelegateChanged', {
  190. delegator: holder,
  191. fromDelegate: holder,
  192. toDelegate: holderDelegatee,
  193. });
  194. expectEvent(receipt, 'DelegateVotesChanged', {
  195. delegate: holder,
  196. previousBalance: supply,
  197. newBalance: '0',
  198. });
  199. expectEvent(receipt, 'DelegateVotesChanged', {
  200. delegate: holderDelegatee,
  201. previousBalance: '0',
  202. newBalance: supply,
  203. });
  204. expect(await this.token.delegates(holder)).to.be.equal(holderDelegatee);
  205. expect(await this.token.getVotes(holder)).to.be.bignumber.equal('0');
  206. expect(await this.token.getVotes(holderDelegatee)).to.be.bignumber.equal(supply);
  207. expect(await this.token.getPastVotes(holder, timepoint - 1)).to.be.bignumber.equal(supply);
  208. expect(await this.token.getPastVotes(holderDelegatee, timepoint - 1)).to.be.bignumber.equal('0');
  209. await time.advanceBlock();
  210. expect(await this.token.getPastVotes(holder, timepoint)).to.be.bignumber.equal('0');
  211. expect(await this.token.getPastVotes(holderDelegatee, timepoint)).to.be.bignumber.equal(supply);
  212. });
  213. });
  214. describe('transfers', function () {
  215. beforeEach(async function () {
  216. await this.token.$_mint(holder, supply);
  217. });
  218. it('no delegation', async function () {
  219. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  220. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  221. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  222. this.holderVotes = '0';
  223. this.recipientVotes = '0';
  224. });
  225. it('sender delegation', async function () {
  226. await this.token.delegate(holder, { from: holder });
  227. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  228. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  229. expectEvent(receipt, 'DelegateVotesChanged', {
  230. delegate: holder,
  231. previousBalance: supply,
  232. newBalance: supply.subn(1),
  233. });
  234. const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
  235. expect(
  236. receipt.logs
  237. .filter(({ event }) => event == 'DelegateVotesChanged')
  238. .every(({ logIndex }) => transferLogIndex < logIndex),
  239. ).to.be.equal(true);
  240. this.holderVotes = supply.subn(1);
  241. this.recipientVotes = '0';
  242. });
  243. it('receiver delegation', async function () {
  244. await this.token.delegate(recipient, { from: recipient });
  245. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  246. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  247. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousBalance: '0', newBalance: '1' });
  248. const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
  249. expect(
  250. receipt.logs
  251. .filter(({ event }) => event == 'DelegateVotesChanged')
  252. .every(({ logIndex }) => transferLogIndex < logIndex),
  253. ).to.be.equal(true);
  254. this.holderVotes = '0';
  255. this.recipientVotes = '1';
  256. });
  257. it('full delegation', async function () {
  258. await this.token.delegate(holder, { from: holder });
  259. await this.token.delegate(recipient, { from: recipient });
  260. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  261. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  262. expectEvent(receipt, 'DelegateVotesChanged', {
  263. delegate: holder,
  264. previousBalance: supply,
  265. newBalance: supply.subn(1),
  266. });
  267. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousBalance: '0', newBalance: '1' });
  268. const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
  269. expect(
  270. receipt.logs
  271. .filter(({ event }) => event == 'DelegateVotesChanged')
  272. .every(({ logIndex }) => transferLogIndex < logIndex),
  273. ).to.be.equal(true);
  274. this.holderVotes = supply.subn(1);
  275. this.recipientVotes = '1';
  276. });
  277. afterEach(async function () {
  278. expect(await this.token.getVotes(holder)).to.be.bignumber.equal(this.holderVotes);
  279. expect(await this.token.getVotes(recipient)).to.be.bignumber.equal(this.recipientVotes);
  280. // need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
  281. const timepoint = await clock[mode]();
  282. await time.advanceBlock();
  283. expect(await this.token.getPastVotes(holder, timepoint)).to.be.bignumber.equal(this.holderVotes);
  284. expect(await this.token.getPastVotes(recipient, timepoint)).to.be.bignumber.equal(this.recipientVotes);
  285. });
  286. });
  287. // The following tests are a adaptation of https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
  288. describe('Compound test suite', function () {
  289. beforeEach(async function () {
  290. await this.token.$_mint(holder, supply);
  291. });
  292. describe('balanceOf', function () {
  293. it('grants to initial account', async function () {
  294. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('10000000000000000000000000');
  295. });
  296. });
  297. describe('numCheckpoints', function () {
  298. it('returns the number of checkpoints for a delegate', async function () {
  299. await this.token.transfer(recipient, '100', { from: holder }); //give an account a few tokens for readability
  300. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  301. const t1 = await this.token.delegate(other1, { from: recipient });
  302. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  303. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  304. const t2 = await this.token.transfer(other2, 10, { from: recipient });
  305. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  306. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  307. const t3 = await this.token.transfer(other2, 10, { from: recipient });
  308. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  309. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('3');
  310. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  311. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  312. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('4');
  313. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([t1.timepoint.toString(), '100']);
  314. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([t2.timepoint.toString(), '90']);
  315. expect(await this.token.checkpoints(other1, 2)).to.be.deep.equal([t3.timepoint.toString(), '80']);
  316. expect(await this.token.checkpoints(other1, 3)).to.be.deep.equal([t4.timepoint.toString(), '100']);
  317. await time.advanceBlock();
  318. expect(await this.token.getPastVotes(other1, t1.timepoint)).to.be.bignumber.equal('100');
  319. expect(await this.token.getPastVotes(other1, t2.timepoint)).to.be.bignumber.equal('90');
  320. expect(await this.token.getPastVotes(other1, t3.timepoint)).to.be.bignumber.equal('80');
  321. expect(await this.token.getPastVotes(other1, t4.timepoint)).to.be.bignumber.equal('100');
  322. });
  323. it('does not add more than one checkpoint in a block', async function () {
  324. await this.token.transfer(recipient, '100', { from: holder });
  325. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  326. const [t1, t2, t3] = await batchInBlock([
  327. () => this.token.delegate(other1, { from: recipient, gas: 200000 }),
  328. () => this.token.transfer(other2, 10, { from: recipient, gas: 200000 }),
  329. () => this.token.transfer(other2, 10, { from: recipient, gas: 200000 }),
  330. ]);
  331. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  332. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  333. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  334. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  335. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([t1.timepoint.toString(), '80']);
  336. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  337. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  338. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  339. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([t4.timepoint.toString(), '100']);
  340. });
  341. });
  342. describe('getPastVotes', function () {
  343. it('reverts if block number >= current block', async function () {
  344. await expectRevert(this.token.getPastVotes(other1, 5e10), 'Votes: future lookup');
  345. });
  346. it('returns 0 if there are no checkpoints', async function () {
  347. expect(await this.token.getPastVotes(other1, 0)).to.be.bignumber.equal('0');
  348. });
  349. it('returns the latest block if >= last checkpoint block', async function () {
  350. const { receipt } = await this.token.delegate(other1, { from: holder });
  351. const timepoint = await clockFromReceipt[mode](receipt);
  352. await time.advanceBlock();
  353. await time.advanceBlock();
  354. expect(await this.token.getPastVotes(other1, timepoint)).to.be.bignumber.equal(
  355. '10000000000000000000000000',
  356. );
  357. expect(await this.token.getPastVotes(other1, timepoint + 1)).to.be.bignumber.equal(
  358. '10000000000000000000000000',
  359. );
  360. });
  361. it('returns zero if < first checkpoint block', async function () {
  362. await time.advanceBlock();
  363. const { receipt } = await this.token.delegate(other1, { from: holder });
  364. const timepoint = await clockFromReceipt[mode](receipt);
  365. await time.advanceBlock();
  366. await time.advanceBlock();
  367. expect(await this.token.getPastVotes(other1, timepoint - 1)).to.be.bignumber.equal('0');
  368. expect(await this.token.getPastVotes(other1, timepoint + 1)).to.be.bignumber.equal(
  369. '10000000000000000000000000',
  370. );
  371. });
  372. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  373. const t1 = await this.token.delegate(other1, { from: holder });
  374. await time.advanceBlock();
  375. await time.advanceBlock();
  376. const t2 = await this.token.transfer(other2, 10, { from: holder });
  377. await time.advanceBlock();
  378. await time.advanceBlock();
  379. const t3 = await this.token.transfer(other2, 10, { from: holder });
  380. await time.advanceBlock();
  381. await time.advanceBlock();
  382. const t4 = await this.token.transfer(holder, 20, { from: other2 });
  383. await time.advanceBlock();
  384. await time.advanceBlock();
  385. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  386. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  387. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  388. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  389. expect(await this.token.getPastVotes(other1, t1.timepoint - 1)).to.be.bignumber.equal('0');
  390. expect(await this.token.getPastVotes(other1, t1.timepoint)).to.be.bignumber.equal(
  391. '10000000000000000000000000',
  392. );
  393. expect(await this.token.getPastVotes(other1, t1.timepoint + 1)).to.be.bignumber.equal(
  394. '10000000000000000000000000',
  395. );
  396. expect(await this.token.getPastVotes(other1, t2.timepoint)).to.be.bignumber.equal(
  397. '9999999999999999999999990',
  398. );
  399. expect(await this.token.getPastVotes(other1, t2.timepoint + 1)).to.be.bignumber.equal(
  400. '9999999999999999999999990',
  401. );
  402. expect(await this.token.getPastVotes(other1, t3.timepoint)).to.be.bignumber.equal(
  403. '9999999999999999999999980',
  404. );
  405. expect(await this.token.getPastVotes(other1, t3.timepoint + 1)).to.be.bignumber.equal(
  406. '9999999999999999999999980',
  407. );
  408. expect(await this.token.getPastVotes(other1, t4.timepoint)).to.be.bignumber.equal(
  409. '10000000000000000000000000',
  410. );
  411. expect(await this.token.getPastVotes(other1, t4.timepoint + 1)).to.be.bignumber.equal(
  412. '10000000000000000000000000',
  413. );
  414. });
  415. });
  416. });
  417. describe('getPastTotalSupply', function () {
  418. beforeEach(async function () {
  419. await this.token.delegate(holder, { from: holder });
  420. });
  421. it('reverts if block number >= current block', async function () {
  422. await expectRevert(this.token.getPastTotalSupply(5e10), 'Votes: future lookup');
  423. });
  424. it('returns 0 if there are no checkpoints', async function () {
  425. expect(await this.token.getPastTotalSupply(0)).to.be.bignumber.equal('0');
  426. });
  427. it('returns the latest block if >= last checkpoint block', async function () {
  428. const { receipt } = await this.token.$_mint(holder, supply);
  429. const timepoint = await clockFromReceipt[mode](receipt);
  430. await time.advanceBlock();
  431. await time.advanceBlock();
  432. expect(await this.token.getPastTotalSupply(timepoint)).to.be.bignumber.equal(supply);
  433. expect(await this.token.getPastTotalSupply(timepoint + 1)).to.be.bignumber.equal(supply);
  434. });
  435. it('returns zero if < first checkpoint block', async function () {
  436. await time.advanceBlock();
  437. const { receipt } = await this.token.$_mint(holder, supply);
  438. const timepoint = await clockFromReceipt[mode](receipt);
  439. await time.advanceBlock();
  440. await time.advanceBlock();
  441. expect(await this.token.getPastTotalSupply(timepoint - 1)).to.be.bignumber.equal('0');
  442. expect(await this.token.getPastTotalSupply(timepoint + 1)).to.be.bignumber.equal(
  443. '10000000000000000000000000',
  444. );
  445. });
  446. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  447. const t1 = await this.token.$_mint(holder, supply);
  448. await time.advanceBlock();
  449. await time.advanceBlock();
  450. const t2 = await this.token.$_burn(holder, 10);
  451. await time.advanceBlock();
  452. await time.advanceBlock();
  453. const t3 = await this.token.$_burn(holder, 10);
  454. await time.advanceBlock();
  455. await time.advanceBlock();
  456. const t4 = await this.token.$_mint(holder, 20);
  457. await time.advanceBlock();
  458. await time.advanceBlock();
  459. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  460. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  461. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  462. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  463. expect(await this.token.getPastTotalSupply(t1.timepoint - 1)).to.be.bignumber.equal('0');
  464. expect(await this.token.getPastTotalSupply(t1.timepoint)).to.be.bignumber.equal('10000000000000000000000000');
  465. expect(await this.token.getPastTotalSupply(t1.timepoint + 1)).to.be.bignumber.equal(
  466. '10000000000000000000000000',
  467. );
  468. expect(await this.token.getPastTotalSupply(t2.timepoint)).to.be.bignumber.equal('9999999999999999999999990');
  469. expect(await this.token.getPastTotalSupply(t2.timepoint + 1)).to.be.bignumber.equal(
  470. '9999999999999999999999990',
  471. );
  472. expect(await this.token.getPastTotalSupply(t3.timepoint)).to.be.bignumber.equal('9999999999999999999999980');
  473. expect(await this.token.getPastTotalSupply(t3.timepoint + 1)).to.be.bignumber.equal(
  474. '9999999999999999999999980',
  475. );
  476. expect(await this.token.getPastTotalSupply(t4.timepoint)).to.be.bignumber.equal('10000000000000000000000000');
  477. expect(await this.token.getPastTotalSupply(t4.timepoint + 1)).to.be.bignumber.equal(
  478. '10000000000000000000000000',
  479. );
  480. });
  481. });
  482. });
  483. }
  484. });