ERC20Votes.test.js 24 KB

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