Skip to content

Instantly share code, notes, and snippets.

@sarvagnakadiya
Last active July 3, 2024 11:31
Show Gist options
  • Save sarvagnakadiya/f125fa5d178054d3c8b122c07c49f4ac to your computer and use it in GitHub Desktop.
Save sarvagnakadiya/f125fa5d178054d3c8b122c07c49f4ac to your computer and use it in GitHub Desktop.
The notes and explanation for vaults

vaults (ERC-4626)

https://ethereum.org/en/developers/docs/standards/tokens/erc-4626/

image

how many shares to mint

  • a = amount to deposit
  • B = balance of vault before deposit
  • s = shares to mint
  • T = total shares before mint

image

eQ: a+B / B = s+T/T

to find s (shares) we can simplify it to:

  1. Cross-multiply: [ (a + B)T = (s + T)B ]

  2. Distribute both sides: [ aT + BT = sB + TB ]

  3. Subtract ( BT ) from both sides: [ aT = sB ]

  4. Solve for ( s ): [ s = aT/B ]

s = aT/B

How much amount to withdraw

  • a = amount to withdraw
  • B = balance of vault before withdraw
  • s = shares to burn
  • T = total shares before burn image

eQ: B-a/B = T-s/T

  1. Cross-multiply: [ (B + a)T = (T - s)B ]

  2. Distribute both sides: [ BT + aT = TB + sB ]

  3. Subtract ( BT ) from both sides: [ aT = sB ]

  4. Solve for ( a ): [ a = sB/T ]

a = sB/T

Example

Step-by-Step Calculation

  1. Initial Shares and Deposits:

    • User 1 deposited 125 and has 125 shares.
    • User 2 deposited 200 and has 200 shares.
    • Total shares: 125 + 200 = 325 shares.
    • Total initial deposits: 125 + 200 = 325.
  2. Vault Yields:

    • The vault yields 100, so the new total value of the vault is: [ 325 + 100 = 425 ]
  3. Share Value:

    • The new share value is: [ \text{New share price} = \frac{\text{Total value of the vault}}{\text{Total shares}} = \frac{425}{325} \approx 1.3077 ]
  4. Withdrawals:

    • Each user will withdraw an amount proportional to the shares they hold.

User 1:

  • User 1 has 125 shares.
  • The value of User 1's shares: [ \text{Value of User 1's shares} = 125 \times 1.3077 \approx 163.46 ]

User 2:

  • User 2 has 200 shares.
  • The value of User 2's shares: [ \text{Value of User 2's shares} = 200 \times 1.3077 \approx 261.54 ]

Summary

  • User 1 can withdraw approximately 163.46.
  • User 2 can withdraw approximately 261.54.

These values add up to the total vault value of 425, ensuring that the withdrawals are proportional to the shares held by each user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment