-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManagefund.sol
142 lines (124 loc) · 4.23 KB
/
Managefund.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
pragma solidity ^0.4.11;
contract Managefund{
struct Bid{
bytes32 name;
address oracle;
address seller;
address buyer;
uint price;
uint timeout;
dealStatus status;
uint fee;
bool isLimited;
}
enum dealStatus{ unPaid, Pending, Closed, Rejected, Refund }
mapping (address => Bid[]) public bids;
mapping (address => uint) public pendingWithdrawals;
event amountRecieved(
address seller,
uint bidId
);
event bidClosed(
address seller,
uint bidId
);
event bidCreated(
address seller,
bytes32 name,
uint bidId
);
event refundDone(
address seller,
uint bidId
);
event withdrawDone(
address person,
uint amount
);
event bidRejected(
address seller,
uint bidId
);
function getBidIndex(address seller, bytes32 name) public constant returns (uint){
for (uint8 i=0;i<bids[seller].length;i++){
if (bids[seller][i].name == name){
return i;
}
}
}
function getBidsNum (address seller) public constant returns (uint bidsNum) {
return bids[seller].length;
}
function sendAmount (address seller, uint bidId) external payable{
Bid storage a = bids[seller][bidId];
require(msg.value == a.price && a.status == dealStatus.unPaid);
if (a.isLimited == true){
require(a.timeout > block.number);
}
a.status = dealStatus.Pending;
amountRecieved(seller, bidId);
}
function createBid (bytes32 name, address seller, address oracle, address buyer, uint price, uint timeout, uint fee) external{
require(name.length != 0 && price !=0);
bool limited = true;
if (timeout == 0){
limited = false;
}
bids[seller].push(Bid({
name: name,
oracle: oracle,
seller: seller,
buyer: buyer,
price: price,
timeout: block.number+timeout,
status: dealStatus.unPaid,
fee: fee,
isLimited: limited
}));
uint bidId = bids[seller].length-1;
bidCreated(seller, name, bidId);
}
function closeBid(address seller, uint bidId) external returns (bool){
Bid storage bid = bids[seller][bidId];
if (bid.isLimited == true){
require(bid.timeout > block.number);
}
require(msg.sender == bid.oracle && bid.status == dealStatus.Pending);
bid.status = dealStatus.Closed;
pendingWithdrawals[bid.seller]+=bid.price-bid.fee;
pendingWithdrawals[bid.oracle]+=bid.fee;
withdraw(bid.seller);
withdraw(bid.oracle);
bidClosed(seller, bidId);
return true;
}
function refund(address seller, uint bidId) external returns (bool){
require(bids[seller][bidId].buyer == msg.sender && bids[seller][bidId].isLimited == true && bids[seller][bidId].timeout < block.number && bids[seller][bidId].status == dealStatus.Pending);
Bid storage a = bids[seller][bidId];
a.status = dealStatus.Refund;
pendingWithdrawals[a.buyer] = a.price;
withdraw(a.buyer);
refundDone(seller,bidId);
return true;
}
function rejectBid(address seller, uint bidId) external returns (bool){
if (bids[seller][bidId].isLimited == true){
require(bids[seller][bidId].timeout > block.number);
}
require(msg.sender == bids[seller][bidId].oracle && bids[seller][bidId].status == dealStatus.Pending);
Bid storage bid = bids[seller][bidId];
bid.status = dealStatus.Rejected;
pendingWithdrawals[bid.oracle] = bid.fee;
pendingWithdrawals[bid.buyer] = bid.price-bid.fee;
withdraw(bid.buyer);
withdraw(bid.oracle);
bidRejected(seller, bidId);
return true;
}
function withdraw(address person) private{
uint amount = pendingWithdrawals[person];
pendingWithdrawals[person] = 0;
person.transfer(amount);
withdrawDone(person, amount);
}
}