-
Notifications
You must be signed in to change notification settings - Fork 2
/
PaymentProcess.sol
104 lines (89 loc) · 3.32 KB
/
PaymentProcess.sol
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
contract PaymentProcess{
mapping(address=>uint)public contributors;
address public manager;
uint public minContribution;
uint public deadline;
uint public targetprice;
uint public raisedAmt;
uint public noOfContributors;
string public proposal;
string public officialName;
struct Request{
string description;
address payable recipient;
uint value;
bool completed;
uint noOfFVoters;
mapping(address=>bool)voters;
}
mapping (uint=>Request) public requests;
uint public ReqId;
enum State {Created, Voting, Ended}
State public state;
constructor(string memory _officialName, uint _targetprice,uint _deadline, string memory _proposal){
officialName=_officialName;
targetprice=_targetprice;
deadline=block.timestamp+_deadline; // 10sec+3600sec(60*60)
minContribution = _targetprice / 100 ;
manager=msg.sender;
proposal = _proposal;
state = State.Created;
}
modifier inState(State _state){
require (state == _state);
_;
}
function fundEth()public payable inState(State.Created){
require(block.timestamp<deadline,"Deadline has been passed");
require(msg.value>=minContribution,"Minimum Contribution is not met");
if(contributors[msg.sender]==0){
noOfContributors ++;
}
contributors[msg.sender]+=msg.value;
raisedAmt+=msg.value;
}
function getTotalBalance() public view inState(State.Created) returns (uint){
return address(this).balance;
}
function refund() public inState(State.Ended){
require(block.timestamp>deadline && raisedAmt<targetprice, "you are not eligible for refund");
require(contributors[msg.sender]>0);
address payable user= payable (msg.sender);
user.transfer(contributors[msg.sender]);
contributors[msg.sender]=0;
}
modifier byManagerOnly(){
require(msg.sender==manager,"Manager function only");
_;
}
modifier bySenderOnly(address payable recipient) {
require(msg.sender != recipient, 'Sender and recipient cannot be the same.');
_;
}
function createRequest(string memory _description, address payable _recipient, uint _value) public byManagerOnly{
Request storage newRequest = requests[ReqId];
ReqId++;
newRequest.description=_description;
newRequest.recipient=_recipient;
newRequest.value=_value;
newRequest.completed=false;
newRequest.noOfFVoters=0;
}
function voteRequest(uint _requestId)public{
require(contributors[msg.sender]>0,"You must be contributor");
Request storage thisRequest=requests[_requestId];
require(thisRequest.voters[msg.sender]==false,"You have already voted");
thisRequest.voters[msg.sender]=true;
thisRequest.noOfFVoters++;
}
function makePayment(uint _requestId)public byManagerOnly{
require(raisedAmt>=targetprice);
Request storage thisRequest=requests[_requestId];
require(thisRequest.completed==false,"The request has been completed");
require(thisRequest.noOfFVoters > noOfContributors/2,"Majority does not support");
thisRequest.recipient.transfer(thisRequest.value);
thisRequest.completed=true;
}
}