Skip to content

Commit

Permalink
API: more wip assets
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jan 18, 2021
1 parent dd0d559 commit ea52ed0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
15 changes: 8 additions & 7 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -725,8 +725,8 @@ mkShelleyWallet ctx wid cp meta pending progress = do
, reward
}
, assets = ApiWalletAssetsBalance
{ available = error "fixme"
, total = error "fixme"
{ available = mempty -- fixme: ADP-604
, total = mempty -- fixme: ADP-604
}
, delegation = apiDelegation
, id = ApiT wid
Expand Down Expand Up @@ -2041,6 +2041,7 @@ mkApiTransaction ti txid mfee ins outs ws (meta, timestamp) txMeta setTimeRefere
, amount = meta ^. #amount
, fee = maybe (Quantity 0) (Quantity . fromIntegral . unCoin) mfee
, deposit = Quantity depositIfAny
, assets = mempty -- fixme: ADP-604
, insertedAt = Nothing
, pendingSince = Nothing
, expiresAt = Nothing
Expand All @@ -2049,6 +2050,7 @@ mkApiTransaction ti txid mfee ins outs ws (meta, timestamp) txMeta setTimeRefere
, inputs = [ApiTxInput (fmap toAddressAmount o) (ApiT i) | (i, o) <- ins]
, outputs = toAddressAmount <$> outs
, withdrawals = mkApiWithdrawal @n <$> Map.toList ws
, forge = mempty -- fixme: ADP-604
, status = ApiT (meta ^. #status)
, metadata = ApiTxMetadata $ ApiT <$> txMeta
}
Expand Down Expand Up @@ -2109,9 +2111,8 @@ mkApiTransaction ti txid mfee ins outs ws (meta, timestamp) txMeta setTimeRefere
txOutValue = fromIntegral . unCoin . txOutCoin

toAddressAmount :: TxOut -> AddressAmount (ApiT Address, Proxy n)
toAddressAmount (TxOut addr tokens) = AddressAmount
(ApiT addr, Proxy @n)
(mkApiCoin $ TokenBundle.getCoin tokens)
toAddressAmount (TxOut addr (TokenBundle.TokenBundle coin assets)) =
AddressAmount (ApiT addr, Proxy @n) (mkApiCoin coin) (ApiT assets)

mkApiCoin
:: Coin
Expand All @@ -2128,8 +2129,8 @@ mkApiWithdrawal (acct, c) =
coerceCoin
:: forall (n :: NetworkDiscriminant). AddressAmount (ApiT Address, Proxy n)
-> TxOut
coerceCoin (AddressAmount (ApiT addr, _) (Quantity c)) =
TxOut addr (TokenBundle.fromCoin $ Coin $ fromIntegral c)
coerceCoin (AddressAmount (ApiT addr, _) (Quantity c) (ApiT assets)) =
TxOut addr (TokenBundle.TokenBundle (Coin $ fromIntegral c) assets)

natural :: Quantity q Word32 -> Quantity q Natural
natural = Quantity . fromIntegral . getQuantity
Expand Down
18 changes: 11 additions & 7 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -765,6 +765,7 @@ data ApiTransaction (n :: NetworkDiscriminant) = ApiTransaction
, amount :: !(Quantity "lovelace" Natural)
, fee :: !(Quantity "lovelace" Natural)
, deposit :: !(Quantity "lovelace" Natural)
, assets :: !(ApiT W.TokenMap)
, insertedAt :: !(Maybe ApiBlockReference)
, pendingSince :: !(Maybe ApiBlockReference)
, expiresAt :: !(Maybe ApiSlotReference)
Expand All @@ -773,6 +774,7 @@ data ApiTransaction (n :: NetworkDiscriminant) = ApiTransaction
, inputs :: ![ApiTxInput n]
, outputs :: ![AddressAmount (ApiT Address, Proxy n)]
, withdrawals :: ![ApiWithdrawal n]
, forge :: !(ApiT W.TokenMap)
, status :: !(ApiT TxStatus)
, metadata :: !ApiTxMetadata
} deriving (Eq, Generic, Show)
Expand Down Expand Up @@ -803,6 +805,7 @@ data ApiTxInput (n :: NetworkDiscriminant) = ApiTxInput
data AddressAmount addr = AddressAmount
{ address :: !addr
, amount :: !(Quantity "lovelace" Natural)
, assets :: !(ApiT W.TokenMap)
} deriving (Eq, Generic, Show)
deriving anyclass NFData

Expand Down Expand Up @@ -1118,6 +1121,7 @@ instance KnownDiscovery (SeqState network key) where
newtype ApiT a =
ApiT { getApiT :: a }
deriving (Generic, Show, Eq, Functor)
deriving newtype (Semigroup, Monoid)
deriving anyclass NFData
deriving instance Ord a => Ord (ApiT a)

Expand Down Expand Up @@ -1627,6 +1631,7 @@ instance FromJSON ApiWalletAssetsBalance where
instance ToJSON ApiWalletAssetsBalance where
toJSON = genericToJSON defaultRecordTypeOptions

-- fixme: doesn't quite match the spec
instance FromJSON (ApiT W.TokenMap) where
parseJSON = fmap (ApiT . W.getFlat) . parseJSON
instance ToJSON (ApiT W.TokenMap) where
Expand Down Expand Up @@ -1772,12 +1777,11 @@ instance ToJSON (ApiT SlotNo) where
toJSON (ApiT (SlotNo sn)) = toJSON sn

instance FromJSON a => FromJSON (AddressAmount a) where
parseJSON bytes = do
v@(AddressAmount _ (Quantity c)) <-
genericParseJSON defaultRecordTypeOptions bytes
if isValidCoin (Coin $ fromIntegral c)
then return v
else fail $
parseJSON = genericParseJSON defaultRecordTypeOptions >=> validate
where
validate v@(AddressAmount _ (Quantity c) _assets)
| isValidCoin (Coin $ fromIntegral c) = pure v
| otherwise = fail $
"invalid coin value: value has to be lower than or equal to "
<> show (unCoin maxBound) <> " lovelace."

Expand Down Expand Up @@ -2016,7 +2020,7 @@ instance FromText (AddressAmount Text) where
case split (=='@') text of
[] -> err
[_] -> err
[l, r] -> AddressAmount r <$> fromText l
[l, r] -> AddressAmount r <$> fromText l <*> pure mempty
_ -> err

instance FromText PostExternalTransactionData where
Expand Down

0 comments on commit ea52ed0

Please sign in to comment.