Skip to content

add permissionless lending features #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions academy/lending-protocol/contracts/LendingPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.28;
import "@nilfoundation/smart-contracts/contracts/Nil.sol";
import "@nilfoundation/smart-contracts/contracts/NilTokenBase.sol";


/// @title LendingPool
/// @dev The LendingPool contract facilitates lending and borrowing of tokens and handles collateral management.
/// It interacts with other contracts such as GlobalLedger, InterestManager, and Oracle for tracking deposits, calculating interest, and fetching token prices.
Expand Down Expand Up @@ -444,3 +445,12 @@ contract LendingPool is NilBase, NilTokenBase {
sendTokenInternal(borrower, collateralToken, collateralAmount);
}
}









76 changes: 76 additions & 0 deletions academy/lending-protocol/contracts/LendingPoolFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

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

contract GlobalLedger is Ownable {
mapping(address => bool) public registeredPools;
address public factory;

modifier onlyFactory() {
require(msg.sender == factory, "Not authorized");
_;
}

modifier onlyRegisteredPool() {
require(registeredPools[msg.sender], "Unauthorized pool");
_;
}

constructor(address _factory, address _owner) Ownable(_owner) {
factory = _factory;
}

function registerLendingPool(address pool) external onlyFactory {
registeredPools[pool] = true;
}

function recordDeposit(address user, uint256 amount) external onlyRegisteredPool {
// Logic for recording deposit
}

function recordLoan(address user, uint256 amount) external onlyRegisteredPool {
// Logic for recording loan
}
}

contract LendingPool {
address public globalLedger;
address public owner;

modifier onlyOwner() {
require(msg.sender == owner, "Not authorized");
_;
}

constructor(address _globalLedger, address _owner) {
globalLedger = _globalLedger;
owner = _owner;
}

function deposit(uint256 amount) external {
// Deposit logic
GlobalLedger(globalLedger).recordDeposit(msg.sender, amount);
}

function borrow(uint256 amount) external {
// Borrow logic
GlobalLedger(globalLedger).recordLoan(msg.sender, amount);
}
}

contract LendingPoolFactory {
address public globalLedger;
event LendingPoolDeployed(address pool, address owner);

constructor(address _globalLedger) {
globalLedger = _globalLedger;
}

function deployLendingPool() external returns (address) {
LendingPool newPool = new LendingPool(globalLedger, msg.sender);
GlobalLedger(globalLedger).registerLendingPool(address(newPool));
emit LendingPoolDeployed(address(newPool), msg.sender);
return address(newPool);
}
}
4 changes: 3 additions & 1 deletion academy/lending-protocol/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion academy/lending-protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "This is an example repository to showcase how a lending protocol can be built on top of =nil;",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox-viem": "^3.0.0",
"hardhat": "^2.22.18"
"hardhat": "^2.22.19"
},
"scripts": {
"compile": "npx hardhat compile",
Expand Down
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.