@@ -125,6 +125,7 @@ module Cardano.Wallet
125125 -- ** Delegation
126126 , PoolRetirementEpochInfo (.. )
127127 , joinStakePool
128+ , getStakePoolCoinSelection
128129 , quitStakePool
129130 , selectCoinsForDelegation
130131 , estimateFeeForDelegation
@@ -418,6 +419,7 @@ import Statistics.Quantile
418419import Type.Reflection
419420 ( Typeable , typeRep )
420421
422+ import qualified Cardano.Api.Typed as Cardano
421423import qualified Cardano.Wallet.Primitive.AddressDiscovery.Random as Rnd
422424import qualified Cardano.Wallet.Primitive.AddressDiscovery.Sequential as Seq
423425import qualified Cardano.Wallet.Primitive.CoinSelection.Random as CoinSelection
@@ -1714,8 +1716,9 @@ signDelegation
17141716 -> Passphrase " raw"
17151717 -> CoinSelection
17161718 -> DelegationAction
1719+ -> [Cardano. Certificate ]
17171720 -> ExceptT ErrSignDelegation IO (Tx , TxMeta , UTCTime , SealedTx )
1718- signDelegation ctx wid argGenChange pwd coinSel action = db & \ DBLayer {.. } -> do
1721+ signDelegation ctx wid argGenChange pwd coinSel action certs = db & \ DBLayer {.. } -> do
17191722 nodeTip <- withExceptT ErrSignDelegationNetwork $ currentNodeTip nl
17201723 withRootKey @ _ @ s ctx wid pwd ErrSignDelegationWithRootKey $ \ xprv scheme -> do
17211724 let pwdP = preparePassphrase scheme pwd
@@ -1733,20 +1736,23 @@ signDelegation ctx wid argGenChange pwd coinSel action = db & \DBLayer{..} -> do
17331736 case action of
17341737 RegisterKeyAndJoin poolId ->
17351738 mkDelegationJoinTx tl poolId
1739+ certs
17361740 (rewardAcnt, pwdP)
17371741 keyFrom
17381742 (nodeTip ^. # slotNo)
17391743 coinSel'
17401744
17411745 Join poolId ->
17421746 mkDelegationJoinTx tl poolId
1747+ certs
17431748 (rewardAcnt, pwdP)
17441749 keyFrom
17451750 (nodeTip ^. # slotNo)
17461751 coinSel'
17471752
17481753 Quit ->
17491754 mkDelegationQuitTx tl
1755+ certs
17501756 (rewardAcnt, pwdP)
17511757 keyFrom
17521758 (nodeTip ^. # slotNo)
@@ -1934,16 +1940,12 @@ getTransaction ctx wid tid = db & \DBLayer{..} -> do
19341940 Delegation
19351941-------------------------------------------------------------------------------}
19361942
1937- -- | Helper function to factor necessary logic for joining a stake pool.
1938- joinStakePool
1943+ -- | Get the coin selection for joining a stake pool.
1944+ getStakePoolCoinSelection
19391945 :: forall ctx s t k .
19401946 ( HasDBLayer s k ctx
19411947 , HasLogger WalletLog ctx
1942- , HasNetworkLayer t ctx
19431948 , HasTransactionLayer t k ctx
1944- , IsOwned s k
1945- , IsOurs s ChimericAccount
1946- , GenChange s
19471949 , HardDerivation k
19481950 , AddressIndexDerivationType k ~ 'Soft
19491951 , WalletKey k
@@ -1954,10 +1956,9 @@ joinStakePool
19541956 -> PoolId
19551957 -> PoolLifeCycleStatus
19561958 -> WalletId
1957- -> ArgGenChange s
19581959 -> Passphrase " raw"
1959- -> ExceptT ErrJoinStakePool IO (Tx , TxMeta , UTCTime )
1960- joinStakePool ctx currentEpoch knownPools pid poolStatus wid argGenChange pwd =
1960+ -> ExceptT ErrJoinStakePool IO (CoinSelection , [ Cardano. Certificate ], DelegationAction )
1961+ getStakePoolCoinSelection ctx currentEpoch knownPools pid poolStatus wid pwd =
19611962 db & \ DBLayer {.. } -> do
19621963
19631964 (isKeyReg, walMeta) <- mapExceptT atomically
@@ -1976,21 +1977,62 @@ joinStakePool ctx currentEpoch knownPools pid poolStatus wid argGenChange pwd =
19761977 let action = if isKeyReg then Join pid else RegisterKeyAndJoin pid
19771978 liftIO $ traceWith tr $ MsgIsStakeKeyRegistered isKeyReg
19781979
1979- selection <- withExceptT ErrJoinStakePoolSelectCoin $
1980+ cs <- withExceptT ErrJoinStakePoolSelectCoin $
19801981 selectCoinsForDelegation @ ctx @ s @ t @ k ctx wid action
19811982
1983+ withRootKey @ _ @ s ctx wid pwd ErrJoinStakePoolRootKey $ \ xprv scheme -> do
1984+ let pwdP = preparePassphrase scheme pwd
1985+ let rewardAcnt = getRawKey $ deriveRewardAccount @ k pwdP xprv
1986+ let certs = (mkDelegationCertificates tl) (W. Join pid)
1987+ (rewardAcnt, pwdP) cs
1988+
1989+ pure (cs, certs, action)
1990+
1991+ where
1992+ tl = ctx ^. transactionLayer @ t @ k
1993+ db = ctx ^. dbLayer @ s @ k
1994+ tr = ctx ^. logger
1995+
1996+ -- | Helper function to factor necessary logic for joining a stake pool.
1997+ joinStakePool
1998+ :: forall ctx s t k .
1999+ ( HasDBLayer s k ctx
2000+ , HasLogger WalletLog ctx
2001+ , HasNetworkLayer t ctx
2002+ , HasTransactionLayer t k ctx
2003+ , IsOwned s k
2004+ , IsOurs s ChimericAccount
2005+ , GenChange s
2006+ , HardDerivation k
2007+ , AddressIndexDerivationType k ~ 'Soft
2008+ , WalletKey k
2009+ )
2010+ => ctx
2011+ -> W. EpochNo
2012+ -> Set PoolId
2013+ -> PoolId
2014+ -> PoolLifeCycleStatus
2015+ -> WalletId
2016+ -> ArgGenChange s
2017+ -> Passphrase " raw"
2018+ -> ExceptT ErrJoinStakePool IO (Tx , TxMeta , UTCTime )
2019+ joinStakePool ctx currentEpoch knownPools pid poolStatus wid argGenChange pwd =
2020+ db & \ DBLayer {.. } -> do
2021+ (selection, certs, action) <- getStakePoolCoinSelection @ ctx @ s @ t @ k
2022+ ctx currentEpoch knownPools pid poolStatus wid pwd
2023+
19822024 (tx, txMeta, txTime, sealedTx) <-
19832025 withExceptT ErrJoinStakePoolSignDelegation $
19842026 signDelegation
19852027 @ ctx @ s @ t @ k ctx wid argGenChange pwd selection action
2028+ certs
19862029
19872030 withExceptT ErrJoinStakePoolSubmitTx $
19882031 submitTx @ ctx @ s @ t @ k ctx wid (tx, txMeta, sealedTx)
19892032
19902033 pure (tx, txMeta, txTime)
19912034 where
19922035 db = ctx ^. dbLayer @ s @ k
1993- tr = ctx ^. logger
19942036
19952037-- | Helper function to factor necessary logic for quitting a stake pool.
19962038quitStakePool
@@ -2024,14 +2066,22 @@ quitStakePool ctx wid argGenChange pwd = db & \DBLayer{..} -> do
20242066 selection <- withExceptT ErrQuitStakePoolSelectCoin $
20252067 selectCoinsForDelegation @ ctx @ s @ t @ k ctx wid action
20262068
2069+ certs <- withRootKey @ _ @ s ctx wid pwd ErrQuitStakePoolRootKey $ \ xprv scheme -> do
2070+ let pwdP = preparePassphrase scheme pwd
2071+ let rewardAcnt = getRawKey $ deriveRewardAccount @ k pwdP xprv
2072+ pure $ (mkDelegationCertificates tl) W. Quit
2073+ (rewardAcnt, pwdP) selection
2074+
20272075 (tx, txMeta, txTime, sealedTx) <- withExceptT ErrQuitStakePoolSignDelegation $
20282076 signDelegation @ ctx @ s @ t @ k ctx wid argGenChange pwd selection action
2077+ certs
20292078
20302079 withExceptT ErrQuitStakePoolSubmitTx $
20312080 submitTx @ ctx @ s @ t @ k ctx wid (tx, txMeta, sealedTx)
20322081
20332082 pure (tx, txMeta, txTime)
20342083 where
2084+ tl = ctx ^. transactionLayer @ t @ k
20352085 db = ctx ^. dbLayer @ s @ k
20362086
20372087
@@ -2309,6 +2359,7 @@ data ErrJoinStakePool
23092359 | ErrJoinStakePoolSignDelegation ErrSignDelegation
23102360 | ErrJoinStakePoolSubmitTx ErrSubmitTx
23112361 | ErrJoinStakePoolCannotJoin ErrCannotJoin
2362+ | ErrJoinStakePoolRootKey ErrWithRootKey
23122363 deriving (Generic , Eq , Show )
23132364
23142365data ErrQuitStakePool
@@ -2317,6 +2368,7 @@ data ErrQuitStakePool
23172368 | ErrQuitStakePoolSignDelegation ErrSignDelegation
23182369 | ErrQuitStakePoolSubmitTx ErrSubmitTx
23192370 | ErrQuitStakePoolCannotQuit ErrCannotQuit
2371+ | ErrQuitStakePoolRootKey ErrWithRootKey
23202372 deriving (Generic , Eq , Show )
23212373
23222374-- | Errors that can occur when fetching the reward balance of a wallet
0 commit comments