Skip to content

Commit

Permalink
demonstrate submitting tx by showing proper Pending
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jan 18, 2022
1 parent d955ea8 commit 1aaead6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Expand Up @@ -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}
Expand All @@ -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")
Expand Down
Expand Up @@ -41,6 +41,7 @@ import Cardano.Wallet.Api.Types
, ApiStakePool
, ApiT (..)
, ApiTransaction
, ApiTxId (..)
, ApiTxInputGeneral (..)
, ApiTxMetadata (..)
, ApiTxOutputGeneral (..)
Expand Down Expand Up @@ -164,6 +165,7 @@ import Test.Integration.Framework.DSL
, selectCoins
, signTx
, submitTx
, submitTxWithWid
, unsafeRequest
, verify
, waitForNextEpoch
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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] ->
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1aaead6

Please sign in to comment.