-
Notifications
You must be signed in to change notification settings - Fork 125
/
EthlanceInvoice.sol
170 lines (129 loc) · 4.86 KB
/
EthlanceInvoice.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
pragma solidity ^0.5.0;
import "./EthlanceRegistry.sol";
import "./EthlanceWorkContract.sol";
import "./EthlanceComment.sol";
import "proxy/Forwarder.sol";
/// @title Represents a Candidate Invoice for work
contract EthlanceInvoice {
uint public constant version = 1;
EthlanceRegistry public constant registry = EthlanceRegistry(0xdaBBdABbDABbDabbDaBbDabbDaBbdaBbdaBbDAbB);
//
// Constants
//
uint public constant GUEST_TYPE = 0;
uint public constant EMPLOYER_TYPE = 1;
uint public constant CANDIDATE_TYPE = 2;
uint public constant ARBITER_TYPE = 3;
//
// Members
//
uint public invoiceIndex;
uint public dateCreated;
uint public dateUpdated;
// When datePaid is set >0, this reflects completion
uint public datePaid;
//FIXME: needs to be more flexible for other currency types
//defined in the contract.
// In Wei, the amount that needs to be paid to the candidate(employee)
uint public amountRequested;
// In Wei, the amount actually paid by the employer to the candidate(employee)
uint public amountPaid;
// The entity that constructed the contract
address public owner;
// The EthlanceWorkContract reference.
EthlanceWorkContract public workInstance;
/// @dev Forwarder Constructor
function construct(EthlanceWorkContract _workInstance,
uint _invoiceIndex,
uint _amountRequested,
string calldata metahash) external {
require(owner == address(0), "EthlanceInvoice contract already constructed.");
owner = msg.sender;
workInstance = _workInstance;
invoiceIndex = _invoiceIndex;
amountRequested = _amountRequested;
dateCreated = now;
dateUpdated = now;
// Fire off comment with provided metahash
createComment(workInstance.candidateAddress(), metahash);
// Fire off event
fireEvent("InvoiceCreated");
}
function updateDateUpdated() private {
dateUpdated = now;
}
/// @dev Pays the given invoice the the amount of `_amountPaid`.
/// @param _amountPaid The amount to pay the invoice in Wei.
function pay(uint _amountPaid) external {
require(workInstance.storeInstance().employerAddress() == msg.sender,
"Only the employer can pay an invoice.");
require(datePaid == 0, "Given invoice has already been paid.");
workInstance.payInvoice(_amountPaid);
amountPaid = _amountPaid;
datePaid = now;
updateDateUpdated();
// Fire off event
fireEvent("InvoicePaid");
}
/// @dev Returns true if the invoice has been paid.
function isInvoicePaid() external view returns(bool) {
return datePaid > 0;
}
/// @dev Fire events specific to the invoice.
/// @param event_name Unique to give the fired event
function fireEvent(string memory event_name) private {
uint[] memory event_data = new uint[](3);
event_data[0] = workInstance.jobIndex();
event_data[1] = workInstance.workIndex();
event_data[2] = invoiceIndex;
registry.fireEvent(event_name, version, event_data);
}
/// @dev Returns the user type, either CANDIDATE_TYPE,
/// EMPLOYER_TYPE, ARBITER_TYPE, or GUEST_TYPE for the given
/// address.
function getUserType(address _address) private returns (uint) {
if (_address == workInstance.candidateAddress()) {
return CANDIDATE_TYPE;
}
else if (_address == workInstance.storeInstance().employerAddress()) {
return EMPLOYER_TYPE;
}
else if (_address == workInstance.storeInstance().acceptedArbiter()) {
return ARBITER_TYPE;
}
else {
return GUEST_TYPE;
}
}
/// @dev Public function for authorized users to create comments
/// on the given invoice.
function addComment(string calldata metahash) external {
createComment(msg.sender, metahash);
}
/// @dev Place a comment on the invoice linked to the given user
/// address
function createComment(address userAddress, string memory metahash) private {
uint userType = getUserType(userAddress);
require(userType != GUEST_TYPE,
"Only the candidate, employer, and arbiter can comment.");
// Create the forwarded contract
Forwarder fwd = new Forwarder(); // Proxy Contract
// target(EthlanceComment)
EthlanceComment comment = EthlanceComment(address(fwd));
// Permit Comment to fire registry events
registry.permitDispatch(address(fwd));
// Add comment to the registry comment listing
registry.pushComment(address(this), address(comment));
uint[4] memory index;
index[0] = workInstance.storeInstance().jobIndex();
index[1] = workInstance.workIndex();
index[2] = invoiceIndex;
index[3] = registry.getCommentCount(address(this)) - 1;
// Construct the comment contract
comment.construct(userAddress,
userType,
metahash,
EthlanceComment.CommentType.Invoice,
index);
}
}