This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSettlement.sol
108 lines (83 loc) · 3.01 KB
/
Settlement.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
pragma solidity >=0.4.21 <0.7.0;
pragma experimental ABIEncoderV2;
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
contract Settlement is Ownable {
enum SwapStatus {Nonexistent, Locked, Unlocked, Aborted}
struct SwapDetail {
address payable transferFromAddress;
address payable transferToAddress;
uint256 weiAmount;
uint256 securityAmount;
SwapStatus status;
}
address cordaNotary;
mapping(string => SwapDetail) public swapIdToDetailMap;
uint256 settlementId;
event Locked(uint256 settlementId, string swapId, bytes encodedSwapDetail);
event Unlocked(uint256 settlementId, string swapId, bytes encodedSwapDetail);
event Aborted(uint256 settlementId, string swapId, bytes encodedSwapDetail);
constructor(address _cordaNotary) public {
cordaNotary = _cordaNotary;
settlementId = 1;
}
modifier onlyCordaNotary {
require(msg.sender == cordaNotary, "caller is not the corda notary");
_;
}
function lock(
string memory _swapId,
address payable _transferFromAddress,
address payable _transferToAddress,
uint256 _weiAmount,
uint256 _securityAmount
) public payable {
require(
msg.sender == _transferFromAddress,
"msg.sender is not _transferFromAddress"
);
require(
msg.value == _weiAmount,
"msg.value is not equivalent to _weiAmount"
);
SwapDetail storage swapDetail = swapIdToDetailMap[_swapId];
swapDetail.transferFromAddress = _transferFromAddress;
swapDetail.transferToAddress = _transferToAddress;
swapDetail.weiAmount = _weiAmount;
swapDetail.securityAmount = _securityAmount;
swapDetail.status = SwapStatus.Locked;
bytes memory encodedSwapDetail = abi.encode(swapDetail);
emit Locked(settlementId++, _swapId, encodedSwapDetail);
}
function unlock(string memory _swapId) public onlyCordaNotary {
SwapDetail storage swapDetail = swapIdToDetailMap[_swapId];
require(
swapDetail.status != SwapStatus.Nonexistent,
"swapDetail does not exist"
);
require(
swapDetail.status == SwapStatus.Locked,
"swapDetail.status is not Locked"
);
// Try sending wei to targetAddress.
require(swapDetail.transferToAddress.send(swapDetail.weiAmount));
swapDetail.status = SwapStatus.Unlocked;
bytes memory encodedSwapDetail = abi.encode(swapDetail);
emit Unlocked(settlementId++, _swapId, encodedSwapDetail);
}
function abort(string memory _swapId) public onlyCordaNotary {
SwapDetail storage swapDetail = swapIdToDetailMap[_swapId];
require(
swapDetail.status != SwapStatus.Nonexistent,
"swapDetail does not exist"
);
require(
swapDetail.status == SwapStatus.Locked,
"swapDetail.status is not Locked"
);
// Try sending wei to targetAddress.
require(swapDetail.transferFromAddress.send(swapDetail.weiAmount));
swapDetail.status = SwapStatus.Aborted;
bytes memory encodedSwapDetail = abi.encode(swapDetail);
emit Aborted(settlementId++, _swapId, encodedSwapDetail);
}
}