Skip to content

Instantly share code, notes, and snippets.

@Fishbiscuit
Created October 22, 2021 08:31
Show Gist options
  • Save Fishbiscuit/b9eb10e3b27754003cae80323b1d68ac to your computer and use it in GitHub Desktop.
Save Fishbiscuit/b9eb10e3b27754003cae80323b1d68ac to your computer and use it in GitHub Desktop.
TrustFund code
pragma solidity ^0.5.9;
// Adding only the ERC-20 function we need
interface DaiToken {
function transfer(address dst, uint wad) external returns (bool);
function balanceOf(address guy) external view returns (uint);
}
contract owned {
DaiToken daitoken;
address owner;
constructor() public{
owner = msg.sender;
daitoken = DaiToken(0xc7AD46e0b8a400Bb3C915120d284AafbA8fc4735);
}
modifier onlyOwner {
require(msg.sender == owner,
"Only the trust fund owner can call this function");
_;
}
}
contract mortal is owned {
// Only owner can shutdown this contract.
// all funds will be sent back to owner
function destroy() public onlyOwner {
daitoken.transfer(owner, daitoken.balanceOf(address(this)));
selfdestruct(msg.sender);
}
}
contract TrustFund is mortal{
address public borrower;
uint256 public collateralAmount;
uint256 public payoffAmount;
uint256 public dueDate;
event Withdrawal(address indexed to, uint amount);
event Deposit(address indexed from, uint amount);
event LoanPaid(address borrower);
// Withdraw amount from Trust fund anytime
function withdraw(uint withdraw_amount) public onlyOwner{
// Limit withdrawal amount
require(daitoken.balanceOf(address(this)) >= withdraw_amount,
"Insufficient balance in trust fund for withdrawal request");
// Send the amount to the address that requested it
daitoken.transfer(msg.sender, withdraw_amount);
emit Withdrawal(msg.sender, withdraw_amount);
}
// Accept any incoming amount
function () external payable {
emit Deposit(msg.sender, msg.value);
}
function Loan(address _borrower, uint256 _collateralAmount, uint256 _payoffAmount, uint256 loanDuration) public
{
borrower = _borrower;
collateralAmount = _collateralAmount;
daitoken.transfer(owner, collateralAmount);
payoffAmount = _payoffAmount;
dueDate = now + loanDuration;
daitoken.transfer(borrower, _payoffAmount);
}
function payLoan() public {
require(msg.sender == borrower);
require(now <= dueDate);
require(daitoken.transfer(borrower, collateralAmount));
emit LoanPaid(msg.sender);
}
function repossess() public {
require(now > dueDate);
require(daitoken.transfer(owner, collateralAmount));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment