ERC20Votes.test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 {
  11. getDomain,
  12. domainType,
  13. types: { Delegation },
  14. } = require('../../../helpers/eip712');
  15. const { clock, clockFromReceipt } = require('../../../helpers/time');
  16. const { expectRevertCustomError } = require('../../../helpers/customError');
  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 ERC6372 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('minting restriction', async function () {
  39. const value = web3.utils.toBN(1).shln(208);
  40. await expectRevertCustomError(this.token.$_mint(holder, value), 'ERC20ExceededSafeSupply', [
  41. value,
  42. value.subn(1),
  43. ]);
  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. previousVotes: '0',
  72. newVotes: 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. previousVotes: '0',
  123. newVotes: 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 expectRevertCustomError(
  139. this.token.delegateBySig(delegatorAddress, nonce, MAX_UINT256, v, r, s),
  140. 'InvalidAccountNonce',
  141. [delegatorAddress, nonce + 1],
  142. );
  143. });
  144. it('rejects bad delegatee', async function () {
  145. const { v, r, s } = await buildData(this.token, {
  146. delegatee: delegatorAddress,
  147. nonce,
  148. expiry: MAX_UINT256,
  149. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  150. const receipt = await this.token.delegateBySig(holderDelegatee, nonce, MAX_UINT256, v, r, s);
  151. const { args } = receipt.logs.find(({ event }) => event == 'DelegateChanged');
  152. expect(args.delegator).to.not.be.equal(delegatorAddress);
  153. expect(args.fromDelegate).to.be.equal(ZERO_ADDRESS);
  154. expect(args.toDelegate).to.be.equal(holderDelegatee);
  155. });
  156. it('rejects bad nonce', async function () {
  157. const sig = await buildData(this.token, {
  158. delegatee: delegatorAddress,
  159. nonce,
  160. expiry: MAX_UINT256,
  161. }).then(data => ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data }));
  162. const { r, s, v } = fromRpcSig(sig);
  163. const domain = await getDomain(this.token);
  164. const typedMessage = {
  165. primaryType: 'Delegation',
  166. types: { EIP712Domain: domainType(domain), Delegation },
  167. domain,
  168. message: { delegatee: delegatorAddress, nonce: nonce + 1, expiry: MAX_UINT256 },
  169. };
  170. await expectRevertCustomError(
  171. this.token.delegateBySig(delegatorAddress, nonce + 1, MAX_UINT256, v, r, s),
  172. 'InvalidAccountNonce',
  173. [ethSigUtil.recoverTypedSignature({ data: typedMessage, sig }), nonce],
  174. );
  175. });
  176. it('rejects expired permit', async function () {
  177. const expiry = (await time.latest()) - time.duration.weeks(1);
  178. const { v, r, s } = await buildData(this.token, {
  179. delegatee: delegatorAddress,
  180. nonce,
  181. expiry,
  182. }).then(data => fromRpcSig(ethSigUtil.signTypedMessage(delegator.getPrivateKey(), { data })));
  183. await expectRevertCustomError(
  184. this.token.delegateBySig(delegatorAddress, nonce, expiry, v, r, s),
  185. 'VotesExpiredSignature',
  186. [expiry],
  187. );
  188. });
  189. });
  190. });
  191. describe('change delegation', function () {
  192. beforeEach(async function () {
  193. await this.token.$_mint(holder, supply);
  194. await this.token.delegate(holder, { from: holder });
  195. });
  196. it('call', async function () {
  197. expect(await this.token.delegates(holder)).to.be.equal(holder);
  198. const { receipt } = await this.token.delegate(holderDelegatee, { from: holder });
  199. const timepoint = await clockFromReceipt[mode](receipt);
  200. expectEvent(receipt, 'DelegateChanged', {
  201. delegator: holder,
  202. fromDelegate: holder,
  203. toDelegate: holderDelegatee,
  204. });
  205. expectEvent(receipt, 'DelegateVotesChanged', {
  206. delegate: holder,
  207. previousVotes: supply,
  208. newVotes: '0',
  209. });
  210. expectEvent(receipt, 'DelegateVotesChanged', {
  211. delegate: holderDelegatee,
  212. previousVotes: '0',
  213. newVotes: supply,
  214. });
  215. expect(await this.token.delegates(holder)).to.be.equal(holderDelegatee);
  216. expect(await this.token.getVotes(holder)).to.be.bignumber.equal('0');
  217. expect(await this.token.getVotes(holderDelegatee)).to.be.bignumber.equal(supply);
  218. expect(await this.token.getPastVotes(holder, timepoint - 1)).to.be.bignumber.equal(supply);
  219. expect(await this.token.getPastVotes(holderDelegatee, timepoint - 1)).to.be.bignumber.equal('0');
  220. await time.advanceBlock();
  221. expect(await this.token.getPastVotes(holder, timepoint)).to.be.bignumber.equal('0');
  222. expect(await this.token.getPastVotes(holderDelegatee, timepoint)).to.be.bignumber.equal(supply);
  223. });
  224. });
  225. describe('transfers', function () {
  226. beforeEach(async function () {
  227. await this.token.$_mint(holder, supply);
  228. });
  229. it('no delegation', async function () {
  230. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  231. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  232. expectEvent.notEmitted(receipt, 'DelegateVotesChanged');
  233. this.holderVotes = '0';
  234. this.recipientVotes = '0';
  235. });
  236. it('sender delegation', async function () {
  237. await this.token.delegate(holder, { from: holder });
  238. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  239. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  240. expectEvent(receipt, 'DelegateVotesChanged', {
  241. delegate: holder,
  242. previousVotes: supply,
  243. newVotes: supply.subn(1),
  244. });
  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 = supply.subn(1);
  252. this.recipientVotes = '0';
  253. });
  254. it('receiver delegation', async function () {
  255. await this.token.delegate(recipient, { from: recipient });
  256. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  257. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  258. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousVotes: '0', newVotes: '1' });
  259. const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
  260. expect(
  261. receipt.logs
  262. .filter(({ event }) => event == 'DelegateVotesChanged')
  263. .every(({ logIndex }) => transferLogIndex < logIndex),
  264. ).to.be.equal(true);
  265. this.holderVotes = '0';
  266. this.recipientVotes = '1';
  267. });
  268. it('full delegation', async function () {
  269. await this.token.delegate(holder, { from: holder });
  270. await this.token.delegate(recipient, { from: recipient });
  271. const { receipt } = await this.token.transfer(recipient, 1, { from: holder });
  272. expectEvent(receipt, 'Transfer', { from: holder, to: recipient, value: '1' });
  273. expectEvent(receipt, 'DelegateVotesChanged', {
  274. delegate: holder,
  275. previousVotes: supply,
  276. newVotes: supply.subn(1),
  277. });
  278. expectEvent(receipt, 'DelegateVotesChanged', { delegate: recipient, previousVotes: '0', newVotes: '1' });
  279. const { logIndex: transferLogIndex } = receipt.logs.find(({ event }) => event == 'Transfer');
  280. expect(
  281. receipt.logs
  282. .filter(({ event }) => event == 'DelegateVotesChanged')
  283. .every(({ logIndex }) => transferLogIndex < logIndex),
  284. ).to.be.equal(true);
  285. this.holderVotes = supply.subn(1);
  286. this.recipientVotes = '1';
  287. });
  288. afterEach(async function () {
  289. expect(await this.token.getVotes(holder)).to.be.bignumber.equal(this.holderVotes);
  290. expect(await this.token.getVotes(recipient)).to.be.bignumber.equal(this.recipientVotes);
  291. // need to advance 2 blocks to see the effect of a transfer on "getPastVotes"
  292. const timepoint = await clock[mode]();
  293. await time.advanceBlock();
  294. expect(await this.token.getPastVotes(holder, timepoint)).to.be.bignumber.equal(this.holderVotes);
  295. expect(await this.token.getPastVotes(recipient, timepoint)).to.be.bignumber.equal(this.recipientVotes);
  296. });
  297. });
  298. // The following tests are a adaptation of https://github.com/compound-finance/compound-protocol/blob/master/tests/Governance/CompTest.js.
  299. describe('Compound test suite', function () {
  300. beforeEach(async function () {
  301. await this.token.$_mint(holder, supply);
  302. });
  303. describe('balanceOf', function () {
  304. it('grants to initial account', async function () {
  305. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('10000000000000000000000000');
  306. });
  307. });
  308. describe('numCheckpoints', function () {
  309. it('returns the number of checkpoints for a delegate', async function () {
  310. await this.token.transfer(recipient, '100', { from: holder }); //give an account a few tokens for readability
  311. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  312. const t1 = await this.token.delegate(other1, { from: recipient });
  313. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  314. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  315. const t2 = await this.token.transfer(other2, 10, { from: recipient });
  316. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  317. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  318. const t3 = await this.token.transfer(other2, 10, { from: recipient });
  319. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  320. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('3');
  321. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  322. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  323. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('4');
  324. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([t1.timepoint.toString(), '100']);
  325. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([t2.timepoint.toString(), '90']);
  326. expect(await this.token.checkpoints(other1, 2)).to.be.deep.equal([t3.timepoint.toString(), '80']);
  327. expect(await this.token.checkpoints(other1, 3)).to.be.deep.equal([t4.timepoint.toString(), '100']);
  328. await time.advanceBlock();
  329. expect(await this.token.getPastVotes(other1, t1.timepoint)).to.be.bignumber.equal('100');
  330. expect(await this.token.getPastVotes(other1, t2.timepoint)).to.be.bignumber.equal('90');
  331. expect(await this.token.getPastVotes(other1, t3.timepoint)).to.be.bignumber.equal('80');
  332. expect(await this.token.getPastVotes(other1, t4.timepoint)).to.be.bignumber.equal('100');
  333. });
  334. it('does not add more than one checkpoint in a block', async function () {
  335. await this.token.transfer(recipient, '100', { from: holder });
  336. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('0');
  337. const [t1, t2, t3] = await batchInBlock([
  338. () => this.token.delegate(other1, { from: recipient, gas: 200000 }),
  339. () => this.token.transfer(other2, 10, { from: recipient, gas: 200000 }),
  340. () => this.token.transfer(other2, 10, { from: recipient, gas: 200000 }),
  341. ]);
  342. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  343. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  344. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  345. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('1');
  346. expect(await this.token.checkpoints(other1, 0)).to.be.deep.equal([t1.timepoint.toString(), '80']);
  347. const t4 = await this.token.transfer(recipient, 20, { from: holder });
  348. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  349. expect(await this.token.numCheckpoints(other1)).to.be.bignumber.equal('2');
  350. expect(await this.token.checkpoints(other1, 1)).to.be.deep.equal([t4.timepoint.toString(), '100']);
  351. });
  352. });
  353. describe('getPastVotes', function () {
  354. it('reverts if block number >= current block', async function () {
  355. const clock = await this.token.clock();
  356. await expectRevertCustomError(this.token.getPastVotes(other1, 5e10), 'ERC5805FutureLookup', [5e10, clock]);
  357. });
  358. it('returns 0 if there are no checkpoints', async function () {
  359. expect(await this.token.getPastVotes(other1, 0)).to.be.bignumber.equal('0');
  360. });
  361. it('returns the latest block if >= last checkpoint block', async function () {
  362. const { receipt } = await this.token.delegate(other1, { from: holder });
  363. const timepoint = await clockFromReceipt[mode](receipt);
  364. await time.advanceBlock();
  365. await time.advanceBlock();
  366. expect(await this.token.getPastVotes(other1, timepoint)).to.be.bignumber.equal(
  367. '10000000000000000000000000',
  368. );
  369. expect(await this.token.getPastVotes(other1, timepoint + 1)).to.be.bignumber.equal(
  370. '10000000000000000000000000',
  371. );
  372. });
  373. it('returns zero if < first checkpoint block', async function () {
  374. await time.advanceBlock();
  375. const { receipt } = await this.token.delegate(other1, { from: holder });
  376. const timepoint = await clockFromReceipt[mode](receipt);
  377. await time.advanceBlock();
  378. await time.advanceBlock();
  379. expect(await this.token.getPastVotes(other1, timepoint - 1)).to.be.bignumber.equal('0');
  380. expect(await this.token.getPastVotes(other1, timepoint + 1)).to.be.bignumber.equal(
  381. '10000000000000000000000000',
  382. );
  383. });
  384. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  385. const t1 = await this.token.delegate(other1, { from: holder });
  386. await time.advanceBlock();
  387. await time.advanceBlock();
  388. const t2 = await this.token.transfer(other2, 10, { from: holder });
  389. await time.advanceBlock();
  390. await time.advanceBlock();
  391. const t3 = await this.token.transfer(other2, 10, { from: holder });
  392. await time.advanceBlock();
  393. await time.advanceBlock();
  394. const t4 = await this.token.transfer(holder, 20, { from: other2 });
  395. await time.advanceBlock();
  396. await time.advanceBlock();
  397. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  398. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  399. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  400. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  401. expect(await this.token.getPastVotes(other1, t1.timepoint - 1)).to.be.bignumber.equal('0');
  402. expect(await this.token.getPastVotes(other1, t1.timepoint)).to.be.bignumber.equal(
  403. '10000000000000000000000000',
  404. );
  405. expect(await this.token.getPastVotes(other1, t1.timepoint + 1)).to.be.bignumber.equal(
  406. '10000000000000000000000000',
  407. );
  408. expect(await this.token.getPastVotes(other1, t2.timepoint)).to.be.bignumber.equal(
  409. '9999999999999999999999990',
  410. );
  411. expect(await this.token.getPastVotes(other1, t2.timepoint + 1)).to.be.bignumber.equal(
  412. '9999999999999999999999990',
  413. );
  414. expect(await this.token.getPastVotes(other1, t3.timepoint)).to.be.bignumber.equal(
  415. '9999999999999999999999980',
  416. );
  417. expect(await this.token.getPastVotes(other1, t3.timepoint + 1)).to.be.bignumber.equal(
  418. '9999999999999999999999980',
  419. );
  420. expect(await this.token.getPastVotes(other1, t4.timepoint)).to.be.bignumber.equal(
  421. '10000000000000000000000000',
  422. );
  423. expect(await this.token.getPastVotes(other1, t4.timepoint + 1)).to.be.bignumber.equal(
  424. '10000000000000000000000000',
  425. );
  426. });
  427. });
  428. });
  429. describe('getPastTotalSupply', function () {
  430. beforeEach(async function () {
  431. await this.token.delegate(holder, { from: holder });
  432. });
  433. it('reverts if block number >= current block', async function () {
  434. const clock = await this.token.clock();
  435. await expectRevertCustomError(this.token.getPastTotalSupply(5e10), 'ERC5805FutureLookup', [5e10, clock]);
  436. });
  437. it('returns 0 if there are no checkpoints', async function () {
  438. expect(await this.token.getPastTotalSupply(0)).to.be.bignumber.equal('0');
  439. });
  440. it('returns the latest block if >= last checkpoint block', async function () {
  441. const { receipt } = await this.token.$_mint(holder, supply);
  442. const timepoint = await clockFromReceipt[mode](receipt);
  443. await time.advanceBlock();
  444. await time.advanceBlock();
  445. expect(await this.token.getPastTotalSupply(timepoint)).to.be.bignumber.equal(supply);
  446. expect(await this.token.getPastTotalSupply(timepoint + 1)).to.be.bignumber.equal(supply);
  447. });
  448. it('returns zero if < first checkpoint block', async function () {
  449. await time.advanceBlock();
  450. const { receipt } = await this.token.$_mint(holder, supply);
  451. const timepoint = await clockFromReceipt[mode](receipt);
  452. await time.advanceBlock();
  453. await time.advanceBlock();
  454. expect(await this.token.getPastTotalSupply(timepoint - 1)).to.be.bignumber.equal('0');
  455. expect(await this.token.getPastTotalSupply(timepoint + 1)).to.be.bignumber.equal(
  456. '10000000000000000000000000',
  457. );
  458. });
  459. it('generally returns the voting balance at the appropriate checkpoint', async function () {
  460. const t1 = await this.token.$_mint(holder, supply);
  461. await time.advanceBlock();
  462. await time.advanceBlock();
  463. const t2 = await this.token.$_burn(holder, 10);
  464. await time.advanceBlock();
  465. await time.advanceBlock();
  466. const t3 = await this.token.$_burn(holder, 10);
  467. await time.advanceBlock();
  468. await time.advanceBlock();
  469. const t4 = await this.token.$_mint(holder, 20);
  470. await time.advanceBlock();
  471. await time.advanceBlock();
  472. t1.timepoint = await clockFromReceipt[mode](t1.receipt);
  473. t2.timepoint = await clockFromReceipt[mode](t2.receipt);
  474. t3.timepoint = await clockFromReceipt[mode](t3.receipt);
  475. t4.timepoint = await clockFromReceipt[mode](t4.receipt);
  476. expect(await this.token.getPastTotalSupply(t1.timepoint - 1)).to.be.bignumber.equal('0');
  477. expect(await this.token.getPastTotalSupply(t1.timepoint)).to.be.bignumber.equal('10000000000000000000000000');
  478. expect(await this.token.getPastTotalSupply(t1.timepoint + 1)).to.be.bignumber.equal(
  479. '10000000000000000000000000',
  480. );
  481. expect(await this.token.getPastTotalSupply(t2.timepoint)).to.be.bignumber.equal('9999999999999999999999990');
  482. expect(await this.token.getPastTotalSupply(t2.timepoint + 1)).to.be.bignumber.equal(
  483. '9999999999999999999999990',
  484. );
  485. expect(await this.token.getPastTotalSupply(t3.timepoint)).to.be.bignumber.equal('9999999999999999999999980');
  486. expect(await this.token.getPastTotalSupply(t3.timepoint + 1)).to.be.bignumber.equal(
  487. '9999999999999999999999980',
  488. );
  489. expect(await this.token.getPastTotalSupply(t4.timepoint)).to.be.bignumber.equal('10000000000000000000000000');
  490. expect(await this.token.getPastTotalSupply(t4.timepoint + 1)).to.be.bignumber.equal(
  491. '10000000000000000000000000',
  492. );
  493. });
  494. });
  495. });
  496. }
  497. });