Created
April 13, 2021 23:44
-
-
Save talkol/fd79ce62e02e67ffd6a900526ccd1f46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Minter { | |
uint256 pairedETH; | |
uint256 pairedUSDC; | |
uint256 pairedShares; | |
uint256 unpairedETH; | |
uint256 unpairedShares; | |
} | |
uint256 totalSushiLP; | |
uint256 totalPairedETH; | |
uint256 totalPairedUSDC; | |
uint256 totalPairedShares; | |
function _deposit(uint256 amountETH) returns (uint256 addedUSDC, uint256 addedETH, uint256 shares) { | |
Minter minter = minters[beneficiary]; | |
uint256 sushiLP; | |
(addedUSDC, addedETH, sushiLP) = _poolAddLiquidityAndStake(amountETH); | |
if (totalPairedShares == 0) { | |
shares = sushiLP; | |
} else { | |
shares = sushiLP.mul(totalPairedShares).div(totalSushiLP); | |
} | |
minter.pairedUSDC = minter.pairedUSDC.add(addedUSDC); | |
minter.pairedETH = minter.pairedETH.add(addedETH); | |
minter.pairedShares = minter.pairedShares.add(shares); | |
totalPairedUSDC = totalPairedUSDC.add(addedUSDC); | |
totalPairedETH = totalPairedETH.add(addedETH); | |
totalPairedShares = totalPairedShares.add(shares); | |
totalSushiLP = totalSushiLP.add(sushiLP); | |
_mint(beneficiary, shares); | |
} | |
function _withdraw(uint shares) returns (uint exitETH) { | |
Minter minter = minters[sender]; | |
shares = Math.min(shares, minter.pairedShares + minter.unpairedShares); | |
require(shares > 0, "sender not in minters"); | |
if (shares > minter.unpairedShares) { | |
_unpair(shares - minter.unpairedShares); | |
} | |
exitETH = minter.unpairedETH.mul(shares).div(minter.unpairedShares); | |
minter.unpairedShares = minter.unpairedShares.sub(shares); | |
_burn(sender, shares); | |
} | |
function _unpair(uint shares) { | |
uint256 sushiLP = shares.mul(totalSushiLP).div(totalPairedShares); | |
(uint256 removedETH, uint256 removedUSDC) = _poolUnstakeAndRemoveLiquidity(sushiLP); | |
uint256 pairedUSDC = minter.pairedUSDC.mul(shares).div(minter.pairedShares); | |
uint256 pairedETH = minter.pairedETH.mul(shares).div(minter.pairedShares); | |
uint256 exitUSDC; | |
(exitUSDC, exitETH) = applyRebalance(removedUSDC, removedETH, pairedUSDC, pairedETH); | |
minter.pairedUSDC = minter.pairedUSDC.sub(pairedUSDC); | |
minter.pairedETH = minter.pairedETH.sub(pairedETH); | |
minter.pairedShares = minter.pairedShares.sub(shares); | |
minter.unpairedETH = minter.unpairedETH.add(exitETH); | |
minter.unpairedShares = minter.unpairedShares.add(shares); | |
totalPairedUSDC = totalPairedUSDC.sub(pairedUSDC); | |
totalPairedETH = totalPairedETH.sub(pairedETH); | |
totalPairedShares = totalPairedShares.sub(shares); | |
totalSushiLP = totalSushiLP.sub(sushiLP); | |
} | |
function emergencyExit(address[] minterAddresses) { | |
if (!paused()) _pause(); | |
for (uint256 i = 0; i < minterAddresses.length; i++) { | |
address minterAddress = minterAddresses[i]; | |
Minter minter = minters[minterAddress]; | |
uint256 shares = minter.pairedShares; | |
if (shares > 0) { | |
_unpair(shares); | |
} | |
} | |
withdrawFreeCapital(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment