ERC20Votes.test.js 26 KB

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