Skip to content

Commit

Permalink
Fix API AddressAmount parseJSON to make assets optional
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jan 19, 2021
1 parent bb84bd8 commit 4558264
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -268,6 +268,7 @@ import Data.Aeson
, tagSingleConstructors
, withObject
, withText
, (.!=)
, (.:)
, (.:?)
, (.=)
Expand Down Expand Up @@ -1838,10 +1839,13 @@ instance ToJSON (ApiT SlotNo) where
toJSON (ApiT (SlotNo sn)) = toJSON sn

instance FromJSON a => FromJSON (AddressAmount a) where
parseJSON = genericParseJSON defaultRecordTypeOptions >=> validate
parseJSON = withObject "AddressAmount " $ \v -> AddressAmount
<$> v .: "address"
<*> (v .: "coin" >>= validateCoin)
<*> v .:? "assets" .!= mempty
where
validate v@(AddressAmount _ (Quantity c) _assets)
| isValidCoin (Coin $ fromIntegral c) = pure v
validateCoin q
| isValidCoin (coinFromQuantity q) = pure q
| otherwise = fail $
"invalid coin value: value has to be lower than or equal to "
<> show (unCoin maxBound) <> " lovelace."
Expand Down

0 comments on commit 4558264

Please sign in to comment.