Transactions
Token Transfers
Tokens
Internal Transactions
Coin Balance History
Logs
Code
Read Contract
Write Contract
Warning! Contract bytecode has been changed and doesn't match the verified one. Therefore, interaction with this smart contract may be risky.
- Contract name:
- Borrowable
- Optimization enabled
- true
- Compiler version
- v0.5.16+commit.9c3226ce
- Optimization runs
- 200
- Verified at
- 2023-03-07T18:05:49.957228Z
contracts/Borrowable.sol
pragma solidity =0.5.16; import "./PoolToken.sol"; import "./BAllowance.sol"; import "./BInterestRateModel.sol"; import "./BSetter.sol"; import "./BStorage.sol"; import "./interfaces/IBorrowable.sol"; import "./interfaces/ICollateral.sol"; import "./interfaces/ITarotCallee.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IFactory.sol"; import "./interfaces/IBorrowTracker.sol"; import "./libraries/Math.sol"; contract Borrowable is IBorrowable, PoolToken, BStorage, BSetter, BInterestRateModel, BAllowance { uint256 public constant BORROW_FEE = 0.0001e18; //0.01% event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); constructor() public {} /*** PoolToken ***/ function _update() internal { super._update(); _calculateBorrowRate(); } function _mintReserves(uint256 _exchangeRate, uint256 _totalSupply) internal returns (uint256) { uint256 _exchangeRateLast = exchangeRateLast; if (_exchangeRate > _exchangeRateLast) { uint256 _exchangeRateNew = _exchangeRate.sub( _exchangeRate.sub(_exchangeRateLast).mul(reserveFactor).div( 1e18 ) ); uint256 liquidity = _totalSupply.mul(_exchangeRate).div(_exchangeRateNew).sub( _totalSupply ); if (liquidity > 0) { address reservesManager = IFactory(factory).reservesManager(); _mint(reservesManager, liquidity); } exchangeRateLast = _exchangeRateNew; return _exchangeRateNew; } else return _exchangeRate; } function exchangeRate() public accrue returns (uint256) { uint256 _totalSupply = totalSupply; uint256 _actualBalance = totalBalance.add(totalBorrows); if (_totalSupply == 0 || _actualBalance == 0) return initialExchangeRate; uint256 _exchangeRate = _actualBalance.mul(1e18).div(_totalSupply); return _mintReserves(_exchangeRate, _totalSupply); } // force totalBalance to match real balance function sync() external nonReentrant update accrue {} /*** Borrowable ***/ // this is the stored borrow balance; the current borrow balance may be slightly higher function borrowBalance(address borrower) public view returns (uint256) { BorrowSnapshot memory borrowSnapshot = borrowBalances[borrower]; if (borrowSnapshot.interestIndex == 0) return 0; // not initialized return uint256(borrowSnapshot.principal).mul(borrowIndex).div( borrowSnapshot.interestIndex ); } function _trackBorrow( address borrower, uint256 accountBorrows, uint256 _borrowIndex ) internal { address _borrowTracker = borrowTracker; if (_borrowTracker == address(0)) return; IBorrowTracker(_borrowTracker).trackBorrow( borrower, accountBorrows, _borrowIndex ); } function _updateBorrow( address borrower, uint256 borrowAmount, uint256 repayAmount ) private returns ( uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 _totalBorrows ) { accountBorrowsPrior = borrowBalance(borrower); if (borrowAmount == repayAmount) return (accountBorrowsPrior, accountBorrowsPrior, totalBorrows); uint112 _borrowIndex = borrowIndex; if (borrowAmount > repayAmount) { BorrowSnapshot storage borrowSnapshot = borrowBalances[borrower]; uint256 increaseAmount = borrowAmount - repayAmount; accountBorrows = accountBorrowsPrior.add(increaseAmount); borrowSnapshot.principal = safe112(accountBorrows); borrowSnapshot.interestIndex = _borrowIndex; _totalBorrows = uint256(totalBorrows).add(increaseAmount); totalBorrows = safe112(_totalBorrows); } else { BorrowSnapshot storage borrowSnapshot = borrowBalances[borrower]; uint256 decreaseAmount = repayAmount - borrowAmount; accountBorrows = accountBorrowsPrior > decreaseAmount ? accountBorrowsPrior - decreaseAmount : 0; borrowSnapshot.principal = safe112(accountBorrows); if (accountBorrows == 0) { borrowSnapshot.interestIndex = 0; } else { borrowSnapshot.interestIndex = _borrowIndex; } uint256 actualDecreaseAmount = accountBorrowsPrior.sub(accountBorrows); _totalBorrows = totalBorrows; // gas savings _totalBorrows = _totalBorrows > actualDecreaseAmount ? _totalBorrows - actualDecreaseAmount : 0; totalBorrows = safe112(_totalBorrows); } _trackBorrow(borrower, accountBorrows, _borrowIndex); } // this low-level function should be called from another contract function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external nonReentrant update accrue { uint256 _totalBalance = totalBalance; require(borrowAmount <= _totalBalance, "Tarot: INSUFFICIENT_CASH"); _checkBorrowAllowance(borrower, msg.sender, borrowAmount); // optimistically transfer funds if (borrowAmount > 0) _safeTransfer(receiver, borrowAmount); if (data.length > 0) ITarotCallee(receiver).tarotBorrow( msg.sender, borrower, borrowAmount, data ); uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 borrowFee = borrowAmount.mul(BORROW_FEE).div(1e18); uint256 adjustedBorrowAmount = borrowAmount.add(borrowFee); uint256 repayAmount = balance.add(borrowAmount).sub(_totalBalance); ( uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 _totalBorrows ) = _updateBorrow(borrower, adjustedBorrowAmount, repayAmount); if (adjustedBorrowAmount > repayAmount) require( ICollateral(collateral).canBorrow( borrower, address(this), accountBorrows ), "Tarot: INSUFFICIENT_LIQUIDITY" ); emit Borrow( msg.sender, borrower, receiver, borrowAmount, repayAmount, accountBorrowsPrior, accountBorrows, _totalBorrows ); } // this low-level function should be called from another contract function liquidate(address borrower, address liquidator) external nonReentrant update accrue returns (uint256 seizeTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 repayAmount = balance.sub(totalBalance); uint256 actualRepayAmount = Math.min(borrowBalance(borrower), repayAmount); seizeTokens = ICollateral(collateral).seize( liquidator, borrower, actualRepayAmount ); ( uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 _totalBorrows ) = _updateBorrow(borrower, 0, repayAmount); emit Liquidate( msg.sender, borrower, liquidator, seizeTokens, repayAmount, accountBorrowsPrior, accountBorrows, _totalBorrows ); } function trackBorrow(address borrower) external { _trackBorrow(borrower, borrowBalance(borrower), borrowIndex); } modifier accrue() { accrueInterest(); _; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
contracts/libraries/Math.sol
pragma solidity =0.5.16; // a library for performing various math operations // forked from: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/libraries/Math.sol library Math { function min(uint x, uint y) internal pure returns (uint z) { z = x < y ? x : y; } // babylonian method (https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method) function sqrt(uint y) internal pure returns (uint z) { if (y > 3) { z = y; uint x = y / 2 + 1; while (x < z) { z = x; x = (y / x + x) / 2; } } else if (y != 0) { z = 1; } } }
contracts/BAllowance.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BAllowance is PoolToken, BStorage { event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); function _borrowApprove( address owner, address spender, uint256 value ) private { borrowAllowance[owner][spender] = value; emit BorrowApproval(owner, spender, value); } function borrowApprove(address spender, uint256 value) external returns (bool) { _borrowApprove(msg.sender, spender, value); return true; } function _checkBorrowAllowance( address owner, address spender, uint256 value ) internal { uint256 _borrowAllowance = borrowAllowance[owner][spender]; if (spender != owner && _borrowAllowance != uint256(-1)) { require(_borrowAllowance >= value, "Tarot: BORROW_NOT_ALLOWED"); borrowAllowance[owner][spender] = _borrowAllowance - value; } } // keccak256("BorrowPermit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant BORROW_PERMIT_TYPEHASH = 0xf6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f; function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, BORROW_PERMIT_TYPEHASH ); _borrowApprove(owner, spender, value); } }
contracts/BInterestRateModel.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; contract BInterestRateModel is PoolToken, BStorage { // When utilization is 100% borrowRate is kinkBorrowRate * KINK_MULTIPLIER // kinkBorrowRate relative adjustment per second belongs to [1-adjustSpeed, 1+adjustSpeed*(KINK_MULTIPLIER-1)] uint public constant KINK_MULTIPLIER = 6; uint public constant KINK_BORROW_RATE_MAX = 8.87874172e9; //28% per year uint public constant KINK_BORROW_RATE_MIN = 0.31709792e9; //1% per year event AccrueInterest(uint interestAccumulated, uint borrowIndex, uint totalBorrows); event CalculateKinkBorrowRate(uint kinkBorrowRate); event CalculateBorrowRate(uint borrowRate); function _calculateBorrowRate() internal { uint _kinkUtilizationRateLower = kinkUtilizationRateLower; uint _kinkUtilizationRateUpper = kinkUtilizationRateUpper; uint _adjustSpeed = adjustSpeed; uint _borrowRate = borrowRate; uint _kinkBorrowRate = kinkBorrowRate; uint32 _rateUpdateTimestamp = rateUpdateTimestamp; // update kinkBorrowRate using previous borrowRate uint32 timeElapsed = getBlockTimestamp() - _rateUpdateTimestamp; // underflow is desired if(timeElapsed > 0) { rateUpdateTimestamp = getBlockTimestamp(); uint adjustFactor; if (_borrowRate < _kinkBorrowRate) { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_kinkBorrowRate - _borrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp > 1e18 ? 0 : 1e18 - tmp; } else { // never overflows, _kinkBorrowRate is never 0 uint tmp = (_borrowRate - _kinkBorrowRate) * 1e18 / _kinkBorrowRate * _adjustSpeed * timeElapsed / 1e18; adjustFactor = tmp + 1e18; } // never overflows _kinkBorrowRate = _kinkBorrowRate * adjustFactor / 1e18; if(_kinkBorrowRate > KINK_BORROW_RATE_MAX) _kinkBorrowRate = KINK_BORROW_RATE_MAX; if(_kinkBorrowRate < KINK_BORROW_RATE_MIN) _kinkBorrowRate = KINK_BORROW_RATE_MIN; kinkBorrowRate = uint48(_kinkBorrowRate); emit CalculateKinkBorrowRate(_kinkBorrowRate); } uint _utilizationRate; { // avoid stack to deep uint _totalBorrows = totalBorrows; // gas savings uint _actualBalance = totalBalance.add(_totalBorrows); _utilizationRate = (_actualBalance == 0) ? 0 : _totalBorrows * 1e18 / _actualBalance; } // update borrowRate using the new kinkBorrowRate if(_utilizationRate <= _kinkUtilizationRateLower) { // never overflows, _kinkUtilizationRateLower is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRateLower; } else if (_utilizationRate <= _kinkUtilizationRateUpper) { _borrowRate = _kinkBorrowRate; } else { // never overflows, _kinkUtilizationRateUpper is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRateUpper) * 1e18 / (1e18 - _kinkUtilizationRateUpper); // never overflows _borrowRate = ((KINK_MULTIPLIER - 1) * overUtilization + 1e18) * _kinkBorrowRate / 1e18; } borrowRate = uint48(_borrowRate); emit CalculateBorrowRate(_borrowRate); } // applies accrued interest to total borrows and reserves function accrueInterest() public { uint _borrowIndex = borrowIndex; uint _totalBorrows = totalBorrows; uint32 _accrualTimestamp = accrualTimestamp; uint32 blockTimestamp = getBlockTimestamp(); if (_accrualTimestamp == blockTimestamp) return; uint32 timeElapsed = blockTimestamp - _accrualTimestamp; // underflow is desired accrualTimestamp = blockTimestamp; uint interestFactor = uint(borrowRate).mul(timeElapsed); uint interestAccumulated = interestFactor.mul(_totalBorrows).div(1e18); _totalBorrows = _totalBorrows.add( interestAccumulated ); _borrowIndex = _borrowIndex.add( interestFactor.mul(_borrowIndex).div(1e18) ); borrowIndex = safe112(_borrowIndex); totalBorrows = safe112(_totalBorrows); emit AccrueInterest(interestAccumulated, _borrowIndex, _totalBorrows); } function getBlockTimestamp() public view returns (uint32) { return uint32(block.timestamp % 2**32); } }
contracts/BSetter.sol
pragma solidity =0.5.16; import "./BStorage.sol"; import "./PoolToken.sol"; import "./interfaces/IFactory.sol"; contract BSetter is PoolToken, BStorage { uint256 public constant RESERVE_FACTOR_MAX = 0.20e18; //20% uint256 public constant KINK_UR_MIN = 0.50e18; //50% uint256 public constant KINK_UR_MAX = 0.99e18; //99% uint256 public constant ADJUST_SPEED_MIN = 0.05787037e12; //0.5% per day uint256 public constant ADJUST_SPEED_MAX = 115.74074e12; //1000% per day event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); // called once by the factory at time of deployment function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external { require(msg.sender == factory, "Tarot: UNAUTHORIZED"); // sufficient check _setName(_name, _symbol); underlying = _underlying; collateral = _collateral; exchangeRateLast = initialExchangeRate; } function _setReserveFactor(uint256 newReserveFactor) external nonReentrant { _checkSetting(newReserveFactor, 0, RESERVE_FACTOR_MAX); reserveFactor = newReserveFactor; emit NewReserveFactor(newReserveFactor); } function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external nonReentrant { _checkSetting(newKinkUtilizationRateLower, KINK_UR_MIN, newKinkUtilizationRateUpper); _checkSetting(newKinkUtilizationRateUpper, newKinkUtilizationRateLower, KINK_UR_MAX); kinkUtilizationRateLower = newKinkUtilizationRateLower; kinkUtilizationRateUpper = newKinkUtilizationRateUpper; emit NewKinkUtilizationRates(newKinkUtilizationRateLower, newKinkUtilizationRateUpper); } function _setAdjustSpeed(uint256 newAdjustSpeed) external nonReentrant { _checkSetting(newAdjustSpeed, ADJUST_SPEED_MIN, ADJUST_SPEED_MAX); adjustSpeed = newAdjustSpeed; emit NewAdjustSpeed(newAdjustSpeed); } function _setBorrowTracker(address newBorrowTracker) external nonReentrant { _checkAdmin(); borrowTracker = newBorrowTracker; emit NewBorrowTracker(newBorrowTracker); } function _checkSetting( uint256 parameter, uint256 min, uint256 max ) internal view { _checkAdmin(); require(parameter >= min, "Tarot: INVALID_SETTING"); require(parameter <= max, "Tarot: INVALID_SETTING"); } function _checkAdmin() internal view { require(msg.sender == IFactory(factory).admin(), "Tarot: UNAUTHORIZED"); } }
contracts/BStorage.sol
pragma solidity =0.5.16; contract BStorage { address public collateral; mapping(address => mapping(address => uint256)) public borrowAllowance; struct BorrowSnapshot { uint112 principal; // amount in underlying when the borrow was last updated uint112 interestIndex; // borrow index when borrow was last updated } mapping(address => BorrowSnapshot) internal borrowBalances; // use one memory slot uint112 public borrowIndex = 1e18; uint112 public totalBorrows; uint32 public accrualTimestamp = uint32(block.timestamp % 2**32); uint256 public exchangeRateLast; // use one memory slot uint48 public borrowRate; uint48 public kinkBorrowRate = 1.5854896e9; //5% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRateLower = 0.8e18; //80% uint256 public kinkUtilizationRateUpper = 0.9e18; //90% uint256 public adjustSpeed = 2.893518e12; //25% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
contracts/PoolToken.sol
pragma solidity =0.5.16; import "./TarotERC20.sol"; import "./interfaces/IERC20.sol"; import "./interfaces/IPoolToken.sol"; import "./libraries/SafeMath.sol"; contract PoolToken is IPoolToken, TarotERC20 { uint256 internal constant initialExchangeRate = 1e18; address public underlying; address public factory; uint256 public totalBalance; uint256 public constant MINIMUM_LIQUIDITY = 1000; event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); /*** Initialize ***/ // called once by the factory function _setFactory() external { require(factory == address(0), "Tarot: FACTORY_ALREADY_SET"); factory = msg.sender; } /*** PoolToken ***/ function _update() internal { totalBalance = IERC20(underlying).balanceOf(address(this)); emit Sync(totalBalance); } function exchangeRate() public returns (uint256) { uint256 _totalSupply = totalSupply; // gas savings uint256 _totalBalance = totalBalance; // gas savings if (_totalSupply == 0 || _totalBalance == 0) return initialExchangeRate; return _totalBalance.mul(1e18).div(_totalSupply); } // this low-level function should be called from another contract function mint(address minter) external nonReentrant update returns (uint256 mintTokens) { uint256 balance = IERC20(underlying).balanceOf(address(this)); uint256 mintAmount = balance.sub(totalBalance); mintTokens = mintAmount.mul(1e18).div(exchangeRate()); if (totalSupply == 0) { // permanently lock the first MINIMUM_LIQUIDITY tokens mintTokens = mintTokens.sub(MINIMUM_LIQUIDITY); _mint(address(0), MINIMUM_LIQUIDITY); } require(mintTokens > 0, "Tarot: MINT_AMOUNT_ZERO"); _mint(minter, mintTokens); emit Mint(msg.sender, minter, mintAmount, mintTokens); } // this low-level function should be called from another contract function redeem(address redeemer) external nonReentrant update returns (uint256 redeemAmount) { uint256 redeemTokens = balanceOf[address(this)]; redeemAmount = redeemTokens.mul(exchangeRate()).div(1e18); require(redeemAmount > 0, "Tarot: REDEEM_AMOUNT_ZERO"); require(redeemAmount <= totalBalance, "Tarot: INSUFFICIENT_CASH"); _burn(address(this), redeemTokens); _safeTransfer(redeemer, redeemAmount); emit Redeem(msg.sender, redeemer, redeemAmount, redeemTokens); } // force real balance to match totalBalance function skim(address to) external nonReentrant { _safeTransfer( to, IERC20(underlying).balanceOf(address(this)).sub(totalBalance) ); } // force totalBalance to match real balance function sync() external nonReentrant update {} /*** Utilities ***/ // same safe transfer function used by UniSwapV2 (with fixed underlying) bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)"))); function _safeTransfer(address to, uint256 amount) internal { (bool success, bytes memory data) = underlying.call(abi.encodeWithSelector(SELECTOR, to, amount)); require( success && (data.length == 0 || abi.decode(data, (bool))), "Tarot: TRANSFER_FAILED" ); } // prevents a contract from calling itself, directly or indirectly. bool internal _notEntered = true; modifier nonReentrant() { require(_notEntered, "Tarot: REENTERED"); _notEntered = false; _; _notEntered = true; } // update totalBalance with current balance modifier update() { _; _update(); } }
contracts/TarotERC20.sol
pragma solidity =0.5.16; import "./libraries/SafeMath.sol"; // This contract is basically UniswapV2ERC20 with small modifications // src: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol contract TarotERC20 { using SafeMath for uint256; string public name; string public symbol; uint8 public decimals = 18; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; bytes32 public DOMAIN_SEPARATOR; mapping(address => uint256) public nonces; event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); constructor() public {} function _setName(string memory _name, string memory _symbol) internal { name = _name; symbol = _symbol; uint256 chainId; assembly { chainId := chainid } DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256( "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)" ), keccak256(bytes(_name)), keccak256(bytes("1")), chainId, address(this) ) ); } function _mint(address to, uint256 value) internal { totalSupply = totalSupply.add(value); balanceOf[to] = balanceOf[to].add(value); emit Transfer(address(0), to, value); } function _burn(address from, uint256 value) internal { balanceOf[from] = balanceOf[from].sub(value); totalSupply = totalSupply.sub(value); emit Transfer(from, address(0), value); } function _approve( address owner, address spender, uint256 value ) private { allowance[owner][spender] = value; emit Approval(owner, spender, value); } function _transfer( address from, address to, uint256 value ) internal { balanceOf[from] = balanceOf[from].sub( value, "Tarot: TRANSFER_TOO_HIGH" ); balanceOf[to] = balanceOf[to].add(value); emit Transfer(from, to, value); } function approve(address spender, uint256 value) external returns (bool) { _approve(msg.sender, spender, value); return true; } function transfer(address to, uint256 value) external returns (bool) { _transfer(msg.sender, to, value); return true; } function transferFrom( address from, address to, uint256 value ) external returns (bool) { if (allowance[from][msg.sender] != uint256(-1)) { allowance[from][msg.sender] = allowance[from][msg.sender].sub( value, "Tarot: TRANSFER_NOT_ALLOWED" ); } _transfer(from, to, value); return true; } function _checkSignature( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s, bytes32 typehash ) internal { require(deadline >= block.timestamp, "Tarot: EXPIRED"); bytes32 digest = keccak256( abi.encodePacked( "\x19\x01", DOMAIN_SEPARATOR, keccak256( abi.encode( typehash, owner, spender, value, nonces[owner]++, deadline ) ) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require( recoveredAddress != address(0) && recoveredAddress == owner, "Tarot: INVALID_SIGNATURE" ); } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external { _checkSignature( owner, spender, value, deadline, v, r, s, PERMIT_TYPEHASH ); _approve(owner, spender, value); } }
contracts/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
contracts/interfaces/IBorrowable.sol
pragma solidity >=0.5.0; interface IBorrowable { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Borrowable ***/ event BorrowApproval( address indexed owner, address indexed spender, uint256 value ); event Borrow( address indexed sender, address indexed borrower, address indexed receiver, uint256 borrowAmount, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); event Liquidate( address indexed sender, address indexed borrower, address indexed liquidator, uint256 seizeTokens, uint256 repayAmount, uint256 accountBorrowsPrior, uint256 accountBorrows, uint256 totalBorrows ); function BORROW_FEE() external pure returns (uint256); function collateral() external view returns (address); function reserveFactor() external view returns (uint256); function exchangeRateLast() external view returns (uint256); function borrowIndex() external view returns (uint256); function totalBorrows() external view returns (uint256); function borrowAllowance(address owner, address spender) external view returns (uint256); function borrowBalance(address borrower) external view returns (uint256); function borrowTracker() external view returns (address); function BORROW_PERMIT_TYPEHASH() external pure returns (bytes32); function borrowApprove(address spender, uint256 value) external returns (bool); function borrowPermit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; function borrow( address borrower, address receiver, uint256 borrowAmount, bytes calldata data ) external; function liquidate(address borrower, address liquidator) external returns (uint256 seizeTokens); function trackBorrow(address borrower) external; /*** Borrowable Interest Rate Model ***/ event AccrueInterest( uint256 interestAccumulated, uint256 borrowIndex, uint256 totalBorrows ); event CalculateKink(uint256 kinkRate); event CalculateBorrowRate(uint256 borrowRate); function KINK_BORROW_RATE_MAX() external pure returns (uint256); function KINK_BORROW_RATE_MIN() external pure returns (uint256); function KINK_MULTIPLIER() external pure returns (uint256); function borrowRate() external view returns (uint256); function kinkBorrowRate() external view returns (uint256); function kinkUtilizationRateLower() external view returns (uint256); function kinkUtilizationRateUpper() external view returns (uint256); function adjustSpeed() external view returns (uint256); function rateUpdateTimestamp() external view returns (uint32); function accrualTimestamp() external view returns (uint32); function accrueInterest() external; /*** Borrowable Setter ***/ event NewReserveFactor(uint256 newReserveFactor); event NewKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper); event NewAdjustSpeed(uint256 newAdjustSpeed); event NewBorrowTracker(address newBorrowTracker); function RESERVE_FACTOR_MAX() external pure returns (uint256); function KINK_UR_MIN() external pure returns (uint256); function KINK_UR_MAX() external pure returns (uint256); function ADJUST_SPEED_MIN() external pure returns (uint256); function ADJUST_SPEED_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _collateral ) external; function _setReserveFactor(uint256 newReserveFactor) external; function _setKinkUtilizationRates(uint256 newKinkUtilizationRateLower, uint256 newKinkUtilizationRateUpper) external; function _setAdjustSpeed(uint256 newAdjustSpeed) external; function _setBorrowTracker(address newBorrowTracker) external; }
contracts/interfaces/ICollateral.sol
pragma solidity >=0.5.0; interface ICollateral { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; /*** Collateral ***/ function borrowable0() external view returns (address); function borrowable1() external view returns (address); function tarotPriceOracle() external view returns (address); function safetyMargin() external view returns (uint256); function mTolerance() external view returns (uint256); function liquidationIncentive() external view returns (uint256); function liquidationFee() external view returns (uint256); function liquidationPenalty() external view returns (uint256); function getPrices() external returns (uint256 price0, uint256 price1); function getReserves() external returns (uint112 reserve0, uint112 reserve1); function tokensUnlocked(address from, uint256 value) external returns (bool); function accountLiquidityAmounts( address account, uint256 amount0, uint256 amount1 ) external returns (uint256 liquidity, uint256 shortfall); function accountLiquidity(address account) external returns (uint256 liquidity, uint256 shortfall); function canBorrow( address account, address borrowable, uint256 accountBorrows ) external returns (bool); function seize( address liquidator, address borrower, uint256 repayAmount ) external returns (uint256 seizeTokens); function flashRedeem( address redeemer, uint256 redeemAmount, bytes calldata data ) external; /*** Collateral Setter ***/ event NewSafetyMargin(uint256 newSafetyMarginSqrt); event NewLiquidationIncentive(uint256 newLiquidationIncentive); event NewLiquidationFee(uint256 newLiquidationFee); event NewMTolerance(uint256 mTolerance); function M_TOLERANCE_MIN() external pure returns (uint256); function M_TOLERANCE_MAX() external pure returns (uint256); function SAFETY_MARGIN_MIN() external pure returns (uint256); function SAFETY_MARGIN_MAX() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MIN() external pure returns (uint256); function LIQUIDATION_INCENTIVE_MAX() external pure returns (uint256); function LIQUIDATION_FEE_MAX() external pure returns (uint256); function _initialize( string calldata _name, string calldata _symbol, address _underlying, address _borrowable0, address _borrowable1 ) external; function _setSafetyMargin(uint256 newSafetyMargin) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; function _setMTolerance(uint256 newMTolerance) external; function isUnderlyingVaultToken() external view returns (bool); }
contracts/interfaces/IERC20.sol
pragma solidity >=0.5.0; interface IERC20 { event Approval(address indexed owner, address indexed spender, uint value); event Transfer(address indexed from, address indexed to, uint value); function name() external view returns (string memory); function symbol() external view returns (string memory); function decimals() external view returns (uint8); function totalSupply() external view returns (uint); function balanceOf(address owner) external view returns (uint); function allowance(address owner, address spender) external view returns (uint); function approve(address spender, uint value) external returns (bool); function transfer(address to, uint value) external returns (bool); function transferFrom(address from, address to, uint value) external returns (bool); }
contracts/interfaces/IFactory.sol
pragma solidity >=0.5.0; interface IFactory { event LendingPoolInitialized(address indexed uniswapV2Pair, address indexed token0, address indexed token1, address collateral, address borrowable0, address borrowable1, uint lendingPoolId); event NewPendingAdmin(address oldPendingAdmin, address newPendingAdmin); event NewAdmin(address oldAdmin, address newAdmin); event NewReservesPendingAdmin(address oldReservesPendingAdmin, address newReservesPendingAdmin); event NewReservesAdmin(address oldReservesAdmin, address newReservesAdmin); event NewReservesManager(address oldReservesManager, address newReservesManager); function admin() external view returns (address); function pendingAdmin() external view returns (address); function reservesAdmin() external view returns (address); function reservesPendingAdmin() external view returns (address); function reservesManager() external view returns (address); function getLendingPool(address uniswapV2Pair) external view returns ( bool initialized, uint24 lendingPoolId, address collateral, address borrowable0, address borrowable1 ); function allLendingPools(uint) external view returns (address uniswapV2Pair); function allLendingPoolsLength() external view returns (uint); function bDeployer() external view returns (address); function cDeployer() external view returns (address); function tarotPriceOracle() external view returns (address); function createCollateral(address uniswapV2Pair) external returns (address collateral); function createBorrowable0(address uniswapV2Pair) external returns (address borrowable0); function createBorrowable1(address uniswapV2Pair) external returns (address borrowable1); function initializeLendingPool(address uniswapV2Pair) external; function _setPendingAdmin(address newPendingAdmin) external; function _acceptAdmin() external; function _setReservesPendingAdmin(address newPendingAdmin) external; function _acceptReservesAdmin() external; function _setReservesManager(address newReservesManager) external; }
contracts/interfaces/IPoolToken.sol
pragma solidity >=0.5.0; interface IPoolToken { /*** Tarot ERC20 ***/ event Transfer(address indexed from, address indexed to, uint256 value); event Approval( address indexed owner, address indexed spender, uint256 value ); function name() external pure returns (string memory); function symbol() external pure returns (string memory); function decimals() external pure returns (uint8); function totalSupply() external view returns (uint256); function balanceOf(address owner) external view returns (uint256); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 value) external returns (bool); function transfer(address to, uint256 value) external returns (bool); function transferFrom( address from, address to, uint256 value ) external returns (bool); function DOMAIN_SEPARATOR() external view returns (bytes32); function PERMIT_TYPEHASH() external pure returns (bytes32); function nonces(address owner) external view returns (uint256); function permit( address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s ) external; /*** Pool Token ***/ event Mint( address indexed sender, address indexed minter, uint256 mintAmount, uint256 mintTokens ); event Redeem( address indexed sender, address indexed redeemer, uint256 redeemAmount, uint256 redeemTokens ); event Sync(uint256 totalBalance); function underlying() external view returns (address); function factory() external view returns (address); function totalBalance() external view returns (uint256); function MINIMUM_LIQUIDITY() external pure returns (uint256); function exchangeRate() external returns (uint256); function mint(address minter) external returns (uint256 mintTokens); function redeem(address redeemer) external returns (uint256 redeemAmount); function skim(address to) external; function sync() external; function _setFactory() external; }
contracts/interfaces/ITarotCallee.sol
pragma solidity >=0.5.0; interface ITarotCallee { function tarotBorrow( address sender, address borrower, uint256 borrowAmount, bytes calldata data ) external; function tarotRedeem( address sender, uint256 redeemAmount, bytes calldata data ) external; }
contracts/libraries/SafeMath.sol
pragma solidity =0.5.16; // From https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/Math.sol // Subject to the MIT license. /** * @dev Wrappers over Solidity's arithmetic operations with added overflow * checks. * * Arithmetic operations in Solidity wrap on overflow. This can easily result * in bugs, because programmers usually assume that an overflow raises an * error, which is the standard behavior in high level programming languages. * `SafeMath` restores this intuition by reverting the transaction when an * operation overflows. * * Using this library instead of the unchecked operations eliminates an entire * class of bugs, so it's recommended to use it always. */ library SafeMath { /** * @dev Returns the addition of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, "SafeMath: addition overflow"); return c; } /** * @dev Returns the addition of two unsigned integers, reverting with custom message on overflow. * * Counterpart to Solidity's `+` operator. * * Requirements: * - Addition cannot overflow. */ function add(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { uint256 c = a + b; require(c >= a, errorMessage); return c; } /** * @dev Returns the subtraction of two unsigned integers, reverting on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { return sub(a, b, "SafeMath: subtraction underflow"); } /** * @dev Returns the subtraction of two unsigned integers, reverting with custom message on underflow (when the result is negative). * * Counterpart to Solidity's `-` operator. * * Requirements: * - Subtraction cannot underflow. */ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b <= a, errorMessage); uint256 c = a - b; return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, "SafeMath: multiplication overflow"); return c; } /** * @dev Returns the multiplication of two unsigned integers, reverting on overflow. * * Counterpart to Solidity's `*` operator. * * Requirements: * - Multiplication cannot overflow. */ function mul(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b, errorMessage); return c; } /** * @dev Returns the integer division of two unsigned integers. * Reverts on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { return div(a, b, "SafeMath: division by zero"); } /** * @dev Returns the integer division of two unsigned integers. * Reverts with custom message on division by zero. The result is rounded towards zero. * * Counterpart to Solidity's `/` operator. Note: this function uses a * `revert` opcode (which leaves remaining gas untouched) while Solidity * uses an invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { // Solidity only automatically asserts when dividing by 0 require(b > 0, errorMessage); uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { return mod(a, b, "SafeMath: modulo by zero"); } /** * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), * Reverts with custom message when dividing by zero. * * Counterpart to Solidity's `%` operator. This function uses a `revert` * opcode (which leaves remaining gas untouched) while Solidity uses an * invalid opcode to revert (consuming all remaining gas). * * Requirements: * - The divisor cannot be zero. */ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) { require(b != 0, errorMessage); return a % b; } }
Contract ABI
[{"type":"constructor","stateMutability":"nonpayable","payable":false,"inputs":[]},{"type":"event","name":"AccrueInterest","inputs":[{"type":"uint256","name":"interestAccumulated","internalType":"uint256","indexed":false},{"type":"uint256","name":"borrowIndex","internalType":"uint256","indexed":false},{"type":"uint256","name":"totalBorrows","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Approval","inputs":[{"type":"address","name":"owner","internalType":"address","indexed":true},{"type":"address","name":"spender","internalType":"address","indexed":true},{"type":"uint256","name":"value","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Borrow","inputs":[{"type":"address","name":"sender","internalType":"address","indexed":true},{"type":"address","name":"borrower","internalType":"address","indexed":true},{"type":"address","name":"receiver","internalType":"address","indexed":true},{"type":"uint256","name":"borrowAmount","internalType":"uint256","indexed":false},{"type":"uint256","name":"repayAmount","internalType":"uint256","indexed":false},{"type":"uint256","name":"accountBorrowsPrior","internalType":"uint256","indexed":false},{"type":"uint256","name":"accountBorrows","internalType":"uint256","indexed":false},{"type":"uint256","name":"totalBorrows","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"BorrowApproval","inputs":[{"type":"address","name":"owner","internalType":"address","indexed":true},{"type":"address","name":"spender","internalType":"address","indexed":true},{"type":"uint256","name":"value","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"CalculateBorrowRate","inputs":[{"type":"uint256","name":"borrowRate","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"CalculateKink","inputs":[{"type":"uint256","name":"kinkRate","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"CalculateKinkBorrowRate","inputs":[{"type":"uint256","name":"kinkBorrowRate","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Liquidate","inputs":[{"type":"address","name":"sender","internalType":"address","indexed":true},{"type":"address","name":"borrower","internalType":"address","indexed":true},{"type":"address","name":"liquidator","internalType":"address","indexed":true},{"type":"uint256","name":"seizeTokens","internalType":"uint256","indexed":false},{"type":"uint256","name":"repayAmount","internalType":"uint256","indexed":false},{"type":"uint256","name":"accountBorrowsPrior","internalType":"uint256","indexed":false},{"type":"uint256","name":"accountBorrows","internalType":"uint256","indexed":false},{"type":"uint256","name":"totalBorrows","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Mint","inputs":[{"type":"address","name":"sender","internalType":"address","indexed":true},{"type":"address","name":"minter","internalType":"address","indexed":true},{"type":"uint256","name":"mintAmount","internalType":"uint256","indexed":false},{"type":"uint256","name":"mintTokens","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"NewAdjustSpeed","inputs":[{"type":"uint256","name":"newAdjustSpeed","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"NewBorrowTracker","inputs":[{"type":"address","name":"newBorrowTracker","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"NewKinkUtilizationRates","inputs":[{"type":"uint256","name":"newKinkUtilizationRateLower","internalType":"uint256","indexed":false},{"type":"uint256","name":"newKinkUtilizationRateUpper","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"NewReserveFactor","inputs":[{"type":"uint256","name":"newReserveFactor","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Redeem","inputs":[{"type":"address","name":"sender","internalType":"address","indexed":true},{"type":"address","name":"redeemer","internalType":"address","indexed":true},{"type":"uint256","name":"redeemAmount","internalType":"uint256","indexed":false},{"type":"uint256","name":"redeemTokens","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Sync","inputs":[{"type":"uint256","name":"totalBalance","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Transfer","inputs":[{"type":"address","name":"from","internalType":"address","indexed":true},{"type":"address","name":"to","internalType":"address","indexed":true},{"type":"uint256","name":"value","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"ADJUST_SPEED_MAX","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"ADJUST_SPEED_MIN","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"BORROW_FEE","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bytes32","name":"","internalType":"bytes32"}],"name":"BORROW_PERMIT_TYPEHASH","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bytes32","name":"","internalType":"bytes32"}],"name":"DOMAIN_SEPARATOR","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"KINK_BORROW_RATE_MAX","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"KINK_BORROW_RATE_MIN","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"KINK_MULTIPLIER","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"KINK_UR_MAX","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"KINK_UR_MIN","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"MINIMUM_LIQUIDITY","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"bytes32","name":"","internalType":"bytes32"}],"name":"PERMIT_TYPEHASH","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"RESERVE_FACTOR_MAX","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_initialize","inputs":[{"type":"string","name":"_name","internalType":"string"},{"type":"string","name":"_symbol","internalType":"string"},{"type":"address","name":"_underlying","internalType":"address"},{"type":"address","name":"_collateral","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_setAdjustSpeed","inputs":[{"type":"uint256","name":"newAdjustSpeed","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_setBorrowTracker","inputs":[{"type":"address","name":"newBorrowTracker","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_setFactory","inputs":[],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_setKinkUtilizationRates","inputs":[{"type":"uint256","name":"newKinkUtilizationRateLower","internalType":"uint256"},{"type":"uint256","name":"newKinkUtilizationRateUpper","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"_setReserveFactor","inputs":[{"type":"uint256","name":"newReserveFactor","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint32","name":"","internalType":"uint32"}],"name":"accrualTimestamp","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"accrueInterest","inputs":[],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"adjustSpeed","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"allowance","inputs":[{"type":"address","name":"","internalType":"address"},{"type":"address","name":"","internalType":"address"}],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"approve","inputs":[{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"balanceOf","inputs":[{"type":"address","name":"","internalType":"address"}],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"borrow","inputs":[{"type":"address","name":"borrower","internalType":"address"},{"type":"address","name":"receiver","internalType":"address"},{"type":"uint256","name":"borrowAmount","internalType":"uint256"},{"type":"bytes","name":"data","internalType":"bytes"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"borrowAllowance","inputs":[{"type":"address","name":"","internalType":"address"},{"type":"address","name":"","internalType":"address"}],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"borrowApprove","inputs":[{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"borrowBalance","inputs":[{"type":"address","name":"borrower","internalType":"address"}],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint112","name":"","internalType":"uint112"}],"name":"borrowIndex","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"borrowPermit","inputs":[{"type":"address","name":"owner","internalType":"address"},{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"},{"type":"uint8","name":"v","internalType":"uint8"},{"type":"bytes32","name":"r","internalType":"bytes32"},{"type":"bytes32","name":"s","internalType":"bytes32"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint48","name":"","internalType":"uint48"}],"name":"borrowRate","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"borrowTracker","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"collateral","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint8","name":"","internalType":"uint8"}],"name":"decimals","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"exchangeRate","inputs":[],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"exchangeRateLast","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"factory","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint32","name":"","internalType":"uint32"}],"name":"getBlockTimestamp","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint48","name":"","internalType":"uint48"}],"name":"kinkBorrowRate","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"kinkUtilizationRateLower","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"kinkUtilizationRateUpper","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"seizeTokens","internalType":"uint256"}],"name":"liquidate","inputs":[{"type":"address","name":"borrower","internalType":"address"},{"type":"address","name":"liquidator","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"mintTokens","internalType":"uint256"}],"name":"mint","inputs":[{"type":"address","name":"minter","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"string","name":"","internalType":"string"}],"name":"name","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"nonces","inputs":[{"type":"address","name":"","internalType":"address"}],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"permit","inputs":[{"type":"address","name":"owner","internalType":"address"},{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"},{"type":"uint8","name":"v","internalType":"uint8"},{"type":"bytes32","name":"r","internalType":"bytes32"},{"type":"bytes32","name":"s","internalType":"bytes32"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint32","name":"","internalType":"uint32"}],"name":"rateUpdateTimestamp","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"uint256","name":"redeemAmount","internalType":"uint256"}],"name":"redeem","inputs":[{"type":"address","name":"redeemer","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"reserveFactor","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"skim","inputs":[{"type":"address","name":"to","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"string","name":"","internalType":"string"}],"name":"symbol","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"sync","inputs":[],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalBalance","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint112","name":"","internalType":"uint112"}],"name":"totalBorrows","inputs":[],"constant":true},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalSupply","inputs":[],"constant":true},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[],"name":"trackBorrow","inputs":[{"type":"address","name":"borrower","internalType":"address"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"transfer","inputs":[{"type":"address","name":"to","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"transferFrom","inputs":[{"type":"address","name":"from","internalType":"address"},{"type":"address","name":"to","internalType":"address"},{"type":"uint256","name":"value","internalType":"uint256"}],"constant":false},{"type":"function","stateMutability":"view","payable":false,"outputs":[{"type":"address","name":"","internalType":"address"}],"name":"underlying","inputs":[],"constant":true}]
Deployed ByteCode
0x608060405234801561001057600080fd5b50600436106103a45760003560e01c806375424ccc116101e9578063b7f1118a1161010f578063c914b437116100ad578063e07660001161007c578063e076600014610a99578063e12b630614610aa1578063fca7820b14610aa9578063fff6cae914610ac6576103a4565b8063c914b43714610a0a578063d505accf14610a12578063d8dfeb4514610a63578063dd62ed3e14610a6b576103a4565b8063bc25cf77116100e9578063bc25cf77146109cc578063be340e32146109f2578063c45a0155146109fa578063c72f3fbb14610a02576103a4565b8063b7f1118a14610996578063b95b92a3146109bc578063ba9a7a56146109c4576103a4565b806395d89b4111610187578063a9059cbb11610156578063a9059cbb14610952578063aa5af0fd1461097e578063abf40d6214610986578063ad7a672f1461098e576103a4565b806395d89b41146108e95780639e79b55c146108f1578063a071571914610942578063a6afed951461094a576103a4565b806386b9d81f116101c357806386b9d81f1461084657806391b4274514610874578063926d845b1461089757806395a2251f146108c3576103a4565b806375424ccc146107f7578063796b89b9146107ff5780637ecebe0014610820576103a4565b80633ba0b9a9116102ce578063559572201161026c5780636a6278421161023b5780636a627842146107755780636bd76d241461079b5780636f307dc3146107c957806370a08231146107d1576103a4565b8063559572201461066e5780635b2b9d1a14610692578063685440651461069a5780636a030c11146106a2576103a4565b806347bd3718116102a857806347bd3718146105f95780634a5d316c1461061d5780634a7a63af146106255780634d73e9ba14610648576103a4565b80633ba0b9a9146105e15780634322b714146105e9578063452ae95f146105f1576103a4565b806323b872dd1161034657806330adf81f1161031557806330adf81f1461058d578063313ce5671461059557806335542822146105b35780633644e515146105d9576103a4565b806323b872dd1461052a578063253c24f31461056057806327549a0b146105685780632d5231d314610585576103a4565b8063095ea7b311610382578063095ea7b31461044857806318160ddd146104885780631e7dcc0d146104905780632374e8a914610522576103a4565b806301f8c1c8146103a957806306fdde03146103c3578063075f4e7f14610440575b600080fd5b6103b1610ace565b60408051918252519081900360200190f35b6103cb610af2565b6040805160208082528351818301528351919283929083019185019080838360005b838110156104055781810151838201526020016103ed565b50505050905090810190601f1680156104325780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6103b1610b80565b6104746004803603604081101561045e57600080fd5b506001600160a01b038135169060200135610b89565b604080519115158252519081900360200190f35b6103b1610ba0565b610520600480360360808110156104a657600080fd5b6001600160a01b038235811692602081013590911691604082013591908101906080810160608201356401000000008111156104e157600080fd5b8201836020820111156104f357600080fd5b8035906020019184600183028401116401000000008311171561051557600080fd5b509092509050610ba6565b005b6103b1610fc8565b6104746004803603606081101561054057600080fd5b506001600160a01b03813581169160208101359091169060400135610fd4565b6103b16110a3565b6105206004803603602081101561057e57600080fd5b50356110ad565b6103b161115f565b6103b1611165565b61059d611189565b6040805160ff9092168252519081900360200190f35b610520600480360360208110156105c957600080fd5b50356001600160a01b0316611192565b6103b161124f565b6103b1611255565b6103b16112dc565b6103b16112e2565b6106016112eb565b604080516001600160701b039092168252519081900360200190f35b610520611301565b6105206004803603604081101561063b57600080fd5b5080359060200135611373565b6103b16004803603602081101561065e57600080fd5b50356001600160a01b0316611443565b6106766114d3565b604080516001600160a01b039092168252519081900360200190f35b6103b16114e2565b6103b16114e7565b610520600480360360808110156106b857600080fd5b8101906020810181356401000000008111156106d357600080fd5b8201836020820111156106e557600080fd5b8035906020019184600183028401116401000000008311171561070757600080fd5b91939092909160208101903564010000000081111561072557600080fd5b82018360208201111561073757600080fd5b8035906020019184600183028401116401000000008311171561075957600080fd5b91935091506001600160a01b03813581169160200135166114f3565b6103b16004803603602081101561078b57600080fd5b50356001600160a01b0316611607565b6103b1600480360360408110156107b157600080fd5b506001600160a01b0381358116916020013516611805565b610676611822565b6103b1600480360360208110156107e757600080fd5b50356001600160a01b0316611831565b6103b1611843565b610807611849565b6040805163ffffffff9092168252519081900360200190f35b6103b16004803603602081101561083657600080fd5b50356001600160a01b0316611853565b6103b16004803603604081101561085c57600080fd5b506001600160a01b0381358116916020013516611865565b61087c611a96565b6040805165ffffffffffff9092168252519081900360200190f35b610474600480360360408110156108ad57600080fd5b506001600160a01b038135169060200135611aab565b6103b1600480360360208110156108d957600080fd5b50356001600160a01b0316611ab8565b6103cb611c51565b610520600480360360e081101561090757600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611cab565b6103b1611cef565b610520611cf9565b6104746004803603604081101561096857600080fd5b506001600160a01b038135169060200135611e99565b610601611ea6565b6103b1611eb5565b6103b1611ebb565b610520600480360360208110156109ac57600080fd5b50356001600160a01b0316611ec1565b610807611ee2565b6103b1611ef5565b610520600480360360208110156109e257600080fd5b50356001600160a01b0316611efb565b6103b1611ff5565b610676611ffb565b6103b161200a565b61087c612016565b610520600480360360e0811015610a2857600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135612024565b61067661205f565b6103b160048036036040811015610a8157600080fd5b506001600160a01b0381358116916020013516612073565b6103b1612090565b610807612098565b61052060048036036020811015610abf57600080fd5b50356120ab565b61052061215b565b7ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f81565b6000805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b785780601f10610b4d57610100808354040283529160200191610b78565b820191906000526020600020905b815481529060010190602001808311610b5b57829003601f168201915b505050505081565b640d7957c4d081565b6000610b963384846121ce565b5060015b92915050565b60035481565b600b5460ff16610bf0576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055610c02611cf9565b600a5480841115610c55576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b610c60863386612230565b8315610c7057610c7085856122f5565b8115610d2257604051631b4a79ff60e21b815233600482018181526001600160a01b038981166024850152604484018890526080606485019081526084850187905290891693636d29e7fc93928b928a928a928a92909160a401848480828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b158015610d0957600080fd5b505af1158015610d1d573d6000803e3d6000fd5b505050505b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b158015610d6d57600080fd5b505afa158015610d81573d6000803e3d6000fd5b505050506040513d6020811015610d9757600080fd5b505190506000610dcb670de0b6b3a7640000610dbf88655af3107a400063ffffffff61248116565b9063ffffffff6124e116565b90506000610ddf878363ffffffff61252316565b90506000610e0385610df7868b63ffffffff61252316565b9063ffffffff61257d16565b90506000806000610e158d86866125bf565b92509250925083851115610f2f57600b60019054906101000a90046001600160a01b03166001600160a01b0316639aac2c538e30856040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b03168152602001836001600160a01b03166001600160a01b031681526020018281526020019350505050602060405180830381600087803b158015610eb257600080fd5b505af1158015610ec6573d6000803e3d6000fd5b505050506040513d6020811015610edc57600080fd5b5051610f2f576040805162461bcd60e51b815260206004820152601d60248201527f5461726f743a20494e53554646494349454e545f4c4951554944495459000000604482015290519081900360640190fd5b8b6001600160a01b03168d6001600160a01b0316336001600160a01b03167f33f3048bd4e6af45e53afb722adfd57dbde82da7e93e44db921fb4b8c6a70c4b8e88888888604051808681526020018581526020018481526020018381526020018281526020019550505050505060405180910390a45050505050505050610fb46127d9565b5050600b805460ff19166001179055505050565b6706f05b59d3b2000081565b6001600160a01b03831660009081526005602090815260408083203384529091528120546000191461108e57604080518082018252601b81527f5461726f743a205452414e534645525f4e4f545f414c4c4f57454400000000006020808301919091526001600160a01b038716600090815260058252838120338252909152919091205461106991849063ffffffff6127e916565b6001600160a01b03851660009081526005602090815260408083203384529091529020555b611099848484612880565b5060019392505050565b656943fdb1990081565b600b5460ff166110f7576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905561111781640d7957c4d0656943fdb19900612972565b60148190556040805182815290517f1396dfcdb64fb7eb77fb84966f27b81afe14aa70b6e966c68d74af3302a9fe909181900360200190a150600b805460ff19166001179055565b60145481565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b60025460ff1681565b600b5460ff166111dc576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556111ee612a1b565b601580546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f468b6598e7e810c65c9858b5f23a2d5b8692fb753b78a032232de4c6ed3cabbf9181900360200190a150600b805460ff19166001179055565b60065481565b600061125f611cf9565b600354600e54600a546000916112859190600160701b90046001600160701b0316612523565b9050811580611292575080155b156112a957670de0b6b3a7640000925050506112d9565b60006112c783610dbf84670de0b6b3a764000063ffffffff61248116565b90506112d38184612ae7565b93505050505b90565b60115481565b64021136d8d881565b600e54600160701b90046001600160701b031681565b6009546001600160a01b03161561135f576040805162461bcd60e51b815260206004820152601a60248201527f5461726f743a20464143544f52595f414c52454144595f534554000000000000604482015290519081900360640190fd5b600980546001600160a01b03191633179055565b600b5460ff166113bd576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556113da826706f05b59d3b2000083612972565b6113ed8183670dbd2fc137a30000612972565b60128290556013819055604080518381526020810183905281517fdfdefd1a83d9c066f897e66cb3930ad66412a81562ec14678f1f44f618923a32929181900390910190a15050600b805460ff19166001179055565b600061144d6134e5565b506001600160a01b0382166000908152600d60209081526040918290208251808401909352546001600160701b038082168452600160701b9091041690820181905261149d5760009150506114ce565b6020810151600e5482516114ca926001600160701b0390811692610dbf928216911663ffffffff61248116565b9150505b919050565b6015546001600160a01b031681565b600681565b670dbd2fc137a3000081565b6009546001600160a01b03163314611548576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b6115bb86868080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525050604080516020601f8a018190048102820181019092528881529250889150879081908401838280828437600092019190915250612bf392505050565b600880546001600160a01b039384166001600160a01b0319909116179055600b80549190921661010002610100600160a81b03199091161790555050670de0b6b3a7640000600f555050565b600b5460009060ff16611654576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b1580156116a957600080fd5b505afa1580156116bd573d6000803e3d6000fd5b505050506040513d60208110156116d357600080fd5b5051600a549091506000906116ef90839063ffffffff61257d16565b90506117146116fc611255565b610dbf83670de0b6b3a764000063ffffffff61248116565b92506003546000141561174257611733836103e863ffffffff61257d16565b925061174260006103e8612cbb565b60008311611797576040805162461bcd60e51b815260206004820152601760248201527f5461726f743a204d494e545f414d4f554e545f5a45524f000000000000000000604482015290519081900360640190fd5b6117a18484612cbb565b604080518281526020810185905281516001600160a01b0387169233927f2f00e3cdd69a77be7ed215ec7b2a36784dd158f921fca79ac29deffa353fe6ee929081900390910190a350506117f36127d9565b600b805460ff19166001179055919050565b600c60209081526000928352604080842090915290825290205481565b6008546001600160a01b031681565b60046020526000908152604090205481565b60135481565b63ffffffff421690565b60076020526000908152604090205481565b600b5460009060ff166118b2576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556118c4611cf9565b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561190f57600080fd5b505afa158015611923573d6000803e3d6000fd5b505050506040513d602081101561193957600080fd5b5051600a5490915060009061195590839063ffffffff61257d16565b9050600061196b61196587611443565b83612d52565b600b546040805163b2a02ff160e01b81526001600160a01b0389811660048301528a81166024830152604482018590529151939450610100909204169163b2a02ff1916064808201926020929091908290030181600087803b1580156119d057600080fd5b505af11580156119e4573d6000803e3d6000fd5b505050506040513d60208110156119fa57600080fd5b5051935060008080611a0d8982876125bf565b604080518b8152602081018a90528082018590526060810184905260808101839052905193965091945092506001600160a01b03808b1692908c169133917fb0dbe18c6ffdf0da655dd690e77211d379205c497be44c64447c3f5f021b51679181900360a00190a4505050505050611a836127d9565b600b805460ff1916600117905592915050565b601054600160301b900465ffffffffffff1681565b6000610b96338484612d68565b600b5460009060ff16611b05576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905530600090815260046020526040902054611b43670de0b6b3a7640000610dbf611b36611255565b849063ffffffff61248116565b915060008211611b9a576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a2052454445454d5f414d4f554e545f5a45524f00000000000000604482015290519081900360640190fd5b600a54821115611bec576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b611bf63082612dca565b611c0083836122f5565b604080518381526020810183905281516001600160a01b0386169233927f3f693fff038bb8a046aa76d9516190ac7444f7d69cf952c4cbdc086fdef2d6fc929081900390910190a3506117f36127d9565b60018054604080516020600284861615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b785780601f10610b4d57610100808354040283529160200191610b78565b611cdb878787878787877ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f612e67565b611ce6878787612d68565b50505050505050565b655af3107a400081565b600e546001600160701b0380821691600160701b810490911690600160e01b900463ffffffff166000611d2a611849565b90508063ffffffff168263ffffffff161415611d495750505050611e97565b600e805463ffffffff808416600160e01b026001600160e01b039092169190911790915560105483830391600091611d909165ffffffffffff909116908085169061248116565b90506000611db0670de0b6b3a7640000610dbf848963ffffffff61248116565b9050611dc2868263ffffffff61252316565b9550611df0611de3670de0b6b3a7640000610dbf858b63ffffffff61248116565b889063ffffffff61252316565b9650611dfb8761303c565b600e80546001600160701b0319166001600160701b0392909216919091179055611e248661303c565b600e80546001600160701b0392909216600160701b02600160701b600160e01b0319909216919091179055604080518281526020810189905280820188905290517f875352fb3fadeb8c0be7cbbe8ff761b308fa7033470cd0287f02f3436fd76cb99181900360600190a1505050505050505b565b6000610b96338484612880565b600e546001600160701b031681565b60125481565b600a5481565b611edf81611ece83611443565b600e546001600160701b031661308b565b50565b600e54600160e01b900463ffffffff1681565b6103e881565b600b5460ff16611f45576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600a54600854604080516370a0823160e01b81523060048201529051611fe5938593611fe09391926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015611fa857600080fd5b505afa158015611fbc573d6000803e3d6000fd5b505050506040513d6020811015611fd257600080fd5b50519063ffffffff61257d16565b6122f5565b50600b805460ff19166001179055565b600f5481565b6009546001600160a01b031681565b6702c68af0bb14000081565b60105465ffffffffffff1681565b612054878787878787877f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9612e67565b611ce68787876121ce565b600b5461010090046001600160a01b031681565b600560209081526000928352604080842090915290825290205481565b6312e687c081565b601054600160601b900463ffffffff1681565b600b5460ff166120f5576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556121138160006702c68af0bb140000612972565b60118190556040805182815290517f9d9cd27245b4e6b06dcf523ac57b6e851b934e199eee376313f906e94bfbfd559181900360200190a150600b805460ff19166001179055565b600b5460ff166121a5576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556121b7611cf9565b6121bf6127d9565b600b805460ff19166001179055565b6001600160a01b03808416600081815260056020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294909152902054911480159061226a57506000198114155b156122ef57818110156122c4576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a20424f52524f575f4e4f545f414c4c4f57454400000000000000604482015290519081900360640190fd5b6001600160a01b038085166000908152600c6020908152604080832093871683529290522082820390555b50505050565b600854604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0386811660248301526044808301879052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009560609594169382918083835b602083106123a25780518252601f199092019160209182019101612383565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d8060008114612404576040519150601f19603f3d011682016040523d82523d6000602084013e612409565b606091505b5091509150818015612437575080511580612437575080806020019051602081101561243457600080fd5b50515b6122ef576040805162461bcd60e51b815260206004820152601660248201527515185c9bdd0e881514905394d1915497d1905253115160521b604482015290519081900360640190fd5b60008261249057506000610b9a565b8282028284828161249d57fe5b04146124da5760405162461bcd60e51b81526004018080602001828103825260218152602001806135dd6021913960400191505060405180910390fd5b9392505050565b60006124da83836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f000000000000815250613119565b6000828201838110156124da576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b60006124da83836040518060400160405280601f81526020017f536166654d6174683a207375627472616374696f6e20756e646572666c6f77008152506127e9565b60008060006125cd86611443565b9250838514156125f3575050600e548190600160701b90046001600160701b03166127d0565b600e546001600160701b0316848611156126ba576001600160a01b0387166000908152600d60205260409020858703612632868263ffffffff61252316565b945061263d8561303c565b82546001600160701b0319166001600160701b0391821617600160701b600160e01b031916600160701b8583168102919091178455600e54612683929190041682612523565b935061268e8461303c565b600e806101000a8154816001600160701b0302191690836001600160701b0316021790555050506127ba565b6001600160a01b0387166000908152600d602052604090208686038086116126e35760006126e7565b8086035b94506126f28561303c565b82546001600160701b0319166001600160701b039190911617825584612728578154600160701b600160e01b031916825561274b565b8154600160701b600160e01b031916600160701b6001600160701b038516021782555b600061275d878763ffffffff61257d16565b600e54600160701b90046001600160701b031695509050808511612782576000612786565b8085035b94506127918561303c565b600e806101000a8154816001600160701b0302191690836001600160701b031602179055505050505b6127ce8784836001600160701b031661308b565b505b93509350939050565b6127e161317e565b611e9761322e565b600081848411156128785760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561283d578181015183820152602001612825565b50505050905090810190601f16801561286a5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b604080518082018252601881527f5461726f743a205452414e534645525f544f4f5f4849474800000000000000006020808301919091526001600160a01b0386166000908152600490915291909120546128e191839063ffffffff6127e916565b6001600160a01b038085166000908152600460205260408082209390935590841681522054612916908263ffffffff61252316565b6001600160a01b0380841660008181526004602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b61297a612a1b565b818310156129c8576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b80831115612a16576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b505050565b600960009054906101000a90046001600160a01b03166001600160a01b031663f851a4406040518163ffffffff1660e01b815260040160206040518083038186803b158015612a6957600080fd5b505afa158015612a7d573d6000803e3d6000fd5b505050506040513d6020811015612a9357600080fd5b50516001600160a01b03163314611e97576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b600f5460009080841115612bea576000612b38612b2b670de0b6b3a7640000610dbf601154612b1f878b61257d90919063ffffffff16565b9063ffffffff61248116565b869063ffffffff61257d16565b90506000612b5485610df784610dbf838b63ffffffff61248116565b90508015612bdb576009546040805163345ef94160e01b815290516000926001600160a01b03169163345ef941916004808301926020929190829003018186803b158015612ba157600080fd5b505afa158015612bb5573d6000803e3d6000fd5b505050506040513d6020811015612bcb57600080fd5b50519050612bd98183612cbb565b505b50600f8190559150610b9a9050565b83915050610b9a565b8151612c069060009060208501906134fc565b508051612c1a9060019060208401906134fc565b50604051469080605261358b8239604080519182900360520182208651602097880120838301835260018452603160f81b93880193909352815180880191909152808201929092527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606083015260808201939093523060a0808301919091528351808303909101815260c090910190925250805192019190912060065550565b600354612cce908263ffffffff61252316565b6003556001600160a01b038216600090815260046020526040902054612cfa908263ffffffff61252316565b6001600160a01b03831660008181526004602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b6000818310612d6157816124da565b5090919050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294825291829020859055815185815291517fc3c1215b41d54142382d54a05fb991007165ae91bcb1879bac8b290d9111aaf49281900390910190a3505050565b6001600160a01b038216600090815260046020526040902054612df3908263ffffffff61257d16565b6001600160a01b038316600090815260046020526040902055600354612e1f908263ffffffff61257d16565b6003556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b42851015612ead576040805162461bcd60e51b815260206004820152600e60248201526d15185c9bdd0e881156141254915160921b604482015290519081900360640190fd5b6006546001600160a01b03808a1660008181526007602090815260408083208054600180820190925582518085018a905280840196909652958e166060860152608085018d905260a085019590955260c08085018c90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff8a166101828501526101a284018990526101c28401889052519193926101e280820193601f1981019281900390910190855afa158015612fa9573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b03811615801590612fdf5750896001600160a01b0316816001600160a01b0316145b613030576040805162461bcd60e51b815260206004820152601860248201527f5461726f743a20494e56414c49445f5349474e41545552450000000000000000604482015290519081900360640190fd5b50505050505050505050565b6000600160701b8210613087576040805162461bcd60e51b815260206004820152600e60248201526d2a30b937ba1d1029a0a32298989960911b604482015290519081900360640190fd5b5090565b6015546001600160a01b0316806130a25750612a16565b604080516305285d7f60e01b81526001600160a01b03868116600483015260248201869052604482018590529151918316916305285d7f9160648082019260009290919082900301818387803b1580156130fb57600080fd5b505af115801561310f573d6000803e3d6000fd5b5050505050505050565b600081836131685760405162461bcd60e51b815260206004820181815283516024840152835190928392604490910191908501908083836000831561283d578181015183820152602001612825565b50600083858161317457fe5b0495945050505050565b600854604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b1580156131c957600080fd5b505afa1580156131dd573d6000803e3d6000fd5b505050506040513d60208110156131f357600080fd5b5051600a81905560408051918252517f8a0df8ef054fae2c3d2d19a7b322e864870cc9fd3cb07fb9526309c596244bf49181900360200190a1565b60125460135460145460105465ffffffffffff80821691600160301b810490911690600160601b900463ffffffff16600081613268611849565b03905063ffffffff8116156133dd5761327f611849565b6010600c6101000a81548163ffffffff021916908363ffffffff160217905550600083851015613308576000670de0b6b3a76400008363ffffffff168887898903670de0b6b3a764000002816132d157fe5b040202816132db57fe5b049050670de0b6b3a764000081116132fd5780670de0b6b3a764000003613300565b60005b915050613349565b6000670de0b6b3a76400008363ffffffff168887888a03670de0b6b3a7640000028161333057fe5b0402028161333a57fe5b04670de0b6b3a7640000019150505b670de0b6b3a764000084820204935064021136d8d884111561336e5764021136d8d893505b6312e687c0841015613382576312e687c093505b601080546bffffffffffff0000000000001916600160301b65ffffffffffff8716021790556040805185815290517f713a98ffb7d769b8e33e2ee945ebb6acb7f397532688164d3ce1081f903c77bc916020908290030190a1505b600e54600a54600091600160701b90046001600160701b03169082906134039083612523565b90508015613424578082670de0b6b3a7640000028161341e57fe5b04613427565b60005b9250505087811161344557878185028161343d57fe5b04945061348e565b8681116134545783945061348e565b600087670de0b6b3a764000003888303670de0b6b3a7640000028161347557fe5b049050670de0b6b3a76400006005820281018602049550505b6010805465ffffffffffff191665ffffffffffff87161790556040805186815290517f338541dc9083f6af6715482fb419e1483c1ae9097764fd68a5dc98109bd5a788916020908290030190a15050505050505050565b604080518082019091526000808252602082015290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061353d57805160ff191683800117855561356a565b8280016001018555821561356a579182015b8281111561356a57825182559160200191906001019061354f565b50613087926112d99250905b80821115613087576000815560010161357656fe454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77a265627a7a72315820239bf9bdea0accfa504815469ef2bc1dbdabf2a01ca5435e62ec6338b7df2fcb64736f6c63430005100032