From 1919f3d069eee5e827d4ce65f78b43a0674b6cfe Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Mon, 10 Jan 2022 14:45:24 +0100 Subject: [PATCH] use decodeTransaction for withdrawals --- lib/core/src/Cardano/Wallet/Api/Server.hs | 24 ++++++++++++++--------- lib/core/src/Cardano/Wallet/Api/Types.hs | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/core/src/Cardano/Wallet/Api/Server.hs b/lib/core/src/Cardano/Wallet/Api/Server.hs index 20778994b34..c36ac81bc67 100644 --- a/lib/core/src/Cardano/Wallet/Api/Server.hs +++ b/lib/core/src/Cardano/Wallet/Api/Server.hs @@ -468,6 +468,8 @@ import Cardano.Wallet.Transaction ) import Cardano.Wallet.Unsafe ( unsafeRunExceptT ) +import Cardano.Wallet.Util + ( invariant ) import Control.Arrow ( second ) import Control.DeepSeq @@ -2337,11 +2339,8 @@ submitTransaction ( ctx ~ ApiLayer s k , HasNetworkLayer IO ctx , IsOwned s k - , WalletKey k - , HardDerivation k , Typeable s , Typeable n - , Bounded (Index (AddressIndexDerivationType k) 'AddressK) ) => ctx -> ApiT WalletId @@ -2351,14 +2350,11 @@ submitTransaction ctx apiw@(ApiT wid) apitx@(ApiSerialisedTransaction (ApiT seal ttl <- liftIO $ W.getTxExpiry ti Nothing apiDecoded <- decodeTransaction @_ @s @k @n ctx apiw apitx let outs = getOurOuts apiDecoded - let (tx@(Tx txId _ _ inps _outs wdrlMap _ _),_,_,_) = txDecoded + let (tx@(Tx txId _ _ inps _ _ _ _),_,_,_) = txDecoded _ <- withWorkerCtx ctx wid liftE liftE $ \wrk -> do - (acct, _, _) <- liftHandler $ W.readRewardAccount @_ @s @k @n wrk wid - (wdrl, _) <- mkRewardAccountBuilder @_ @s @_ @n ctx wid $ - if Map.member acct wdrlMap - then Just SelfWithdrawal - else Nothing + (acct, _, path) <- liftHandler $ W.readRewardAccount @_ @s @k @n wrk wid + let wdrl = getOurWdrl acct path apiDecoded let txCtx = defaultTransactionCtx { txTimeToLive = ttl , txWithdrawal = wdrl @@ -2383,6 +2379,16 @@ submitTransaction ctx apiw@(ApiT wid) apitx@(ApiSerialisedTransaction (ApiT seal let generalOuts = apiDecodedTx ^. #outputs in map toTxOut $ filter isOutOurs generalOuts + getOurWdrl rewardAcct path apiDecodedTx = + let generalWdrls = apiDecodedTx ^. #withdrawals + isWdrlOurs (ApiWithdrawalGeneral _ _ context) = context == Our + in case filter isWdrlOurs generalWdrls of + [ApiWithdrawalGeneral (ApiT acct, _) (Quantity amt) _] -> + let acct' = invariant "reward account should be the same" acct (rewardAcct ==) + in WithdrawalSelf acct' path (Coin $ fromIntegral amt) + _ -> + NoWithdrawal + joinStakePool :: forall ctx s n k. ( ctx ~ ApiLayer s k diff --git a/lib/core/src/Cardano/Wallet/Api/Types.hs b/lib/core/src/Cardano/Wallet/Api/Types.hs index 731e7dd104c..2e6228f116f 100644 --- a/lib/core/src/Cardano/Wallet/Api/Types.hs +++ b/lib/core/src/Cardano/Wallet/Api/Types.hs @@ -1155,7 +1155,7 @@ data ResourceContext = External | Our deriving (Eq, Generic, Show, Typeable) deriving anyclass NFData -data ApiWithdrawalGeneral n = ApiWithdrawalGeneral +data ApiWithdrawalGeneral (n :: NetworkDiscriminant) = ApiWithdrawalGeneral { stakeAddress :: !(ApiT W.RewardAccount, Proxy n) , amount :: !(Quantity "lovelace" Natural) , context :: !ResourceContext