ERC20Votes.test.js 26 KB

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