Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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);
}
}
Loading