Debt Consolidation Smart Contract
What it does:
A smart contract that aggregates multiple outstanding debts into a single consolidated obligation with unified repayment terms.
Why it matters:
Managing many debts is stressful, error-prone, and expensive. This contract simplifies repayment, improves transparency, and reduces default risk by enforcing one clear on-chain repayment plan.
How it works:
-
A borrower registers multiple existing debts.
-
Lenders fund a consolidated loan that pays off all registered debts.
-
The borrower receives one unified debt with clear terms.
-
Repayments are made to a single smart contract.
-
Funds are automatically distributed to lenders until fully repaid.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/**
* @title DebtConsolidation
* @author Nam
* @notice Consolidates multiple debts into a single on-chain obligation
*/
contract DebtConsolidation {
// -------------------- DATA STRUCTURES --------------------
struct Debt {
address creditor;
uint256 amount;
bool settled;
}
struct ConsolidatedLoan {
address borrower;
uint256 totalDebt;
uint256 interest; // flat interest
uint256 totalRepaid;
bool active;
}
Debt[] public debts;
ConsolidatedLoan public loan;
address public lender;
// -------------------- EVENTS --------------------
event DebtRegistered(address indexed creditor, uint256 amount);
event LoanFunded(address indexed lender, uint256 amount);
event DebtSettled(address indexed creditor, uint256 amount);
event Repayment(address indexed borrower, uint256 amount);
event LoanCompleted();
// -------------------- MODIFIERS --------------------
modifier onlyBorrower() {
require(msg.sender == loan.borrower, "Not borrower");
_;
}
modifier onlyLender() {
require(msg.sender == lender, "Not lender");
_;
}
modifier loanActive() {
require(loan.active, "Loan inactive");
_;
}
// -------------------- CONSTRUCTOR --------------------
constructor(address _borrower, uint256 _interest) {
require(_borrower != address(0), "Invalid borrower");
loan.borrower = _borrower;
loan.interest = _interest;
}
// -------------------- DEBT REGISTRATION --------------------
/**
* @notice Register an existing debt
*/
function registerDebt(address _creditor, uint256 _amount)
external
onlyBorrower
{
require(_amount > 0, "Invalid amount");
debts.push(
Debt({
creditor: _creditor,
amount: _amount,
settled: false
})
);
loan.totalDebt += _amount;
emit DebtRegistered(_creditor, _amount);
}
// -------------------- CONSOLIDATION LOGIC --------------------
/**
* @notice Fund consolidated loan and settle all debts
*/
function fundLoan() external payable {
require(!loan.active, "Loan already active");
require(msg.value == loan.totalDebt, "Incorrect funding");
lender = msg.sender;
loan.active = true;
emit LoanFunded(msg.sender, msg.value);
// Pay creditors directly
for (uint256 i = 0; i 0, "Zero repayment");
loan.totalRepaid += msg.value;
payable(lender).transfer(msg.value);
emit Repayment(msg.sender, msg.value);
if (loan.totalRepaid >= loan.totalDebt + loan.interest) {
loan.active = false;
emit LoanCompleted();
}
}
// -------------------- VIEW FUNCTIONS --------------------
/**
* @notice Get registered debt count
*/
function debtCount() external view returns (uint256) {
return debts.length;
}
/**
* @notice Get consolidated loan status
*/
function getLoanStatus()
external
view
returns (
address borrower,
uint256 totalDebt,
uint256 interest,
uint256 totalRepaid,
bool active
)
{
return (
loan.borrower,
loan.totalDebt,
loan.interest,
loan.totalRepaid,
loan.active
);
}
}