Skip to content

Commit

Permalink
unify withdrawal requests frmo self or external wallets under one com…
Browse files Browse the repository at this point in the history
…mon API

  This is so-to-speak a breaking change compared to what we just released yet, it's for the best. Now there's one consistent way of creating withdrawals! This also gets rid of a few edge-case that are not possible anymore by design.
  • Loading branch information
KtorZ committed Jul 29, 2020
1 parent 84cbc86 commit 7c8289c
Show file tree
Hide file tree
Showing 24 changed files with 1,366 additions and 911 deletions.
4 changes: 4 additions & 0 deletions lib/byron/src/Cardano/Wallet/Byron.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ import System.Exit
( ExitCode (..) )
import System.IOManager
( withIOManager )
import Type.Reflection
( Typeable )

import qualified Cardano.Wallet.Api.Server as Server
import qualified Cardano.Wallet.DB.Sqlite as Sqlite
Expand All @@ -160,6 +162,7 @@ data SomeNetworkDiscriminant where
, EncodeStakeAddress n
, MaxSizeOf Address n IcarusKey
, MaxSizeOf Address n ByronKey
, Typeable n
)
=> Proxy n
-> SomeNetworkDiscriminant
Expand Down Expand Up @@ -244,6 +247,7 @@ serveWallet
, DecodeAddress n
, EncodeAddress n
, EncodeStakeAddress n
, Typeable n
)
=> Proxy n
-> Socket
Expand Down
15 changes: 9 additions & 6 deletions lib/byron/src/Cardano/Wallet/Byron/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@ import Network.Ntp
( NtpClient )
import Servant
( (:<|>) (..), Server, err501, throwError )
import Type.Reflection
( Typeable )

-- | A diminished servant server to serve Byron wallets only.
server
:: forall t n.
( Buildable (ErrValidateSelection t)
, PaymentAddress n IcarusKey
, PaymentAddress n ByronKey
, Typeable n
)
=> ApiLayer (RndState n) t ByronKey
-> ApiLayer (SeqState n IcarusKey) t IcarusKey
Expand Down Expand Up @@ -153,9 +156,9 @@ server byron icarus ntp =

transactions :: Server (Transactions n)
transactions =
(\_ _ _ -> throwError err501)
(\_ _ -> throwError err501)
:<|> (\_ _ _ _ _ -> throwError err501)
:<|> (\_ _ _ -> throwError err501)
:<|> (\_ _ -> throwError err501)
:<|> (\_ _ -> throwError err501)
:<|> (\_ _ -> throwError err501)

Expand Down Expand Up @@ -243,11 +246,11 @@ server byron icarus ntp =
(byron , do
let pwd = coerce (getApiT $ tx ^. #passphrase)
genChange <- rndStateChange byron wid pwd
postTransaction byron genChange wid False tx
postTransaction byron genChange wid tx
)
(icarus, do
let genChange k _ = paymentAddress @n k
postTransaction icarus genChange wid False tx
postTransaction icarus genChange wid tx
)
)
:<|>
Expand All @@ -257,8 +260,8 @@ server byron icarus ntp =
)
:<|>
(\wid tx -> withLegacyLayer wid
(byron , postTransactionFee byron wid False tx)
(icarus, postTransactionFee icarus wid False tx)
(byron , postTransactionFee byron wid tx)
(icarus, postTransactionFee icarus wid tx)
)
:<|> (\wid txid -> withLegacyLayer wid
(byron , deleteTransaction byron wid txid)
Expand Down
17 changes: 2 additions & 15 deletions lib/cli/src/Cardano/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ import Cardano.Wallet.Api.Types
, ApiT (..)
, ApiTxId (ApiTxId)
, ApiWallet
, ApiWithdrawRewards (..)
, ByronWalletPostData (..)
, ByronWalletStyle (..)
, Iso8601Time (..)
Expand Down Expand Up @@ -688,7 +687,6 @@ data TransactionCreateArgs t = TransactionCreateArgs
{ _port :: Port "Wallet"
, _id :: WalletId
, _payments :: NonEmpty Text
, _withdrawRewards :: Bool
}

cmdTransactionCreate
Expand All @@ -704,8 +702,7 @@ cmdTransactionCreate mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> withdrawRewardsFlag
exec (TransactionCreateArgs wPort wId wAddressAmounts wWithdraw) = do
exec (TransactionCreateArgs wPort wId wAddressAmounts) = do
wPayments <- either (fail . getTextDecodingError) pure $
traverse (fromText @(AddressAmount Text)) wAddressAmounts
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
Expand All @@ -715,7 +712,6 @@ cmdTransactionCreate mkTxClient mkWalletClient =
runClient wPort Aeson.encodePretty $ postTransaction
mkTxClient
(ApiT wId)
(ApiWithdrawRewards wWithdraw)
(Aeson.object
[ "payments" .= wPayments
, "passphrase" .= ApiT wPwd
Expand All @@ -737,8 +733,7 @@ cmdTransactionFees mkTxClient mkWalletClient =
<$> portOption
<*> walletIdArgument
<*> fmap NE.fromList (some paymentOption)
<*> withdrawRewardsFlag
exec (TransactionCreateArgs wPort wId wAddressAmounts wWithdraw) = do
exec (TransactionCreateArgs wPort wId wAddressAmounts) = do
wPayments <- either (fail . getTextDecodingError) pure $
traverse (fromText @(AddressAmount Text)) wAddressAmounts
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
Expand All @@ -747,7 +742,6 @@ cmdTransactionFees mkTxClient mkWalletClient =
runClient wPort Aeson.encodePretty $ postTransactionFee
mkTxClient
(ApiT wId)
(ApiWithdrawRewards wWithdraw)
(Aeson.object [ "payments" .= wPayments ])
Left _ ->
handleResponse Aeson.encodePretty res
Expand Down Expand Up @@ -1359,13 +1353,6 @@ addressIdArgument :: Parser Text
addressIdArgument = argumentT $ mempty
<> metavar "ADDRESS"

-- | [--withdraw-rewards]
withdrawRewardsFlag :: Parser Bool
withdrawRewardsFlag = switch $ mempty
<> long "withdraw-rewards"
<> help "Withdraw rewards as change in this transaction, provided they \
\contribute positively to the balance."

-- | Helper for writing an option 'Parser' using a 'FromText' instance.
optionT :: FromText a => Mod OptionFields a -> Parser a
optionT = option (eitherReader fromTextS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Cardano.Wallet.Api.Types
, ApiTransaction
, ApiWallet
, ApiWalletDelegationStatus (..)
, ApiWithdrawRewards (..)
, DecodeAddress
, DecodeStakeAddress
, EncodeAddress
Expand Down Expand Up @@ -225,11 +224,23 @@ spec = do
Default Empty
verify rw1 [ expectListSize 0 ]

-- can use rewards with special transaction query param
-- (ApiWithdrawRewards True)
-- can use rewards with an explicit withdrawal request to self.
let payloadWithdrawal = [json|
{ "payments":
[ { "address": #{addr}
, "amount":
{ "quantity": #{coin}
, "unit": "lovelace"
}
}
]
, "passphrase": #{fixturePassphrase},
"withdrawal": "self"
}|]

rTx <- request @(ApiTransaction n) ctx
(Link.createTransaction' @'Shelley src (ApiWithdrawRewards True))
Default (Json payload)
(Link.createTransaction @'Shelley src)
Default (Json payloadWithdrawal)
verify rTx
[ expectField #amount (.> (Quantity coin))
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
Expand Down
7 changes: 5 additions & 2 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ import Data.List.NonEmpty
( NonEmpty )
import Data.Maybe
( fromJust, isJust, mapMaybe )
import Data.Proxy
( Proxy )
import Data.Quantity
( Quantity (..) )
import Data.Set
Expand Down Expand Up @@ -998,10 +1000,11 @@ manageRewardBalance
, Typeable s
, Typeable n
)
=> ctx
=> Proxy n
-> ctx
-> WalletId
-> IO ()
manageRewardBalance ctx wid = db & \DBLayer{..} -> do
manageRewardBalance _ ctx wid = db & \DBLayer{..} -> do
watchNodeTip $ \bh -> do
traceWith tr $ MsgRewardBalanceQuery bh
query <- runExceptT $ do
Expand Down
8 changes: 2 additions & 6 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ import Cardano.Wallet.Api.Types
, Iso8601Time
, MinWithdrawal
, PostExternalTransactionData
, PostPaymentOrWithdrawalDataT
, PostPaymentOrWithdrawalFeeDataT
, PostTransactionDataT
, PostTransactionFeeDataT
, SomeByronWalletPostData
Expand Down Expand Up @@ -314,8 +312,7 @@ type Transactions n =
type CreateTransaction n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "transactions"
:> QueryFlag "withdrawRewards"
:> ReqBody '[JSON] (PostPaymentOrWithdrawalDataT n)
:> ReqBody '[JSON] (PostTransactionDataT n)
:> PostAccepted '[JSON] (ApiTransactionT n)

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/listTransactions
Expand All @@ -339,8 +336,7 @@ type GetTransaction n = "wallets"
type PostTransactionFee n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "payment-fees"
:> QueryFlag "withdrawRewards"
:> ReqBody '[JSON] (PostPaymentOrWithdrawalFeeDataT n)
:> ReqBody '[JSON] (PostTransactionFeeDataT n)
:> PostAccepted '[JSON] ApiFee

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteTransaction
Expand Down
15 changes: 4 additions & 11 deletions lib/core/src/Cardano/Wallet/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ import Cardano.Wallet.Api.Types
, ApiUtxoStatistics
, ApiWallet (..)
, ApiWalletPassphrase
, ApiWithdrawRewards (..)
, ByronWalletPutPassphraseData (..)
, Iso8601Time (..)
, PostExternalTransactionData (..)
, PostPaymentOrWithdrawalDataT
, PostPaymentOrWithdrawalFeeDataT
, PostTransactionDataT
, PostTransactionFeeDataT
, WalletPutData (..)
Expand Down Expand Up @@ -147,12 +144,10 @@ data TransactionClient = TransactionClient
-> ClientM [ApiTransactionT Aeson.Value]
, postTransaction
:: ApiT WalletId
-> ApiWithdrawRewards
-> PostTransactionDataT Aeson.Value
-> ClientM (ApiTransactionT Aeson.Value)
, postTransactionFee
:: ApiT WalletId
-> ApiWithdrawRewards
-> PostTransactionFeeDataT Aeson.Value
-> ClientM ApiFee
, postExternalTransaction
Expand Down Expand Up @@ -279,8 +274,8 @@ transactionClient =
in
TransactionClient
{ listTransactions = (`_listTransactions` Nothing)
, postTransaction = \wid -> _postTransaction wid . coerce
, postTransactionFee = \wid -> _postTransactionFee wid . coerce
, postTransaction = _postTransaction
, postTransactionFee = _postTransactionFee
, postExternalTransaction = _postExternalTransaction
, deleteTransaction = _deleteTransaction
, getTransaction = _getTransaction
Expand All @@ -303,8 +298,8 @@ byronTransactionClient =

in TransactionClient
{ listTransactions = _listTransactions
, postTransaction = \wid _ -> _postTransaction wid
, postTransactionFee = \wid _ -> _postTransactionFee wid
, postTransaction = _postTransaction
, postTransactionFee = _postTransactionFee
, postExternalTransaction = _postExternalTransaction
, deleteTransaction = _deleteTransaction
, getTransaction = _getTransaction
Expand Down Expand Up @@ -389,5 +384,3 @@ type instance ApiTransactionT Aeson.Value = Aeson.Value
type instance PostTransactionDataT Aeson.Value = Aeson.Value
type instance PostTransactionFeeDataT Aeson.Value = Aeson.Value
type instance ApiPutAddressesDataT Aeson.Value = Aeson.Value
type instance PostPaymentOrWithdrawalDataT Aeson.Value = Aeson.Value
type instance PostPaymentOrWithdrawalFeeDataT Aeson.Value = Aeson.Value
37 changes: 2 additions & 35 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ module Cardano.Wallet.Api.Link

-- * Transactions
, createTransaction
, createTransaction'
, listTransactions
, listTransactions'
, getTransactionFee
, getTransactionFee'
, deleteTransaction
, getTransaction

Expand Down Expand Up @@ -93,7 +91,6 @@ import Cardano.Wallet.Api.Types
( ApiPoolId (..)
, ApiT (..)
, ApiTxId (ApiTxId)
, ApiWithdrawRewards (..)
, Iso8601Time
, MinWithdrawal (..)
, WalletStyle (..)
Expand Down Expand Up @@ -331,26 +328,11 @@ createTransaction
=> w
-> (Method, Text)
createTransaction w = discriminate @style
(endpoint @(Api.CreateTransaction Net) (($ False) . ($ wid)))
(endpoint @(Api.CreateTransaction Net) (wid &))
(endpoint @(Api.CreateByronTransaction Net) (wid &))
where
wid = w ^. typed @(ApiT WalletId)

createTransaction'
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> ApiWithdrawRewards
-> (Method, Text)
createTransaction' w (ApiWithdrawRewards withdraw) = discriminate @style
(endpoint @(Api.CreateTransaction Net) (($ withdraw) . ($ wid)))
(endpoint @(Api.CreateByronTransaction Net) (wid &))
where
wid = w ^. typed @(ApiT WalletId)


listTransactions
:: forall (style :: WalletStyle) w.
( Discriminate style
Expand Down Expand Up @@ -388,26 +370,11 @@ getTransactionFee
=> w
-> (Method, Text)
getTransactionFee w = discriminate @style
(endpoint @(Api.PostTransactionFee Net) (($ False) . ($ wid)))
(endpoint @(Api.PostTransactionFee Net) (wid &))
(endpoint @(Api.PostByronTransactionFee Net) (wid &))
where
wid = w ^. typed @(ApiT WalletId)

getTransactionFee'
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> ApiWithdrawRewards
-> (Method, Text)
getTransactionFee' w (ApiWithdrawRewards withdraw) = discriminate @style
(endpoint @(Api.PostTransactionFee Net) (($ withdraw) . ($ wid)))
(endpoint @(Api.PostByronTransactionFee Net) (wid &))
where
wid = w ^. typed @(ApiT WalletId)


deleteTransaction
:: forall (style :: WalletStyle) w t.
( Discriminate style
Expand Down
Loading

0 comments on commit 7c8289c

Please sign in to comment.