Skip to content

Commit

Permalink
chore: add core contracts and implement its logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thomgabriel committed Nov 20, 2023
1 parent e8e1319 commit 27259bc
Show file tree
Hide file tree
Showing 12 changed files with 1,191 additions and 42 deletions.
48 changes: 48 additions & 0 deletions contracts/DREX.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//SPDX-License-Identifier: Unlicense
pragma solidity >=0.8.0;

contract TSELIC29 {
string public name = "Real Digital X";
string public symbol = "DREX";
uint256 public totalSupply = 1000000e6; // 1 million tokens
uint8 public decimals = 6;

event Transfer(address indexed _from, address indexed _to, uint256 _value);

event Approval(address indexed _owner, address indexed _spender, uint256 _value);

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

constructor() {
balanceOf[msg.sender] = totalSupply;
}

function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}

function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}

function transferFrom(
address _from,
address _to,
uint256 _value
) public returns (bool success) {
require(_value <= balanceOf[_from]);
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}
22 changes: 0 additions & 22 deletions contracts/Greeter.sol

This file was deleted.

44 changes: 44 additions & 0 deletions contracts/InterestRateModel.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import "@openzeppelin/contracts/access/AccessControl.sol";

/**
* @title Interest rate model for Lyra Loans.
*
* linear function
*
*/
contract InterestRateModel is AccessControl {
constructor() {
// _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

/**
* @notice get APR from chainlink functions?
* TODO: implement function
*/
function getInterestRate() public view returns (uint256) {
return 0;
}

/**
* @notice Calculates the current supply interest rate.
* @param totalSupply The amount of supply.
* @param totalBorrow The amount of borrows.
* @return The supply rate percentage.
*/
function getSupplyInterestRate(
uint256 totalSupply,
uint256 totalBorrow
) public view returns (uint) {
uint256 apr = getInterestRate();

if (totalBorrow == 0) {
return 0;
} else if (totalBorrow >= totalSupply) {
return apr;
}
return ((totalBorrow * apr) / totalSupply);
}
}
134 changes: 134 additions & 0 deletions contracts/LiquidatePool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "@openzeppelin/contracts/access/AccessControl.sol";

import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";

contract LiquidatePool is AccessControl {
using SafeERC20 for IERC20;
using SafeMath for uint256;

bytes32 public constant POOL_MANAGER_ROLE = keccak256("POOL_MANAGER_ROLE");

address public admin;
// rbrllpool
address public rbrllpool;
// stbt address
IERC20 public stbt;
// usdc address
IERC20 public usdc;
// uniswap router
ISwapRouter swapRouter;

// Used to calculate the fee base.
uint256 public constant FEE_COEFFICIENT = 1e8;
// Max fee rates can't over then 1%
uint256 public constant maxLiquidateFeeRate = FEE_COEFFICIENT / 100;

// It's used when call liquidate method.
uint256 public liquidateFeeRate;
// Fee Collector, used to receive fee.
address public feeCollector;

event FeeCollectorChanged(address newFeeCollector);
event LiquidateFeeRateChanged(uint256 newLiquidateFeeRate);
event UniRouterChanged(address newUniRouter);

constructor(
address _admin,
address _rbrllpool,
address _stbt,
address _usdc,
ISwapRouter _swapRouter
) {
require(_admin != address(0), "!_admin");
require(_rbrllpool != address(0), "!_rbrllpool");
require(_stbt != address(0), "!_stbt");
require(_usdc != address(0), "!_usdc");

admin = _admin;
rbrllpool = _rbrllpool;
stbt = IERC20(_stbt);
usdc = IERC20(_usdc);
swapRouter = _swapRouter;

_setRoleAdmin(POOL_MANAGER_ROLE, DEFAULT_ADMIN_ROLE);
_setupRole(DEFAULT_ADMIN_ROLE, admin);
_setupRole(POOL_MANAGER_ROLE, admin);
}

/**
* @dev to set the collector of fee
* @param _feeCollector the address of collector
*/
function setFeeCollector(address _feeCollector) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(_feeCollector != address(0), "!_feeCollector");
feeCollector = _feeCollector;
emit FeeCollectorChanged(feeCollector);
}

/**
* @dev to set the rate of liquidate fee
* @param _liquidateFeeRate the rate. it should be multiply 10**6
*/
function setLiquidateFeeRate(uint256 _liquidateFeeRate) external onlyRole(POOL_MANAGER_ROLE) {
require(
_liquidateFeeRate <= maxLiquidateFeeRate,
"Liquidate fee rate should be less than 1%."
);
liquidateFeeRate = _liquidateFeeRate;
emit LiquidateFeeRateChanged(liquidateFeeRate);
}

/**
* @dev to set the uniswap router
* @param _swapRouter the address of uniswap router
*/
function setUniRouter(address _swapRouter) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(_swapRouter != address(0), "!_swapRouter");
swapRouter = ISwapRouter(_swapRouter);
emit UniRouterChanged(_swapRouter);
}

/**
* @dev Transfer a give amout of stbt to matrixport's mint pool
* @param stbtAmount the amout of stbt
* @param minReturn the minimum amount of return
* @param receiver used to receive token
*/
function flashLiquidatetSELIC(
uint256 stbtAmount,
uint256 minReturn,
address receiver
) external {
require(msg.sender == rbrllpool, "unauthorized");

stbt.approve(address(swapRouter), stbtAmount);

// We set the sqrtPriceLimitx96 to be 0 to ensure we swap our exact input amount.
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
tokenIn: address(stbt),
tokenOut: address(usdc),
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: stbtAmount,
amountOutMinimum: minReturn,
sqrtPriceLimitX96: 0
});

// The call to `exactInputSingle` executes the swap.
uint256 amountOut = swapRouter.exactInputSingle(params);

uint256 feeAmount = amountOut.mul(liquidateFeeRate).div(FEE_COEFFICIENT);
uint256 amountAfterFee = amountOut.sub(feeAmount);
usdc.safeTransfer(receiver, amountAfterFee);
usdc.safeTransfer(feeCollector, feeAmount);
}
}
9 changes: 9 additions & 0 deletions contracts/interfaces/IInterestRateModel.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

interface IInterestRateModel {
function getSupplyInterestRate(
uint256 totalSupply,
uint256 totalBorrow
) external pure returns (uint);
}
10 changes: 10 additions & 0 deletions contracts/interfaces/ILiquidatePool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

interface ILiquidatePool {
function flashLiquidatetSELIC(
uint256 tselicAmount,
uint256 minReturn,
address receiver
) external;
}
23 changes: 23 additions & 0 deletions contracts/mocks/MockPriceFeed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;

contract MockPriceFeed {
// mock usdc to $1
function latestAnswer() public pure returns (int256) {
return 100000000;
}

function latestRoundData()
public
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
return (1, 100000000, block.timestamp, block.timestamp, 1);
}
}
Loading

0 comments on commit 27259bc

Please sign in to comment.