Skip to content

Commit

Permalink
Merge pull request #21 from BarnBridge/events
Browse files Browse the repository at this point in the history
added user events
  • Loading branch information
popra committed Feb 16, 2021
2 parents 1e8b34b + 488eb0b commit 66d037f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
54 changes: 44 additions & 10 deletions contracts/SmartYield.sol
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ contract SmartYield is

bool public _setup;

// emitted when user buys junior ERC20 tokens
event BuyTokens(address indexed buyer, uint256 underlyingIn, uint256 tokensOut, uint256 fee);
// emitted when user sells junior ERC20 tokens and forfeits their share of the debt
event SellTokens(address indexed seller, uint256 tokensIn, uint256 underlyingOut, uint256 forfeits);

event BuySeniorBond(address indexed buyer, uint256 indexed seniorBondId, uint256 underlyingIn, uint256 gain, uint256 forDays);

event RedeemSeniorBond(address indexed owner, uint256 indexed seniorBondId, uint256 fee);

event BuyJuniorBond(address indexed buyer, uint256 indexed juniorBondId, uint256 tokensIn, uint256 maturesAt);

event RedeemJuniorBond(address indexed owner, uint256 indexed juniorBondId, uint256 underlyingOut);

constructor(
string memory name_,
string memory symbol_
Expand Down Expand Up @@ -138,9 +151,13 @@ contract SmartYield is

// ---

IProvider(pool)._takeUnderlying(msg.sender, underlyingAmount_);
address buyer = msg.sender;

IProvider(pool)._takeUnderlying(buyer, underlyingAmount_);
IProvider(pool)._depositProvider(underlyingAmount_, fee);
_mint(msg.sender, getsTokens);
_mint(buyer, getsTokens);

emit BuyTokens(buyer, underlyingAmount_, getsTokens, fee);
}

// sell _tokens for at least _minUnderlying, before _deadline and forfeit potential future gains
Expand All @@ -160,8 +177,9 @@ contract SmartYield is

// share of these tokens in the debt
uint256 debtShare = tokenAmount_ * 1e18 / totalSupply();
uint256 forfeits = (this.abondDebt() * debtShare) / 1e18;
// debt share is forfeit, and only diff is returned to user
uint256 toPay = (tokenAmount_ * this.price() - this.abondDebt() * debtShare) / 1e18;
uint256 toPay = (tokenAmount_ * this.price()) / 1e18 - forfeits;

require(
toPay >= minUnderlying_,
Expand All @@ -170,9 +188,13 @@ contract SmartYield is

// ---

_burn(msg.sender, tokenAmount_);
address seller = msg.sender;

_burn(seller, tokenAmount_);
IProvider(pool)._withdrawProvider(toPay, 0);
IProvider(pool)._sendUnderlying(msg.sender, toPay);
IProvider(pool)._sendUnderlying(seller, toPay);

emit SellTokens(seller, tokenAmount_, toPay, forfeits);
}

// Purchase a senior bond with principalAmount_ underlying for forDays_, buyer gets a bond with gain >= minGain_ or revert. deadline_ is timestamp before which tx is not rejected.
Expand Down Expand Up @@ -222,7 +244,9 @@ contract SmartYield is

// ---

IProvider(pool)._takeUnderlying(msg.sender, principalAmount_);
address buyer = msg.sender;

IProvider(pool)._takeUnderlying(buyer, principalAmount_);
IProvider(pool)._depositProvider(principalAmount_, 0);

SeniorBond memory b =
Expand All @@ -234,7 +258,9 @@ contract SmartYield is
false
);

_mintBond(msg.sender, b);
_mintBond(buyer, b);

emit BuySeniorBond(buyer, seniorBondId, principalAmount_, gain, forDays_);
}

// buy an nft with tokenAmount_ jTokens, that matures at abond maturesAt
Expand Down Expand Up @@ -264,8 +290,12 @@ contract SmartYield is

// ---

_takeTokens(msg.sender, tokenAmount_);
_mintJuniorBond(msg.sender, jb);
address buyer = msg.sender;

_takeTokens(buyer, tokenAmount_);
_mintJuniorBond(buyer, jb);

emit BuyJuniorBond(buyer, juniorBondId, tokenAmount_, maturesAt);

// if abond.maturesAt is past we can liquidate, but juniorBondsMaturingAt might have already been liquidated
if (this.currentTime() >= maturesAt) {
Expand Down Expand Up @@ -311,10 +341,12 @@ contract SmartYield is

// bondToken.burn will revert for already burned tokens
IBond(seniorBond).burn(bondId_);
delete seniorBonds[bondId_];
//delete seniorBonds[bondId_];

IProvider(pool)._withdrawProvider(payAmnt, fee);
IProvider(pool)._sendUnderlying(payTo, payAmnt);

emit RedeemSeniorBond(payTo, seniorBondId, fee);
}

// once matured, redeem a jBond for underlying
Expand All @@ -341,6 +373,8 @@ contract SmartYield is
IProvider(pool)._withdrawProvider(payAmnt, 0);
IProvider(pool)._sendUnderlying(payTo, payAmnt);
underlyingLiquidatedJuniors -= payAmnt;

emit RedeemJuniorBond(payTo, jBondId_, payAmnt);
}


Expand Down
22 changes: 20 additions & 2 deletions contracts/providers/CompoundProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ contract CompoundProvider is IProvider {

bool public _setup;

event Harvest(address indexed caller, uint256 underlyingGot, uint256 rewardExpected, uint256 underlyingDeposited, uint256 fees, uint256 reward);

event TransferFees(address indexed caller, address indexed feesOwner, uint256 fees);

modifier accountYield {
_accountYieldInternal();
IYieldOracle(IController(this.controller()).oracle()).update();
Expand Down Expand Up @@ -137,6 +141,8 @@ contract CompoundProvider is IProvider {
"PPC: harvest later"
);

address caller = msg.sender;

// this is 0 unless someone transfers underlying to the contract
uint256 underlyingBefore = IERC20(uToken).balanceOf(address(this));

Expand Down Expand Up @@ -209,8 +215,12 @@ contract CompoundProvider is IProvider {
// any extra goodies go to fees
_depositProviderInternal(underlyingGot - toCaller + extra, extra);

uint256 reward = IERC20(uToken).balanceOf(address(this));

// pay this man
IERC20(uToken).transfer(msg.sender, IERC20(uToken).balanceOf(address(this)));
IERC20(uToken).transfer(caller, reward);

emit Harvest(caller, rewardGot, rewardExpected, underlyingGot - toCaller + extra, extra, reward);
}

function transferFees()
Expand All @@ -226,10 +236,18 @@ contract CompoundProvider is IProvider {
uint256 err = ICToken(cToken).redeem(
MathUtils.min(ctokensToPay, ICTokenErc20(cToken).balanceOf(address(this)))
);

require(0 == err, "PPC: transferFees redeem");

underlyingFees = 0;
cTokenBalance = ICTokenErc20(cToken).balanceOf(address(this));
IERC20(uToken).transfer(IController(controller).feesOwner(), IERC20(uToken).balanceOf(address(this)));

uint256 fees = IERC20(uToken).balanceOf(address(this));
address to = IController(controller).feesOwner();

IERC20(uToken).transfer(to, fees);

emit TransferFees(msg.sender, to, fees);
}

// returns cumulatives and accumulates/updates internal state
Expand Down

0 comments on commit 66d037f

Please sign in to comment.