-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyTransfers.sol
More file actions
39 lines (32 loc) · 1.37 KB
/
MoneyTransfers.sol
File metadata and controls
39 lines (32 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
/**
* Money Transfer Smart Contract
* This contracts allows a grantor to transfer money with time maturity to one or more beneficiaries
*/
contract MonetaryTransfer {
struct Beneficiary {
uint256 amount;
uint256 maturity;
bool paid;
}
mapping(address => Beneficiary) public beneficiaries;
address public grantor;
constructor() {
grantor = msg.sender;
}
function addBeneficiary(address beneficiary, uint256 timeToMaturity) external payable {
require(msg.sender == grantor, "Only the grantor can set up the beneficiary");
require(beneficiaries[msg.sender].amount == 0, "The beneficiary already exists");
beneficiaries[beneficiary] = Beneficiary(msg.value, block.timestamp + timeToMaturity, false);
}
uint256 public maturity;
function withdraw() external {
Beneficiary storage beneficiary = beneficiaries[msg.sender];
require(beneficiary.maturity <= block.timestamp, "The maturity time has not yet expired");
require(beneficiary.amount > 0, "Only the beneficiary can withdrawn the funds");
require(beneficiary.paid == false, "The funds have been already withdrawn");
beneficiary.paid = true;
payable(msg.sender).transfer(beneficiary.amount);
}
}