From 403c7d0792ad44cc5494a50063cf9c135593baa2 Mon Sep 17 00:00:00 2001 From: Kapten boneng Date: Mon, 28 Oct 2024 19:12:41 +0700 Subject: [PATCH] Create Smart Contract untuk Lending and Borrowing Deskripsi Kontrak Lending (Pemberian Pinjaman): Pemberi pinjaman dapat membuat pinjaman dengan menetapkan jumlah, suku bunga, dan tanggal jatuh tempo. Pinjaman dibuat dengan menyetorkan Ether sebagai kolateral. Borrowing (Pengambilan Pinjaman): Peminjam dapat meminjam Ether dari kontrak tersebut dan akan membayar kembali dengan bunga sebelum tanggal jatuh tempo. Repayment (Pembayaran): Peminjam harus mengembalikan jumlah pinjaman dengan bunga sebelum tanggal jatuh tempo. Jika berhasil, dana akan dikirimkan kembali ke pemberi pinjaman. --- Smart Contract untuk Lending and Borrowing | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Smart Contract untuk Lending and Borrowing diff --git a/Smart Contract untuk Lending and Borrowing b/Smart Contract untuk Lending and Borrowing new file mode 100644 index 0000000..d8442b2 --- /dev/null +++ b/Smart Contract untuk Lending and Borrowing @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract LendingBorrowing { + struct Loan { + uint256 amount; + uint256 interestRate; + uint256 dueDate; + address lender; + address borrower; + bool repaid; + } + + mapping(uint256 => Loan) public loans; + uint256 public loanCounter = 0; + + event LoanCreated(uint256 loanId, address indexed lender, address indexed borrower, uint256 amount, uint256 interestRate, uint256 dueDate); + event LoanRepaid(uint256 loanId, address indexed borrower, uint256 amount); + + function createLoan(address _borrower, uint256 _amount, uint256 _interestRate, uint256 _dueDate) public payable { + require(msg.value == _amount, "Lender must deposit the correct amount"); + + loanCounter++; + loans[loanCounter] = Loan({ + amount: _amount, + interestRate: _interestRate, + dueDate: _dueDate, + lender: msg.sender, + borrower: _borrower, + repaid: false + }); + + emit LoanCreated(loanCounter, msg.sender, _borrower, _amount, _interestRate, _dueDate); + } + + function repayLoan(uint256 _loanId) public payable { + Loan storage loan = loans[_loanId]; + require(msg.sender == loan.borrower, "Only the borrower can repay the loan"); + require(!loan.repaid, "Loan already repaid"); + require(block.timestamp <= loan.dueDate, "Loan is overdue"); + + uint256 repaymentAmount = loan.amount + (loan.amount * loan.interestRate / 100); + require(msg.value == repaymentAmount, "Incorrect repayment amount"); + + loan.repaid = true; + payable(loan.lender).transfer(msg.value); + + emit LoanRepaid(_loanId, msg.sender, msg.value); + } + + function getLoanDetails(uint256 _loanId) public view returns (uint256, uint256, uint256, address, address, bool) { + Loan memory loan = loans[_loanId]; + return (loan.amount, loan.interestRate, loan.dueDate, loan.lender, loan.borrower, loan.repaid); + } +}