Skip to content

Commit

Permalink
use decodeTransaction for withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jan 17, 2022
1 parent 001590e commit 4a53725
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
24 changes: 15 additions & 9 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -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
Expand Down

0 comments on commit 4a53725

Please sign in to comment.