Skip to content

Commit

Permalink
add unguarded initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Aodhgan authored and PierrickGT committed Jul 7, 2021
1 parent 59192c8 commit 441bce7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
17 changes: 0 additions & 17 deletions contracts/ISushi.sol

This file was deleted.

34 changes: 19 additions & 15 deletions contracts/SushiYieldSource.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ import "./ISushiBar.sol";
contract SushiYieldSource is IYieldSource {
using SafeERC20 for IERC20;
using SafeMath for uint256;

ISushiBar public immutable sushiBar;
IERC20 public immutable sushiAddr;

mapping(address => uint256) public balances;

constructor(ISushiBar _sushiBar, IERC20 _sushiAddr) public {
sushiBar = _sushiBar;
sushiAddr = _sushiAddr;
}

/// @notice Approve SUSHI to spend infinite sushiBar (xSUSHI)
function intialize() external {
sushiAddr.safeApprove(address(sushiBar), type(uint256).max);
}

/// @notice Returns the ERC20 asset token used for deposits.
/// @return The ERC20 asset token
function depositToken() public view override returns (address) {
Expand All @@ -38,56 +43,55 @@ contract SushiYieldSource is IYieldSource {
uint256 totalShares = sushiBar.totalSupply();
uint256 barSushiBalance = sushiAddr.balanceOf(address(sushiBar));

return balances[addr].mul(barSushiBalance).div(totalShares);
return balances[addr].mul(barSushiBalance).div(totalShares);
}

/// @notice Allows assets to be supplied on other user's behalf using the `to` param.
/// @param amount The amount of `token()` to be supplied
/// @param to The user whose balance will receive the tokens
function supplyTokenTo(uint256 amount, address to) public override {
sushiAddr.safeTransferFrom(msg.sender, address(this), amount);
sushiAddr.approve(address(sushiBar), amount);

ISushiBar bar = sushiBar;
uint256 beforeBalance = bar.balanceOf(address(this));

bar.enter(amount);

uint256 afterBalance = bar.balanceOf(address(this));
uint256 balanceDiff = afterBalance.sub(beforeBalance);

balances[to] = balances[to].add(balanceDiff);
}

/// @notice Redeems tokens from the yield source to the msg.sender, it burns yield bearing tokens and returns token to the sender.
/// @param amount The amount of `token()` to withdraw. Denominated in `token()` as above.
/// @dev The maxiumum that can be called for token() is calculated by balanceOfToken() above.
/// @return The actual amount of tokens that were redeemed. This may be different from the amount passed due to the fractional math involved.
/// @return The actual amount of tokens that were redeemed. This may be different from the amount passed due to the fractional math involved.
function redeemToken(uint256 amount) public override returns (uint256) {
ISushiBar bar = sushiBar;
IERC20 sushi = sushiAddr;

uint256 totalShares = bar.totalSupply();
if(totalShares == 0) return 0;
if (totalShares == 0) return 0;

uint256 barSushiBalance = sushi.balanceOf(address(bar));
if(barSushiBalance == 0) return 0;
if (barSushiBalance == 0) return 0;

uint256 sushiBeforeBalance = sushi.balanceOf(address(this));

uint256 requiredShares = ((amount.mul(totalShares) + totalShares)).div(barSushiBalance);
if(requiredShares == 0) return 0;
if (requiredShares == 0) return 0;

uint256 requiredSharesBalance = requiredShares.sub(1);
bar.leave(requiredSharesBalance);

uint256 sushiAfterBalance = sushi.balanceOf(address(this));

uint256 sushiBalanceDiff = sushiAfterBalance.sub(sushiBeforeBalance);

balances[msg.sender] = balances[msg.sender].sub(requiredSharesBalance);
sushi.safeTransfer(msg.sender, sushiBalanceDiff);

return (sushiBalanceDiff);
}

Expand Down
2 changes: 2 additions & 0 deletions test/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ describe("SushiYieldSource integration", function () {
"0x6B3595068778DD592e39A122f4f5a5cF09C90fE2",
{ gasLimit: 9500000 }
);
await yieldSource.intialize()

const yieldSourcePrizePoolConfig = {
yieldSource: yieldSource.address,
maxExitFeeMantissa: toWei("0.5"),
Expand Down
1 change: 1 addition & 0 deletions test/unit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe("SushiYieldSource", function () {
overrides
);
amount = toWei("100");
await yieldSource.intialize()
await sushi.mint(wallet.address, amount);
await sushi.mint(wallet2.address, amount.mul(99));
await sushi.connect(wallet2).approve(sushiBar.address, amount.mul(99));
Expand Down

0 comments on commit 441bce7

Please sign in to comment.