Skip to content

Instantly share code, notes, and snippets.

@lwj010
lwj010 / YieldMath.sol
Created March 1, 2022 01:32
fyTokenInForBaseOut()
contract YieldMath {
/**
* Calculate the amount of fyToken a user could sell for given amount of Base.
* https://www.desmos.com/calculator/0rgnmtckvy
* @param baseReserves base reserves amount
* @param fyTokenReserves fyToken reserves amount
* @param baseAmount Base amount to be traded
* @param timeTillMaturity time till maturity in seconds
* @param ts time till maturity coefficient, multiplied by 2^64
@lwj010
lwj010 / Pool.sol
Created March 1, 2022 01:29
_buyBasePreview()
contract Pool {
/// @dev Returns how much fyToken would be required to buy `tokenOut` base.
function _buyBasePreview(
uint128 tokenOut,
uint112 baseBalance,
uint112 fyTokenBalance
)
private view
beforeMaturity
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:28
buyBase()
contract Pool {
/// @dev Buy base for fyToken
/// The trader needs to have called `fyToken.approve`
/// @param to Wallet receiving the base being bought
/// @param tokenOut Amount of base being bought that will be deposited in `to` wallet
/// @param max Maximum amount of fyToken that will be paid for the trade
/// @return Amount of fyToken that will be taken from caller
function buyBase(address to, uint128 tokenOut, uint128 max)
external override
returns(uint128)
@lwj010
lwj010 / ERC20.sol
Created March 1, 2022 01:26
ERC20.sol burn()
contract ERC20 {
function _burn(address src, uint wad) internal virtual returns (bool) {
unchecked {
require(_balanceOf[src] >= wad, "ERC20: Insufficient balance");
_balanceOf[src] = _balanceOf[src] - wad;
_totalSupply = _totalSupply - wad;
emit Transfer(src, address(0), wad);
}
return true;
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:25
function burn()
contract Pool {
/// @dev Burn liquidity tokens in exchange for base and fyToken.
/// The liquidity tokens need to be in this contract.
/// @param baseTo Wallet receiving the base.
/// @param fyTokenTo Wallet receiving the fyToken.
/// @param minRatio Minimum ratio of base to fyToken in the pool.
/// @param maxRatio Maximum ratio of base to fyToken in the pool.
/// @return The amount of tokens burned and returned (tokensBurned, bases, fyTokens).
function burn(address baseTo, address fyTokenTo, uint256 minRatio, uint256 maxRatio)
external override
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:24
function _update()
contract Pool {
/// @dev Update cache and, on the first call per block, ratio accumulators
function _update(uint128 baseBalance, uint128 fyBalance, uint112 _baseCached, uint112 _fyTokenCached) private {
uint32 blockTimestamp = uint32(block.timestamp);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _baseCached != 0 && _fyTokenCached != 0) {
// We multiply by 1e27 here so that r = t * y/x is a fixed point factor with 27 decimals
uint256 scaledFYTokenCached = uint256(_fyTokenCached) * 1e27;
cumulativeBalancesRatio += scaledFYTokenCached * timeElapsed / _baseCached;
}
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:24
function _mintInternal()
contract Pool {
/// @dev Mint liquidity tokens, with an optional internal trade to buy fyToken beforehand.
/// The amount of liquidity tokens is calculated from the amount of fyToken to buy from the pool, plus the amount of unaccounted for fyToken in this contract.
/// The base tokens need to be present in this contract, unaccounted for.
/// @param fyTokenToBuy Amount of `fyToken` being bought in the Pool, from this we calculate how much base it will be taken in.
function _mintInternal(address to, address remainder, uint256 fyTokenToBuy, uint256 minRatio, uint256 maxRatio)
internal
returns (uint256 baseIn, uint256 fyTokenIn, uint256 tokensMinted)
{
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:24
function mint()
contract pool {
/// @param to Wallet receiving the minted liquidity tokens.
/// @param remainder Wallet receiving any surplus base.
/// @param minRatio Minimum ratio of base to fyToken in the pool.
/// @param maxRatio Maximum ratio of base to fyToken in the pool.
/// @return The amount of liquidity tokens minted.
function mint(address to, address remainder, uint256 minRatio, uint256 maxRatio)
external override
returns (uint256, uint256, uint256)
@lwj010
lwj010 / Pool.sol
Last active March 1, 2022 01:24
function _burnInternal()
contract pool {
/// @dev Burn liquidity tokens in exchange for base.
/// The liquidity provider needs to have called `pool.approve`.
/// @return tokensBurned The amount of pool tokens burned.
/// @return tokenOut The amount of base tokens returned.
/// @return fyTokenOut The amount of fyTokens returned.
function _burnInternal(address baseTo, address fyTokenTo, bool tradeToBase, uint256 minRatio, uint256 maxRatio)
internal
returns (uint256 tokensBurned, uint256 tokenOut, uint256 fyTokenOut)
// CErc20.sol
contract CErc20 is CToken, CErc20Interface {
// 중간 코드는 생략
function liquidateBorrow(address borrower, uint repayAmount, CTokenInterface cTokenCollateral) external returns (uint) {
(uint err,) = liquidateBorrowInternal(borrower, repayAmount, cTokenCollateral);
return err;
}
}