ERC4626.test.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ERC20Decimals = artifacts.require('$ERC20DecimalsMock');
  4. const ERC4626 = artifacts.require('$ERC4626');
  5. const ERC4626OffsetMock = artifacts.require('$ERC4626OffsetMock');
  6. contract('ERC4626', function (accounts) {
  7. const [holder, recipient, spender, other, user1, user2] = accounts;
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. const decimals = web3.utils.toBN(18);
  11. it('inherit decimals if from asset', async function () {
  12. for (const decimals of [0, 9, 12, 18, 36].map(web3.utils.toBN)) {
  13. const token = await ERC20Decimals.new('', '', decimals);
  14. const vault = await ERC4626.new('', '', token.address);
  15. expect(await vault.decimals()).to.be.bignumber.equal(decimals);
  16. }
  17. });
  18. for (const offset of [0, 6, 18].map(web3.utils.toBN)) {
  19. const parseToken = token => web3.utils.toBN(10).pow(decimals).muln(token);
  20. const parseShare = share => web3.utils.toBN(10).pow(decimals.add(offset)).muln(share);
  21. const virtualAssets = web3.utils.toBN(1);
  22. const virtualShares = web3.utils.toBN(10).pow(offset);
  23. describe(`offset: ${offset}`, function () {
  24. beforeEach(async function () {
  25. this.token = await ERC20Decimals.new(name, symbol, decimals);
  26. this.vault = await ERC4626OffsetMock.new(name + ' Vault', symbol + 'V', this.token.address, offset);
  27. await this.token.$_mint(holder, constants.MAX_INT256); // 50% of maximum
  28. await this.token.approve(this.vault.address, constants.MAX_UINT256, { from: holder });
  29. await this.vault.approve(spender, constants.MAX_UINT256, { from: holder });
  30. });
  31. it('metadata', async function () {
  32. expect(await this.vault.name()).to.be.equal(name + ' Vault');
  33. expect(await this.vault.symbol()).to.be.equal(symbol + 'V');
  34. expect(await this.vault.decimals()).to.be.bignumber.equal(decimals.add(offset));
  35. expect(await this.vault.asset()).to.be.equal(this.token.address);
  36. });
  37. describe('empty vault: no assets & no shares', function () {
  38. it('status', async function () {
  39. expect(await this.vault.totalAssets()).to.be.bignumber.equal('0');
  40. });
  41. it('deposit', async function () {
  42. expect(await this.vault.maxDeposit(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  43. expect(await this.vault.previewDeposit(parseToken(1))).to.be.bignumber.equal(parseShare(1));
  44. const { tx } = await this.vault.deposit(parseToken(1), recipient, { from: holder });
  45. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  46. from: holder,
  47. to: this.vault.address,
  48. value: parseToken(1),
  49. });
  50. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  51. from: constants.ZERO_ADDRESS,
  52. to: recipient,
  53. value: parseShare(1),
  54. });
  55. });
  56. it('mint', async function () {
  57. expect(await this.vault.maxMint(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  58. expect(await this.vault.previewMint(parseShare(1))).to.be.bignumber.equal(parseToken(1));
  59. const { tx } = await this.vault.mint(parseShare(1), recipient, { from: holder });
  60. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  61. from: holder,
  62. to: this.vault.address,
  63. value: parseToken(1),
  64. });
  65. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  66. from: constants.ZERO_ADDRESS,
  67. to: recipient,
  68. value: parseShare(1),
  69. });
  70. });
  71. it('withdraw', async function () {
  72. expect(await this.vault.maxWithdraw(holder)).to.be.bignumber.equal('0');
  73. expect(await this.vault.previewWithdraw('0')).to.be.bignumber.equal('0');
  74. const { tx } = await this.vault.withdraw('0', recipient, holder, { from: holder });
  75. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  76. from: this.vault.address,
  77. to: recipient,
  78. value: '0',
  79. });
  80. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  81. from: holder,
  82. to: constants.ZERO_ADDRESS,
  83. value: '0',
  84. });
  85. });
  86. it('redeem', async function () {
  87. expect(await this.vault.maxRedeem(holder)).to.be.bignumber.equal('0');
  88. expect(await this.vault.previewRedeem('0')).to.be.bignumber.equal('0');
  89. const { tx } = await this.vault.redeem('0', recipient, holder, { from: holder });
  90. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  91. from: this.vault.address,
  92. to: recipient,
  93. value: '0',
  94. });
  95. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  96. from: holder,
  97. to: constants.ZERO_ADDRESS,
  98. value: '0',
  99. });
  100. });
  101. });
  102. describe('inflation attack: offset price by direct deposit of assets', function () {
  103. beforeEach(async function () {
  104. // Donate 1 token to the vault to offset the price
  105. await this.token.$_mint(this.vault.address, parseToken(1));
  106. });
  107. it('status', async function () {
  108. expect(await this.vault.totalSupply()).to.be.bignumber.equal('0');
  109. expect(await this.vault.totalAssets()).to.be.bignumber.equal(parseToken(1));
  110. });
  111. /**
  112. * | offset | deposited assets | redeemable assets |
  113. * |--------|----------------------|----------------------|
  114. * | 0 | 1.000000000000000000 | 0. |
  115. * | 6 | 1.000000000000000000 | 0.999999000000000000 |
  116. * | 18 | 1.000000000000000000 | 0.999999999999999999 |
  117. *
  118. * Attack is possible, but made difficult by the offset. For the attack to be successful
  119. * the attacker needs to frontrun a deposit 10**offset times bigger than what the victim
  120. * was trying to deposit
  121. */
  122. it('deposit', async function () {
  123. const effectiveAssets = await this.vault.totalAssets().then(x => x.add(virtualAssets));
  124. const effectiveShares = await this.vault.totalSupply().then(x => x.add(virtualShares));
  125. const depositAssets = parseToken(1);
  126. const expectedShares = depositAssets.mul(effectiveShares).div(effectiveAssets);
  127. expect(await this.vault.maxDeposit(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  128. expect(await this.vault.previewDeposit(depositAssets)).to.be.bignumber.equal(expectedShares);
  129. const { tx } = await this.vault.deposit(depositAssets, recipient, { from: holder });
  130. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  131. from: holder,
  132. to: this.vault.address,
  133. value: depositAssets,
  134. });
  135. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  136. from: constants.ZERO_ADDRESS,
  137. to: recipient,
  138. value: expectedShares,
  139. });
  140. });
  141. /**
  142. * | offset | deposited assets | redeemable assets |
  143. * |--------|----------------------|----------------------|
  144. * | 0 | 1000000000000000001. | 1000000000000000001. |
  145. * | 6 | 1000000000000000001. | 1000000000000000001. |
  146. * | 18 | 1000000000000000001. | 1000000000000000001. |
  147. *
  148. * Using mint protects against inflation attack, but makes minting shares very expensive.
  149. * The ER20 allowance for the underlying asset is needed to protect the user from (too)
  150. * large deposits.
  151. */
  152. it('mint', async function () {
  153. const effectiveAssets = await this.vault.totalAssets().then(x => x.add(virtualAssets));
  154. const effectiveShares = await this.vault.totalSupply().then(x => x.add(virtualShares));
  155. const mintShares = parseShare(1);
  156. const expectedAssets = mintShares.mul(effectiveAssets).div(effectiveShares);
  157. expect(await this.vault.maxMint(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  158. expect(await this.vault.previewMint(mintShares)).to.be.bignumber.equal(expectedAssets);
  159. const { tx } = await this.vault.mint(mintShares, recipient, { from: holder });
  160. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  161. from: holder,
  162. to: this.vault.address,
  163. value: expectedAssets,
  164. });
  165. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  166. from: constants.ZERO_ADDRESS,
  167. to: recipient,
  168. value: mintShares,
  169. });
  170. });
  171. it('withdraw', async function () {
  172. expect(await this.vault.maxWithdraw(holder)).to.be.bignumber.equal('0');
  173. expect(await this.vault.previewWithdraw('0')).to.be.bignumber.equal('0');
  174. const { tx } = await this.vault.withdraw('0', recipient, holder, { from: holder });
  175. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  176. from: this.vault.address,
  177. to: recipient,
  178. value: '0',
  179. });
  180. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  181. from: holder,
  182. to: constants.ZERO_ADDRESS,
  183. value: '0',
  184. });
  185. });
  186. it('redeem', async function () {
  187. expect(await this.vault.maxRedeem(holder)).to.be.bignumber.equal('0');
  188. expect(await this.vault.previewRedeem('0')).to.be.bignumber.equal('0');
  189. const { tx } = await this.vault.redeem('0', recipient, holder, { from: holder });
  190. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  191. from: this.vault.address,
  192. to: recipient,
  193. value: '0',
  194. });
  195. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  196. from: holder,
  197. to: constants.ZERO_ADDRESS,
  198. value: '0',
  199. });
  200. });
  201. });
  202. describe('full vault: assets & shares', function () {
  203. beforeEach(async function () {
  204. // Add 1 token of underlying asset and 100 shares to the vault
  205. await this.token.$_mint(this.vault.address, parseToken(1));
  206. await this.vault.$_mint(holder, parseShare(100));
  207. });
  208. it('status', async function () {
  209. expect(await this.vault.totalSupply()).to.be.bignumber.equal(parseShare(100));
  210. expect(await this.vault.totalAssets()).to.be.bignumber.equal(parseToken(1));
  211. });
  212. /**
  213. * | offset | deposited assets | redeemable assets |
  214. * |--------|--------------------- |----------------------|
  215. * | 0 | 1.000000000000000000 | 0.999999999999999999 |
  216. * | 6 | 1.000000000000000000 | 0.999999999999999999 |
  217. * | 18 | 1.000000000000000000 | 0.999999999999999999 |
  218. *
  219. * Virtual shares & assets captures part of the value
  220. */
  221. it('deposit', async function () {
  222. const effectiveAssets = await this.vault.totalAssets().then(x => x.add(virtualAssets));
  223. const effectiveShares = await this.vault.totalSupply().then(x => x.add(virtualShares));
  224. const depositAssets = parseToken(1);
  225. const expectedShares = depositAssets.mul(effectiveShares).div(effectiveAssets);
  226. expect(await this.vault.maxDeposit(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  227. expect(await this.vault.previewDeposit(depositAssets)).to.be.bignumber.equal(expectedShares);
  228. const { tx } = await this.vault.deposit(depositAssets, recipient, { from: holder });
  229. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  230. from: holder,
  231. to: this.vault.address,
  232. value: depositAssets,
  233. });
  234. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  235. from: constants.ZERO_ADDRESS,
  236. to: recipient,
  237. value: expectedShares,
  238. });
  239. });
  240. /**
  241. * | offset | deposited assets | redeemable assets |
  242. * |--------|--------------------- |----------------------|
  243. * | 0 | 0.010000000000000001 | 0.010000000000000000 |
  244. * | 6 | 0.010000000000000001 | 0.010000000000000000 |
  245. * | 18 | 0.010000000000000001 | 0.010000000000000000 |
  246. *
  247. * Virtual shares & assets captures part of the value
  248. */
  249. it('mint', async function () {
  250. const effectiveAssets = await this.vault.totalAssets().then(x => x.add(virtualAssets));
  251. const effectiveShares = await this.vault.totalSupply().then(x => x.add(virtualShares));
  252. const mintShares = parseShare(1);
  253. const expectedAssets = mintShares.mul(effectiveAssets).div(effectiveShares).addn(1); // add for the rounding
  254. expect(await this.vault.maxMint(holder)).to.be.bignumber.equal(constants.MAX_UINT256);
  255. expect(await this.vault.previewMint(mintShares)).to.be.bignumber.equal(expectedAssets);
  256. const { tx } = await this.vault.mint(mintShares, recipient, { from: holder });
  257. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  258. from: holder,
  259. to: this.vault.address,
  260. value: expectedAssets,
  261. });
  262. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  263. from: constants.ZERO_ADDRESS,
  264. to: recipient,
  265. value: mintShares,
  266. });
  267. });
  268. it('withdraw', async function () {
  269. const effectiveAssets = await this.vault.totalAssets().then(x => x.add(virtualAssets));
  270. const effectiveShares = await this.vault.totalSupply().then(x => x.add(virtualShares));
  271. const withdrawAssets = parseToken(1);
  272. const expectedShares = withdrawAssets.mul(effectiveShares).div(effectiveAssets).addn(1); // add for the rounding
  273. expect(await this.vault.maxWithdraw(holder)).to.be.bignumber.equal(withdrawAssets);
  274. expect(await this.vault.previewWithdraw(withdrawAssets)).to.be.bignumber.equal(expectedShares);
  275. const { tx } = await this.vault.withdraw(withdrawAssets, recipient, holder, { from: holder });
  276. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  277. from: this.vault.address,
  278. to: recipient,
  279. value: withdrawAssets,
  280. });
  281. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  282. from: holder,
  283. to: constants.ZERO_ADDRESS,
  284. value: expectedShares,
  285. });
  286. });
  287. it('withdraw with approval', async function () {
  288. await expectRevert(
  289. this.vault.withdraw(parseToken(1), recipient, holder, { from: other }),
  290. 'ERC20: insufficient allowance',
  291. );
  292. await this.vault.withdraw(parseToken(1), recipient, holder, { from: spender });
  293. });
  294. it('redeem', async function () {
  295. expect(await this.vault.maxRedeem(holder)).to.be.bignumber.equal(parseShare(100));
  296. expect(await this.vault.previewRedeem(parseShare(100))).to.be.bignumber.equal(parseToken(1));
  297. const { tx } = await this.vault.redeem(parseShare(100), recipient, holder, { from: holder });
  298. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  299. from: this.vault.address,
  300. to: recipient,
  301. value: parseToken(1),
  302. });
  303. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  304. from: holder,
  305. to: constants.ZERO_ADDRESS,
  306. value: parseShare(100),
  307. });
  308. });
  309. it('redeem with approval', async function () {
  310. await expectRevert(
  311. this.vault.redeem(parseShare(100), recipient, holder, { from: other }),
  312. 'ERC20: insufficient allowance',
  313. );
  314. await this.vault.redeem(parseShare(100), recipient, holder, { from: spender });
  315. });
  316. });
  317. });
  318. }
  319. /// Scenario inspired by solmate ERC4626 tests:
  320. /// https://github.com/transmissions11/solmate/blob/main/src/test/ERC4626.t.sol
  321. it('multiple mint, deposit, redeem & withdrawal', async function () {
  322. // test designed with both asset using similar decimals
  323. this.token = await ERC20Decimals.new(name, symbol, 18);
  324. this.vault = await ERC4626.new(name + ' Vault', symbol + 'V', this.token.address);
  325. await this.token.$_mint(user1, 4000);
  326. await this.token.$_mint(user2, 7001);
  327. await this.token.approve(this.vault.address, 4000, { from: user1 });
  328. await this.token.approve(this.vault.address, 7001, { from: user2 });
  329. // 1. Alice mints 2000 shares (costs 2000 tokens)
  330. {
  331. const { tx } = await this.vault.mint(2000, user1, { from: user1 });
  332. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  333. from: user1,
  334. to: this.vault.address,
  335. value: '2000',
  336. });
  337. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  338. from: constants.ZERO_ADDRESS,
  339. to: user1,
  340. value: '2000',
  341. });
  342. expect(await this.vault.previewDeposit(2000)).to.be.bignumber.equal('2000');
  343. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('2000');
  344. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('0');
  345. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('2000');
  346. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('0');
  347. expect(await this.vault.totalSupply()).to.be.bignumber.equal('2000');
  348. expect(await this.vault.totalAssets()).to.be.bignumber.equal('2000');
  349. }
  350. // 2. Bob deposits 4000 tokens (mints 4000 shares)
  351. {
  352. const { tx } = await this.vault.mint(4000, user2, { from: user2 });
  353. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  354. from: user2,
  355. to: this.vault.address,
  356. value: '4000',
  357. });
  358. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  359. from: constants.ZERO_ADDRESS,
  360. to: user2,
  361. value: '4000',
  362. });
  363. expect(await this.vault.previewDeposit(4000)).to.be.bignumber.equal('4000');
  364. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('2000');
  365. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('4000');
  366. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('2000');
  367. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('4000');
  368. expect(await this.vault.totalSupply()).to.be.bignumber.equal('6000');
  369. expect(await this.vault.totalAssets()).to.be.bignumber.equal('6000');
  370. }
  371. // 3. Vault mutates by +3000 tokens (simulated yield returned from strategy)
  372. await this.token.$_mint(this.vault.address, 3000);
  373. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('2000');
  374. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('4000');
  375. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('2999'); // used to be 3000, but virtual assets/shares captures part of the yield
  376. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('5999'); // used to be 6000, but virtual assets/shares captures part of the yield
  377. expect(await this.vault.totalSupply()).to.be.bignumber.equal('6000');
  378. expect(await this.vault.totalAssets()).to.be.bignumber.equal('9000');
  379. // 4. Alice deposits 2000 tokens (mints 1333 shares)
  380. {
  381. const { tx } = await this.vault.deposit(2000, user1, { from: user1 });
  382. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  383. from: user1,
  384. to: this.vault.address,
  385. value: '2000',
  386. });
  387. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  388. from: constants.ZERO_ADDRESS,
  389. to: user1,
  390. value: '1333',
  391. });
  392. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('3333');
  393. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('4000');
  394. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('4999');
  395. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('6000');
  396. expect(await this.vault.totalSupply()).to.be.bignumber.equal('7333');
  397. expect(await this.vault.totalAssets()).to.be.bignumber.equal('11000');
  398. }
  399. // 5. Bob mints 2000 shares (costs 3001 assets)
  400. // NOTE: Bob's assets spent got rounded up
  401. // NOTE: Alices's vault assets got rounded up
  402. {
  403. const { tx } = await this.vault.mint(2000, user2, { from: user2 });
  404. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  405. from: user2,
  406. to: this.vault.address,
  407. value: '3000', // used to be 3001
  408. });
  409. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  410. from: constants.ZERO_ADDRESS,
  411. to: user2,
  412. value: '2000',
  413. });
  414. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('3333');
  415. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('6000');
  416. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('4999'); // used to be 5000
  417. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('9000');
  418. expect(await this.vault.totalSupply()).to.be.bignumber.equal('9333');
  419. expect(await this.vault.totalAssets()).to.be.bignumber.equal('14000'); // used to be 14001
  420. }
  421. // 6. Vault mutates by +3000 tokens
  422. // NOTE: Vault holds 17001 tokens, but sum of assetsOf() is 17000.
  423. await this.token.$_mint(this.vault.address, 3000);
  424. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('3333');
  425. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('6000');
  426. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('6070'); // used to be 6071
  427. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('10928'); // used to be 10929
  428. expect(await this.vault.totalSupply()).to.be.bignumber.equal('9333');
  429. expect(await this.vault.totalAssets()).to.be.bignumber.equal('17000'); // used to be 17001
  430. // 7. Alice redeem 1333 shares (2428 assets)
  431. {
  432. const { tx } = await this.vault.redeem(1333, user1, user1, { from: user1 });
  433. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  434. from: user1,
  435. to: constants.ZERO_ADDRESS,
  436. value: '1333',
  437. });
  438. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  439. from: this.vault.address,
  440. to: user1,
  441. value: '2427', // used to be 2428
  442. });
  443. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('2000');
  444. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('6000');
  445. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('3643');
  446. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('10929');
  447. expect(await this.vault.totalSupply()).to.be.bignumber.equal('8000');
  448. expect(await this.vault.totalAssets()).to.be.bignumber.equal('14573');
  449. }
  450. // 8. Bob withdraws 2929 assets (1608 shares)
  451. {
  452. const { tx } = await this.vault.withdraw(2929, user2, user2, { from: user2 });
  453. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  454. from: user2,
  455. to: constants.ZERO_ADDRESS,
  456. value: '1608',
  457. });
  458. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  459. from: this.vault.address,
  460. to: user2,
  461. value: '2929',
  462. });
  463. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('2000');
  464. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('4392');
  465. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('3643');
  466. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('8000');
  467. expect(await this.vault.totalSupply()).to.be.bignumber.equal('6392');
  468. expect(await this.vault.totalAssets()).to.be.bignumber.equal('11644');
  469. }
  470. // 9. Alice withdraws 3643 assets (2000 shares)
  471. // NOTE: Bob's assets have been rounded back up
  472. {
  473. const { tx } = await this.vault.withdraw(3643, user1, user1, { from: user1 });
  474. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  475. from: user1,
  476. to: constants.ZERO_ADDRESS,
  477. value: '2000',
  478. });
  479. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  480. from: this.vault.address,
  481. to: user1,
  482. value: '3643',
  483. });
  484. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('0');
  485. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('4392');
  486. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('0');
  487. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('8000'); // used to be 8001
  488. expect(await this.vault.totalSupply()).to.be.bignumber.equal('4392');
  489. expect(await this.vault.totalAssets()).to.be.bignumber.equal('8001');
  490. }
  491. // 10. Bob redeem 4392 shares (8001 tokens)
  492. {
  493. const { tx } = await this.vault.redeem(4392, user2, user2, { from: user2 });
  494. await expectEvent.inTransaction(tx, this.vault, 'Transfer', {
  495. from: user2,
  496. to: constants.ZERO_ADDRESS,
  497. value: '4392',
  498. });
  499. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  500. from: this.vault.address,
  501. to: user2,
  502. value: '8000', // used to be 8001
  503. });
  504. expect(await this.vault.balanceOf(user1)).to.be.bignumber.equal('0');
  505. expect(await this.vault.balanceOf(user2)).to.be.bignumber.equal('0');
  506. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user1))).to.be.bignumber.equal('0');
  507. expect(await this.vault.convertToAssets(await this.vault.balanceOf(user2))).to.be.bignumber.equal('0');
  508. expect(await this.vault.totalSupply()).to.be.bignumber.equal('0');
  509. expect(await this.vault.totalAssets()).to.be.bignumber.equal('1'); // used to be 0
  510. }
  511. });
  512. });