ERC4626.test.js 24 KB

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