-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathescrow.sol
More file actions
219 lines (189 loc) · 5.88 KB
/
Copy pathescrow.sol
File metadata and controls
219 lines (189 loc) · 5.88 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Escrow - Trustless escrow contract
// Demonstrates state machines and multi-party transactions
contract Escrow {
// Escrow states
enum State {
Created,
Funded,
Released,
Refunded,
Disputed,
Resolved
}
// Escrow details
struct EscrowDetails {
address buyer;
address seller;
address arbiter;
uint256 amount;
uint256 deadline;
State state;
string description;
}
// State
mapping(uint256 => EscrowDetails) public escrows;
uint256 public escrowCount;
uint256 public arbiterFee = 100; // 1% in basis points
// Events
event EscrowCreated(
uint256 indexed id,
address indexed buyer,
address indexed seller,
uint256 amount
);
event EscrowFunded(uint256 indexed id);
event EscrowReleased(uint256 indexed id);
event EscrowRefunded(uint256 indexed id);
event DisputeRaised(uint256 indexed id, address indexed by);
event DisputeResolved(uint256 indexed id, bool releasedToSeller);
// Errors
error InvalidState(State current, State required);
error NotAuthorized();
error DeadlineNotReached();
error DeadlinePassed();
error InvalidAddress();
error InvalidAmount();
// Modifiers
modifier inState(uint256 escrowId, State required) {
if (escrows[escrowId].state != required) {
revert InvalidState(escrows[escrowId].state, required);
}
_;
}
modifier onlyBuyer(uint256 escrowId) {
if (msg.sender != escrows[escrowId].buyer) revert NotAuthorized();
_;
}
modifier onlySeller(uint256 escrowId) {
if (msg.sender != escrows[escrowId].seller) revert NotAuthorized();
_;
}
modifier onlyArbiter(uint256 escrowId) {
if (msg.sender != escrows[escrowId].arbiter) revert NotAuthorized();
_;
}
modifier onlyParty(uint256 escrowId) {
EscrowDetails storage e = escrows[escrowId];
if (msg.sender != e.buyer && msg.sender != e.seller) {
revert NotAuthorized();
}
_;
}
// Create and fund a new escrow
function createEscrow(
address seller,
address arbiter,
uint256 deadline,
string memory description
) public payable returns (uint256) {
if (seller == address(0)) revert InvalidAddress();
if (arbiter == address(0)) revert InvalidAddress();
if (msg.value == 0) revert InvalidAmount();
require(deadline > block.timestamp, "Deadline must be in future");
escrowCount++;
escrows[escrowCount] = EscrowDetails({
buyer: msg.sender,
seller: seller,
arbiter: arbiter,
amount: msg.value,
deadline: deadline,
state: State.Funded,
description: description
});
emit EscrowCreated(escrowCount, msg.sender, seller, msg.value);
emit EscrowFunded(escrowCount);
return escrowCount;
}
// Buyer releases funds to seller
function release(uint256 escrowId)
public
onlyBuyer(escrowId)
inState(escrowId, State.Funded)
{
EscrowDetails storage e = escrows[escrowId];
e.state = State.Released;
// Transfer funds to seller
// (Implementation depends on Solana transfer mechanism)
emit EscrowReleased(escrowId);
}
// Refund to buyer
function refund(uint256 escrowId)
public
inState(escrowId, State.Funded)
{
EscrowDetails storage e = escrows[escrowId];
// Seller can voluntarily refund anytime
if (msg.sender == e.seller) {
e.state = State.Refunded;
emit EscrowRefunded(escrowId);
return;
}
// Buyer can claim refund after deadline
if (msg.sender == e.buyer) {
if (block.timestamp < e.deadline) {
revert DeadlineNotReached();
}
e.state = State.Refunded;
emit EscrowRefunded(escrowId);
return;
}
revert NotAuthorized();
}
// Raise a dispute
function raiseDispute(uint256 escrowId)
public
onlyParty(escrowId)
inState(escrowId, State.Funded)
{
escrows[escrowId].state = State.Disputed;
emit DisputeRaised(escrowId, msg.sender);
}
// Arbiter resolves dispute
function resolveDispute(uint256 escrowId, bool releaseToSeller)
public
onlyArbiter(escrowId)
inState(escrowId, State.Disputed)
{
EscrowDetails storage e = escrows[escrowId];
e.state = State.Resolved;
uint256 fee = (e.amount * arbiterFee) / 10000;
uint256 remaining = e.amount - fee;
if (releaseToSeller) {
// Transfer remaining to seller
// Transfer fee to arbiter
} else {
// Transfer remaining to buyer
// Transfer fee to arbiter
}
emit DisputeResolved(escrowId, releaseToSeller);
}
// View functions
function getEscrow(uint256 escrowId) public view returns (
address buyer,
address seller,
address arbiter,
uint256 amount,
uint256 deadline,
State state,
string memory description
) {
EscrowDetails storage e = escrows[escrowId];
return (
e.buyer,
e.seller,
e.arbiter,
e.amount,
e.deadline,
e.state,
e.description
);
}
function isExpired(uint256 escrowId) public view returns (bool) {
return block.timestamp >= escrows[escrowId].deadline;
}
function getTimeRemaining(uint256 escrowId) public view returns (uint256) {
EscrowDetails storage e = escrows[escrowId];
if (block.timestamp >= e.deadline) return 0;
return e.deadline - block.timestamp;
}
}