From 1aaead65998813e837974e9072f64a616ddd52b1 Mon Sep 17 00:00:00 2001 From: Pawel Jakubas Date: Tue, 14 Dec 2021 16:21:49 +0100 Subject: [PATCH] demonstrate submitting tx by showing proper Pending --- .../src/Test/Integration/Framework/DSL.hs | 8 +- .../Scenario/API/Shelley/TransactionsNew.hs | 94 ++++++++++++++++--- 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/lib/core-integration/src/Test/Integration/Framework/DSL.hs b/lib/core-integration/src/Test/Integration/Framework/DSL.hs index ad927b56dcf..3af53793e0d 100644 --- a/lib/core-integration/src/Test/Integration/Framework/DSL.hs +++ b/lib/core-integration/src/Test/Integration/Framework/DSL.hs @@ -2267,7 +2267,7 @@ signTx -> ApiWallet -> ApiT SealedTx -> [(HTTP.Status, Either RequestException ApiSerialisedTransaction) -> m ()] - -> m (ApiT SealedTx) + -> m ApiSerialisedTransaction signTx ctx w sealedTx expectations = do let toSign = Json [aesonQQ| { "transaction": #{sealedTx} @@ -2276,16 +2276,16 @@ signTx ctx w sealedTx expectations = do let signEndpoint = Link.signTransaction @'Shelley w r <- request @ApiSerialisedTransaction ctx signEndpoint Default toSign verify r expectations - pure $ getFromResponse #transaction r + pure $ getFromResponse Prelude.id r submitTx :: MonadUnliftIO m => Context - -> ApiT SealedTx + -> ApiSerialisedTransaction -> [(HTTP.Status, Either RequestException ApiTxId) -> m ()] -> m ApiTxId submitTx ctx tx expectations = do - let bytes = serialisedTx $ getApiT tx + let bytes = serialisedTx $ getApiT (tx ^. #transaction) let submitEndpoint = Link.postExternalTransaction let headers = Headers [ ("Content-Type", "application/octet-stream") diff --git a/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs b/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs index 86e71656c94..f3e847fab9e 100644 --- a/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs +++ b/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/TransactionsNew.hs @@ -41,6 +41,7 @@ import Cardano.Wallet.Api.Types , ApiStakePool , ApiT (..) , ApiTransaction + , ApiTxId (..) , ApiTxInputGeneral (..) , ApiTxMetadata (..) , ApiTxOutputGeneral (..) @@ -164,6 +165,7 @@ import Test.Integration.Framework.DSL , selectCoins , signTx , submitTx + , submitTxWithWid , unsafeRequest , verify , waitForNextEpoch @@ -275,14 +277,14 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do Just m ) - case getMetadata (cardanoTx $ getApiT signedTx) of + case getMetadata (cardanoTx $ getApiT (signedTx ^. #transaction)) of Nothing -> error "Tx doesn't include metadata" Just m -> case Map.lookup 1 m of Nothing -> error "Tx doesn't include metadata" Just (Cardano.TxMetaText "hello") -> pure () Just _ -> error "Tx metadata incorrect" - let txCbor = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx) + let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx) let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor) let expMetadata = ApiT (TxMetadata (Map.fromList [(1,TxMetaText "hello")])) rDecodedTx <- request @(ApiDecodedTransaction n) ctx @@ -324,7 +326,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ] - let txCbor = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx) + let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx) let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor) rDecodedTx <- request @(ApiDecodedTransaction n) ctx (Link.decodeTransaction @'Shelley wa) Default decodePayload @@ -370,7 +372,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ] - let txCbor = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx) + let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx) let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor) let withdrawalWith ownership wdrls = case wdrls of [wdrl] -> @@ -606,7 +608,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do , assets = ApiT TokenMap.empty , derivationPath = derPath } - let txCbor' = getFromResponse #transaction (HTTP.status202, Right $ ApiSerialisedTransaction signedTx) + let txCbor' = getFromResponse #transaction (HTTP.status202, Right signedTx) let decodePayload' = Json (toJSON $ ApiSerialisedTransaction txCbor') rDecodedTxSource' <- request @(ApiDecodedTransaction n) ctx (Link.decodeTransaction @'Shelley wa) Default decodePayload' @@ -766,6 +768,76 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do (`shouldBe` (initialAmt - 2*amt - expectedFee)) ] + it "TRANS_NEW_CREATE_04a - Single Output Transaction with submitWithWid" $ \ctx -> runResourceT $ do + + let initialAmt = 3 * minUTxOValue (_mainEra ctx) + wa <- fixtureWalletWith @n ctx [initialAmt] + wb <- emptyWallet ctx + let amt = (minUTxOValue (_mainEra ctx) :: Natural) + + payload <- liftIO $ mkTxPayload ctx wb amt + + rTx <- request @(ApiConstructTransaction n) ctx + (Link.createUnsignedTransaction @'Shelley wa) Default payload + verify rTx + [ expectSuccess + , expectResponseCode HTTP.status202 + , expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null)) + , expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null)) + , expectField (#coinSelection . #change) (`shouldSatisfy` (not . null)) + ] + let expectedFee = getFromResponse (#fee . #getQuantity) rTx + + let apiTx = getFromResponse #transaction rTx + + signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ] + + submittedTx <- submitTxWithWid ctx wa signedTx + verify submittedTx + [ expectSuccess + , expectResponseCode HTTP.status202 + ] + let txid = getFromResponse (#id) submittedTx + + let queryTx = Link.getTransaction @'Shelley wa (ApiTxId txid) + rGetTx <- request @(ApiTransaction n) ctx queryTx Default Empty + verify rGetTx + [ expectResponseCode HTTP.status200 + , expectField + (#amount . #getQuantity) + (`shouldBe` initialAmt - (amt + fromIntegral expectedFee)) + , expectField (#direction . #getApiT) (`shouldBe` Outgoing) + , expectField (#status . #getApiT) (`shouldBe` Pending) + ] + + eventually "Source wallet balance is decreased by amt + fee" $ do + rWa <- request @ApiWallet ctx + (Link.getWallet @'Shelley wa) Default Empty + verify rWa + [ expectSuccess + , expectField + (#balance . #available . #getQuantity) + (`shouldBe` initialAmt - (amt + fromIntegral expectedFee)) + ] + + eventually "transaction is eventually in ledger after submitting" $ do + let queryTx' = Link.getTransaction @'Shelley wa (ApiTxId txid) + rSrc <- request @(ApiTransaction n) ctx queryTx' Default Empty + verify rSrc + [ expectResponseCode HTTP.status200 + , expectField (#status . #getApiT) (`shouldBe` InLedger) + ] + + eventually "Target wallet balance is amt" $ do + rWr <- request @ApiWallet ctx + (Link.getWallet @'Shelley wb) Default Empty + verify rWr + [ expectSuccess + , expectField + (#balance . #available . #getQuantity) + (`shouldBe` amt) + ] + it "TRANS_NEW_ASSETS_CREATE_01a - Multi-asset tx with Ada" $ \ctx -> runResourceT $ do wa <- fixtureMultiAssetWallet ctx wb <- emptyWallet ctx @@ -1922,7 +1994,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do , "passphrase": #{fixturePassphrase} }|] let signEndpoint = Link.signTransaction @'Shelley w - signedTx <- getFromResponse #transaction <$> + signedTx <- getFromResponse Prelude.id <$> request @ApiSerialisedTransaction ctx signEndpoint Default toSign -- Submit tx @@ -1938,7 +2010,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do request @(ApiConstructTransaction n) ctx constructEndpoint Default payload -- Submit tx - void $ submitTx ctx sealedTx [ expectResponseCode HTTP.status500 ] + void $ submitTx ctx (ApiSerialisedTransaction sealedTx) [ expectResponseCode HTTP.status500 ] it "TRANS_NEW_SIGN_03 - Sign withdrawals" $ \ctx -> runResourceT $ do (w, _) <- rewardWallet ctx @@ -1956,7 +2028,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do , "passphrase": #{fixturePassphrase} }|] let signEndpoint = Link.signTransaction @'Shelley w - signedTx <- getFromResponse #transaction + signedTx <- getFromResponse Prelude.id <$> request @ApiSerialisedTransaction ctx signEndpoint Default toSign -- Submit tx @@ -1988,7 +2060,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do , "passphrase": #{fixturePassphrase} }|] let signEndpoint = Link.signTransaction @'Shelley w - signedTx <- getFromResponse #transaction <$> + signedTx <- getFromResponse Prelude.id <$> request @ApiSerialisedTransaction ctx signEndpoint Default toSign -- Submit Tx @@ -2086,7 +2158,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do { "transaction": #{sealedTx} , "passphrase": #{fixturePassphrase} }|] - (_, signedTx) <- second (view #transaction) <$> + (_, signedTx) <- second Prelude.id <$> unsafeRequest @ApiSerialisedTransaction ctx signEndpoint toSign -- Submit @@ -2106,7 +2178,7 @@ spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do { "transaction": #{sealedTx'} , "passphrase": #{fixturePassphrase} }|] - (_, signedTx') <- second (view #transaction) <$> + (_, signedTx') <- second Prelude.id <$> unsafeRequest @ApiSerialisedTransaction ctx signEndpoint toSign' -- Submit