-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEnhancedAtomicSwap.pseudo
104 lines (94 loc) · 4.02 KB
/
EnhancedAtomicSwap.pseudo
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
class AtomicSwap{
SC Relay;
struct Swap{
int timelock;
int value;
float exchangeRatio;
address sender;
address receiver;
bytes secret;
bytes hashedSecret;
string targetNetwork;
}
mapping (bytes => Swap) swaps;
enum States {
INVALID,
PRECOMMITED,
COMMITED,
REDEEMED,
REFUNDED,
}
mapping (bytes => States) swapStates;
public function preCommit(bytes swapID, bytes hashedSecret, int value, float ratio, string targetNetwork) do
//Check that the swap ID doesn't exist
requires swaps[swapID] == null;
//Store the details of the swap
Swap memory swap = Swap({
this.timelock = 0, //It is not timelocked yet because there it is not commited yet
this.value = value,
this.sender = msg.sender,
this.receiver = msg.sender,//No receiver has been set yet so receiver set to sender address
hashedSecret = hashedSecret,
this.secretKey = null,
this.targetNetwork = targetNetwork
});
//Store the swap
this.swaps[swapID] = swap;
//Set the state
this.swapStates[swapID] = States.PRECOMMITED;
return true
end
//This function should be called by the other party's adapter
//It checks that the provided transaction correspond to a pre commited swap in the other blockchain
//If it is a valid transaction then it commits the swap
public function reqCommit(string rawTransaction, int transactionIndex, bytes merkleSibling, bytes blockHash) do
//Calls relay to audit given tx
tx = this.Relay.call.verifyTransaction(string rawTransaction, int transactionIndex, bytes merkleSibling, bytes blockHash);
requires data.hashedSecret == swaps[data.targetSwapID].hashedSecret;
requires tx.data.value == swaps[tx.data.targetSwapID].value * swaps[tx.data.targetSwapID].exchangeRatio;
this.commit(tx.data.swapID, tx.from);
end
//Internal only commit fucntion
internal function commit (bytes swapID, address receiver) do
requires this.swapStates[swapID] == PRECOMMITED
this.swaps[swapID].receiver = receiver;
this.swaps[swapID].timelock = now + timeDelta;
//Commit happends when the other ACCS is in pre commit phase
//Therefore it is reasonable at this point to set a timelock
this.swapStates[swapID] = States.COMMITED;
end
//Public redeem function
public function redeem(bytes swapID, bytes secret) do
requires this.swapStates[swapID] == COMMITED
requires this.swap[swapID].receiver == msg.sender
requires this.swaps[swapID].timelock < now
requires SHA256(secret) == this.swaps[swapID].hashedSecret
//Set the secret key in order to reveal it to the other party
this.swaps[swapID].secret = secret;
// Close the swap.
this.swapStates[swapID] = States.REDEEMED;
// Transfer the ETH funds from this contract to the participant
this.swaps[swapID].receiver.transfer(this.swaps[swapID].value);
end
public function refund(bytes swapID) do
requires this.swapStates[swapID] == OPEN
requires this.swaps[swapID].sender == msg.sender
requires this.swaps[swapID].timelock > now
swapStates[swapID] = States.EXPIRED;
// Transfer the ETH value from this contract back to the ETH trader.
this.swaps[swapID].sender.transfer(this.swap[swapID].value)
end
//Returns the state of the provided swap ID
public function checkState(bytes swapID) do
return this.swapStates[swapID]
end
//Returns the timelock of the provided swap ID
puublic function checkTimeLock(bytes swapID) do
return this.swaps[swapID].timelock
end
//Returns the secret of the provided swap ID if it has been revelated
public function checkSecretKey(bytes swapID) do
requires this.swapStates[swapsID] == REDEEMED
return this.swaps[swapID].secret
end
}