Created
April 18, 2020 12:19
-
-
Save perfectmak/d32bad707d0b0c2caee6f257f2368f70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.6.1; | |
interface MoneyToken { | |
function transferFrom(address _from, address _to, uint256 _value) external returns (bool success); | |
} | |
contract Loan { | |
mapping(address => uint256) public loanRegistry; | |
address owner; | |
address public ngntTokenAddress; | |
constructor(address ownerAddress, address erc20TokenAddress) public { | |
owner = ownerAddress; | |
ngntTokenAddress = erc20TokenAddress; | |
} | |
function sendLoan(uint256 amount, address payable recipientsAddress) external { | |
// ensure we are not loaning negative value | |
require(amount != 0, "Loaning zero value wrong"); | |
require(msg.sender == owner, "You are not owner"); | |
// if we have loaned recipient previously update the loanRegistry | |
uint256 currentValue = loanRegistry[recipientsAddress]; | |
loanRegistry[recipientsAddress] = currentValue + amount; | |
// transfer actual money token | |
MoneyToken ngntToken = MoneyToken(ngntTokenAddress); | |
require(ngntToken.transferFrom(owner, recipientsAddress, amount), "Failed to transfer loan to receipient"); | |
} | |
function repayLoan(uint256 amount) external { | |
// check amount is not zero | |
require(amount != 0, "Zero repayment not allowed"); | |
// update loan registry | |
uint256 currentValue = loanRegistry[msg.sender]; | |
require(currentValue > amount, 'You are repaying too much'); | |
loanRegistry[msg.sender] = currentValue - amount; | |
// transfer money from sender to owners account | |
MoneyToken ngntToken = MoneyToken(ngntTokenAddress); | |
require(ngntToken.transferFrom(msg.sender, owner, amount), "Failed to transfer repayment"); | |
// above will fail if msg.sender has not called | |
// ngntToken.approve(address(this), amount); | |
} | |
function sendLoanEth(address payable recipientsAddress) payable external { | |
// ensure we are not loaning negative value | |
require(msg.value != 0, "Loaning zero value wrong"); | |
require(msg.sender == owner, "You are not owner"); | |
// if we have loaned recipient previously update the loanRegistry | |
uint256 currentValue = loanRegistry[recipientsAddress]; | |
loanRegistry[recipientsAddress] = currentValue + msg.value; | |
// transfer actual eth token | |
recipientsAddress.transfer(msg.value); | |
} | |
function repayLoanEth() payable external { | |
// check amount is not zero | |
require(msg.value != 0, "Zero repayment not allowed"); | |
// update loan registry | |
uint256 currentValue = loanRegistry[msg.sender]; | |
require(currentValue > msg.value, 'You are repaying too much'); | |
loanRegistry[msg.sender] = currentValue - msg.value; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment