Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
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
75 changes: 75 additions & 0 deletions academy/lending-protocol/contracts/GlobalLedger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import "@nilfoundation/smart-contracts/contracts/Nil.sol";

// @title GlobalLedger
// @dev Tracks deposits, loans, and repayments across LendingPool contracts
contract GlobalLedger {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While there already exists CollateralManager contract, why would we need this GlobalLedger Contract?

struct Deposit {
uint256 amount;
bool exists;
}

struct Loan {
uint256 amount;
bool exists;
}

mapping(address => mapping(TokenId => Deposit)) public deposits;
mapping(address => mapping(TokenId => Loan)) public loans;

event DepositRecorded(address indexed user, TokenId token, uint256 amount);
event LoanRecorded(address indexed user, TokenId token, uint256 amount);
event LoanRepaid(address indexed user, TokenId token, uint256 amount);

/// @notice Record a deposit from LendingPool
function recordDeposit(address user, TokenId token, uint256 amount) external payable {
require(amount > 0, "Invalid deposit amount");

if (!deposits[user][token].exists) {
deposits[user][token] = Deposit(amount, true);
} else {
deposits[user][token].amount += amount;
}

emit DepositRecorded(user, token, amount);
}

/// @notice Get user's deposit for a specific token
function getDeposit(address user, TokenId token) external view returns (uint256) {
return deposits[user][token].amount;
}

/// @notice Record a loan taken from a LendingPool
function recordLoan(address user, TokenId token, uint256 amount) external payable {
require(amount > 0, "Invalid loan amount");

if (!loans[user][token].exists) {
loans[user][token] = Loan(amount, true);
} else {
loans[user][token].amount += amount;
}

emit LoanRecorded(user, token, amount);
}

/// @notice Get user's outstanding loan for a specific token
function getLoan(address user, TokenId token) external view returns (uint256) {
return loans[user][token].amount;
}

/// @notice Record loan repayment and reduce outstanding balance
function repayLoan(address user, TokenId token, uint256 amount) external payable {
require(amount > 0, "Invalid repayment amount");
require(loans[user][token].exists, "No active loan");

if (loans[user][token].amount <= amount) {
delete loans[user][token];
} else {
loans[user][token].amount -= amount;
}

emit LoanRepaid(user, token, amount);
}
}
389 changes: 55 additions & 334 deletions academy/lending-protocol/contracts/LendingPool.sol

Large diffs are not rendered by default.

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

import "@nilfoundation/smart-contracts/contracts/Nil.sol";
import "./LendingPool.sol"; // Import LendingPool contract

/// @title LendingPoolFactory
/// @dev Handles deployment of LendingPool contracts across different shards
contract LendingPoolFactory {
address public globalLedger;
address public interestManager;
address public oracle;
TokenId public usdt;
TokenId public eth;
uint8 public shardCounter; // Tracks which shard to deploy to (0-3)

event LendingPoolDeployed(address pool, uint8 shardId, address owner);

constructor(address _globalLedger, address _interestManager, address _oracle, TokenId _usdt, TokenId _eth) {
globalLedger = _globalLedger;
interestManager = _interestManager;
oracle = _oracle;
usdt = _usdt;
eth = _eth;
shardCounter = 0;
}

/// @notice Deploys a new LendingPool contract to a shard
function deployLendingPool() external {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the deployment of the contract, there should be a function which calls the globalLedger to register the lending pool

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well noted Sir, I will make changes

bytes memory constructorArgs = abi.encode(
globalLedger,
interestManager,
oracle,
usdt,
eth,
msg.sender
);

// Compute full contract creation bytecode
bytes memory bytecode = bytes.concat(type(LendingPool).creationCode, constructorArgs);

// Call asyncDeploy with correct parameters
address poolAddress = Nil.asyncDeploy(
shardCounter, // Shard ID
msg.sender, // Refund to the sender
address(0), // Bounce to (set to 0 for now)
0, // Fee credit (set to 0 unless required)
0, // Forward kind (set to 0 unless forwarding behavior is needed)
0, // Value (set to 0 unless ETH needs to be sent)
bytecode, // Contract creation code + constructor args
0 // Salt (set to 0; can be changed for deterministic addresses)
);

require(poolAddress != address(0), "Deployment failed");

emit LendingPoolDeployed(poolAddress, shardCounter, msg.sender);
shardCounter = (shardCounter + 1) % 4; // Cycle through shards
}
}
23 changes: 7 additions & 16 deletions academy/lending-protocol/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,25 @@ import "@nomicfoundation/hardhat-ignition-ethers";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-ignition-ethers";
import "@typechain/hardhat";
import "@nomicfoundation/hardhat-toolbox";

import * as dotenv from "dotenv";
import type { HardhatUserConfig } from "hardhat/config";

import "./task/run-lending-protocol";

dotenv.config();
dotenv.config(); // Load .env variables

const config: HardhatUserConfig = {
ignition: {
requiredConfirmations: 1,
},
defaultNetwork: "nil",
solidity: {
version: "0.8.28", // or your desired version
settings: {
viaIR: true, // needed to compile router
optimizer: {
enabled: true,
runs: 200,
},
},
},
solidity: "0.8.28",
networks: {
nil: {
url: process.env.NIL_RPC_ENDPOINT,
url: process.env.NIL_RPC_ENDPOINT || "", // Ensure it's a string
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
},
},
};

export default config;


4 changes: 3 additions & 1 deletion academy/lending-protocol/package-lock.json
Comment thread
ukorvl marked this conversation as resolved.

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"
Comment thread
ukorvl marked this conversation as resolved.
Outdated
},
"scripts": {
"compile": "npx hardhat compile",
Expand Down
Loading