Skip to content

Commit 1736316

Browse files
Merge #1859
1859: install a query flag for driving withdrawals creation on Shelley tx r=KtorZ a=KtorZ # Issue Number <!-- Put here a reference to the issue this PR relates to and which requirements it tackles --> # Overview <!-- Detail in a few bullet points the work accomplished in this PR --> - b448b07 📍 **install a query flag for driving withdrawals creation on Shelley transactions** Withdrawals currently occur by default, implicitely which turns out to be very confusing for end-users (seeing their rewards balance disappear freak them out, as well as transaction which seems much bigger than what they requested). # Comments <!-- Additional comments or screenshots to attach if any --> ![Screenshot from 2020-07-06 07-53-08](https://user-images.githubusercontent.com/5680256/86563306-6c21a300-bf64-11ea-81b4-6ad2d53fee65.png) <!-- Don't forget to: ✓ Self-review your changes to make sure nothing unexpected slipped through ✓ Assign yourself to the PR ✓ Assign one or several reviewer(s) ✓ Once created, link this PR to its corresponding ticket ✓ Assign the PR to a corresponding milestone ✓ Acknowledge any changes required to the Wiki --> Co-authored-by: KtorZ <matthias.benkort@gmail.com>
2 parents d4736bc + bcdf6a4 commit 1736316

File tree

11 files changed

+113
-28
lines changed

11 files changed

+113
-28
lines changed

lib/byron/src/Cardano/Wallet/Byron/Api/Server.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ server byron icarus ntp =
152152

153153
transactions :: Server (Transactions n)
154154
transactions =
155-
(\_ _ -> throwError err501)
155+
(\_ _ _ -> throwError err501)
156156
:<|> (\_ _ _ _ -> throwError err501)
157-
:<|> (\_ _ -> throwError err501)
157+
:<|> (\_ _ _ -> throwError err501)
158158
:<|> (\_ _ -> throwError err501)
159159
:<|> (\_ _ -> throwError err501)
160160

@@ -237,11 +237,11 @@ server byron icarus ntp =
237237
(byron , do
238238
let pwd = coerce (getApiT $ tx ^. #passphrase)
239239
genChange <- rndStateChange byron wid pwd
240-
postTransaction byron genChange wid tx
240+
postTransaction byron genChange wid False tx
241241
)
242242
(icarus, do
243243
let genChange k _ = paymentAddress @n k
244-
postTransaction icarus genChange wid tx
244+
postTransaction icarus genChange wid False tx
245245
)
246246
)
247247
:<|>
@@ -251,8 +251,8 @@ server byron icarus ntp =
251251
)
252252
:<|>
253253
(\wid tx -> withLegacyLayer wid
254-
(byron , postTransactionFee byron wid tx)
255-
(icarus, postTransactionFee icarus wid tx)
254+
(byron , postTransactionFee byron wid False tx)
255+
(icarus, postTransactionFee icarus wid False tx)
256256
)
257257
:<|> (\wid txid -> withLegacyLayer wid
258258
(byron , deleteTransaction byron wid txid)

lib/cli/src/Cardano/CLI.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ import Cardano.Wallet.Api.Types
128128
, ApiT (..)
129129
, ApiTxId (ApiTxId)
130130
, ApiWallet
131+
, ApiWithdrawRewards (..)
131132
, ByronWalletPostData (..)
132133
, ByronWalletStyle (..)
133134
, Iso8601Time (..)
@@ -689,6 +690,7 @@ data TransactionCreateArgs t = TransactionCreateArgs
689690
{ _port :: Port "Wallet"
690691
, _id :: WalletId
691692
, _payments :: NonEmpty Text
693+
, _withdrawRewards :: Bool
692694
}
693695

694696
cmdTransactionCreate
@@ -704,7 +706,8 @@ cmdTransactionCreate mkTxClient mkWalletClient =
704706
<$> portOption
705707
<*> walletIdArgument
706708
<*> fmap NE.fromList (some paymentOption)
707-
exec (TransactionCreateArgs wPort wId wAddressAmounts) = do
709+
<*> withdrawRewardsFlag
710+
exec (TransactionCreateArgs wPort wId wAddressAmounts wWithdraw) = do
708711
wPayments <- either (fail . getTextDecodingError) pure $
709712
traverse (fromText @(AddressAmount Text)) wAddressAmounts
710713
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
@@ -714,6 +717,7 @@ cmdTransactionCreate mkTxClient mkWalletClient =
714717
runClient wPort Aeson.encodePretty $ postTransaction
715718
mkTxClient
716719
(ApiT wId)
720+
(ApiWithdrawRewards wWithdraw)
717721
(Aeson.object
718722
[ "payments" .= wPayments
719723
, "passphrase" .= ApiT wPwd
@@ -735,7 +739,8 @@ cmdTransactionFees mkTxClient mkWalletClient =
735739
<$> portOption
736740
<*> walletIdArgument
737741
<*> fmap NE.fromList (some paymentOption)
738-
exec (TransactionCreateArgs wPort wId wAddressAmounts) = do
742+
<*> withdrawRewardsFlag
743+
exec (TransactionCreateArgs wPort wId wAddressAmounts wWithdraw) = do
739744
wPayments <- either (fail . getTextDecodingError) pure $
740745
traverse (fromText @(AddressAmount Text)) wAddressAmounts
741746
res <- sendRequest wPort $ getWallet mkWalletClient $ ApiT wId
@@ -744,6 +749,7 @@ cmdTransactionFees mkTxClient mkWalletClient =
744749
runClient wPort Aeson.encodePretty $ postTransactionFee
745750
mkTxClient
746751
(ApiT wId)
752+
(ApiWithdrawRewards wWithdraw)
747753
(Aeson.object [ "payments" .= wPayments ])
748754
Left _ ->
749755
handleResponse Aeson.encodePretty res
@@ -1335,6 +1341,13 @@ addressIdArgument :: Parser Text
13351341
addressIdArgument = argumentT $ mempty
13361342
<> metavar "ADDRESS"
13371343

1344+
-- | [--withdraw-rewards]
1345+
withdrawRewardsFlag :: Parser Bool
1346+
withdrawRewardsFlag = switch $ mempty
1347+
<> long "withdraw-rewards"
1348+
<> help "Withdraw rewards as change in this transaction, provided they \
1349+
\contribute positively to the balance."
1350+
13381351
-- | Helper for writing an option 'Parser' using a 'FromText' instance.
13391352
optionT :: FromText a => Mod OptionFields a -> Parser a
13401353
optionT = option (eitherReader fromTextS)

lib/cli/test/unit/Cardano/CLISpec.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ spec = do
251251

252252
["transaction", "create", "--help"] `shouldShowUsage`
253253
[ "Usage: transaction create [--port INT] WALLET_ID"
254-
, " --payment PAYMENT"
254+
, " --payment PAYMENT [--withdraw-rewards]"
255255
, " Create and submit a new transaction."
256256
, ""
257257
, "Available options:"
@@ -261,10 +261,14 @@ spec = do
261261
, " --payment PAYMENT address to send to and amount to send"
262262
, " separated by @, e.g."
263263
, " '<amount>@<address>'"
264+
," --withdraw-rewards Withdraw rewards as change in this"
265+
," transaction, provided they contribute"
266+
," positively to the balance."
264267
]
265268

266269
["transaction", "fees", "--help"] `shouldShowUsage`
267270
[ "Usage: transaction fees [--port INT] WALLET_ID --payment PAYMENT"
271+
, " [--withdraw-rewards]"
268272
, " Estimate fees for a transaction."
269273
, ""
270274
, "Available options:"
@@ -274,6 +278,9 @@ spec = do
274278
, " --payment PAYMENT address to send to and amount to send"
275279
, " separated by @, e.g."
276280
, " '<amount>@<address>'"
281+
," --withdraw-rewards Withdraw rewards as change in this"
282+
," transaction, provided they contribute"
283+
," positively to the balance."
277284
]
278285

279286
["transaction", "list", "--help"] `shouldShowUsage`

lib/core-integration/src/Test/Integration/Scenario/API/Shelley/StakePools.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Cardano.Wallet.Api.Types
1919
, ApiT (..)
2020
, ApiTransaction
2121
, ApiWallet
22+
, ApiWithdrawRewards (..)
2223
, DecodeAddress
2324
, EncodeAddress
2425
, WalletStyle (..)
@@ -184,10 +185,12 @@ spec = do
184185
]
185186
, "passphrase": #{fixturePassphrase}
186187
}|]
187-
request @(ApiTransaction n) ctx (Link.createTransaction @'Shelley w) Default (Json payload) >>= flip verify
188-
[ expectField #amount (.> (Quantity coin))
189-
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
190-
]
188+
request @(ApiTransaction n) ctx
189+
(Link.createTransaction' @'Shelley w (ApiWithdrawRewards True))
190+
Default (Json payload) >>= flip verify
191+
[ expectField #amount (.> (Quantity coin))
192+
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
193+
]
191194

192195
-- Rewards are have been consumed.
193196
eventually "Wallet has consumed rewards" $ do

lib/core/src/Cardano/Wallet/Api.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ type Transactions n =
308308
type CreateTransaction n = "wallets"
309309
:> Capture "walletId" (ApiT WalletId)
310310
:> "transactions"
311+
:> QueryFlag "withdrawRewards"
311312
:> ReqBody '[JSON] (PostTransactionDataT n)
312313
:> PostAccepted '[JSON] (ApiTransactionT n)
313314

@@ -331,6 +332,7 @@ type GetTransaction n = "wallets"
331332
type PostTransactionFee n = "wallets"
332333
:> Capture "walletId" (ApiT WalletId)
333334
:> "payment-fees"
335+
:> QueryFlag "withdrawRewards"
334336
:> ReqBody '[JSON] (PostTransactionFeeDataT n)
335337
:> PostAccepted '[JSON] ApiFee
336338

lib/core/src/Cardano/Wallet/Api/Client.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import Cardano.Wallet.Api.Types
7474
, ApiUtxoStatistics
7575
, ApiWallet (..)
7676
, ApiWalletPassphrase
77+
, ApiWithdrawRewards (..)
7778
, ByronWalletPutPassphraseData (..)
7879
, Iso8601Time (..)
7980
, PostExternalTransactionData (..)
@@ -143,10 +144,12 @@ data TransactionClient = TransactionClient
143144
-> ClientM [ApiTransactionT Aeson.Value]
144145
, postTransaction
145146
:: ApiT WalletId
147+
-> ApiWithdrawRewards
146148
-> PostTransactionDataT Aeson.Value
147149
-> ClientM (ApiTransactionT Aeson.Value)
148150
, postTransactionFee
149151
:: ApiT WalletId
152+
-> ApiWithdrawRewards
150153
-> PostTransactionFeeDataT Aeson.Value
151154
-> ClientM ApiFee
152155
, postExternalTransaction
@@ -269,8 +272,8 @@ transactionClient =
269272
in
270273
TransactionClient
271274
{ listTransactions = _listTransactions
272-
, postTransaction = _postTransaction
273-
, postTransactionFee = _postTransactionFee
275+
, postTransaction = \wid -> _postTransaction wid . coerce
276+
, postTransactionFee = \wid -> _postTransactionFee wid . coerce
274277
, postExternalTransaction = _postExternalTransaction
275278
, deleteTransaction = _deleteTransaction
276279
, getTransaction = _getTransaction
@@ -293,8 +296,8 @@ byronTransactionClient =
293296

294297
in TransactionClient
295298
{ listTransactions = _listTransactions
296-
, postTransaction = _postTransaction
297-
, postTransactionFee = _postTransactionFee
299+
, postTransaction = \wid _ -> _postTransaction wid
300+
, postTransactionFee = \wid _ -> _postTransactionFee wid
298301
, postExternalTransaction = _postExternalTransaction
299302
, deleteTransaction = _deleteTransaction
300303
, getTransaction = _getTransaction

lib/core/src/Cardano/Wallet/Api/Link.hs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ module Cardano.Wallet.Api.Link
5858

5959
-- * Transactions
6060
, createTransaction
61+
, createTransaction'
6162
, listTransactions
6263
, listTransactions'
6364
, getTransactionFee
65+
, getTransactionFee'
6466
, deleteTransaction
6567
, getTransaction
6668

@@ -90,6 +92,7 @@ import Cardano.Wallet.Api.Types
9092
( ApiPoolId (..)
9193
, ApiT (..)
9294
, ApiTxId (ApiTxId)
95+
, ApiWithdrawRewards (..)
9396
, Iso8601Time
9497
, WalletStyle (..)
9598
)
@@ -313,11 +316,26 @@ createTransaction
313316
=> w
314317
-> (Method, Text)
315318
createTransaction w = discriminate @style
316-
(endpoint @(Api.CreateTransaction Net) (wid &))
319+
(endpoint @(Api.CreateTransaction Net) (($ False) . ($ wid)))
317320
(endpoint @(Api.CreateByronTransaction Net) (wid &))
318321
where
319322
wid = w ^. typed @(ApiT WalletId)
320323

324+
createTransaction'
325+
:: forall style w.
326+
( HasType (ApiT WalletId) w
327+
, Discriminate style
328+
)
329+
=> w
330+
-> ApiWithdrawRewards
331+
-> (Method, Text)
332+
createTransaction' w (ApiWithdrawRewards withdraw) = discriminate @style
333+
(endpoint @(Api.CreateTransaction Net) (($ withdraw) . ($ wid)))
334+
(endpoint @(Api.CreateByronTransaction Net) (wid &))
335+
where
336+
wid = w ^. typed @(ApiT WalletId)
337+
338+
321339
listTransactions
322340
:: forall (style :: WalletStyle) w.
323341
( Discriminate style
@@ -353,11 +371,26 @@ getTransactionFee
353371
=> w
354372
-> (Method, Text)
355373
getTransactionFee w = discriminate @style
356-
(endpoint @(Api.PostTransactionFee Net) (wid &))
374+
(endpoint @(Api.PostTransactionFee Net) (($ False) . ($ wid)))
357375
(endpoint @(Api.PostByronTransactionFee Net) (wid &))
358376
where
359377
wid = w ^. typed @(ApiT WalletId)
360378

379+
getTransactionFee'
380+
:: forall style w.
381+
( HasType (ApiT WalletId) w
382+
, Discriminate style
383+
)
384+
=> w
385+
-> ApiWithdrawRewards
386+
-> (Method, Text)
387+
getTransactionFee' w (ApiWithdrawRewards withdraw) = discriminate @style
388+
(endpoint @(Api.PostTransactionFee Net) (($ withdraw) . ($ wid)))
389+
(endpoint @(Api.PostByronTransactionFee Net) (wid &))
390+
where
391+
wid = w ^. typed @(ApiT WalletId)
392+
393+
361394
deleteTransaction
362395
:: forall (style :: WalletStyle) w t.
363396
( Discriminate style

lib/core/src/Cardano/Wallet/Api/Server.hs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,9 @@ selectCoins ctx gen (ApiT wid) body =
10401040
fmap mkApiCoinSelection
10411041
$ withWorkerCtx ctx wid liftE liftE
10421042
$ \wrk -> do
1043-
withdrawal <- liftIO $ W.readNextWithdrawal @_ @s @t @k wrk wid
1043+
-- TODO:
1044+
-- Allow representing withdrawals as part of external coin selections.
1045+
let withdrawal = Quantity 0
10441046
let outs = coerceCoin <$> body ^. #payments
10451047
liftHandler $ W.selectCoinsExternal @_ @s @t @k wrk wid gen outs withdrawal
10461048

@@ -1123,14 +1125,17 @@ postTransaction
11231125
=> ctx
11241126
-> ArgGenChange s
11251127
-> ApiT WalletId
1128+
-> Bool
11261129
-> PostTransactionData n
11271130
-> Handler (ApiTransaction n)
1128-
postTransaction ctx genChange (ApiT wid) body = do
1131+
postTransaction ctx genChange (ApiT wid) withdrawRewards body = do
11291132
let outs = coerceCoin <$> (body ^. #payments)
11301133
let pwd = coerce $ getApiT $ body ^. #passphrase
11311134

11321135
selection <- withWorkerCtx ctx wid liftE liftE $ \wrk -> do
1133-
withdrawal <- liftIO $ W.readNextWithdrawal @_ @s @t @k wrk wid
1136+
withdrawal <- if withdrawRewards
1137+
then liftIO $ W.readNextWithdrawal @_ @s @t @k wrk wid
1138+
else pure (Quantity 0)
11341139
liftHandler $ W.selectCoinsForPayment @_ @s @t wrk wid outs withdrawal
11351140

11361141
(tx, meta, time, wit) <- withWorkerCtx ctx wid liftE liftE $ \wrk -> liftHandler $
@@ -1212,12 +1217,15 @@ postTransactionFee
12121217
)
12131218
=> ctx
12141219
-> ApiT WalletId
1220+
-> Bool
12151221
-> PostTransactionFeeData n
12161222
-> Handler ApiFee
1217-
postTransactionFee ctx (ApiT wid) body = do
1223+
postTransactionFee ctx (ApiT wid) withdrawRewards body = do
12181224
let outs = coerceCoin <$> (body ^. #payments)
12191225
withWorkerCtx ctx wid liftE liftE $ \wrk -> do
1220-
withdrawal <- liftIO $ W.readNextWithdrawal @_ @s @t @k wrk wid
1226+
withdrawal <- if withdrawRewards
1227+
then liftIO $ W.readNextWithdrawal @_ @s @t @k wrk wid
1228+
else pure $ Quantity 0
12211229
fee <- liftHandler $ W.estimateFeeForPayment @_ @s @t @k wrk wid outs withdrawal
12221230
pure $ apiFee fee
12231231

lib/core/src/Cardano/Wallet/Api/Types.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module Cardano.Wallet.Api.Types
7979
, ApiPoolId (..)
8080
, ApiWalletMigrationPostData (..)
8181
, ApiWalletMigrationInfo (..)
82+
, ApiWithdrawRewards (..)
8283

8384
-- * API Types (Byron)
8485
, ApiByronWallet (..)
@@ -598,6 +599,9 @@ newtype ApiWalletMigrationInfo = ApiWalletMigrationInfo
598599
{ migrationCost :: Quantity "lovelace" Natural
599600
} deriving (Eq, Generic, Show)
600601

602+
newtype ApiWithdrawRewards = ApiWithdrawRewards Bool
603+
deriving (Eq, Generic, Show)
604+
601605
-- | Error codes returned by the API, in the form of snake_cased strings
602606
data ApiErrorCode
603607
= NoSuchWallet

lib/shelley/src/Cardano/Wallet/Shelley/Api/Server.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ server byron icarus shelley spl ntp =
262262
(byron , do
263263
let pwd = coerce (getApiT $ tx ^. #passphrase)
264264
genChange <- rndStateChange byron wid pwd
265-
postTransaction byron genChange wid tx
265+
postTransaction byron genChange wid False tx
266266
)
267267
(icarus, do
268268
let genChange k _ = paymentAddress @n k
269-
postTransaction icarus genChange wid tx
269+
postTransaction icarus genChange wid False tx
270270
)
271271
)
272272
:<|>
@@ -276,8 +276,8 @@ server byron icarus shelley spl ntp =
276276
)
277277
:<|>
278278
(\wid tx -> withLegacyLayer wid
279-
(byron , postTransactionFee byron wid tx)
280-
(icarus, postTransactionFee icarus wid tx)
279+
(byron , postTransactionFee byron wid False tx)
280+
(icarus, postTransactionFee icarus wid False tx)
281281
)
282282
:<|> (\wid txid -> withLegacyLayer wid
283283
(byron , deleteTransaction byron wid txid)

0 commit comments

Comments
 (0)