ERC20Votes.test.js 27 KB

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