-
Notifications
You must be signed in to change notification settings - Fork 210
/
Coin.hs
344 lines (300 loc) · 9.74 KB
/
Coin.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE TypeApplications #-}
-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- This module provides the 'Coin' data type, which represents a quantity of
-- lovelace.
--
module Cardano.Wallet.Primitive.Types.Coin
( -- * Type
Coin (..)
-- * Conversions (Safe)
, fromIntegral
, fromWord64
, toInteger
, toQuantity
, toWord64
-- * Conversions (Unsafe)
, unsafeFromIntegral
, unsafeToQuantity
, unsafeToWord64
-- * Compatibility
, coinToNatural
, unsafeNaturalToCoin
-- * Checks
, isValidCoin
-- * Arithmetic operations
, addCoin
, subtractCoin
, sumCoins
, difference
, distance
-- * Partitioning
, equipartition
, partition
, unsafePartition
) where
import Prelude hiding
( fromIntegral, toInteger )
import Cardano.Numeric.Util
( equipartitionNatural, partitionNatural )
import Control.DeepSeq
( NFData (..) )
import Control.Monad
( (<=<) )
import Data.Bits
( Bits )
import Data.Foldable
( foldl' )
import Data.Hashable
( Hashable )
import Data.IntCast
( intCast, intCastMaybe )
import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Maybe
( fromMaybe )
import Data.Quantity
( Quantity (..) )
import Data.Text.Class
( FromText (..), TextDecodingError (..), ToText (..) )
import Data.Word
( Word64 )
import Fmt
( Buildable (..), fixedF )
import GHC.Generics
( Generic )
import GHC.Stack
( HasCallStack )
import Numeric.Natural
( Natural )
import Quiet
( Quiet (..) )
import qualified Data.Text as T
import qualified Prelude
-- | A 'Coin' represents a quantity of lovelace.
--
-- Reminder: 1 ada = 1,000,000 lovelace.
--
newtype Coin = Coin
{ unCoin :: Natural
}
deriving stock (Ord, Eq, Generic)
deriving (Read, Show) via (Quiet Coin)
instance Semigroup Coin where
-- Word64 doesn't have a default Semigroup instance.
Coin a <> Coin b = Coin (a + b)
instance Monoid Coin where
mempty = Coin 0
instance ToText Coin where
toText (Coin c) = T.pack $ show c
instance FromText Coin where
fromText = validate <=< (fmap Coin . fromText @Natural)
where
validate x
| isValidCoin x =
return x
| otherwise =
Left $ TextDecodingError "Coin value is out of bounds"
instance NFData Coin
instance Hashable Coin
instance Bounded Coin where
minBound = Coin 0
maxBound = Coin 45_000_000_000_000_000
instance Buildable Coin where
build (Coin c) = fixedF @Double 6 (Prelude.fromIntegral c / 1e6)
--------------------------------------------------------------------------------
-- Conversions (Safe)
--------------------------------------------------------------------------------
-- | Constructs a 'Coin' from an 'Integral' value.
--
-- Returns 'Nothing' if the given value is negative.
--
fromIntegral :: (Bits i, Integral i) => i -> Maybe Coin
fromIntegral i = Coin <$> intCastMaybe i
-- | Constructs a 'Coin' from a 'Word64' value.
--
fromWord64 :: Word64 -> Coin
fromWord64 = Coin . intCast
-- | Converts a 'Coin' to an 'Integer' value.
--
toInteger :: Coin -> Integer
toInteger = intCast . unCoin
-- | Converts a 'Coin' to a 'Quantity'.
--
-- Returns 'Nothing' if the given value does not fit within the bounds of
-- the target type.
--
toQuantity :: (Bits i, Integral i) => Coin -> Maybe (Quantity n i)
toQuantity (Coin c) = Quantity <$> intCastMaybe c
-- | Converts a 'Coin' to a 'Word64' value.
--
-- Returns 'Nothing' if the given value does not fit within the bounds of a
-- 64-bit word.
--
toWord64 :: Coin -> Maybe Word64
toWord64 (Coin c) = intCastMaybe c
--------------------------------------------------------------------------------
-- Conversions (Unsafe)
-------------------------------------------------------------------------------
-- | Constructs a 'Coin' from an 'Integral' value.
--
-- Callers of this function must take responsibility for checking that the
-- given value is not negative.
--
-- Produces a run-time error if the given value is negative.
--
unsafeFromIntegral
:: HasCallStack
=> (Bits i, Integral i, Show i)
=> i
-> Coin
unsafeFromIntegral i = fromMaybe onError (fromIntegral i)
where
onError = error $ unwords
[ "Coin.unsafeFromIntegral:"
, show i
, "is not a natural number."
]
-- | Converts a 'Coin' to a 'Quantity'.
--
-- Callers of this function must take responsibility for checking that the
-- given value will fit within the bounds of the target type.
--
-- Produces a run-time error if the given value is out of bounds.
--
unsafeToQuantity
:: HasCallStack
=> (Bits i, Integral i)
=> Coin
-> Quantity n i
unsafeToQuantity c = fromMaybe onError (toQuantity c)
where
onError = error $ unwords
[ "Coin.unsafeToQuantity:"
, show c
, "does not fit within the bounds of the target type."
]
-- | Converts a 'Coin' to a 'Word64' value.
--
-- Callers of this function must take responsibility for checking that the
-- given value will fit within the bounds of a 64-bit word.
--
-- Produces a run-time error if the given value is out of bounds.
--
unsafeToWord64 :: HasCallStack => Coin -> Word64
unsafeToWord64 c = fromMaybe onError (toWord64 c)
where
onError = error $ unwords
[ "Coin.unsafeToWord64:"
, show c
, "does not fit within the bounds of a 64-bit word."
]
{-------------------------------------------------------------------------------
Compatibility
-------------------------------------------------------------------------------}
coinToNatural :: Coin -> Natural
coinToNatural = unCoin
unsafeNaturalToCoin :: Natural -> Coin
unsafeNaturalToCoin = Coin
{-------------------------------------------------------------------------------
Checks
-------------------------------------------------------------------------------}
-- | Whether the coin amount is less than the total amount of Ada.
isValidCoin :: Coin -> Bool
isValidCoin c = c >= minBound && c <= maxBound
{-------------------------------------------------------------------------------
Operations
-------------------------------------------------------------------------------}
-- | Subtracts the second coin from the first.
--
-- Returns 'Nothing' if the second coin is strictly greater than the first.
--
subtractCoin :: Coin -> Coin -> Maybe Coin
subtractCoin (Coin a) (Coin b)
| a >= b = Just $ Coin (a - b)
| otherwise = Nothing
-- | Calculate the combined value of two coins.
--
-- NOTE: It is generally safe to add coins and stay in the same domain because
-- the max supply is known (45B), which easily fits within a 'Word64'. So for
-- the vast majority of usages of this function within cardano-wallet, it is a
-- safe operation.
--
addCoin :: Coin -> Coin -> Coin
addCoin (Coin a) (Coin b) = Coin (a + b)
-- | Add a list of coins together.
sumCoins :: Foldable t => t Coin -> Coin
sumCoins = foldl' addCoin (Coin 0)
-- | Subtracts the second coin from the first.
--
-- Returns 'Coin 0' if the second coin is strictly greater than the first.
--
difference :: Coin -> Coin -> Coin
difference a b = fromMaybe (Coin 0) (subtractCoin a b)
-- | Absolute difference between two coin amounts. The result is never negative.
distance :: Coin -> Coin -> Coin
distance (Coin a) (Coin b) = if a < b then Coin (b - a) else Coin (a - b)
--------------------------------------------------------------------------------
-- Partitioning
--------------------------------------------------------------------------------
-- | Computes the equipartition of a coin into 'n' smaller coins.
--
-- An /equipartition/ of a coin is a /partition/ of that coin into 'n' smaller
-- coins whose values differ by no more than 1.
--
-- The resultant list is sorted in ascending order.
--
equipartition
:: Coin
-- ^ The coin to be partitioned.
-> NonEmpty a
-- ^ Represents the number of portions in which to partition the coin.
-> NonEmpty Coin
-- ^ The partitioned coins.
equipartition c =
-- Note: the natural-to-coin conversion is safe, as partitioning guarantees
-- to produce values that are less than or equal to the original value.
fmap unsafeNaturalToCoin . equipartitionNatural (coinToNatural c)
-- | Partitions a coin into a number of parts, where the size of each part is
-- proportional to the size of its corresponding element in the given list
-- of weights, and the number of parts is equal to the number of weights.
--
-- Returns 'Nothing' if the sum of weights is equal to zero.
--
partition
:: Coin
-- ^ The coin to be partitioned.
-> NonEmpty Coin
-- ^ The list of weights.
-> Maybe (NonEmpty Coin)
-- ^ The partitioned coins.
partition c
-- Note: the natural-to-coin conversion is safe, as partitioning guarantees
-- to produce values that are less than or equal to the original value.
= fmap (fmap unsafeNaturalToCoin)
. partitionNatural (coinToNatural c)
. fmap coinToNatural
-- | Partitions a coin into a number of parts, where the size of each part is
-- proportional to the size of its corresponding element in the given list
-- of weights, and the number of parts is equal to the number of weights.
--
-- Throws a run-time error if the sum of weights is equal to zero.
--
unsafePartition
:: HasCallStack
=> Coin
-- ^ The coin to be partitioned.
-> NonEmpty Coin
-- ^ The list of weights.
-> NonEmpty Coin
-- ^ The partitioned coins.
unsafePartition = (fromMaybe zeroWeightSumError .) . partition
where
zeroWeightSumError = error
"Coin.unsafePartition: weights must have a non-zero sum."