This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImpl.hs
235 lines (204 loc) · 7.07 KB
/
Impl.hs
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
module Contract.Bridge.Impl
( entrypoints
, Parameter (..)
) where
import Indigo
import Contract.Bridge.Errors ()
import Contract.Bridge.Storage (HasBridge)
import Contract.Bridge.Types (ClaimRefundParams, GetOutcomeParams, GetSwapParams, LockParams,
Outcome (..), RedeemParams, RevealSecretHashParams)
import Contract.Token.Storage (HasManagedLedgerStorage, LedgerValue)
data Parameter
= Lock LockParams
| RevealSecretHash RevealSecretHashParams
| Redeem RedeemParams
| ClaimRefund ClaimRefundParams
| GetSwap GetSwapParams
| GetOutcome GetOutcomeParams
deriving stock Generic
deriving anyclass IsoValue
instance ParameterHasEntrypoints Parameter where
type ParameterEntrypointsDerivation Parameter = EpdPlain
entrypoints
:: forall storage param.
( param :~> Parameter
, HasManagedLedgerStorage storage
, HasBridge storage
, HasSideEffects
)
=> IndigoEntrypoint param
entrypoints param = do
entryCaseSimple param
( #cLock //-> lock @storage
, #cRevealSecretHash //-> revealSecretHash @storage
, #cRedeem //-> redeem @storage
, #cClaimRefund //-> claimRefund @storage
, #cGetSwap //-> getSwap @storage
, #cGetOutcome //-> getOutcome @storage
)
lock
:: forall s sp.
( sp :~> LockParams
, HasManagedLedgerStorage s
, HasBridge s
)
=> IndigoEntrypoint sp
lock parameter = do
swaps <- getStorageField @s #swaps
swapId <- new$ parameter #! #lpId
whenSome (swaps #: swapId) $ \_ -> failCustom #swapLockAlreadyExists swapId
debitFrom @s sender $ parameter #! #lpAmount
outcomes <- getStorageField @s #outcomes
setStorageField @s #swaps $ swaps +: (swapId, construct
( sender
, parameter #! #lpTo
, parameter #! #lpAmount
, parameter #! #lpReleaseTime
))
whenSome (parameter #! #lpSecretHash) $ \hash ->
setStorageField @s #outcomes $ outcomes +: (swapId, wrap #cHashRevealed hash)
revealSecretHash
:: forall s sp.
( sp :~> RevealSecretHashParams
, HasBridge s
)
=> IndigoEntrypoint sp
revealSecretHash parameter = do
swaps <- getStorageField @s #swaps
swapId <- new$ parameter #! #rshpId
ifNone (swaps #: swapId)
(void $ failCustom #swapLockDoesNotExist swapId) $
\s -> when (sender /= (s #! #sFrom)) $ failCustom_ #senderIsNotTheInitiator
outcomes <- getStorageField @s #outcomes
whenSome (outcomes #: swapId) $ \_ -> failCustom #secretHashIsAlreadySet swapId
setStorageField @s #outcomes $ outcomes +:
(swapId, wrap #cHashRevealed $ parameter #! #rshpSecretHash)
redeem
:: forall s sp.
( sp :~> RedeemParams
, HasBridge s
, HasManagedLedgerStorage s
)
=> IndigoEntrypoint sp
redeem parameter = do
swapId <- new$ parameter #! #rpId
secret <- new$ parameter #! #rpSecret
maxSecretLength <- new$ 32 nat -- TODO now const but maybe make it configurable
when (size secret > maxSecretLength) $
failCustom #tooLongSecret $ construct (varExpr maxSecretLength, size secret)
outcomes <- getStorageField @s #outcomes
ifSome (outcomes #: swapId)
(\o -> case_ o
( #cRefunded //->
\_ -> void $ failCustom #wrongOutcomeStatus [mt|Refunded|]
, #cHashRevealed //->
\secretHash -> when (sha256 secret /= secretHash) $ failCustom_ #invalidSecret
, #cSecretRevealed //->
\_ -> void $ failCustom #wrongOutcomeStatus [mt|SecretRevealed|]
)
)
(void $ failCustom #swapLockDoesNotExist swapId)
swaps <- getStorageField @s #swaps
ifSome (swaps #: swapId)
(\s -> do
when (now >= s #! #sReleaseTime) $ failCustom #swapIsOver $ s #! #sReleaseTime
setStorageField @s #outcomes $ outcomes +: (swapId, wrap #cSecretRevealed secret)
creditTo @s (s #! #sTo) (s #! #sAmount)
)
(void $ failCustom #swapLockDoesNotExist swapId)
claimRefund
:: forall s sp.
( sp :~> ClaimRefundParams
, HasBridge s
, HasManagedLedgerStorage s
)
=> IndigoEntrypoint sp
claimRefund parameter = do
swapId <- new$ parameter #! #crpId
outcomes <- getStorageField @s #outcomes
ifSome (outcomes #: swapId)
(\o -> case_ o
( #cRefunded //-> \_ -> void $ failCustom #wrongOutcomeStatus [mt|Refunded|]
, #cHashRevealed //-> \_ -> pass
, #cSecretRevealed //-> \_ -> void $ failCustom #wrongOutcomeStatus [mt|SecretRevealed|]
)
)
$ pass
swaps <- getStorageField @s #swaps
ifSome (swaps #: swapId)
(\s -> do
when (now < s #! #sReleaseTime) $ failCustom #fundsLock $ s #! #sReleaseTime
setStorageField @s #outcomes $ outcomes +: (swapId, Refunded ())
creditTo @s (s #! #sFrom) (s #! #sAmount)
)
(void $ failCustom #swapLockDoesNotExist swapId)
getSwap
:: forall s sp.
( sp :~> GetSwapParams
, HasBridge s
, HasSideEffects
)
=> IndigoEntrypoint sp
getSwap parameter = do
swaps <- getStorageField @s #swaps
project parameter $ \p -> return $ swaps #: p
getOutcome
:: forall s sp.
( sp :~> GetOutcomeParams
, HasBridge s
, HasSideEffects
)
=> IndigoEntrypoint sp
getOutcome parameter = do
outcomes <- getStorageField @s #outcomes
project parameter $ \p -> return $ outcomes #: p
----------------------------------------------------------------------------
-- Helpers
----------------------------------------------------------------------------
-- | Debit the given amount of tokens from a given address in ledger.
debitFrom
:: forall s from value.
( from :~> Address, value :~> Natural
, HasManagedLedgerStorage s
)
=> from -> value -> IndigoProcedure
debitFrom from val = do
-- Balance check and the corresponding update.
ledger_ <- getStorageField @s #ledger
ifSome (ledger_ #: from)
(\ledgerValue -> do
oldBalance <- new$ ledgerValue #~ #balance
newBalance <- ifSome (isNat $ oldBalance - val)
return (failNotEnoughBalance @Natural oldBalance)
newLedgerValue <- nonEmptyLedgerValue (newBalance !~ #balance)
setStorageField @s #ledger $ ledger_ !: (from, newLedgerValue)
)
(failNotEnoughBalance @() (0 nat))
where
failNotEnoughBalance :: forall r ex. (ex :~> Natural) => ex -> IndigoM r
failNotEnoughBalance curBalance = failCustom @r #notEnoughBalance $ pair
(val !~ #required)
(curBalance !~ #present)
-- | Credit the given amount of tokens to a given address in ledger.
creditTo
:: forall s to value.
( to :~> Address, value :~> Natural
, HasManagedLedgerStorage s
)
=> to -> value -> IndigoProcedure
creditTo to value = do
ledger_ <- getStorageField @s #ledger
when (value /= 0 nat) do
newBalance <- ifSome (ledger_ #: to)
(\ledgerValue -> return $ (value + ledgerValue #~ #balance) !~ #balance)
(return $ value !~ #balance)
setStorageField @s #ledger $ ledger_ +: (to, newBalance)
-- | Ensure that given 'LedgerValue' value cannot be safely removed
-- and return it.
nonEmptyLedgerValue
:: ( ledgerValue :~> LedgerValue )
=> ledgerValue -> IndigoFunction (Maybe LedgerValue)
nonEmptyLedgerValue ledgerValue =
if ledgerValue #~ #balance == 0 nat
then return none
else return (some ledgerValue)