|
1 | 1 | // SPDX-License-Identifier: UNLICENSED |
2 | 2 | pragma solidity ^0.8.13; |
3 | 3 |
|
4 | | -contract Counter { |
5 | | - uint256 public number; |
6 | 4 |
|
7 | | - function setNumber(uint256 newNumber) public { |
8 | | - number = newNumber; |
| 5 | +import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol"; |
| 6 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | + |
| 8 | + |
| 9 | +contract LBP{ |
| 10 | + |
| 11 | + error UserAlreadyRegistered(); |
| 12 | + error AmountMustBePositive(); |
| 13 | + error InsufficientBalance(); |
| 14 | + error InsufficientCollateral(); |
| 15 | + error ExceedsDebt(); |
| 16 | + error CollateralRatioMaintained(); |
| 17 | + |
| 18 | + IERC20 tokenA; |
| 19 | + IERC20 tokenB; |
| 20 | + LPToken lpToken; |
| 21 | + |
| 22 | + uint256 public constant COLLATERAL_RATIO = 150; |
| 23 | + uint256 public constant BASE_INTEREST_RATE = 5; |
| 24 | + |
| 25 | + uint256 public totalA; |
| 26 | + uint256 public totalB; |
| 27 | + uint256 public totalLPTokens; |
| 28 | + |
| 29 | + struct User{ |
| 30 | + bool isLender; |
| 31 | + uint256 depositedA; |
| 32 | + uint256 borrowedB; |
| 33 | + uint256 collateral; |
| 34 | + uint256 lastTransactionTime; |
| 35 | + } |
| 36 | + |
| 37 | + mapping(address => User) public users; |
| 38 | + |
| 39 | + event UserRegistered(address indexed user,bool isLender); |
| 40 | + event Deposited(address indexed user, uint256 amount); |
| 41 | + event Withdrawn(address indexed user, uint256 amount); |
| 42 | + event DepositedCollateral(address indexed user, uint256 amount); |
| 43 | + event Borrowed(address indexed user, uint256 amount); |
| 44 | + event Repaid(address indexed user, uint256 amount); |
| 45 | + |
| 46 | + modifier onlyLender() { |
| 47 | + require(users[msg.sender].isLender, "Not a lender"); |
| 48 | + _; |
| 49 | + } |
| 50 | + |
| 51 | + modifier onlyBorrower() { |
| 52 | + require(!users[msg.sender].isLender, "Not a borrower"); |
| 53 | + _; |
| 54 | + } |
| 55 | + |
| 56 | + constructor(address _tokenA,address _tokenB){ |
| 57 | + tokenA = IERC20(_tokenA); |
| 58 | + tokenB = IERC20(_tokenB); |
| 59 | + lpToken = new LPToken("LP Token","LPT"); |
| 60 | + } |
| 61 | + |
| 62 | + function registerUser(address user,bool isLender){ |
| 63 | + if(users[user].depositedA>0 || users[user].collateral>0){ |
| 64 | + revert UserAlreadyRegistered(); |
| 65 | + } |
| 66 | + users[user].isLender = isLender; |
| 67 | + emit UserRegistered(user,isLender); |
| 68 | + } |
| 69 | + |
| 70 | + //Lender Functions |
| 71 | + function depositA(address user,uint256 amount) external onlyLender{ |
| 72 | + if(amount<0){revert AmountMustBePositive();} |
| 73 | + tokenA.transferFrom(user,address(this),amount); |
| 74 | + users[user].depositedA += amount; |
| 75 | + totalA += amount; |
| 76 | + |
| 77 | + uint256 lpTokens = amount*totalLPTokens/totalA; |
| 78 | + lpToken.mint(user,lpTokens); |
| 79 | + totalLPTokens += lpTokens; |
| 80 | + |
| 81 | + emit Deposited(user,amount); |
| 82 | + |
9 | 83 | } |
10 | 84 |
|
11 | | - function increment() public { |
12 | | - number++; |
| 85 | + function withdrawA(address user,uint256 amount) external onlyLender{ |
| 86 | + if(users[user].depositedA<amount){revert InsufficientBalance();} |
| 87 | + |
| 88 | + uint256 lpTokens = amount*totalLPTokens/totalA; |
| 89 | + lpToken.burn(user,lpTokens); |
| 90 | + totalLPTokens -= lpTokens; |
| 91 | + |
| 92 | + uint256 interest=calcIntersert(user); |
| 93 | + totalA-=amount; |
| 94 | + users[user].depositedA -= amount; |
| 95 | + tokenA.transfer(user,amount+interest); |
| 96 | + emit Withdrawn(user,amount); |
| 97 | + } |
| 98 | + |
| 99 | + function calcIntersert(address user){ |
| 100 | + uint256 timePassed = block.timestamp - users[user].lastTransactionTime; |
| 101 | + uint256 interest = users[user].depositedA*BASE_INTEREST_RATE*timePassed/100; |
| 102 | + return interest; |
| 103 | + } |
| 104 | + |
| 105 | + //Borrower Functions |
| 106 | + function depositCollateral(address user,uint256 amount) external onlyBorrower{ |
| 107 | + if(amount<0){revert AmountMustBePositive();} |
| 108 | + tokenA.transferFrom(user,address(this),amount); |
| 109 | + users[user].collateral += amount; |
| 110 | + totalA += amount; |
| 111 | + emit Deposited(user,amount); |
13 | 112 | } |
| 113 | + |
| 114 | + function borrowB(address user,uint256 amount) external onlyBorrower{ |
| 115 | + uint256 maxBorrow = users[user].collateral*100/COLLATERAL_RATIO; |
| 116 | + if(amount>maxBorrow){revert InsufficientCollateral();} |
| 117 | + tokenB.transfer(user,amount); |
| 118 | + users[user].borrowedB += amount; |
| 119 | + totalB += amount; |
| 120 | + users[user].lastTransactionTime = block.timestamp; |
| 121 | + emit Borrowed(user,amount); |
| 122 | + } |
| 123 | + |
| 124 | + function repayB(address user,uint256 amount)external onlyBorrower{ |
| 125 | + if(users[user].borrowedB<amount){revert ExceedsDebt();} |
| 126 | + uint256 interest=calcIntersert(user); |
| 127 | + tokenB.transferFrom(user,address(this),amount+interest); |
| 128 | + users[user].borrowedB -= amount; |
| 129 | + |
| 130 | + if(users[user].borrowedB==0){ |
| 131 | + tokenA.transfer(user,users[user].collateral); |
| 132 | + totalA -= users[user].collateral; |
| 133 | + users[user].collateral = 0; |
| 134 | + } |
| 135 | + |
| 136 | + emit Repaid(user,amount); |
| 137 | + } |
| 138 | + |
| 139 | + function liquidate(address user){ |
| 140 | + if(users[user].collateral<users[user].borrowedB*100/COLLATERAL_RATIO){ |
| 141 | + revert CollateralRatioMaintained(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
14 | 148 | } |
0 commit comments