Skip to content

Commit

Permalink
Make migrateWallet endpoint return non-empty tx list.
Browse files Browse the repository at this point in the history
In the case where there really is nothing to migrate, we already return
a 403 error. So there's no point in using types that make it possible to
return an empty list of transactions as part of a successful response.
  • Loading branch information
jonathanknowles committed May 14, 2021
1 parent 5e4f4f7 commit fa958d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -1849,9 +1849,10 @@ migrationPlanToSelectionWithdrawals
=> MigrationPlan
-> Withdrawal
-> NonEmpty Address
-> [(SelectionResult noChange, Withdrawal)]
-> Maybe (NonEmpty (SelectionResult noChange, Withdrawal))
migrationPlanToSelectionWithdrawals plan rewardWithdrawal outputAddressesToCycle
= fst
= NE.nonEmpty
$ fst
$ L.foldr
(accumulate)
([], NE.toList $ NE.cycle outputAddressesToCycle)
Expand Down
6 changes: 4 additions & 2 deletions lib/core/src/Cardano/Wallet/Api.hs
Expand Up @@ -232,6 +232,8 @@ import Data.Generics.Product.Typed
( HasType, typed )
import Data.Kind
( Type )
import Data.List.NonEmpty
( NonEmpty )
import GHC.Generics
( Generic )
import Servant.API
Expand Down Expand Up @@ -519,7 +521,7 @@ type MigrateShelleyWallet n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "migrations"
:> ReqBody '[JSON] (ApiWalletMigrationPostDataT n "raw")
:> PostAccepted '[JSON] [ApiTransactionT n]
:> PostAccepted '[JSON] (NonEmpty (ApiTransactionT n))

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/createShelleyWalletMigrationPlan
type CreateShelleyWalletMigrationPlan n = "wallets"
Expand Down Expand Up @@ -803,7 +805,7 @@ type MigrateByronWallet n = "byron-wallets"
:> Capture "walletId" (ApiT WalletId)
:> "migrations"
:> ReqBody '[JSON] (ApiWalletMigrationPostDataT n "lenient")
:> PostAccepted '[JSON] [ApiTransactionT n]
:> PostAccepted '[JSON] (NonEmpty (ApiTransactionT n))

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/createByronWalletMigrationPlan
type CreateByronWalletMigrationPlan n = "byron-wallets"
Expand Down
28 changes: 16 additions & 12 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -2076,8 +2076,18 @@ mkApiWalletMigrationPlan s addresses rewardWithdrawal plan
Nothing
where
maybeSelections :: Maybe (NonEmpty (ApiCoinSelection n))
maybeSelections = NE.nonEmpty $
mkApiCoinSelectionForMigration <$> unsignedTxs
maybeSelections = fmap mkApiCoinSelectionForMigration <$> maybeUnsignedTxs

maybeSelectionWithdrawals
:: Maybe (NonEmpty (SelectionResult Void, Withdrawal))
maybeSelectionWithdrawals
= W.migrationPlanToSelectionWithdrawals plan rewardWithdrawal
$ getApiT . fst <$> addresses

maybeUnsignedTxs = fmap mkUnsignedTx <$> maybeSelectionWithdrawals
where
mkUnsignedTx (selection, withdrawal) = W.selectionToUnsignedTx
withdrawal (selection {changeGenerated = []}) s

totalFee :: Quantity "lovelace" Natural
totalFee = coinToQuantity $ view #totalFee plan
Expand All @@ -2094,14 +2104,6 @@ mkApiWalletMigrationPlan s addresses rewardWithdrawal plan
& F.foldMap (view #inputBalance)
& mkApiWalletMigrationBalance

selectionWithdrawals :: [(SelectionResult Void, Withdrawal)]
selectionWithdrawals
= W.migrationPlanToSelectionWithdrawals plan rewardWithdrawal
$ getApiT . fst <$> addresses

unsignedTxs = selectionWithdrawals <&> \(selection, withdrawal) ->
W.selectionToUnsignedTx withdrawal (selection {changeGenerated = []}) s

mkApiCoinSelectionForMigration unsignedTx =
mkApiCoinSelection [] Nothing Nothing unsignedTx

Expand All @@ -2125,14 +2127,16 @@ migrateWallet
=> ctx
-> ApiT WalletId
-> ApiWalletMigrationPostData n p
-> Handler [ApiTransaction n]
-> Handler (NonEmpty (ApiTransaction n))
migrateWallet ctx (ApiT wid) postData = do
(rewardWithdrawal, mkRewardAccount) <-
mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
withWorkerCtx ctx wid liftE liftE $ \wrk -> do
plan <- liftHandler $ W.createMigrationPlan wrk wid rewardWithdrawal
txTimeToLive <- liftIO $ W.getTxExpiry ti Nothing
let selectionWithdrawals = W.migrationPlanToSelectionWithdrawals
selectionWithdrawals <- liftHandler
$ failWith ErrCreateMigrationPlanEmpty
$ W.migrationPlanToSelectionWithdrawals
plan rewardWithdrawal addresses
forM selectionWithdrawals $ \(selection, txWithdrawal) -> do
let txContext = defaultTransactionCtx
Expand Down
1 change: 1 addition & 0 deletions specifications/api/swagger.yaml
Expand Up @@ -3712,6 +3712,7 @@ x-responsesMigrateWallet: &responsesMigrateWallet
schema:
type: array
items: *ApiTransaction
minItems: 1

x-responsesDeleteWallet: &responsesDeleteWallet
<<: *responsesErr400
Expand Down

0 comments on commit fa958d9

Please sign in to comment.