Skip to content

Commit

Permalink
Add partial conversion of token bundle to mary value
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Jan 11, 2021
1 parent 82a8bf0 commit ba6e536
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Expand Up @@ -1142,14 +1142,8 @@ toAllegraTxOut (W.TxOut (W.Address addr) tokens) =

toMaryTxOut :: W.TxOut -> Cardano.TxOut MaryEra
toMaryTxOut (W.TxOut (W.Address addr) tokens) =
-- FIXME: Also convert non-ada tokens for full Mary-support.
Cardano.TxOut addrInEra
$ adaOnly
$ Cardano.lovelaceToValue
$ toCardanoLovelace
$ TokenBundle.coin tokens
Cardano.TxOut addrInEra $ toMaryValue tokens
where
adaOnly = Cardano.TxOutValue Cardano.MultiAssetInMaryEra
addrInEra = fromMaybe (error "toCardanoTxOut: malformed address") $
asum
[ Cardano.AddressInEra (Cardano.ShelleyAddressInEra Cardano.ShelleyBasedEraMary)
Expand All @@ -1159,6 +1153,23 @@ toMaryTxOut (W.TxOut (W.Address addr) tokens) =
<$> deserialiseFromRawBytes AsByronAddress addr
]

toMaryValue tb = let (coin, bundle) = TokenBundle.toFlatList tb in
Cardano.TxOutValue Cardano.MultiAssetInMaryEra $
Cardano.valueFromList $
(Cardano.AdaAssetId, coinToQuantity coin) :
map (bimap toCardanoAssetId toQuantity) bundle

toCardanoAssetId (TokenBundle.AssetId (W.TokenPolicyId pid) (W.TokenName name)) =
Cardano.AssetId
(Cardano.PolicyId (toScriptHash pid))
(Cardano.AssetName $ T.encodeUtf8 name)

toScriptHash :: W.Hash p -> Cardano.ScriptHash
toScriptHash = error "TODO: toMaryTxOut: Cardano.Api is missing a ScriptHash constructor"

coinToQuantity = fromIntegral . W.unCoin
toQuantity = fromIntegral . W.unTokenQuantity

-- | Convert from reward account address (which is a hash of a public key)
-- to a shelley ledger stake credential.
toStakeCredential :: W.RewardAccount -> SL.StakeCredential crypto
Expand Down

0 comments on commit ba6e536

Please sign in to comment.