ERC4626.test.js 29 KB

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