-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement simple lending protocol smart contract
- Loading branch information
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
broadcast/SimpleLendingProtocol.s.sol/4202/run-1728039577.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"transactions": [ | ||
{ | ||
"hash": "0x0dc452f6a0443be91c8167b6080c424cfc19c6b0c4e2fa544997dc30d11716ad", | ||
"transactionType": "CREATE", | ||
"contractName": "SimpleLendingProtocol", | ||
"contractAddress": "0xf669aeb8401ed77ed8834f95194252e01ff62023", | ||
"function": null, | ||
"arguments": null, | ||
"transaction": { | ||
"from": "0xdab8892c07fb4c362dd99d9a2fbff8b555d39cb5", | ||
"gas": "0xe64fe", | ||
"value": "0x0", | ||
"input": "0x6080604052600a60025562278d006003556096600455348015602057600080fd5b50610af6806100306000396000f3fe6080604052600436106100a75760003560e01c8063b4eae1cb11610064578063b4eae1cb146101d8578063d0e30db0146101ee578063e8a7da8e146101f6578063f5521bf11461025e578063f966ade714610274578063fc7e286d1461027c57600080fd5b80632e1a7d4d146100ac57806373b4086b146100ce5780637c3a00fd1461015f5780637f596208146101835780638c803fc2146101a557806391b0fcc2146101b8575b600080fd5b3480156100b857600080fd5b506100cc6100c73660046109fc565b6102a9565b005b3480156100da57600080fd5b506101266100e9366004610a15565b6001602081905260009182526040909120805491810154600282015460038301546004909301546001600160a01b03909416939192909160ff1685565b604080516001600160a01b03909616865260208601949094529284019190915260608301521515608082015260a0015b60405180910390f35b34801561016b57600080fd5b5061017560025481565b604051908152602001610156565b34801561018f57600080fd5b5033600090815260208190526040902054610175565b6100cc6101b33660046109fc565b61038f565b3480156101c457600080fd5b506100cc6101d3366004610a15565b61055d565b3480156101e457600080fd5b5061017560045481565b6100cc6106fb565b34801561020257600080fd5b50610216610211366004610a15565b6107b4565b604051610156919081516001600160a01b0316815260208083015190820152604080830151908201526060808301519082015260809182015115159181019190915260a00190565b34801561026a57600080fd5b5061017560035481565b6100cc61084d565b34801561028857600080fd5b50610175610297366004610a15565b60006020819052908152604090205481565b336000908152602081905260409020548111156103045760405162461bcd60e51b8152602060048201526014602482015273496e73756666696369656e742062616c616e636560601b60448201526064015b60405180910390fd5b3360009081526020819052604081208054839290610323908490610a5b565b9091555050604051339082156108fc029083906000818181858888f19350505050158015610355573d6000803e3d6000fd5b5060405181815233907f884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364906020015b60405180910390a250565b60646004548261039f9190610a74565b6103a99190610a8b565b3410156103f85760405162461bcd60e51b815260206004820152601760248201527f496e73756666696369656e7420636f6c6c61746572616c00000000000000000060448201526064016102fb565b3360009081526001602081905260409091200154156104595760405162461bcd60e51b815260206004820152601c60248201527f4578697374696e67206c6f616e206d757374206265207265706169640000000060448201526064016102fb565b6040518060a00160405280336001600160a01b031681526020018281526020013481526020016003544261048d9190610aad565b8152600060209182018190523380825260018084526040808420865181546001600160a01b0319166001600160a01b039091161781559486015191850191909155848101516002850155606085015160038501556080909401516004909301805460ff191693151593909317909255915190916108fc841502918491818181858888f19350505050158015610526573d6000803e3d6000fd5b506040805182815234602082015233917e6b9bb4766cf5ab74b890df959e4fad0e54b1c3885e49c9541d36253a6e39e69101610384565b6001600160a01b0381166000908152600160208190526040909120908101546105b95760405162461bcd60e51b815260206004820152600e60248201526d27379030b1ba34bb32903637b0b760911b60448201526064016102fb565b806003015442116106025760405162461bcd60e51b81526020600482015260136024820152724c6f616e206973206e6f74206f76657264756560681b60448201526064016102fb565b600481015460ff161561064d5760405162461bcd60e51b8152602060048201526013602482015272131bd85b88185b1c9958591e481c995c185a59606a1b60448201526064016102fb565b60028101543060009081526020819052604081208054909190610671908490610aad565b90915550506001600160a01b038216600081815260016020818152604080842080546001600160a01b0319168155928301849055600280840185905560038401949094556004909201805460ff191690559184015490519081527ffa149b6f966139cc74dfaf2610f7b54d8823dbeed646b7da48d8144f510c3f2191015b60405180910390a25050565b600034116107595760405162461bcd60e51b815260206004820152602560248201527f4465706f73697420616d6f756e74206d75737420626520677265617465722074604482015264068616e20360dc1b60648201526084016102fb565b3360009081526020819052604081208054349290610778908490610aad565b909155505060405134815233907fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c9060200160405180910390a2565b6107f16040518060a0016040528060006001600160a01b031681526020016000815260200160008152602001600081526020016000151581525090565b506001600160a01b03908116600090815260016020818152604092839020835160a081018552815490951685529182015490840152600281015491830191909152600381015460608301526004015460ff161515608082015290565b336000908152600160208190526040909120908101546108a05760405162461bcd60e51b815260206004820152600e60248201526d27379030b1ba34bb32903637b0b760911b60448201526064016102fb565b80600301544211156108e65760405162461bcd60e51b815260206004820152600f60248201526e4c6f616e206973206f76657264756560881b60448201526064016102fb565b6000606460025483600101546108fc9190610a74565b6109069190610a8b565b82600101546109159190610aad565b90508034146109665760405162461bcd60e51b815260206004820152601a60248201527f496e636f72726563742072657061796d656e7420616d6f756e7400000000000060448201526064016102fb565b60048201805460ff191660011790553060009081526020819052604081208054839290610994908490610aad565b90915550506002820154604051339180156108fc02916000818181858888f193505050501580156109c9573d6000803e3d6000fd5b5060405181815233907fc200a1f31dd659e356e0f112c82558e25f49f7b0f84438691cd96f5cb3558823906020016106ef565b600060208284031215610a0e57600080fd5b5035919050565b600060208284031215610a2757600080fd5b81356001600160a01b0381168114610a3e57600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b81810381811115610a6e57610a6e610a45565b92915050565b8082028115828204841417610a6e57610a6e610a45565b600082610aa857634e487b7160e01b600052601260045260246000fd5b500490565b80820180821115610a6e57610a6e610a4556fea26469706673582212209306963b76018b68cfef11db1c3e29df1328861a807ffe40e2d836b99825937f64736f6c634300081a0033", | ||
"nonce": "0x19", | ||
"chainId": "0x106a" | ||
}, | ||
"additionalContracts": [], | ||
"isFixedGasLimit": false | ||
} | ||
], | ||
"receipts": [ | ||
{ | ||
"status": "0x1", | ||
"cumulativeGasUsed": "0xbbe8f", | ||
"logs": [], | ||
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
"type": "0x2", | ||
"transactionHash": "0x0dc452f6a0443be91c8167b6080c424cfc19c6b0c4e2fa544997dc30d11716ad", | ||
"transactionIndex": "0x1", | ||
"blockHash": "0xf2fb624d8f889b823d55a6995548a065652e3959a53f91236521ca5f7403c6f9", | ||
"blockNumber": "0xad63da", | ||
"gasUsed": "0xb1350", | ||
"effectiveGasPrice": "0xf433d", | ||
"from": "0xdab8892c07fb4c362dd99d9a2fbff8b555d39cb5", | ||
"to": null, | ||
"contractAddress": "0xf669aeb8401ed77ed8834f95194252e01ff62023", | ||
"l1BaseFeeScalar": "0x2af8", | ||
"l1BlobBaseFee": "0x1", | ||
"l1BlobBaseFeeScalar": "0x109618", | ||
"l1Fee": "0x5be7b440dc98", | ||
"l1GasPrice": "0x4e0739f173", | ||
"l1GasUsed": "0x6b13" | ||
} | ||
], | ||
"libraries": [], | ||
"pending": [], | ||
"returns": {}, | ||
"timestamp": 1728039577, | ||
"chain": 4202, | ||
"commit": "56079c6" | ||
} |
52 changes: 52 additions & 0 deletions
52
broadcast/SimpleLendingProtocol.s.sol/4202/run-latest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"transactions": [ | ||
{ | ||
"hash": "0x0dc452f6a0443be91c8167b6080c424cfc19c6b0c4e2fa544997dc30d11716ad", | ||
"transactionType": "CREATE", | ||
"contractName": "SimpleLendingProtocol", | ||
"contractAddress": "0xf669aeb8401ed77ed8834f95194252e01ff62023", | ||
"function": null, | ||
"arguments": null, | ||
"transaction": { | ||
"from": "0xdab8892c07fb4c362dd99d9a2fbff8b555d39cb5", | ||
"gas": "0xe64fe", | ||
"value": "0x0", | ||
"input": "0x6080604052600a60025562278d006003556096600455348015602057600080fd5b50610af6806100306000396000f3fe6080604052600436106100a75760003560e01c8063b4eae1cb11610064578063b4eae1cb146101d8578063d0e30db0146101ee578063e8a7da8e146101f6578063f5521bf11461025e578063f966ade714610274578063fc7e286d1461027c57600080fd5b80632e1a7d4d146100ac57806373b4086b146100ce5780637c3a00fd1461015f5780637f596208146101835780638c803fc2146101a557806391b0fcc2146101b8575b600080fd5b3480156100b857600080fd5b506100cc6100c73660046109fc565b6102a9565b005b3480156100da57600080fd5b506101266100e9366004610a15565b6001602081905260009182526040909120805491810154600282015460038301546004909301546001600160a01b03909416939192909160ff1685565b604080516001600160a01b03909616865260208601949094529284019190915260608301521515608082015260a0015b60405180910390f35b34801561016b57600080fd5b5061017560025481565b604051908152602001610156565b34801561018f57600080fd5b5033600090815260208190526040902054610175565b6100cc6101b33660046109fc565b61038f565b3480156101c457600080fd5b506100cc6101d3366004610a15565b61055d565b3480156101e457600080fd5b5061017560045481565b6100cc6106fb565b34801561020257600080fd5b50610216610211366004610a15565b6107b4565b604051610156919081516001600160a01b0316815260208083015190820152604080830151908201526060808301519082015260809182015115159181019190915260a00190565b34801561026a57600080fd5b5061017560035481565b6100cc61084d565b34801561028857600080fd5b50610175610297366004610a15565b60006020819052908152604090205481565b336000908152602081905260409020548111156103045760405162461bcd60e51b8152602060048201526014602482015273496e73756666696369656e742062616c616e636560601b60448201526064015b60405180910390fd5b3360009081526020819052604081208054839290610323908490610a5b565b9091555050604051339082156108fc029083906000818181858888f19350505050158015610355573d6000803e3d6000fd5b5060405181815233907f884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364906020015b60405180910390a250565b60646004548261039f9190610a74565b6103a99190610a8b565b3410156103f85760405162461bcd60e51b815260206004820152601760248201527f496e73756666696369656e7420636f6c6c61746572616c00000000000000000060448201526064016102fb565b3360009081526001602081905260409091200154156104595760405162461bcd60e51b815260206004820152601c60248201527f4578697374696e67206c6f616e206d757374206265207265706169640000000060448201526064016102fb565b6040518060a00160405280336001600160a01b031681526020018281526020013481526020016003544261048d9190610aad565b8152600060209182018190523380825260018084526040808420865181546001600160a01b0319166001600160a01b039091161781559486015191850191909155848101516002850155606085015160038501556080909401516004909301805460ff191693151593909317909255915190916108fc841502918491818181858888f19350505050158015610526573d6000803e3d6000fd5b506040805182815234602082015233917e6b9bb4766cf5ab74b890df959e4fad0e54b1c3885e49c9541d36253a6e39e69101610384565b6001600160a01b0381166000908152600160208190526040909120908101546105b95760405162461bcd60e51b815260206004820152600e60248201526d27379030b1ba34bb32903637b0b760911b60448201526064016102fb565b806003015442116106025760405162461bcd60e51b81526020600482015260136024820152724c6f616e206973206e6f74206f76657264756560681b60448201526064016102fb565b600481015460ff161561064d5760405162461bcd60e51b8152602060048201526013602482015272131bd85b88185b1c9958591e481c995c185a59606a1b60448201526064016102fb565b60028101543060009081526020819052604081208054909190610671908490610aad565b90915550506001600160a01b038216600081815260016020818152604080842080546001600160a01b0319168155928301849055600280840185905560038401949094556004909201805460ff191690559184015490519081527ffa149b6f966139cc74dfaf2610f7b54d8823dbeed646b7da48d8144f510c3f2191015b60405180910390a25050565b600034116107595760405162461bcd60e51b815260206004820152602560248201527f4465706f73697420616d6f756e74206d75737420626520677265617465722074604482015264068616e20360dc1b60648201526084016102fb565b3360009081526020819052604081208054349290610778908490610aad565b909155505060405134815233907fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c9060200160405180910390a2565b6107f16040518060a0016040528060006001600160a01b031681526020016000815260200160008152602001600081526020016000151581525090565b506001600160a01b03908116600090815260016020818152604092839020835160a081018552815490951685529182015490840152600281015491830191909152600381015460608301526004015460ff161515608082015290565b336000908152600160208190526040909120908101546108a05760405162461bcd60e51b815260206004820152600e60248201526d27379030b1ba34bb32903637b0b760911b60448201526064016102fb565b80600301544211156108e65760405162461bcd60e51b815260206004820152600f60248201526e4c6f616e206973206f76657264756560881b60448201526064016102fb565b6000606460025483600101546108fc9190610a74565b6109069190610a8b565b82600101546109159190610aad565b90508034146109665760405162461bcd60e51b815260206004820152601a60248201527f496e636f72726563742072657061796d656e7420616d6f756e7400000000000060448201526064016102fb565b60048201805460ff191660011790553060009081526020819052604081208054839290610994908490610aad565b90915550506002820154604051339180156108fc02916000818181858888f193505050501580156109c9573d6000803e3d6000fd5b5060405181815233907fc200a1f31dd659e356e0f112c82558e25f49f7b0f84438691cd96f5cb3558823906020016106ef565b600060208284031215610a0e57600080fd5b5035919050565b600060208284031215610a2757600080fd5b81356001600160a01b0381168114610a3e57600080fd5b9392505050565b634e487b7160e01b600052601160045260246000fd5b81810381811115610a6e57610a6e610a45565b92915050565b8082028115828204841417610a6e57610a6e610a45565b600082610aa857634e487b7160e01b600052601260045260246000fd5b500490565b80820180821115610a6e57610a6e610a4556fea26469706673582212209306963b76018b68cfef11db1c3e29df1328861a807ffe40e2d836b99825937f64736f6c634300081a0033", | ||
"nonce": "0x19", | ||
"chainId": "0x106a" | ||
}, | ||
"additionalContracts": [], | ||
"isFixedGasLimit": false | ||
} | ||
], | ||
"receipts": [ | ||
{ | ||
"status": "0x1", | ||
"cumulativeGasUsed": "0xbbe8f", | ||
"logs": [], | ||
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
"type": "0x2", | ||
"transactionHash": "0x0dc452f6a0443be91c8167b6080c424cfc19c6b0c4e2fa544997dc30d11716ad", | ||
"transactionIndex": "0x1", | ||
"blockHash": "0xf2fb624d8f889b823d55a6995548a065652e3959a53f91236521ca5f7403c6f9", | ||
"blockNumber": "0xad63da", | ||
"gasUsed": "0xb1350", | ||
"effectiveGasPrice": "0xf433d", | ||
"from": "0xdab8892c07fb4c362dd99d9a2fbff8b555d39cb5", | ||
"to": null, | ||
"contractAddress": "0xf669aeb8401ed77ed8834f95194252e01ff62023", | ||
"l1BaseFeeScalar": "0x2af8", | ||
"l1BlobBaseFee": "0x1", | ||
"l1BlobBaseFeeScalar": "0x109618", | ||
"l1Fee": "0x5be7b440dc98", | ||
"l1GasPrice": "0x4e0739f173", | ||
"l1GasUsed": "0x6b13" | ||
} | ||
], | ||
"libraries": [], | ||
"pending": [], | ||
"returns": {}, | ||
"timestamp": 1728039577, | ||
"chain": 4202, | ||
"commit": "56079c6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
import {Script, console} from "forge-std/Script.sol"; | ||
import {SimpleLendingProtocol} from "../src/SimpleLendingProtocol.sol"; | ||
|
||
contract SimpleLendingProtocolScript is Script { | ||
SimpleLendingProtocol public protocol; | ||
|
||
function setUp() public {} | ||
|
||
function run() public { | ||
vm.startBroadcast(); | ||
|
||
protocol = new SimpleLendingProtocol(); | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
contract SimpleLendingProtocol { | ||
struct Loan { | ||
address borrower; | ||
uint256 amountBorrowed; | ||
uint256 collateralAmount; | ||
uint256 dueDate; | ||
bool isRepaid; | ||
} | ||
|
||
mapping(address => uint256) public deposits; // Lender deposits | ||
mapping(address => Loan) public loans; // Active loans by borrowers | ||
|
||
uint256 public interestRate = 10; // 10% interest | ||
uint256 public loanDuration = 30 days; | ||
uint256 public collateralRatio = 150; // Borrower must provide 150% collateral | ||
|
||
event Deposit(address indexed lender, uint256 amount); | ||
event Withdraw(address indexed lender, uint256 amount); | ||
event LoanTaken(address indexed borrower, uint256 loanAmount, uint256 collateralAmount); | ||
event LoanRepaid(address indexed borrower, uint256 repaymentAmount); | ||
event CollateralLiquidated(address indexed borrower, uint256 collateralAmount); | ||
|
||
// Deposit funds as a lender | ||
function deposit() external payable { | ||
require(msg.value > 0, "Deposit amount must be greater than 0"); | ||
deposits[msg.sender] += msg.value; | ||
emit Deposit(msg.sender, msg.value); | ||
} | ||
|
||
// Withdraw funds as a lender | ||
function withdraw(uint256 amount) external { | ||
require(deposits[msg.sender] >= amount, "Insufficient balance"); | ||
deposits[msg.sender] -= amount; | ||
payable(msg.sender).transfer(amount); | ||
emit Withdraw(msg.sender, amount); | ||
} | ||
|
||
// Take a loan by providing collateral | ||
function takeLoan(uint256 loanAmount) external payable { | ||
require(msg.value >= (loanAmount * collateralRatio) / 100, "Insufficient collateral"); | ||
require(loans[msg.sender].amountBorrowed == 0, "Existing loan must be repaid"); | ||
|
||
loans[msg.sender] = Loan({ | ||
borrower: msg.sender, | ||
amountBorrowed: loanAmount, | ||
collateralAmount: msg.value, | ||
dueDate: block.timestamp + loanDuration, | ||
isRepaid: false | ||
}); | ||
|
||
payable(msg.sender).transfer(loanAmount); | ||
emit LoanTaken(msg.sender, loanAmount, msg.value); | ||
} | ||
|
||
// Repay the loan with interest | ||
function repayLoan() external payable { | ||
Loan storage loan = loans[msg.sender]; | ||
require(loan.amountBorrowed > 0, "No active loan"); | ||
require(block.timestamp <= loan.dueDate, "Loan is overdue"); | ||
|
||
uint256 repaymentAmount = loan.amountBorrowed + (loan.amountBorrowed * interestRate) / 100; | ||
require(msg.value == repaymentAmount, "Incorrect repayment amount"); | ||
|
||
loan.isRepaid = true; | ||
deposits[address(this)] += repaymentAmount; // Add interest to pool | ||
payable(msg.sender).transfer(loan.collateralAmount); // Return collateral | ||
emit LoanRepaid(msg.sender, repaymentAmount); | ||
} | ||
|
||
// Liquidate overdue loan and seize collateral | ||
function liquidateOverdueLoan(address borrower) external { | ||
Loan storage loan = loans[borrower]; | ||
require(loan.amountBorrowed > 0, "No active loan"); | ||
require(block.timestamp > loan.dueDate, "Loan is not overdue"); | ||
require(!loan.isRepaid, "Loan already repaid"); | ||
|
||
deposits[address(this)] += loan.collateralAmount; // Lenders take the collateral | ||
delete loans[borrower]; // Remove loan record | ||
emit CollateralLiquidated(borrower, loan.collateralAmount); | ||
} | ||
|
||
// Check lender balance | ||
function getLenderBalance() external view returns (uint256) { | ||
return deposits[msg.sender]; | ||
} | ||
|
||
// Check borrower loan details | ||
function getLoanDetails(address borrower) external view returns (Loan memory) { | ||
return loans[borrower]; | ||
} | ||
} |