ERC4626.test.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  5. const {
  6. bigint: { Enum },
  7. } = require('../../../helpers/enums');
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. const decimals = 18n;
  11. async function fixture() {
  12. const [holder, recipient, spender, other, ...accounts] = await ethers.getSigners();
  13. return { holder, recipient, spender, other, accounts };
  14. }
  15. describe('ERC4626', function () {
  16. beforeEach(async function () {
  17. Object.assign(this, await loadFixture(fixture));
  18. });
  19. it('inherit decimals if from asset', async function () {
  20. for (const decimals of [0n, 9n, 12n, 18n, 36n]) {
  21. const token = await ethers.deployContract('$ERC20DecimalsMock', ['', '', decimals]);
  22. const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
  23. expect(await vault.decimals()).to.equal(decimals);
  24. }
  25. });
  26. it('asset has not yet been created', async function () {
  27. const vault = await ethers.deployContract('$ERC4626', ['', '', this.other.address]);
  28. expect(await vault.decimals()).to.equal(decimals);
  29. });
  30. it('underlying excess decimals', async function () {
  31. const token = await ethers.deployContract('$ERC20ExcessDecimalsMock');
  32. const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
  33. expect(await vault.decimals()).to.equal(decimals);
  34. });
  35. it('decimals overflow', async function () {
  36. for (const offset of [243n, 250n, 255n]) {
  37. const token = await ethers.deployContract('$ERC20DecimalsMock', ['', '', decimals]);
  38. const vault = await ethers.deployContract('$ERC4626OffsetMock', ['', '', token, offset]);
  39. await expect(vault.decimals()).to.be.revertedWithPanic(PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW);
  40. }
  41. });
  42. describe('reentrancy', async function () {
  43. const reenterType = Enum('No', 'Before', 'After');
  44. const value = 1_000_000_000_000_000_000n;
  45. const reenterValue = 1_000_000_000n;
  46. beforeEach(async function () {
  47. // Use offset 1 so the rate is not 1:1 and we can't possibly confuse assets and shares
  48. const token = await ethers.deployContract('$ERC20Reentrant');
  49. const vault = await ethers.deployContract('$ERC4626OffsetMock', ['', '', token, 1n]);
  50. // Funds and approval for tests
  51. await token.$_mint(this.holder, value);
  52. await token.$_mint(this.other, value);
  53. await token.$_approve(this.holder, vault, ethers.MaxUint256);
  54. await token.$_approve(this.other, vault, ethers.MaxUint256);
  55. await token.$_approve(token, vault, ethers.MaxUint256);
  56. Object.assign(this, { token, vault });
  57. });
  58. // During a `_deposit`, the vault does `transferFrom(depositor, vault, assets)` -> `_mint(receiver, shares)`
  59. // such that a reentrancy BEFORE the transfer guarantees the price is kept the same.
  60. // If the order of transfer -> mint is changed to mint -> transfer, the reentrancy could be triggered on an
  61. // intermediate state in which the ratio of assets/shares has been decreased (more shares than assets).
  62. it('correct share price is observed during reentrancy before deposit', async function () {
  63. // mint token for deposit
  64. await this.token.$_mint(this.token, reenterValue);
  65. // Schedules a reentrancy from the token contract
  66. await this.token.scheduleReenter(
  67. reenterType.Before,
  68. this.vault,
  69. this.vault.interface.encodeFunctionData('deposit', [reenterValue, this.holder.address]),
  70. );
  71. // Initial share price
  72. const sharesForDeposit = await this.vault.previewDeposit(value);
  73. const sharesForReenter = await this.vault.previewDeposit(reenterValue);
  74. await expect(this.vault.connect(this.holder).deposit(value, this.holder))
  75. // Deposit normally, reentering before the internal `_update`
  76. .to.emit(this.vault, 'Deposit')
  77. .withArgs(this.holder.address, this.holder.address, value, sharesForDeposit)
  78. // Reentrant deposit event → uses the same price
  79. .to.emit(this.vault, 'Deposit')
  80. .withArgs(this.token.target, this.holder.address, reenterValue, sharesForReenter);
  81. // Assert prices is kept
  82. expect(await this.vault.previewDeposit(value)).to.equal(sharesForDeposit);
  83. });
  84. // During a `_withdraw`, the vault does `_burn(owner, shares)` -> `transfer(receiver, assets)`
  85. // such that a reentrancy AFTER the transfer guarantees the price is kept the same.
  86. // If the order of burn -> transfer is changed to transfer -> burn, the reentrancy could be triggered on an
  87. // intermediate state in which the ratio of shares/assets has been decreased (more assets than shares).
  88. it('correct share price is observed during reentrancy after withdraw', async function () {
  89. // Deposit into the vault: holder gets `value` share, token.address gets `reenterValue` shares
  90. await this.vault.connect(this.holder).deposit(value, this.holder);
  91. await this.vault.connect(this.other).deposit(reenterValue, this.token);
  92. // Schedules a reentrancy from the token contract
  93. await this.token.scheduleReenter(
  94. reenterType.After,
  95. this.vault,
  96. this.vault.interface.encodeFunctionData('withdraw', [reenterValue, this.holder.address, this.token.target]),
  97. );
  98. // Initial share price
  99. const sharesForWithdraw = await this.vault.previewWithdraw(value);
  100. const sharesForReenter = await this.vault.previewWithdraw(reenterValue);
  101. // Do withdraw normally, triggering the _afterTokenTransfer hook
  102. await expect(this.vault.connect(this.holder).withdraw(value, this.holder, this.holder))
  103. // Main withdraw event
  104. .to.emit(this.vault, 'Withdraw')
  105. .withArgs(this.holder.address, this.holder.address, this.holder.address, value, sharesForWithdraw)
  106. // Reentrant withdraw event → uses the same price
  107. .to.emit(this.vault, 'Withdraw')
  108. .withArgs(this.token.target, this.holder.address, this.token.target, reenterValue, sharesForReenter);
  109. // Assert price is kept
  110. expect(await this.vault.previewWithdraw(value)).to.equal(sharesForWithdraw);
  111. });
  112. // Donate newly minted tokens to the vault during the reentracy causes the share price to increase.
  113. // Still, the deposit that trigger the reentracy is not affected and get the previewed price.
  114. // Further deposits will get a different price (getting fewer shares for the same value of assets)
  115. it('share price change during reentracy does not affect deposit', async function () {
  116. // Schedules a reentrancy from the token contract that mess up the share price
  117. await this.token.scheduleReenter(
  118. reenterType.Before,
  119. this.token,
  120. this.token.interface.encodeFunctionData('$_mint', [this.vault.target, reenterValue]),
  121. );
  122. // Price before
  123. const sharesBefore = await this.vault.previewDeposit(value);
  124. // Deposit, reentering before the internal `_update`
  125. await expect(this.vault.connect(this.holder).deposit(value, this.holder))
  126. // Price is as previewed
  127. .to.emit(this.vault, 'Deposit')
  128. .withArgs(this.holder.address, this.holder.address, value, sharesBefore);
  129. // Price was modified during reentrancy
  130. expect(await this.vault.previewDeposit(value)).to.lt(sharesBefore);
  131. });
  132. // Burn some tokens from the vault during the reentracy causes the share price to drop.
  133. // Still, the withdraw that trigger the reentracy is not affected and get the previewed price.
  134. // Further withdraw will get a different price (needing more shares for the same value of assets)
  135. it('share price change during reentracy does not affect withdraw', async function () {
  136. await this.vault.connect(this.holder).deposit(value, this.holder);
  137. await this.vault.connect(this.other).deposit(value, this.other);
  138. // Schedules a reentrancy from the token contract that mess up the share price
  139. await this.token.scheduleReenter(
  140. reenterType.After,
  141. this.token,
  142. this.token.interface.encodeFunctionData('$_burn', [this.vault.target, reenterValue]),
  143. );
  144. // Price before
  145. const sharesBefore = await this.vault.previewWithdraw(value);
  146. // Withdraw, triggering the _afterTokenTransfer hook
  147. await expect(this.vault.connect(this.holder).withdraw(value, this.holder, this.holder))
  148. // Price is as previewed
  149. .to.emit(this.vault, 'Withdraw')
  150. .withArgs(this.holder.address, this.holder.address, this.holder.address, value, sharesBefore);
  151. // Price was modified during reentrancy
  152. expect(await this.vault.previewWithdraw(value)).to.gt(sharesBefore);
  153. });
  154. });
  155. describe('limits', async function () {
  156. beforeEach(async function () {
  157. const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, decimals]);
  158. const vault = await ethers.deployContract('$ERC4626LimitsMock', ['', '', token]);
  159. Object.assign(this, { token, vault });
  160. });
  161. it('reverts on deposit() above max deposit', async function () {
  162. const maxDeposit = await this.vault.maxDeposit(this.holder);
  163. await expect(this.vault.connect(this.holder).deposit(maxDeposit + 1n, this.recipient))
  164. .to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxDeposit')
  165. .withArgs(this.recipient.address, maxDeposit + 1n, maxDeposit);
  166. });
  167. it('reverts on mint() above max mint', async function () {
  168. const maxMint = await this.vault.maxMint(this.holder);
  169. await expect(this.vault.connect(this.holder).mint(maxMint + 1n, this.recipient))
  170. .to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxMint')
  171. .withArgs(this.recipient.address, maxMint + 1n, maxMint);
  172. });
  173. it('reverts on withdraw() above max withdraw', async function () {
  174. const maxWithdraw = await this.vault.maxWithdraw(this.holder);
  175. await expect(this.vault.connect(this.holder).withdraw(maxWithdraw + 1n, this.recipient, this.holder))
  176. .to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxWithdraw')
  177. .withArgs(this.holder.address, maxWithdraw + 1n, maxWithdraw);
  178. });
  179. it('reverts on redeem() above max redeem', async function () {
  180. const maxRedeem = await this.vault.maxRedeem(this.holder);
  181. await expect(this.vault.connect(this.holder).redeem(maxRedeem + 1n, this.recipient, this.holder))
  182. .to.be.revertedWithCustomError(this.vault, 'ERC4626ExceededMaxRedeem')
  183. .withArgs(this.holder.address, maxRedeem + 1n, maxRedeem);
  184. });
  185. });
  186. for (const offset of [0n, 6n, 18n]) {
  187. const parseToken = token => token * 10n ** decimals;
  188. const parseShare = share => share * 10n ** (decimals + offset);
  189. const virtualAssets = 1n;
  190. const virtualShares = 10n ** offset;
  191. describe(`offset: ${offset}`, function () {
  192. beforeEach(async function () {
  193. const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, decimals]);
  194. const vault = await ethers.deployContract('$ERC4626OffsetMock', [name + ' Vault', symbol + 'V', token, offset]);
  195. await token.$_mint(this.holder, ethers.MaxUint256 / 2n); // 50% of maximum
  196. await token.$_approve(this.holder, vault, ethers.MaxUint256);
  197. await vault.$_approve(this.holder, this.spender, ethers.MaxUint256);
  198. Object.assign(this, { token, vault });
  199. });
  200. it('metadata', async function () {
  201. expect(await this.vault.name()).to.equal(name + ' Vault');
  202. expect(await this.vault.symbol()).to.equal(symbol + 'V');
  203. expect(await this.vault.decimals()).to.equal(decimals + offset);
  204. expect(await this.vault.asset()).to.equal(this.token.target);
  205. });
  206. describe('empty vault: no assets & no shares', function () {
  207. it('status', async function () {
  208. expect(await this.vault.totalAssets()).to.equal(0n);
  209. });
  210. it('deposit', async function () {
  211. expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
  212. expect(await this.vault.previewDeposit(parseToken(1n))).to.equal(parseShare(1n));
  213. const tx = this.vault.connect(this.holder).deposit(parseToken(1n), this.recipient);
  214. await expect(tx).to.changeTokenBalances(
  215. this.token,
  216. [this.holder, this.vault],
  217. [-parseToken(1n), parseToken(1n)],
  218. );
  219. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, parseShare(1n));
  220. await expect(tx)
  221. .to.emit(this.token, 'Transfer')
  222. .withArgs(this.holder.address, this.vault.target, parseToken(1n))
  223. .to.emit(this.vault, 'Transfer')
  224. .withArgs(ethers.ZeroAddress, this.recipient.address, parseShare(1n))
  225. .to.emit(this.vault, 'Deposit')
  226. .withArgs(this.holder.address, this.recipient.address, parseToken(1n), parseShare(1n));
  227. });
  228. it('mint', async function () {
  229. expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
  230. expect(await this.vault.previewMint(parseShare(1n))).to.equal(parseToken(1n));
  231. const tx = this.vault.connect(this.holder).mint(parseShare(1n), this.recipient);
  232. await expect(tx).to.changeTokenBalances(
  233. this.token,
  234. [this.holder, this.vault],
  235. [-parseToken(1n), parseToken(1n)],
  236. );
  237. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, parseShare(1n));
  238. await expect(tx)
  239. .to.emit(this.token, 'Transfer')
  240. .withArgs(this.holder.address, this.vault.target, parseToken(1n))
  241. .to.emit(this.vault, 'Transfer')
  242. .withArgs(ethers.ZeroAddress, this.recipient.address, parseShare(1n))
  243. .to.emit(this.vault, 'Deposit')
  244. .withArgs(this.holder.address, this.recipient.address, parseToken(1n), parseShare(1n));
  245. });
  246. it('withdraw', async function () {
  247. expect(await this.vault.maxWithdraw(this.holder)).to.equal(0n);
  248. expect(await this.vault.previewWithdraw(0n)).to.equal(0n);
  249. const tx = this.vault.connect(this.holder).withdraw(0n, this.recipient, this.holder);
  250. await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
  251. await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
  252. await expect(tx)
  253. .to.emit(this.token, 'Transfer')
  254. .withArgs(this.vault.target, this.recipient.address, 0n)
  255. .to.emit(this.vault, 'Transfer')
  256. .withArgs(this.holder.address, ethers.ZeroAddress, 0n)
  257. .to.emit(this.vault, 'Withdraw')
  258. .withArgs(this.holder.address, this.recipient.address, this.holder.address, 0n, 0n);
  259. });
  260. it('redeem', async function () {
  261. expect(await this.vault.maxRedeem(this.holder)).to.equal(0n);
  262. expect(await this.vault.previewRedeem(0n)).to.equal(0n);
  263. const tx = this.vault.connect(this.holder).redeem(0n, this.recipient, this.holder);
  264. await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
  265. await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
  266. await expect(tx)
  267. .to.emit(this.token, 'Transfer')
  268. .withArgs(this.vault.target, this.recipient.address, 0n)
  269. .to.emit(this.vault, 'Transfer')
  270. .withArgs(this.holder.address, ethers.ZeroAddress, 0n)
  271. .to.emit(this.vault, 'Withdraw')
  272. .withArgs(this.holder.address, this.recipient.address, this.holder.address, 0n, 0n);
  273. });
  274. });
  275. describe('inflation attack: offset price by direct deposit of assets', function () {
  276. beforeEach(async function () {
  277. // Donate 1 token to the vault to offset the price
  278. await this.token.$_mint(this.vault, parseToken(1n));
  279. });
  280. it('status', async function () {
  281. expect(await this.vault.totalSupply()).to.equal(0n);
  282. expect(await this.vault.totalAssets()).to.equal(parseToken(1n));
  283. });
  284. /**
  285. * | offset | deposited assets | redeemable assets |
  286. * |--------|----------------------|----------------------|
  287. * | 0 | 1.000000000000000000 | 0. |
  288. * | 6 | 1.000000000000000000 | 0.999999000000000000 |
  289. * | 18 | 1.000000000000000000 | 0.999999999999999999 |
  290. *
  291. * Attack is possible, but made difficult by the offset. For the attack to be successful
  292. * the attacker needs to frontrun a deposit 10**offset times bigger than what the victim
  293. * was trying to deposit
  294. */
  295. it('deposit', async function () {
  296. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  297. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  298. const depositAssets = parseToken(1n);
  299. const expectedShares = (depositAssets * effectiveShares) / effectiveAssets;
  300. expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
  301. expect(await this.vault.previewDeposit(depositAssets)).to.equal(expectedShares);
  302. const tx = this.vault.connect(this.holder).deposit(depositAssets, this.recipient);
  303. await expect(tx).to.changeTokenBalances(
  304. this.token,
  305. [this.holder, this.vault],
  306. [-depositAssets, depositAssets],
  307. );
  308. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, expectedShares);
  309. await expect(tx)
  310. .to.emit(this.token, 'Transfer')
  311. .withArgs(this.holder.address, this.vault.target, depositAssets)
  312. .to.emit(this.vault, 'Transfer')
  313. .withArgs(ethers.ZeroAddress, this.recipient.address, expectedShares)
  314. .to.emit(this.vault, 'Deposit')
  315. .withArgs(this.holder.address, this.recipient.address, depositAssets, expectedShares);
  316. });
  317. /**
  318. * | offset | deposited assets | redeemable assets |
  319. * |--------|----------------------|----------------------|
  320. * | 0 | 1000000000000000001. | 1000000000000000001. |
  321. * | 6 | 1000000000000000001. | 1000000000000000001. |
  322. * | 18 | 1000000000000000001. | 1000000000000000001. |
  323. *
  324. * Using mint protects against inflation attack, but makes minting shares very expensive.
  325. * The ER20 allowance for the underlying asset is needed to protect the user from (too)
  326. * large deposits.
  327. */
  328. it('mint', async function () {
  329. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  330. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  331. const mintShares = parseShare(1n);
  332. const expectedAssets = (mintShares * effectiveAssets) / effectiveShares;
  333. expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
  334. expect(await this.vault.previewMint(mintShares)).to.equal(expectedAssets);
  335. const tx = this.vault.connect(this.holder).mint(mintShares, this.recipient);
  336. await expect(tx).to.changeTokenBalances(
  337. this.token,
  338. [this.holder, this.vault],
  339. [-expectedAssets, expectedAssets],
  340. );
  341. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, mintShares);
  342. await expect(tx)
  343. .to.emit(this.token, 'Transfer')
  344. .withArgs(this.holder.address, this.vault.target, expectedAssets)
  345. .to.emit(this.vault, 'Transfer')
  346. .withArgs(ethers.ZeroAddress, this.recipient.address, mintShares)
  347. .to.emit(this.vault, 'Deposit')
  348. .withArgs(this.holder.address, this.recipient.address, expectedAssets, mintShares);
  349. });
  350. it('withdraw', async function () {
  351. expect(await this.vault.maxWithdraw(this.holder)).to.equal(0n);
  352. expect(await this.vault.previewWithdraw(0n)).to.equal(0n);
  353. const tx = this.vault.connect(this.holder).withdraw(0n, this.recipient, this.holder);
  354. await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
  355. await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
  356. await expect(tx)
  357. .to.emit(this.token, 'Transfer')
  358. .withArgs(this.vault.target, this.recipient.address, 0n)
  359. .to.emit(this.vault, 'Transfer')
  360. .withArgs(this.holder.address, ethers.ZeroAddress, 0n)
  361. .to.emit(this.vault, 'Withdraw')
  362. .withArgs(this.holder.address, this.recipient.address, this.holder.address, 0n, 0n);
  363. });
  364. it('redeem', async function () {
  365. expect(await this.vault.maxRedeem(this.holder)).to.equal(0n);
  366. expect(await this.vault.previewRedeem(0n)).to.equal(0n);
  367. const tx = this.vault.connect(this.holder).redeem(0n, this.recipient, this.holder);
  368. await expect(tx).to.changeTokenBalances(this.token, [this.vault, this.recipient], [0n, 0n]);
  369. await expect(tx).to.changeTokenBalance(this.vault, this.holder, 0n);
  370. await expect(tx)
  371. .to.emit(this.token, 'Transfer')
  372. .withArgs(this.vault.target, this.recipient.address, 0n)
  373. .to.emit(this.vault, 'Transfer')
  374. .withArgs(this.holder.address, ethers.ZeroAddress, 0n)
  375. .to.emit(this.vault, 'Withdraw')
  376. .withArgs(this.holder.address, this.recipient.address, this.holder.address, 0n, 0n);
  377. });
  378. });
  379. describe('full vault: assets & shares', function () {
  380. beforeEach(async function () {
  381. // Add 1 token of underlying asset and 100 shares to the vault
  382. await this.token.$_mint(this.vault, parseToken(1n));
  383. await this.vault.$_mint(this.holder, parseShare(100n));
  384. });
  385. it('status', async function () {
  386. expect(await this.vault.totalSupply()).to.equal(parseShare(100n));
  387. expect(await this.vault.totalAssets()).to.equal(parseToken(1n));
  388. });
  389. /**
  390. * | offset | deposited assets | redeemable assets |
  391. * |--------|--------------------- |----------------------|
  392. * | 0 | 1.000000000000000000 | 0.999999999999999999 |
  393. * | 6 | 1.000000000000000000 | 0.999999999999999999 |
  394. * | 18 | 1.000000000000000000 | 0.999999999999999999 |
  395. *
  396. * Virtual shares & assets captures part of the value
  397. */
  398. it('deposit', async function () {
  399. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  400. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  401. const depositAssets = parseToken(1n);
  402. const expectedShares = (depositAssets * effectiveShares) / effectiveAssets;
  403. expect(await this.vault.maxDeposit(this.holder)).to.equal(ethers.MaxUint256);
  404. expect(await this.vault.previewDeposit(depositAssets)).to.equal(expectedShares);
  405. const tx = this.vault.connect(this.holder).deposit(depositAssets, this.recipient);
  406. await expect(tx).to.changeTokenBalances(
  407. this.token,
  408. [this.holder, this.vault],
  409. [-depositAssets, depositAssets],
  410. );
  411. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, expectedShares);
  412. await expect(tx)
  413. .to.emit(this.token, 'Transfer')
  414. .withArgs(this.holder.address, this.vault.target, depositAssets)
  415. .to.emit(this.vault, 'Transfer')
  416. .withArgs(ethers.ZeroAddress, this.recipient.address, expectedShares)
  417. .to.emit(this.vault, 'Deposit')
  418. .withArgs(this.holder.address, this.recipient.address, depositAssets, expectedShares);
  419. });
  420. /**
  421. * | offset | deposited assets | redeemable assets |
  422. * |--------|--------------------- |----------------------|
  423. * | 0 | 0.010000000000000001 | 0.010000000000000000 |
  424. * | 6 | 0.010000000000000001 | 0.010000000000000000 |
  425. * | 18 | 0.010000000000000001 | 0.010000000000000000 |
  426. *
  427. * Virtual shares & assets captures part of the value
  428. */
  429. it('mint', async function () {
  430. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  431. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  432. const mintShares = parseShare(1n);
  433. const expectedAssets = (mintShares * effectiveAssets) / effectiveShares + 1n; // add for the rounding
  434. expect(await this.vault.maxMint(this.holder)).to.equal(ethers.MaxUint256);
  435. expect(await this.vault.previewMint(mintShares)).to.equal(expectedAssets);
  436. const tx = this.vault.connect(this.holder).mint(mintShares, this.recipient);
  437. await expect(tx).to.changeTokenBalances(
  438. this.token,
  439. [this.holder, this.vault],
  440. [-expectedAssets, expectedAssets],
  441. );
  442. await expect(tx).to.changeTokenBalance(this.vault, this.recipient, mintShares);
  443. await expect(tx)
  444. .to.emit(this.token, 'Transfer')
  445. .withArgs(this.holder.address, this.vault.target, expectedAssets)
  446. .to.emit(this.vault, 'Transfer')
  447. .withArgs(ethers.ZeroAddress, this.recipient.address, mintShares)
  448. .to.emit(this.vault, 'Deposit')
  449. .withArgs(this.holder.address, this.recipient.address, expectedAssets, mintShares);
  450. });
  451. it('withdraw', async function () {
  452. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  453. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  454. const withdrawAssets = parseToken(1n);
  455. const expectedShares = (withdrawAssets * effectiveShares) / effectiveAssets + 1n; // add for the rounding
  456. expect(await this.vault.maxWithdraw(this.holder)).to.equal(withdrawAssets);
  457. expect(await this.vault.previewWithdraw(withdrawAssets)).to.equal(expectedShares);
  458. const tx = this.vault.connect(this.holder).withdraw(withdrawAssets, this.recipient, this.holder);
  459. await expect(tx).to.changeTokenBalances(
  460. this.token,
  461. [this.vault, this.recipient],
  462. [-withdrawAssets, withdrawAssets],
  463. );
  464. await expect(tx).to.changeTokenBalance(this.vault, this.holder, -expectedShares);
  465. await expect(tx)
  466. .to.emit(this.token, 'Transfer')
  467. .withArgs(this.vault.target, this.recipient.address, withdrawAssets)
  468. .to.emit(this.vault, 'Transfer')
  469. .withArgs(this.holder.address, ethers.ZeroAddress, expectedShares)
  470. .to.emit(this.vault, 'Withdraw')
  471. .withArgs(this.holder.address, this.recipient.address, this.holder.address, withdrawAssets, expectedShares);
  472. });
  473. it('withdraw with approval', async function () {
  474. const assets = await this.vault.previewWithdraw(parseToken(1n));
  475. await expect(this.vault.connect(this.other).withdraw(parseToken(1n), this.recipient, this.holder))
  476. .to.be.revertedWithCustomError(this.vault, 'ERC20InsufficientAllowance')
  477. .withArgs(this.other.address, 0n, assets);
  478. await expect(this.vault.connect(this.spender).withdraw(parseToken(1n), this.recipient, this.holder)).to.not.be
  479. .reverted;
  480. });
  481. it('redeem', async function () {
  482. const effectiveAssets = (await this.vault.totalAssets()) + virtualAssets;
  483. const effectiveShares = (await this.vault.totalSupply()) + virtualShares;
  484. const redeemShares = parseShare(100n);
  485. const expectedAssets = (redeemShares * effectiveAssets) / effectiveShares;
  486. expect(await this.vault.maxRedeem(this.holder)).to.equal(redeemShares);
  487. expect(await this.vault.previewRedeem(redeemShares)).to.equal(expectedAssets);
  488. const tx = this.vault.connect(this.holder).redeem(redeemShares, this.recipient, this.holder);
  489. await expect(tx).to.changeTokenBalances(
  490. this.token,
  491. [this.vault, this.recipient],
  492. [-expectedAssets, expectedAssets],
  493. );
  494. await expect(tx).to.changeTokenBalance(this.vault, this.holder, -redeemShares);
  495. await expect(tx)
  496. .to.emit(this.token, 'Transfer')
  497. .withArgs(this.vault.target, this.recipient.address, expectedAssets)
  498. .to.emit(this.vault, 'Transfer')
  499. .withArgs(this.holder.address, ethers.ZeroAddress, redeemShares)
  500. .to.emit(this.vault, 'Withdraw')
  501. .withArgs(this.holder.address, this.recipient.address, this.holder.address, expectedAssets, redeemShares);
  502. });
  503. it('redeem with approval', async function () {
  504. await expect(this.vault.connect(this.other).redeem(parseShare(100n), this.recipient, this.holder))
  505. .to.be.revertedWithCustomError(this.vault, 'ERC20InsufficientAllowance')
  506. .withArgs(this.other.address, 0n, parseShare(100n));
  507. await expect(this.vault.connect(this.spender).redeem(parseShare(100n), this.recipient, this.holder)).to.not.be
  508. .reverted;
  509. });
  510. });
  511. });
  512. }
  513. describe('ERC4626Fees', function () {
  514. const feeBasisPoints = 500n; // 5%
  515. const valueWithoutFees = 10_000n;
  516. const fees = (valueWithoutFees * feeBasisPoints) / 10_000n;
  517. const valueWithFees = valueWithoutFees + fees;
  518. describe('input fees', function () {
  519. beforeEach(async function () {
  520. const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
  521. const vault = await ethers.deployContract('$ERC4626FeesMock', [
  522. '',
  523. '',
  524. token,
  525. feeBasisPoints,
  526. this.other,
  527. 0n,
  528. ethers.ZeroAddress,
  529. ]);
  530. await token.$_mint(this.holder, ethers.MaxUint256 / 2n);
  531. await token.$_approve(this.holder, vault, ethers.MaxUint256 / 2n);
  532. Object.assign(this, { token, vault });
  533. });
  534. it('deposit', async function () {
  535. expect(await this.vault.previewDeposit(valueWithFees)).to.equal(valueWithoutFees);
  536. this.tx = this.vault.connect(this.holder).deposit(valueWithFees, this.recipient);
  537. });
  538. it('mint', async function () {
  539. expect(await this.vault.previewMint(valueWithoutFees)).to.equal(valueWithFees);
  540. this.tx = this.vault.connect(this.holder).mint(valueWithoutFees, this.recipient);
  541. });
  542. afterEach(async function () {
  543. await expect(this.tx).to.changeTokenBalances(
  544. this.token,
  545. [this.holder, this.vault, this.other],
  546. [-valueWithFees, valueWithoutFees, fees],
  547. );
  548. await expect(this.tx).to.changeTokenBalance(this.vault, this.recipient, valueWithoutFees);
  549. await expect(this.tx)
  550. // get total
  551. .to.emit(this.token, 'Transfer')
  552. .withArgs(this.holder.address, this.vault.target, valueWithFees)
  553. // redirect fees
  554. .to.emit(this.token, 'Transfer')
  555. .withArgs(this.vault.target, this.other.address, fees)
  556. // mint shares
  557. .to.emit(this.vault, 'Transfer')
  558. .withArgs(ethers.ZeroAddress, this.recipient.address, valueWithoutFees)
  559. // deposit event
  560. .to.emit(this.vault, 'Deposit')
  561. .withArgs(this.holder.address, this.recipient.address, valueWithFees, valueWithoutFees);
  562. });
  563. });
  564. describe('output fees', function () {
  565. beforeEach(async function () {
  566. const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
  567. const vault = await ethers.deployContract('$ERC4626FeesMock', [
  568. '',
  569. '',
  570. token,
  571. 0n,
  572. ethers.ZeroAddress,
  573. feeBasisPoints,
  574. this.other,
  575. ]);
  576. await token.$_mint(vault, ethers.MaxUint256 / 2n);
  577. await vault.$_mint(this.holder, ethers.MaxUint256 / 2n);
  578. Object.assign(this, { token, vault });
  579. });
  580. it('redeem', async function () {
  581. expect(await this.vault.previewRedeem(valueWithFees)).to.equal(valueWithoutFees);
  582. this.tx = this.vault.connect(this.holder).redeem(valueWithFees, this.recipient, this.holder);
  583. });
  584. it('withdraw', async function () {
  585. expect(await this.vault.previewWithdraw(valueWithoutFees)).to.equal(valueWithFees);
  586. this.tx = this.vault.connect(this.holder).withdraw(valueWithoutFees, this.recipient, this.holder);
  587. });
  588. afterEach(async function () {
  589. await expect(this.tx).to.changeTokenBalances(
  590. this.token,
  591. [this.vault, this.recipient, this.other],
  592. [-valueWithFees, valueWithoutFees, fees],
  593. );
  594. await expect(this.tx).to.changeTokenBalance(this.vault, this.holder, -valueWithFees);
  595. await expect(this.tx)
  596. // withdraw principal
  597. .to.emit(this.token, 'Transfer')
  598. .withArgs(this.vault.target, this.recipient.address, valueWithoutFees)
  599. // redirect fees
  600. .to.emit(this.token, 'Transfer')
  601. .withArgs(this.vault.target, this.other.address, fees)
  602. // mint shares
  603. .to.emit(this.vault, 'Transfer')
  604. .withArgs(this.holder.address, ethers.ZeroAddress, valueWithFees)
  605. // withdraw event
  606. .to.emit(this.vault, 'Withdraw')
  607. .withArgs(this.holder.address, this.recipient.address, this.holder.address, valueWithoutFees, valueWithFees);
  608. });
  609. });
  610. });
  611. /// Scenario inspired by solmate ERC4626 tests:
  612. /// https://github.com/transmissions11/solmate/blob/main/src/test/ERC4626.t.sol
  613. it('multiple mint, deposit, redeem & withdrawal', async function () {
  614. // test designed with both asset using similar decimals
  615. const [alice, bruce] = this.accounts;
  616. const token = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 18n]);
  617. const vault = await ethers.deployContract('$ERC4626', ['', '', token]);
  618. await token.$_mint(alice, 4000n);
  619. await token.$_mint(bruce, 7001n);
  620. await token.connect(alice).approve(vault, 4000n);
  621. await token.connect(bruce).approve(vault, 7001n);
  622. // 1. Alice mints 2000 shares (costs 2000 tokens)
  623. await expect(vault.connect(alice).mint(2000n, alice))
  624. .to.emit(token, 'Transfer')
  625. .withArgs(alice.address, vault.target, 2000n)
  626. .to.emit(vault, 'Transfer')
  627. .withArgs(ethers.ZeroAddress, alice.address, 2000n);
  628. expect(await vault.previewDeposit(2000n)).to.equal(2000n);
  629. expect(await vault.balanceOf(alice)).to.equal(2000n);
  630. expect(await vault.balanceOf(bruce)).to.equal(0n);
  631. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2000n);
  632. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(0n);
  633. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(2000n);
  634. expect(await vault.totalSupply()).to.equal(2000n);
  635. expect(await vault.totalAssets()).to.equal(2000n);
  636. // 2. Bruce deposits 4000 tokens (mints 4000 shares)
  637. await expect(vault.connect(bruce).mint(4000n, bruce))
  638. .to.emit(token, 'Transfer')
  639. .withArgs(bruce.address, vault.target, 4000n)
  640. .to.emit(vault, 'Transfer')
  641. .withArgs(ethers.ZeroAddress, bruce.address, 4000n);
  642. expect(await vault.previewDeposit(4000n)).to.equal(4000n);
  643. expect(await vault.balanceOf(alice)).to.equal(2000n);
  644. expect(await vault.balanceOf(bruce)).to.equal(4000n);
  645. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2000n);
  646. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(4000n);
  647. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6000n);
  648. expect(await vault.totalSupply()).to.equal(6000n);
  649. expect(await vault.totalAssets()).to.equal(6000n);
  650. // 3. Vault mutates by +3000 tokens (simulated yield returned from strategy)
  651. await token.$_mint(vault, 3000n);
  652. expect(await vault.balanceOf(alice)).to.equal(2000n);
  653. expect(await vault.balanceOf(bruce)).to.equal(4000n);
  654. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(2999n); // used to be 3000, but virtual assets/shares captures part of the yield
  655. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(5999n); // used to be 6000, but virtual assets/shares captures part of the yield
  656. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6000n);
  657. expect(await vault.totalSupply()).to.equal(6000n);
  658. expect(await vault.totalAssets()).to.equal(9000n);
  659. // 4. Alice deposits 2000 tokens (mints 1333 shares)
  660. await expect(vault.connect(alice).deposit(2000n, alice))
  661. .to.emit(token, 'Transfer')
  662. .withArgs(alice.address, vault.target, 2000n)
  663. .to.emit(vault, 'Transfer')
  664. .withArgs(ethers.ZeroAddress, alice.address, 1333n);
  665. expect(await vault.balanceOf(alice)).to.equal(3333n);
  666. expect(await vault.balanceOf(bruce)).to.equal(4000n);
  667. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(4999n);
  668. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(6000n);
  669. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(7333n);
  670. expect(await vault.totalSupply()).to.equal(7333n);
  671. expect(await vault.totalAssets()).to.equal(11000n);
  672. // 5. Bruce mints 2000 shares (costs 3001 assets)
  673. // NOTE: Bruce's assets spent got rounded towards infinity
  674. // NOTE: Alices's vault assets got rounded towards infinity
  675. await expect(vault.connect(bruce).mint(2000n, bruce))
  676. .to.emit(token, 'Transfer')
  677. .withArgs(bruce.address, vault.target, 3000n)
  678. .to.emit(vault, 'Transfer')
  679. .withArgs(ethers.ZeroAddress, bruce.address, 2000n);
  680. expect(await vault.balanceOf(alice)).to.equal(3333n);
  681. expect(await vault.balanceOf(bruce)).to.equal(6000n);
  682. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(4999n); // used to be 5000
  683. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(9000n);
  684. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(9333n);
  685. expect(await vault.totalSupply()).to.equal(9333n);
  686. expect(await vault.totalAssets()).to.equal(14000n); // used to be 14001
  687. // 6. Vault mutates by +3000 tokens
  688. // NOTE: Vault holds 17001 tokens, but sum of assetsOf() is 17000.
  689. await token.$_mint(vault, 3000n);
  690. expect(await vault.balanceOf(alice)).to.equal(3333n);
  691. expect(await vault.balanceOf(bruce)).to.equal(6000n);
  692. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(6070n); // used to be 6071
  693. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(10928n); // used to be 10929
  694. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(9333n);
  695. expect(await vault.totalSupply()).to.equal(9333n);
  696. expect(await vault.totalAssets()).to.equal(17000n); // used to be 17001
  697. // 7. Alice redeem 1333 shares (2428 assets)
  698. await expect(vault.connect(alice).redeem(1333n, alice, alice))
  699. .to.emit(vault, 'Transfer')
  700. .withArgs(alice.address, ethers.ZeroAddress, 1333n)
  701. .to.emit(token, 'Transfer')
  702. .withArgs(vault.target, alice.address, 2427n); // used to be 2428
  703. expect(await vault.balanceOf(alice)).to.equal(2000n);
  704. expect(await vault.balanceOf(bruce)).to.equal(6000n);
  705. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(3643n);
  706. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(10929n);
  707. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(8000n);
  708. expect(await vault.totalSupply()).to.equal(8000n);
  709. expect(await vault.totalAssets()).to.equal(14573n);
  710. // 8. Bruce withdraws 2929 assets (1608 shares)
  711. await expect(vault.connect(bruce).withdraw(2929n, bruce, bruce))
  712. .to.emit(vault, 'Transfer')
  713. .withArgs(bruce.address, ethers.ZeroAddress, 1608n)
  714. .to.emit(token, 'Transfer')
  715. .withArgs(vault.target, bruce.address, 2929n);
  716. expect(await vault.balanceOf(alice)).to.equal(2000n);
  717. expect(await vault.balanceOf(bruce)).to.equal(4392n);
  718. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(3643n);
  719. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(8000n);
  720. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(6392n);
  721. expect(await vault.totalSupply()).to.equal(6392n);
  722. expect(await vault.totalAssets()).to.equal(11644n);
  723. // 9. Alice withdraws 3643 assets (2000 shares)
  724. // NOTE: Bruce's assets have been rounded back towards infinity
  725. await expect(vault.connect(alice).withdraw(3643n, alice, alice))
  726. .to.emit(vault, 'Transfer')
  727. .withArgs(alice.address, ethers.ZeroAddress, 2000n)
  728. .to.emit(token, 'Transfer')
  729. .withArgs(vault.target, alice.address, 3643n);
  730. expect(await vault.balanceOf(alice)).to.equal(0n);
  731. expect(await vault.balanceOf(bruce)).to.equal(4392n);
  732. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(0n);
  733. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(8000n); // used to be 8001
  734. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(4392n);
  735. expect(await vault.totalSupply()).to.equal(4392n);
  736. expect(await vault.totalAssets()).to.equal(8001n);
  737. // 10. Bruce redeem 4392 shares (8001 tokens)
  738. await expect(vault.connect(bruce).redeem(4392n, bruce, bruce))
  739. .to.emit(vault, 'Transfer')
  740. .withArgs(bruce.address, ethers.ZeroAddress, 4392n)
  741. .to.emit(token, 'Transfer')
  742. .withArgs(vault.target, bruce.address, 8000n); // used to be 8001
  743. expect(await vault.balanceOf(alice)).to.equal(0n);
  744. expect(await vault.balanceOf(bruce)).to.equal(0n);
  745. expect(await vault.convertToAssets(await vault.balanceOf(alice))).to.equal(0n);
  746. expect(await vault.convertToAssets(await vault.balanceOf(bruce))).to.equal(0n);
  747. expect(await vault.convertToShares(await token.balanceOf(vault))).to.equal(0n);
  748. expect(await vault.totalSupply()).to.equal(0n);
  749. expect(await vault.totalAssets()).to.equal(1n); // used to be 0
  750. });
  751. });