Warning! Contract bytecode has been changed and doesn't match the verified one. Therefore, interaction with this smart contract may be risky.
- Contract name:
- BDeployer
- Optimization enabled
- true
- Compiler version
- v0.5.16+commit.9c3226ce
- Optimization runs
- 200
- Verified at
- 2023-03-05T17:19:35.578200Z
contracts/BDeployer.sol
pragma solidity =0.5.16; import "./Borrowable.sol"; import "./interfaces/IBDeployer.sol"; /* * This contract is used by the Factory to deploy Borrowable(s) * The bytecode would be too long to fit in the Factory */ contract BDeployer is IBDeployer { constructor () public {} function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable) { bytes memory bytecode = type(Borrowable).creationCode; bytes32 salt = keccak256(abi.encodePacked(msg.sender, uniswapV2Pair, index)); assembly { borrowable := create2(0, add(bytecode, 32), mload(bytecode), salt) } } }
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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/interfaces/IBorrowTracker.sol
pragma solidity >=0.5.0; interface IBorrowTracker { function trackBorrow(address borrower, uint borrowBalance, uint borrowIndex) external; }
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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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/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/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 = 6.3419584e9; //20% per year uint32 public rateUpdateTimestamp = uint32(block.timestamp % 2**32); uint256 public reserveFactor = 0.10e18; //10% uint256 public kinkUtilizationRate = 0.75e18; //75% uint256 public adjustSpeed = 50e12; //432% per day address public borrowTracker; function safe112(uint256 n) internal pure returns (uint112) { require(n < 2**112, "Tarot: SAFE112"); return uint112(n); } }
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 = 3; uint public constant KINK_BORROW_RATE_MAX = 281.582952e9; //888% 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 _kinkUtilizationRate = kinkUtilizationRate; 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 <= _kinkUtilizationRate) { // never overflows, _kinkUtilizationRate is never 0 _borrowRate = _kinkBorrowRate * _utilizationRate / _kinkUtilizationRate; } else { // never overflows, _kinkUtilizationRate is always < 1e18 uint overUtilization = (_utilizationRate - _kinkUtilizationRate) * 1e18 / (1e18 - _kinkUtilizationRate); // 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) external nonReentrant { _checkSetting(newKinkUtilizationRate, KINK_UR_MIN, KINK_UR_MAX); kinkUtilizationRate = newKinkUtilizationRate; emit NewKinkUtilizationRate(newKinkUtilizationRate); } 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/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/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/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/IBDeployer.sol
pragma solidity >=0.5.0; interface IBDeployer { function deployBorrowable(address uniswapV2Pair, uint8 index) external returns (address borrowable); }
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 kinkUtilizationRate() 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 NewKinkUtilizationRate(uint256 newKinkUtilizationRate); 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 _setKinkUtilizationRate(uint256 newKinkUtilizationRate) 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 safetyMarginSqrt() 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 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); function SAFETY_MARGIN_SQRT_MIN() external pure returns (uint256); function SAFETY_MARGIN_SQRT_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 _setSafetyMarginSqrt(uint256 newSafetyMarginSqrt) external; function _setLiquidationIncentive(uint256 newLiquidationIncentive) external; function _setLiquidationFee(uint256 newLiquidationFee) external; }
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/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/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":"function","stateMutability":"nonpayable","payable":false,"outputs":[{"type":"address","name":"borrowable","internalType":"address"}],"name":"deployBorrowable","inputs":[{"type":"address","name":"uniswapV2Pair","internalType":"address"},{"type":"uint8","name":"index","internalType":"uint8"}],"constant":false}]
Contract Creation Code
0x608060405234801561001057600080fd5b5061380e806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c806354bcd7ad14610030575b600080fd5b61005f6004803603604081101561004657600080fd5b5080356001600160a01b0316906020013560ff1661007b565b604080516001600160a01b039092168252519081900360200190f35b600060606040518060200161008f90610111565b818103601f199081018352601f90910116604081815233606090811b6020808501919091529088901b6bffffffffffffffffffffffff1916603484015260f887901b6001600160f81b0319166048840152815180840360290181526049909301909152815191810191909120825192935091829184016000f595945050505050565b6136bb8061011f8339019056fe60806040526002805460ff199081166012908117909255600b80549091166001179055600e80546001600160701b031916670de0b6b3a7640000176001600160e01b0316600160e01b4263ffffffff16908102919091179091556010805465ffffffffffff60301b19166a017a029b000000000000001763ffffffff60601b19166c0100000000000000000000000090920291909117905567016345785d8a0000601155670a688906bd8b00009055652d79883d20006013553480156100c457600080fd5b506135e7806100d46000396000f3fe608060405234801561001057600080fd5b50600436106103995760003560e01c806370a08231116101e9578063b7f1118a1161010f578063c914b437116100ad578063e07660001161007c578063e076600014610a80578063e12b630614610a88578063fca7820b14610a90578063fff6cae914610aad57610399565b8063c914b437146109f1578063d505accf146109f9578063d8dfeb4514610a4a578063dd62ed3e14610a5257610399565b8063bc25cf77116100e9578063bc25cf77146109b3578063be340e32146109d9578063c45a0155146109e1578063c72f3fbb146109e957610399565b8063b7f1118a1461097d578063b95b92a3146109a3578063ba9a7a56146109ab57610399565b806395a2251f11610187578063a6afed9511610156578063a6afed9514610939578063a9059cbb14610941578063aa5af0fd1461096d578063ad7a672f1461097557610399565b806395a2251f146108b257806395d89b41146108d85780639e79b55c146108e0578063a07157191461093157610399565b806386b9d81f116101c357806386b9d81f1461081857806391b4274514610846578063926d845b146108695780639292b0321461089557610399565b806370a08231146107ab578063796b89b9146107d15780637ecebe00146107f257610399565b806335542822116102ce5780634d73e9ba1161026c5780636a030c111161023b5780636a030c111461067c5780636a6278421461074f5780636bd76d24146107755780636f307dc3146107a357610399565b80634d73e9ba1461062257806355957220146106485780635b2b9d1a1461066c578063685440651461067457610399565b80634322b714116102a85780634322b714146105e6578063452ae95f146105ee57806347bd3718146105f65780634a5d316c1461061a57610399565b806335542822146105b05780633644e515146105d65780633ba0b9a9146105de57610399565b80632374e8a91161033b57806327549a0b1161031557806327549a0b146105655780632d5231d31461058257806330adf81f1461058a578063313ce5671461059257610399565b80632374e8a91461051f57806323b872dd14610527578063253c24f31461055d57610399565b8063095ea7b311610377578063095ea7b31461043d57806318160ddd1461047d5780631aebf12f146104855780631e7dcc0d1461048d57610399565b806301f8c1c81461039e57806306fdde03146103b8578063075f4e7f14610435575b600080fd5b6103a6610ab5565b60408051918252519081900360200190f35b6103c0610ad9565b6040805160208082528351818301528351919283929083019185019080838360005b838110156103fa5781810151838201526020016103e2565b50505050905090810190601f1680156104275780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6103a6610b67565b6104696004803603604081101561045357600080fd5b506001600160a01b038135169060200135610b70565b604080519115158252519081900360200190f35b6103a6610b87565b6103a6610b8d565b61051d600480360360808110156104a357600080fd5b6001600160a01b038235811692602081013590911691604082013591908101906080810160608201356401000000008111156104de57600080fd5b8201836020820111156104f057600080fd5b8035906020019184600183028401116401000000008311171561051257600080fd5b509092509050610b93565b005b6103a6610fb5565b6104696004803603606081101561053d57600080fd5b506001600160a01b03813581169160208101359091169060400135610fc1565b6103a6611090565b61051d6004803603602081101561057b57600080fd5b503561109a565b6103a661114c565b6103a6611152565b61059a611176565b6040805160ff9092168252519081900360200190f35b61051d600480360360208110156105c657600080fd5b50356001600160a01b031661117f565b6103a661123c565b6103a6611242565b6103a66112c9565b6103a66112cf565b6105fe6112d8565b604080516001600160701b039092168252519081900360200190f35b61051d6112ee565b6103a66004803603602081101561063857600080fd5b50356001600160a01b0316611360565b6106506113f0565b604080516001600160a01b039092168252519081900360200190f35b6103a66113ff565b6103a6611404565b61051d6004803603608081101561069257600080fd5b8101906020810181356401000000008111156106ad57600080fd5b8201836020820111156106bf57600080fd5b803590602001918460018302840111640100000000831117156106e157600080fd5b9193909290916020810190356401000000008111156106ff57600080fd5b82018360208201111561071157600080fd5b8035906020019184600183028401116401000000008311171561073357600080fd5b91935091506001600160a01b0381358116916020013516611410565b6103a66004803603602081101561076557600080fd5b50356001600160a01b0316611524565b6103a66004803603604081101561078b57600080fd5b506001600160a01b0381358116916020013516611722565b61065061173f565b6103a6600480360360208110156107c157600080fd5b50356001600160a01b031661174e565b6107d9611760565b6040805163ffffffff9092168252519081900360200190f35b6103a66004803603602081101561080857600080fd5b50356001600160a01b031661176a565b6103a66004803603604081101561082e57600080fd5b506001600160a01b038135811691602001351661177c565b61084e6119ad565b6040805165ffffffffffff9092168252519081900360200190f35b6104696004803603604081101561087f57600080fd5b506001600160a01b0381351690602001356119c2565b61051d600480360360208110156108ab57600080fd5b50356119cf565b6103a6600480360360208110156108c857600080fd5b50356001600160a01b0316611a86565b6103c0611c1f565b61051d600480360360e08110156108f657600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611c79565b6103a6611cbd565b61051d611cc7565b6104696004803603604081101561095757600080fd5b506001600160a01b038135169060200135611e67565b6105fe611e74565b6103a6611e83565b61051d6004803603602081101561099357600080fd5b50356001600160a01b0316611e89565b6107d9611eaa565b6103a6611ebd565b61051d600480360360208110156109c957600080fd5b50356001600160a01b0316611ec3565b6103a6611fbd565b610650611fc3565b6103a6611fd2565b61084e611fde565b61051d600480360360e0811015610a0f57600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611fec565b610650612027565b6103a660048036036040811015610a6857600080fd5b506001600160a01b038135811691602001351661203b565b6103a6612058565b6107d9612060565b61051d60048036036020811015610aa657600080fd5b5035612073565b61051d612123565b7ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f81565b6000805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b5f5780601f10610b3457610100808354040283529160200191610b5f565b820191906000526020600020905b815481529060010190602001808311610b4257829003601f168201915b505050505081565b640d7957c4d081565b6000610b7d338484612196565b5060015b92915050565b60035481565b60125481565b600b5460ff16610bdd576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055610bef611cc7565b600a5480841115610c42576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b610c4d8633866121f8565b8315610c5d57610c5d85856122bd565b8115610d0f57604051631b4a79ff60e21b815233600482018181526001600160a01b038981166024850152604484018890526080606485019081526084850187905290891693636d29e7fc93928b928a928a928a92909160a401848480828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b158015610cf657600080fd5b505af1158015610d0a573d6000803e3d6000fd5b505050505b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b158015610d5a57600080fd5b505afa158015610d6e573d6000803e3d6000fd5b505050506040513d6020811015610d8457600080fd5b505190506000610db8670de0b6b3a7640000610dac88655af3107a400063ffffffff61244916565b9063ffffffff6124a916565b90506000610dcc878363ffffffff6124eb16565b90506000610df085610de4868b63ffffffff6124eb16565b9063ffffffff61254516565b90506000806000610e028d8686612587565b92509250925083851115610f1c57600b60019054906101000a90046001600160a01b03166001600160a01b0316639aac2c538e30856040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b03168152602001836001600160a01b03166001600160a01b031681526020018281526020019350505050602060405180830381600087803b158015610e9f57600080fd5b505af1158015610eb3573d6000803e3d6000fd5b505050506040513d6020811015610ec957600080fd5b5051610f1c576040805162461bcd60e51b815260206004820152601d60248201527f5461726f743a20494e53554646494349454e545f4c4951554944495459000000604482015290519081900360640190fd5b8b6001600160a01b03168d6001600160a01b0316336001600160a01b03167f33f3048bd4e6af45e53afb722adfd57dbde82da7e93e44db921fb4b8c6a70c4b8e88888888604051808681526020018581526020018481526020018381526020018281526020019550505050505060405180910390a45050505050505050610fa16127a1565b5050600b805460ff19166001179055505050565b6706f05b59d3b2000081565b6001600160a01b03831660009081526005602090815260408083203384529091528120546000191461107b57604080518082018252601b81527f5461726f743a205452414e534645525f4e4f545f414c4c4f57454400000000006020808301919091526001600160a01b038716600090815260058252838120338252909152919091205461105691849063ffffffff6127b116565b6001600160a01b03851660009081526005602090815260408083203384529091529020555b611086848484612848565b5060019392505050565b656943fdb1990081565b600b5460ff166110e4576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905561110481640d7957c4d0656943fdb1990061293a565b60138190556040805182815290517f1396dfcdb64fb7eb77fb84966f27b81afe14aa70b6e966c68d74af3302a9fe909181900360200190a150600b805460ff19166001179055565b60135481565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b60025460ff1681565b600b5460ff166111c9576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556111db6129e3565b601480546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f468b6598e7e810c65c9858b5f23a2d5b8692fb753b78a032232de4c6ed3cabbf9181900360200190a150600b805460ff19166001179055565b60065481565b600061124c611cc7565b600354600e54600a546000916112729190600160701b90046001600160701b03166124eb565b905081158061127f575080155b1561129657670de0b6b3a7640000925050506112c6565b60006112b483610dac84670de0b6b3a764000063ffffffff61244916565b90506112c08184612aaf565b93505050505b90565b60115481565b64418fa6de4081565b600e54600160701b90046001600160701b031681565b6009546001600160a01b03161561134c576040805162461bcd60e51b815260206004820152601a60248201527f5461726f743a20464143544f52595f414c52454144595f534554000000000000604482015290519081900360640190fd5b600980546001600160a01b03191633179055565b600061136a61349a565b506001600160a01b0382166000908152600d60209081526040918290208251808401909352546001600160701b038082168452600160701b909104169082018190526113ba5760009150506113eb565b6020810151600e5482516113e7926001600160701b0390811692610dac928216911663ffffffff61244916565b9150505b919050565b6014546001600160a01b031681565b600381565b670dbd2fc137a3000081565b6009546001600160a01b03163314611465576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b6114d886868080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525050604080516020601f8a018190048102820181019092528881529250889150879081908401838280828437600092019190915250612bbb92505050565b600880546001600160a01b039384166001600160a01b0319909116179055600b80549190921661010002610100600160a81b03199091161790555050670de0b6b3a7640000600f555050565b600b5460009060ff16611571576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b1580156115c657600080fd5b505afa1580156115da573d6000803e3d6000fd5b505050506040513d60208110156115f057600080fd5b5051600a5490915060009061160c90839063ffffffff61254516565b9050611631611619611242565b610dac83670de0b6b3a764000063ffffffff61244916565b92506003546000141561165f57611650836103e863ffffffff61254516565b925061165f60006103e8612c83565b600083116116b4576040805162461bcd60e51b815260206004820152601760248201527f5461726f743a204d494e545f414d4f554e545f5a45524f000000000000000000604482015290519081900360640190fd5b6116be8484612c83565b604080518281526020810185905281516001600160a01b0387169233927f2f00e3cdd69a77be7ed215ec7b2a36784dd158f921fca79ac29deffa353fe6ee929081900390910190a350506117106127a1565b600b805460ff19166001179055919050565b600c60209081526000928352604080842090915290825290205481565b6008546001600160a01b031681565b60046020526000908152604090205481565b63ffffffff421690565b60076020526000908152604090205481565b600b5460009060ff166117c9576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556117db611cc7565b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561182657600080fd5b505afa15801561183a573d6000803e3d6000fd5b505050506040513d602081101561185057600080fd5b5051600a5490915060009061186c90839063ffffffff61254516565b9050600061188261187c87611360565b83612d1a565b600b546040805163b2a02ff160e01b81526001600160a01b0389811660048301528a81166024830152604482018590529151939450610100909204169163b2a02ff1916064808201926020929091908290030181600087803b1580156118e757600080fd5b505af11580156118fb573d6000803e3d6000fd5b505050506040513d602081101561191157600080fd5b5051935060008080611924898287612587565b604080518b8152602081018a90528082018590526060810184905260808101839052905193965091945092506001600160a01b03808b1692908c169133917fb0dbe18c6ffdf0da655dd690e77211d379205c497be44c64447c3f5f021b51679181900360a00190a450505050505061199a6127a1565b600b805460ff1916600117905592915050565b601054600160301b900465ffffffffffff1681565b6000610b7d338484612d30565b600b5460ff16611a19576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055611a3e816706f05b59d3b20000670dbd2fc137a3000061293a565b60128190556040805182815290517f7a550b1995ff63260fb313f12024e66e73bad425372e5af6b1e04cb3799ef38c9181900360200190a150600b805460ff19166001179055565b600b5460009060ff16611ad3576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905530600090815260046020526040902054611b11670de0b6b3a7640000610dac611b04611242565b849063ffffffff61244916565b915060008211611b68576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a2052454445454d5f414d4f554e545f5a45524f00000000000000604482015290519081900360640190fd5b600a54821115611bba576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b611bc43082612d92565b611bce83836122bd565b604080518381526020810183905281516001600160a01b0386169233927f3f693fff038bb8a046aa76d9516190ac7444f7d69cf952c4cbdc086fdef2d6fc929081900390910190a3506117106127a1565b60018054604080516020600284861615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b5f5780601f10610b3457610100808354040283529160200191610b5f565b611ca9878787878787877ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f612e2f565b611cb4878787612d30565b50505050505050565b655af3107a400081565b600e546001600160701b0380821691600160701b810490911690600160e01b900463ffffffff166000611cf8611760565b90508063ffffffff168263ffffffff161415611d175750505050611e65565b600e805463ffffffff808416600160e01b026001600160e01b039092169190911790915560105483830391600091611d5e9165ffffffffffff909116908085169061244916565b90506000611d7e670de0b6b3a7640000610dac848963ffffffff61244916565b9050611d90868263ffffffff6124eb16565b9550611dbe611db1670de0b6b3a7640000610dac858b63ffffffff61244916565b889063ffffffff6124eb16565b9650611dc987613004565b600e80546001600160701b0319166001600160701b0392909216919091179055611df286613004565b600e80546001600160701b0392909216600160701b02600160701b600160e01b0319909216919091179055604080518281526020810189905280820188905290517f875352fb3fadeb8c0be7cbbe8ff761b308fa7033470cd0287f02f3436fd76cb99181900360600190a1505050505050505b565b6000610b7d338484612848565b600e546001600160701b031681565b600a5481565b611ea781611e9683611360565b600e546001600160701b0316613053565b50565b600e54600160e01b900463ffffffff1681565b6103e881565b600b5460ff16611f0d576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600a54600854604080516370a0823160e01b81523060048201529051611fad938593611fa89391926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015611f7057600080fd5b505afa158015611f84573d6000803e3d6000fd5b505050506040513d6020811015611f9a57600080fd5b50519063ffffffff61254516565b6122bd565b50600b805460ff19166001179055565b600f5481565b6009546001600160a01b031681565b6702c68af0bb14000081565b60105465ffffffffffff1681565b61201c878787878787877f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9612e2f565b611cb4878787612196565b600b5461010090046001600160a01b031681565b600560209081526000928352604080842090915290825290205481565b6312e687c081565b601054600160601b900463ffffffff1681565b600b5460ff166120bd576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556120db8160006702c68af0bb14000061293a565b60118190556040805182815290517f9d9cd27245b4e6b06dcf523ac57b6e851b934e199eee376313f906e94bfbfd559181900360200190a150600b805460ff19166001179055565b600b5460ff1661216d576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905561217f611cc7565b6121876127a1565b600b805460ff19166001179055565b6001600160a01b03808416600081815260056020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294909152902054911480159061223257506000198114155b156122b7578181101561228c576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a20424f52524f575f4e4f545f414c4c4f57454400000000000000604482015290519081900360640190fd5b6001600160a01b038085166000908152600c6020908152604080832093871683529290522082820390555b50505050565b600854604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0386811660248301526044808301879052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009560609594169382918083835b6020831061236a5780518252601f19909201916020918201910161234b565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d80600081146123cc576040519150601f19603f3d011682016040523d82523d6000602084013e6123d1565b606091505b50915091508180156123ff5750805115806123ff57508080602001905160208110156123fc57600080fd5b50515b6122b7576040805162461bcd60e51b815260206004820152601660248201527515185c9bdd0e881514905394d1915497d1905253115160521b604482015290519081900360640190fd5b60008261245857506000610b81565b8282028284828161246557fe5b04146124a25760405162461bcd60e51b81526004018080602001828103825260218152602001806135926021913960400191505060405180910390fd5b9392505050565b60006124a283836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f0000000000008152506130e1565b6000828201838110156124a2576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b60006124a283836040518060400160405280601f81526020017f536166654d6174683a207375627472616374696f6e20756e646572666c6f77008152506127b1565b600080600061259586611360565b9250838514156125bb575050600e548190600160701b90046001600160701b0316612798565b600e546001600160701b031684861115612682576001600160a01b0387166000908152600d602052604090208587036125fa868263ffffffff6124eb16565b945061260585613004565b82546001600160701b0319166001600160701b0391821617600160701b600160e01b031916600160701b8583168102919091178455600e5461264b9291900416826124eb565b935061265684613004565b600e806101000a8154816001600160701b0302191690836001600160701b031602179055505050612782565b6001600160a01b0387166000908152600d602052604090208686038086116126ab5760006126af565b8086035b94506126ba85613004565b82546001600160701b0319166001600160701b0391909116178255846126f0578154600160701b600160e01b0319168255612713565b8154600160701b600160e01b031916600160701b6001600160701b038516021782555b6000612725878763ffffffff61254516565b600e54600160701b90046001600160701b03169550905080851161274a57600061274e565b8085035b945061275985613004565b600e806101000a8154816001600160701b0302191690836001600160701b031602179055505050505b6127968784836001600160701b0316613053565b505b93509350939050565b6127a9613146565b611e656131f6565b600081848411156128405760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b838110156128055781810151838201526020016127ed565b50505050905090810190601f1680156128325780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b604080518082018252601881527f5461726f743a205452414e534645525f544f4f5f4849474800000000000000006020808301919091526001600160a01b0386166000908152600490915291909120546128a991839063ffffffff6127b116565b6001600160a01b0380851660009081526004602052604080822093909355908416815220546128de908263ffffffff6124eb16565b6001600160a01b0380841660008181526004602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b6129426129e3565b81831015612990576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b808311156129de576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b505050565b600960009054906101000a90046001600160a01b03166001600160a01b031663f851a4406040518163ffffffff1660e01b815260040160206040518083038186803b158015612a3157600080fd5b505afa158015612a45573d6000803e3d6000fd5b505050506040513d6020811015612a5b57600080fd5b50516001600160a01b03163314611e65576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b600f5460009080841115612bb2576000612b00612af3670de0b6b3a7640000610dac601154612ae7878b61254590919063ffffffff16565b9063ffffffff61244916565b869063ffffffff61254516565b90506000612b1c85610de484610dac838b63ffffffff61244916565b90508015612ba3576009546040805163345ef94160e01b815290516000926001600160a01b03169163345ef941916004808301926020929190829003018186803b158015612b6957600080fd5b505afa158015612b7d573d6000803e3d6000fd5b505050506040513d6020811015612b9357600080fd5b50519050612ba18183612c83565b505b50600f8190559150610b819050565b83915050610b81565b8151612bce9060009060208501906134b1565b508051612be29060019060208401906134b1565b5060405146908060526135408239604080519182900360520182208651602097880120838301835260018452603160f81b93880193909352815180880191909152808201929092527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606083015260808201939093523060a0808301919091528351808303909101815260c090910190925250805192019190912060065550565b600354612c96908263ffffffff6124eb16565b6003556001600160a01b038216600090815260046020526040902054612cc2908263ffffffff6124eb16565b6001600160a01b03831660008181526004602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b6000818310612d2957816124a2565b5090919050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294825291829020859055815185815291517fc3c1215b41d54142382d54a05fb991007165ae91bcb1879bac8b290d9111aaf49281900390910190a3505050565b6001600160a01b038216600090815260046020526040902054612dbb908263ffffffff61254516565b6001600160a01b038316600090815260046020526040902055600354612de7908263ffffffff61254516565b6003556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b42851015612e75576040805162461bcd60e51b815260206004820152600e60248201526d15185c9bdd0e881156141254915160921b604482015290519081900360640190fd5b6006546001600160a01b03808a1660008181526007602090815260408083208054600180820190925582518085018a905280840196909652958e166060860152608085018d905260a085019590955260c08085018c90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff8a166101828501526101a284018990526101c28401889052519193926101e280820193601f1981019281900390910190855afa158015612f71573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b03811615801590612fa75750896001600160a01b0316816001600160a01b0316145b612ff8576040805162461bcd60e51b815260206004820152601860248201527f5461726f743a20494e56414c49445f5349474e41545552450000000000000000604482015290519081900360640190fd5b50505050505050505050565b6000600160701b821061304f576040805162461bcd60e51b815260206004820152600e60248201526d2a30b937ba1d1029a0a32298989960911b604482015290519081900360640190fd5b5090565b6014546001600160a01b03168061306a57506129de565b604080516305285d7f60e01b81526001600160a01b03868116600483015260248201869052604482018590529151918316916305285d7f9160648082019260009290919082900301818387803b1580156130c357600080fd5b505af11580156130d7573d6000803e3d6000fd5b5050505050505050565b600081836131305760405162461bcd60e51b81526020600482018181528351602484015283519092839260449091019190850190808383600083156128055781810151838201526020016127ed565b50600083858161313c57fe5b0495945050505050565b600854604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b15801561319157600080fd5b505afa1580156131a5573d6000803e3d6000fd5b505050506040513d60208110156131bb57600080fd5b5051600a81905560408051918252517f8a0df8ef054fae2c3d2d19a7b322e864870cc9fd3cb07fb9526309c596244bf49181900360200190a1565b60125460135460105465ffffffffffff80821691600160301b810490911690600160601b900463ffffffff1660008161322d611760565b03905063ffffffff8116156133a257613244611760565b6010600c6101000a81548163ffffffff021916908363ffffffff1602179055506000838510156132cd576000670de0b6b3a76400008363ffffffff168887898903670de0b6b3a7640000028161329657fe5b040202816132a057fe5b049050670de0b6b3a764000081116132c25780670de0b6b3a7640000036132c5565b60005b91505061330e565b6000670de0b6b3a76400008363ffffffff168887888a03670de0b6b3a764000002816132f557fe5b040202816132ff57fe5b04670de0b6b3a7640000019150505b670de0b6b3a764000084820204935064418fa6de408411156133335764418fa6de4093505b6312e687c0841015613347576312e687c093505b601080546bffffffffffff0000000000001916600160301b65ffffffffffff8716021790556040805185815290517f713a98ffb7d769b8e33e2ee945ebb6acb7f397532688164d3ce1081f903c77bc916020908290030190a1505b600e54600a54600091600160701b90046001600160701b03169082906133c890836124eb565b905080156133e9578082670de0b6b3a764000002816133e357fe5b046133ec565b60005b9250505086811161340a57868185028161340257fe5b049450613444565b600087670de0b6b3a764000003888303670de0b6b3a7640000028161342b57fe5b049050670de0b6b3a76400006002820281018602049550505b6010805465ffffffffffff191665ffffffffffff87161790556040805186815290517f338541dc9083f6af6715482fb419e1483c1ae9097764fd68a5dc98109bd5a788916020908290030190a150505050505050565b604080518082019091526000808252602082015290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106134f257805160ff191683800117855561351f565b8280016001018555821561351f579182015b8281111561351f578251825591602001919060010190613504565b5061304f926112c69250905b8082111561304f576000815560010161352b56fe454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77a265627a7a7231582035698883bf0a14c30ead7e3ee05209121422a6862f53e57db0fff3d4aade686764736f6c63430005100032a265627a7a72315820c4caca8b62bec2925a0b6256a2d852dc7dca0e23950d89ef1e70f7cc720097c664736f6c63430005100032
Deployed ByteCode
0x608060405234801561001057600080fd5b506004361061002b5760003560e01c806354bcd7ad14610030575b600080fd5b61005f6004803603604081101561004657600080fd5b5080356001600160a01b0316906020013560ff1661007b565b604080516001600160a01b039092168252519081900360200190f35b600060606040518060200161008f90610111565b818103601f199081018352601f90910116604081815233606090811b6020808501919091529088901b6bffffffffffffffffffffffff1916603484015260f887901b6001600160f81b0319166048840152815180840360290181526049909301909152815191810191909120825192935091829184016000f595945050505050565b6136bb8061011f8339019056fe60806040526002805460ff199081166012908117909255600b80549091166001179055600e80546001600160701b031916670de0b6b3a7640000176001600160e01b0316600160e01b4263ffffffff16908102919091179091556010805465ffffffffffff60301b19166a017a029b000000000000001763ffffffff60601b19166c0100000000000000000000000090920291909117905567016345785d8a0000601155670a688906bd8b00009055652d79883d20006013553480156100c457600080fd5b506135e7806100d46000396000f3fe608060405234801561001057600080fd5b50600436106103995760003560e01c806370a08231116101e9578063b7f1118a1161010f578063c914b437116100ad578063e07660001161007c578063e076600014610a80578063e12b630614610a88578063fca7820b14610a90578063fff6cae914610aad57610399565b8063c914b437146109f1578063d505accf146109f9578063d8dfeb4514610a4a578063dd62ed3e14610a5257610399565b8063bc25cf77116100e9578063bc25cf77146109b3578063be340e32146109d9578063c45a0155146109e1578063c72f3fbb146109e957610399565b8063b7f1118a1461097d578063b95b92a3146109a3578063ba9a7a56146109ab57610399565b806395a2251f11610187578063a6afed9511610156578063a6afed9514610939578063a9059cbb14610941578063aa5af0fd1461096d578063ad7a672f1461097557610399565b806395a2251f146108b257806395d89b41146108d85780639e79b55c146108e0578063a07157191461093157610399565b806386b9d81f116101c357806386b9d81f1461081857806391b4274514610846578063926d845b146108695780639292b0321461089557610399565b806370a08231146107ab578063796b89b9146107d15780637ecebe00146107f257610399565b806335542822116102ce5780634d73e9ba1161026c5780636a030c111161023b5780636a030c111461067c5780636a6278421461074f5780636bd76d24146107755780636f307dc3146107a357610399565b80634d73e9ba1461062257806355957220146106485780635b2b9d1a1461066c578063685440651461067457610399565b80634322b714116102a85780634322b714146105e6578063452ae95f146105ee57806347bd3718146105f65780634a5d316c1461061a57610399565b806335542822146105b05780633644e515146105d65780633ba0b9a9146105de57610399565b80632374e8a91161033b57806327549a0b1161031557806327549a0b146105655780632d5231d31461058257806330adf81f1461058a578063313ce5671461059257610399565b80632374e8a91461051f57806323b872dd14610527578063253c24f31461055d57610399565b8063095ea7b311610377578063095ea7b31461043d57806318160ddd1461047d5780631aebf12f146104855780631e7dcc0d1461048d57610399565b806301f8c1c81461039e57806306fdde03146103b8578063075f4e7f14610435575b600080fd5b6103a6610ab5565b60408051918252519081900360200190f35b6103c0610ad9565b6040805160208082528351818301528351919283929083019185019080838360005b838110156103fa5781810151838201526020016103e2565b50505050905090810190601f1680156104275780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6103a6610b67565b6104696004803603604081101561045357600080fd5b506001600160a01b038135169060200135610b70565b604080519115158252519081900360200190f35b6103a6610b87565b6103a6610b8d565b61051d600480360360808110156104a357600080fd5b6001600160a01b038235811692602081013590911691604082013591908101906080810160608201356401000000008111156104de57600080fd5b8201836020820111156104f057600080fd5b8035906020019184600183028401116401000000008311171561051257600080fd5b509092509050610b93565b005b6103a6610fb5565b6104696004803603606081101561053d57600080fd5b506001600160a01b03813581169160208101359091169060400135610fc1565b6103a6611090565b61051d6004803603602081101561057b57600080fd5b503561109a565b6103a661114c565b6103a6611152565b61059a611176565b6040805160ff9092168252519081900360200190f35b61051d600480360360208110156105c657600080fd5b50356001600160a01b031661117f565b6103a661123c565b6103a6611242565b6103a66112c9565b6103a66112cf565b6105fe6112d8565b604080516001600160701b039092168252519081900360200190f35b61051d6112ee565b6103a66004803603602081101561063857600080fd5b50356001600160a01b0316611360565b6106506113f0565b604080516001600160a01b039092168252519081900360200190f35b6103a66113ff565b6103a6611404565b61051d6004803603608081101561069257600080fd5b8101906020810181356401000000008111156106ad57600080fd5b8201836020820111156106bf57600080fd5b803590602001918460018302840111640100000000831117156106e157600080fd5b9193909290916020810190356401000000008111156106ff57600080fd5b82018360208201111561071157600080fd5b8035906020019184600183028401116401000000008311171561073357600080fd5b91935091506001600160a01b0381358116916020013516611410565b6103a66004803603602081101561076557600080fd5b50356001600160a01b0316611524565b6103a66004803603604081101561078b57600080fd5b506001600160a01b0381358116916020013516611722565b61065061173f565b6103a6600480360360208110156107c157600080fd5b50356001600160a01b031661174e565b6107d9611760565b6040805163ffffffff9092168252519081900360200190f35b6103a66004803603602081101561080857600080fd5b50356001600160a01b031661176a565b6103a66004803603604081101561082e57600080fd5b506001600160a01b038135811691602001351661177c565b61084e6119ad565b6040805165ffffffffffff9092168252519081900360200190f35b6104696004803603604081101561087f57600080fd5b506001600160a01b0381351690602001356119c2565b61051d600480360360208110156108ab57600080fd5b50356119cf565b6103a6600480360360208110156108c857600080fd5b50356001600160a01b0316611a86565b6103c0611c1f565b61051d600480360360e08110156108f657600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611c79565b6103a6611cbd565b61051d611cc7565b6104696004803603604081101561095757600080fd5b506001600160a01b038135169060200135611e67565b6105fe611e74565b6103a6611e83565b61051d6004803603602081101561099357600080fd5b50356001600160a01b0316611e89565b6107d9611eaa565b6103a6611ebd565b61051d600480360360208110156109c957600080fd5b50356001600160a01b0316611ec3565b6103a6611fbd565b610650611fc3565b6103a6611fd2565b61084e611fde565b61051d600480360360e0811015610a0f57600080fd5b506001600160a01b03813581169160208101359091169060408101359060608101359060ff6080820135169060a08101359060c00135611fec565b610650612027565b6103a660048036036040811015610a6857600080fd5b506001600160a01b038135811691602001351661203b565b6103a6612058565b6107d9612060565b61051d60048036036020811015610aa657600080fd5b5035612073565b61051d612123565b7ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f81565b6000805460408051602060026001851615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b5f5780601f10610b3457610100808354040283529160200191610b5f565b820191906000526020600020905b815481529060010190602001808311610b4257829003601f168201915b505050505081565b640d7957c4d081565b6000610b7d338484612196565b5060015b92915050565b60035481565b60125481565b600b5460ff16610bdd576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055610bef611cc7565b600a5480841115610c42576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b610c4d8633866121f8565b8315610c5d57610c5d85856122bd565b8115610d0f57604051631b4a79ff60e21b815233600482018181526001600160a01b038981166024850152604484018890526080606485019081526084850187905290891693636d29e7fc93928b928a928a928a92909160a401848480828437600081840152601f19601f8201169050808301925050509650505050505050600060405180830381600087803b158015610cf657600080fd5b505af1158015610d0a573d6000803e3d6000fd5b505050505b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b158015610d5a57600080fd5b505afa158015610d6e573d6000803e3d6000fd5b505050506040513d6020811015610d8457600080fd5b505190506000610db8670de0b6b3a7640000610dac88655af3107a400063ffffffff61244916565b9063ffffffff6124a916565b90506000610dcc878363ffffffff6124eb16565b90506000610df085610de4868b63ffffffff6124eb16565b9063ffffffff61254516565b90506000806000610e028d8686612587565b92509250925083851115610f1c57600b60019054906101000a90046001600160a01b03166001600160a01b0316639aac2c538e30856040518463ffffffff1660e01b815260040180846001600160a01b03166001600160a01b03168152602001836001600160a01b03166001600160a01b031681526020018281526020019350505050602060405180830381600087803b158015610e9f57600080fd5b505af1158015610eb3573d6000803e3d6000fd5b505050506040513d6020811015610ec957600080fd5b5051610f1c576040805162461bcd60e51b815260206004820152601d60248201527f5461726f743a20494e53554646494349454e545f4c4951554944495459000000604482015290519081900360640190fd5b8b6001600160a01b03168d6001600160a01b0316336001600160a01b03167f33f3048bd4e6af45e53afb722adfd57dbde82da7e93e44db921fb4b8c6a70c4b8e88888888604051808681526020018581526020018481526020018381526020018281526020019550505050505060405180910390a45050505050505050610fa16127a1565b5050600b805460ff19166001179055505050565b6706f05b59d3b2000081565b6001600160a01b03831660009081526005602090815260408083203384529091528120546000191461107b57604080518082018252601b81527f5461726f743a205452414e534645525f4e4f545f414c4c4f57454400000000006020808301919091526001600160a01b038716600090815260058252838120338252909152919091205461105691849063ffffffff6127b116565b6001600160a01b03851660009081526005602090815260408083203384529091529020555b611086848484612848565b5060019392505050565b656943fdb1990081565b600b5460ff166110e4576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905561110481640d7957c4d0656943fdb1990061293a565b60138190556040805182815290517f1396dfcdb64fb7eb77fb84966f27b81afe14aa70b6e966c68d74af3302a9fe909181900360200190a150600b805460ff19166001179055565b60135481565b7f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c981565b60025460ff1681565b600b5460ff166111c9576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556111db6129e3565b601480546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f468b6598e7e810c65c9858b5f23a2d5b8692fb753b78a032232de4c6ed3cabbf9181900360200190a150600b805460ff19166001179055565b60065481565b600061124c611cc7565b600354600e54600a546000916112729190600160701b90046001600160701b03166124eb565b905081158061127f575080155b1561129657670de0b6b3a7640000925050506112c6565b60006112b483610dac84670de0b6b3a764000063ffffffff61244916565b90506112c08184612aaf565b93505050505b90565b60115481565b64418fa6de4081565b600e54600160701b90046001600160701b031681565b6009546001600160a01b03161561134c576040805162461bcd60e51b815260206004820152601a60248201527f5461726f743a20464143544f52595f414c52454144595f534554000000000000604482015290519081900360640190fd5b600980546001600160a01b03191633179055565b600061136a61349a565b506001600160a01b0382166000908152600d60209081526040918290208251808401909352546001600160701b038082168452600160701b909104169082018190526113ba5760009150506113eb565b6020810151600e5482516113e7926001600160701b0390811692610dac928216911663ffffffff61244916565b9150505b919050565b6014546001600160a01b031681565b600381565b670dbd2fc137a3000081565b6009546001600160a01b03163314611465576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b6114d886868080601f01602080910402602001604051908101604052809392919081815260200183838082843760009201919091525050604080516020601f8a018190048102820181019092528881529250889150879081908401838280828437600092019190915250612bbb92505050565b600880546001600160a01b039384166001600160a01b0319909116179055600b80549190921661010002610100600160a81b03199091161790555050670de0b6b3a7640000600f555050565b600b5460009060ff16611571576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b1580156115c657600080fd5b505afa1580156115da573d6000803e3d6000fd5b505050506040513d60208110156115f057600080fd5b5051600a5490915060009061160c90839063ffffffff61254516565b9050611631611619611242565b610dac83670de0b6b3a764000063ffffffff61244916565b92506003546000141561165f57611650836103e863ffffffff61254516565b925061165f60006103e8612c83565b600083116116b4576040805162461bcd60e51b815260206004820152601760248201527f5461726f743a204d494e545f414d4f554e545f5a45524f000000000000000000604482015290519081900360640190fd5b6116be8484612c83565b604080518281526020810185905281516001600160a01b0387169233927f2f00e3cdd69a77be7ed215ec7b2a36784dd158f921fca79ac29deffa353fe6ee929081900390910190a350506117106127a1565b600b805460ff19166001179055919050565b600c60209081526000928352604080842090915290825290205481565b6008546001600160a01b031681565b60046020526000908152604090205481565b63ffffffff421690565b60076020526000908152604090205481565b600b5460009060ff166117c9576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556117db611cc7565b600854604080516370a0823160e01b815230600482015290516000926001600160a01b0316916370a08231916024808301926020929190829003018186803b15801561182657600080fd5b505afa15801561183a573d6000803e3d6000fd5b505050506040513d602081101561185057600080fd5b5051600a5490915060009061186c90839063ffffffff61254516565b9050600061188261187c87611360565b83612d1a565b600b546040805163b2a02ff160e01b81526001600160a01b0389811660048301528a81166024830152604482018590529151939450610100909204169163b2a02ff1916064808201926020929091908290030181600087803b1580156118e757600080fd5b505af11580156118fb573d6000803e3d6000fd5b505050506040513d602081101561191157600080fd5b5051935060008080611924898287612587565b604080518b8152602081018a90528082018590526060810184905260808101839052905193965091945092506001600160a01b03808b1692908c169133917fb0dbe18c6ffdf0da655dd690e77211d379205c497be44c64447c3f5f021b51679181900360a00190a450505050505061199a6127a1565b600b805460ff1916600117905592915050565b601054600160301b900465ffffffffffff1681565b6000610b7d338484612d30565b600b5460ff16611a19576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055611a3e816706f05b59d3b20000670dbd2fc137a3000061293a565b60128190556040805182815290517f7a550b1995ff63260fb313f12024e66e73bad425372e5af6b1e04cb3799ef38c9181900360200190a150600b805460ff19166001179055565b600b5460009060ff16611ad3576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905530600090815260046020526040902054611b11670de0b6b3a7640000610dac611b04611242565b849063ffffffff61244916565b915060008211611b68576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a2052454445454d5f414d4f554e545f5a45524f00000000000000604482015290519081900360640190fd5b600a54821115611bba576040805162461bcd60e51b81526020600482015260186024820152770a8c2e4dee87440929ca6aa8c8c9286928a9ca8be8682a6960431b604482015290519081900360640190fd5b611bc43082612d92565b611bce83836122bd565b604080518381526020810183905281516001600160a01b0386169233927f3f693fff038bb8a046aa76d9516190ac7444f7d69cf952c4cbdc086fdef2d6fc929081900390910190a3506117106127a1565b60018054604080516020600284861615610100026000190190941693909304601f81018490048402820184019092528181529291830182828015610b5f5780601f10610b3457610100808354040283529160200191610b5f565b611ca9878787878787877ff6d86ed606f871fa1a557ac0ba607adce07767acf53f492fb215a1a4db4aea6f612e2f565b611cb4878787612d30565b50505050505050565b655af3107a400081565b600e546001600160701b0380821691600160701b810490911690600160e01b900463ffffffff166000611cf8611760565b90508063ffffffff168263ffffffff161415611d175750505050611e65565b600e805463ffffffff808416600160e01b026001600160e01b039092169190911790915560105483830391600091611d5e9165ffffffffffff909116908085169061244916565b90506000611d7e670de0b6b3a7640000610dac848963ffffffff61244916565b9050611d90868263ffffffff6124eb16565b9550611dbe611db1670de0b6b3a7640000610dac858b63ffffffff61244916565b889063ffffffff6124eb16565b9650611dc987613004565b600e80546001600160701b0319166001600160701b0392909216919091179055611df286613004565b600e80546001600160701b0392909216600160701b02600160701b600160e01b0319909216919091179055604080518281526020810189905280820188905290517f875352fb3fadeb8c0be7cbbe8ff761b308fa7033470cd0287f02f3436fd76cb99181900360600190a1505050505050505b565b6000610b7d338484612848565b600e546001600160701b031681565b600a5481565b611ea781611e9683611360565b600e546001600160701b0316613053565b50565b600e54600160e01b900463ffffffff1681565b6103e881565b600b5460ff16611f0d576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff19169055600a54600854604080516370a0823160e01b81523060048201529051611fad938593611fa89391926001600160a01b03909116916370a08231916024808301926020929190829003018186803b158015611f7057600080fd5b505afa158015611f84573d6000803e3d6000fd5b505050506040513d6020811015611f9a57600080fd5b50519063ffffffff61254516565b6122bd565b50600b805460ff19166001179055565b600f5481565b6009546001600160a01b031681565b6702c68af0bb14000081565b60105465ffffffffffff1681565b61201c878787878787877f6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9612e2f565b611cb4878787612196565b600b5461010090046001600160a01b031681565b600560209081526000928352604080842090915290825290205481565b6312e687c081565b601054600160601b900463ffffffff1681565b600b5460ff166120bd576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff191690556120db8160006702c68af0bb14000061293a565b60118190556040805182815290517f9d9cd27245b4e6b06dcf523ac57b6e851b934e199eee376313f906e94bfbfd559181900360200190a150600b805460ff19166001179055565b600b5460ff1661216d576040805162461bcd60e51b815260206004820152601060248201526f15185c9bdd0e8814915153951154915160821b604482015290519081900360640190fd5b600b805460ff1916905561217f611cc7565b6121876127a1565b600b805460ff19166001179055565b6001600160a01b03808416600081815260056020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294909152902054911480159061223257506000198114155b156122b7578181101561228c576040805162461bcd60e51b815260206004820152601960248201527f5461726f743a20424f52524f575f4e4f545f414c4c4f57454400000000000000604482015290519081900360640190fd5b6001600160a01b038085166000908152600c6020908152604080832093871683529290522082820390555b50505050565b600854604080518082018252601981527f7472616e7366657228616464726573732c75696e74323536290000000000000060209182015281516001600160a01b0386811660248301526044808301879052845180840390910181526064909201845291810180516001600160e01b031663a9059cbb60e01b1781529251815160009560609594169382918083835b6020831061236a5780518252601f19909201916020918201910161234b565b6001836020036101000a0380198251168184511680821785525050505050509050019150506000604051808303816000865af19150503d80600081146123cc576040519150601f19603f3d011682016040523d82523d6000602084013e6123d1565b606091505b50915091508180156123ff5750805115806123ff57508080602001905160208110156123fc57600080fd5b50515b6122b7576040805162461bcd60e51b815260206004820152601660248201527515185c9bdd0e881514905394d1915497d1905253115160521b604482015290519081900360640190fd5b60008261245857506000610b81565b8282028284828161246557fe5b04146124a25760405162461bcd60e51b81526004018080602001828103825260218152602001806135926021913960400191505060405180910390fd5b9392505050565b60006124a283836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f0000000000008152506130e1565b6000828201838110156124a2576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b60006124a283836040518060400160405280601f81526020017f536166654d6174683a207375627472616374696f6e20756e646572666c6f77008152506127b1565b600080600061259586611360565b9250838514156125bb575050600e548190600160701b90046001600160701b0316612798565b600e546001600160701b031684861115612682576001600160a01b0387166000908152600d602052604090208587036125fa868263ffffffff6124eb16565b945061260585613004565b82546001600160701b0319166001600160701b0391821617600160701b600160e01b031916600160701b8583168102919091178455600e5461264b9291900416826124eb565b935061265684613004565b600e806101000a8154816001600160701b0302191690836001600160701b031602179055505050612782565b6001600160a01b0387166000908152600d602052604090208686038086116126ab5760006126af565b8086035b94506126ba85613004565b82546001600160701b0319166001600160701b0391909116178255846126f0578154600160701b600160e01b0319168255612713565b8154600160701b600160e01b031916600160701b6001600160701b038516021782555b6000612725878763ffffffff61254516565b600e54600160701b90046001600160701b03169550905080851161274a57600061274e565b8085035b945061275985613004565b600e806101000a8154816001600160701b0302191690836001600160701b031602179055505050505b6127968784836001600160701b0316613053565b505b93509350939050565b6127a9613146565b611e656131f6565b600081848411156128405760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b838110156128055781810151838201526020016127ed565b50505050905090810190601f1680156128325780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b604080518082018252601881527f5461726f743a205452414e534645525f544f4f5f4849474800000000000000006020808301919091526001600160a01b0386166000908152600490915291909120546128a991839063ffffffff6127b116565b6001600160a01b0380851660009081526004602052604080822093909355908416815220546128de908263ffffffff6124eb16565b6001600160a01b0380841660008181526004602090815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b6129426129e3565b81831015612990576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b808311156129de576040805162461bcd60e51b81526020600482015260166024820152755461726f743a20494e56414c49445f53455454494e4760501b604482015290519081900360640190fd5b505050565b600960009054906101000a90046001600160a01b03166001600160a01b031663f851a4406040518163ffffffff1660e01b815260040160206040518083038186803b158015612a3157600080fd5b505afa158015612a45573d6000803e3d6000fd5b505050506040513d6020811015612a5b57600080fd5b50516001600160a01b03163314611e65576040805162461bcd60e51b815260206004820152601360248201527215185c9bdd0e8815539055551213d492569151606a1b604482015290519081900360640190fd5b600f5460009080841115612bb2576000612b00612af3670de0b6b3a7640000610dac601154612ae7878b61254590919063ffffffff16565b9063ffffffff61244916565b869063ffffffff61254516565b90506000612b1c85610de484610dac838b63ffffffff61244916565b90508015612ba3576009546040805163345ef94160e01b815290516000926001600160a01b03169163345ef941916004808301926020929190829003018186803b158015612b6957600080fd5b505afa158015612b7d573d6000803e3d6000fd5b505050506040513d6020811015612b9357600080fd5b50519050612ba18183612c83565b505b50600f8190559150610b819050565b83915050610b81565b8151612bce9060009060208501906134b1565b508051612be29060019060208401906134b1565b5060405146908060526135408239604080519182900360520182208651602097880120838301835260018452603160f81b93880193909352815180880191909152808201929092527fc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6606083015260808201939093523060a0808301919091528351808303909101815260c090910190925250805192019190912060065550565b600354612c96908263ffffffff6124eb16565b6003556001600160a01b038216600090815260046020526040902054612cc2908263ffffffff6124eb16565b6001600160a01b03831660008181526004602090815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b6000818310612d2957816124a2565b5090919050565b6001600160a01b038084166000818152600c6020908152604080832094871680845294825291829020859055815185815291517fc3c1215b41d54142382d54a05fb991007165ae91bcb1879bac8b290d9111aaf49281900390910190a3505050565b6001600160a01b038216600090815260046020526040902054612dbb908263ffffffff61254516565b6001600160a01b038316600090815260046020526040902055600354612de7908263ffffffff61254516565b6003556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b42851015612e75576040805162461bcd60e51b815260206004820152600e60248201526d15185c9bdd0e881156141254915160921b604482015290519081900360640190fd5b6006546001600160a01b03808a1660008181526007602090815260408083208054600180820190925582518085018a905280840196909652958e166060860152608085018d905260a085019590955260c08085018c90528151808603909101815260e08501825280519083012061190160f01b6101008601526101028501969096526101228085019690965280518085039096018652610142840180825286519683019690962095839052610162840180825286905260ff8a166101828501526101a284018990526101c28401889052519193926101e280820193601f1981019281900390910190855afa158015612f71573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b03811615801590612fa75750896001600160a01b0316816001600160a01b0316145b612ff8576040805162461bcd60e51b815260206004820152601860248201527f5461726f743a20494e56414c49445f5349474e41545552450000000000000000604482015290519081900360640190fd5b50505050505050505050565b6000600160701b821061304f576040805162461bcd60e51b815260206004820152600e60248201526d2a30b937ba1d1029a0a32298989960911b604482015290519081900360640190fd5b5090565b6014546001600160a01b03168061306a57506129de565b604080516305285d7f60e01b81526001600160a01b03868116600483015260248201869052604482018590529151918316916305285d7f9160648082019260009290919082900301818387803b1580156130c357600080fd5b505af11580156130d7573d6000803e3d6000fd5b5050505050505050565b600081836131305760405162461bcd60e51b81526020600482018181528351602484015283519092839260449091019190850190808383600083156128055781810151838201526020016127ed565b50600083858161313c57fe5b0495945050505050565b600854604080516370a0823160e01b815230600482015290516001600160a01b03909216916370a0823191602480820192602092909190829003018186803b15801561319157600080fd5b505afa1580156131a5573d6000803e3d6000fd5b505050506040513d60208110156131bb57600080fd5b5051600a81905560408051918252517f8a0df8ef054fae2c3d2d19a7b322e864870cc9fd3cb07fb9526309c596244bf49181900360200190a1565b60125460135460105465ffffffffffff80821691600160301b810490911690600160601b900463ffffffff1660008161322d611760565b03905063ffffffff8116156133a257613244611760565b6010600c6101000a81548163ffffffff021916908363ffffffff1602179055506000838510156132cd576000670de0b6b3a76400008363ffffffff168887898903670de0b6b3a7640000028161329657fe5b040202816132a057fe5b049050670de0b6b3a764000081116132c25780670de0b6b3a7640000036132c5565b60005b91505061330e565b6000670de0b6b3a76400008363ffffffff168887888a03670de0b6b3a764000002816132f557fe5b040202816132ff57fe5b04670de0b6b3a7640000019150505b670de0b6b3a764000084820204935064418fa6de408411156133335764418fa6de4093505b6312e687c0841015613347576312e687c093505b601080546bffffffffffff0000000000001916600160301b65ffffffffffff8716021790556040805185815290517f713a98ffb7d769b8e33e2ee945ebb6acb7f397532688164d3ce1081f903c77bc916020908290030190a1505b600e54600a54600091600160701b90046001600160701b03169082906133c890836124eb565b905080156133e9578082670de0b6b3a764000002816133e357fe5b046133ec565b60005b9250505086811161340a57868185028161340257fe5b049450613444565b600087670de0b6b3a764000003888303670de0b6b3a7640000028161342b57fe5b049050670de0b6b3a76400006002820281018602049550505b6010805465ffffffffffff191665ffffffffffff87161790556040805186815290517f338541dc9083f6af6715482fb419e1483c1ae9097764fd68a5dc98109bd5a788916020908290030190a150505050505050565b604080518082019091526000808252602082015290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106134f257805160ff191683800117855561351f565b8280016001018555821561351f579182015b8281111561351f578251825591602001919060010190613504565b5061304f926112c69250905b8082111561304f576000815560010161352b56fe454950373132446f6d61696e28737472696e67206e616d652c737472696e672076657273696f6e2c75696e7432353620636861696e49642c6164647265737320766572696679696e67436f6e747261637429536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77a265627a7a7231582035698883bf0a14c30ead7e3ee05209121422a6862f53e57db0fff3d4aade686764736f6c63430005100032a265627a7a72315820c4caca8b62bec2925a0b6256a2d852dc7dca0e23950d89ef1e70f7cc720097c664736f6c63430005100032