Skip to content

Commit

Permalink
integration for snapshot - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Feb 7, 2023
1 parent 1f3b2d6 commit 8f29e01
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 69 deletions.
71 changes: 70 additions & 1 deletion lib/wallet/integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ module Test.Integration.Framework.DSL
, emptyByronWalletFromXPrvWith
, rewardWallet
, emptySharedWallet
, fundSharedWallet
, fixtureSharedWallet
, postSharedWallet
, deleteSharedWallet
, getSharedWallet
Expand Down Expand Up @@ -257,7 +259,7 @@ import Cardano.Wallet.Api.Types
, ApiByronWallet
, ApiCoinSelection
, ApiEra (..)
, ApiFee
, ApiFee (..)
, ApiMaintenanceAction (..)
, ApiNetworkInformation
, ApiNetworkParameters (..)
Expand Down Expand Up @@ -1662,6 +1664,73 @@ emptySharedWallet ctx = do
]
pure $ getFromResponse Prelude.id rPost

fundSharedWallet
:: forall (n :: NetworkDiscriminant) m.
( MonadUnliftIO m
, DecodeStakeAddress n
, DecodeAddress n
, EncodeAddress n )
=> Context
-> Natural
-> NonEmpty ApiSharedWallet
-> ResourceT m ()
fundSharedWallet ctx amt sharedWals = do
let wal = case NE.head sharedWals of
ApiSharedWallet (Right wal') -> wal'
_ -> error
"funding of shared wallet make sense only for active one"

rAddr <- request @[ApiAddress n] ctx
(Link.listAddresses @'Shared wal) Default Empty
expectResponseCode HTTP.status200 rAddr
let sharedAddrs = getFromResponse Prelude.id rAddr
let destination = (sharedAddrs !! 1) ^. #id

wShelley <- fixtureWallet ctx
let payloadTx = Json [aesonQQ|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}],
"passphrase": #{fixturePassphrase}
}|]
(_, ApiFee (Quantity _) (Quantity feeMax) _ _) <- unsafeRequest ctx
(Link.getTransactionFeeOld @'Shelley wShelley) payloadTx
let ep = Link.createTransactionOld @'Shelley
rTx <- request @(ApiTransaction n) ctx (ep wShelley) Default payloadTx
expectResponseCode HTTP.status202 rTx
eventually "wShelley balance is decreased" $ do
ra <- request @ApiWallet ctx
(Link.getWallet @'Shelley wShelley) Default Empty
expectField
(#balance . #available)
(`shouldBe` Quantity (faucetAmt - feeMax - amt)) ra

forM_ sharedWals $ \walShared -> do
rWal <- getSharedWallet ctx walShared
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField (traverse . #balance . #available)
(`shouldBe` Quantity amt)
]

fixtureSharedWallet
:: forall (n :: NetworkDiscriminant) m.
( MonadUnliftIO m
, MonadFail m
, DecodeStakeAddress n
, DecodeAddress n
, EncodeAddress n )
=> Context
-> ResourceT m ApiActiveSharedWallet
fixtureSharedWallet ctx = do
walShared@(ApiSharedWallet (Right wal)) <- emptySharedWallet ctx
fundSharedWallet @n ctx faucetUtxoAmt (NE.fromList [walShared])
return wal

postSharedWallet
:: MonadUnliftIO m
=> Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import Cardano.Wallet.Api.Types
( ApiAddress
, ApiConstructTransaction (..)
, ApiDecodedTransaction (..)
, ApiFee (..)
, ApiScriptTemplate (..)
, ApiSerialisedTransaction (..)
, ApiSharedWallet (..)
Expand Down Expand Up @@ -122,10 +121,10 @@ import Test.Integration.Framework.DSL
, expectListSize
, expectResponseCode
, expectSuccess
, faucetAmt
, faucetUtxoAmt
, fixturePassphrase
, fixtureWallet
, fixtureSharedWallet
, fundSharedWallet
, genMnemonics
, getFromResponse
, getSharedWallet
Expand All @@ -140,7 +139,6 @@ import Test.Integration.Framework.DSL
, submitSharedTxWithWid
, toQueryString
, unsafeGetTransactionTime
, unsafeRequest
, utcIso8601ToText
, verify
, walletId
Expand Down Expand Up @@ -273,7 +271,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
]

let amt = 10 * minUTxOValue (_mainEra ctx)
fundSharedWallet ctx amt (NE.fromList [walShared])
fundSharedWallet @n ctx amt (NE.fromList [walShared])

rTx2 <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
Expand Down Expand Up @@ -383,7 +381,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let metadata = Json [json|{ "metadata": { "1": "hello" } }|]

let amt = 10 * minUTxOValue (_mainEra ctx)
fundSharedWallet ctx amt (NE.fromList [walShared])
fundSharedWallet @n ctx amt (NE.fromList [walShared])

rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
Expand Down Expand Up @@ -498,7 +496,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Empty payload is not allowed" $
\ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
let emptyPayload = Json [json|{}|]

rTx <- request @(ApiConstructTransaction n) ctx
Expand All @@ -512,7 +510,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Validity interval only is not allowed" $
\ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
let validityInterval = Json [json|
{ "validity_interval":
{ "invalid_before":
Expand All @@ -538,7 +536,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Single Output Transaction with decode transaction - single party" $
\ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)

Expand Down Expand Up @@ -669,7 +667,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Cannot spend less than minUTxOValue" $
\ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
let amt = minUTxOValue (_mainEra ctx) - 1

Expand All @@ -686,7 +684,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Can't cover fee" $
\ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx

payload <- liftIO $ mkTxPayload ctx wb faucetUtxoAmt
Expand All @@ -702,7 +700,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Multiple Output Tx to single wallet"
$ \ctx -> runResourceT $ do

wa <- fixtureSharedWallet ctx
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
addrs <- listAddresses @n ctx wb
let amt = minUTxOValue (_mainEra ctx) :: Natural
Expand Down Expand Up @@ -982,7 +980,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\Can list Incoming and Outgoing transactions" $
\ctx -> runResourceT $ do

(wSrc, wDest) <- (,) <$> fixtureSharedWallet ctx <*> emptyWallet ctx
(wSrc, wDest) <- (,) <$> fixtureSharedWallet @n ctx <*> emptyWallet ctx
addrs <- listAddresses @n ctx wDest

let amt = minUTxOValue (_mainEra ctx) :: Natural
Expand Down Expand Up @@ -1052,7 +1050,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let amt1 = minUTxOValue (_mainEra ctx)
let amt2 = 2 * amt1
(wSrc, wDest@(ApiSharedWallet (Right walDest))) <-
(,) <$> fixtureSharedWallet ctx <*> emptySharedWallet ctx
(,) <$> fixtureSharedWallet @n ctx <*> emptySharedWallet ctx

-- destination wallet
rAddr <- request @[ApiAddress n] ctx
Expand Down Expand Up @@ -1476,7 +1474,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\ctx -> runResourceT $ do

(wSrc, (ApiSharedWallet (Right walDest))) <-
(,) <$> fixtureSharedWallet ctx <*> emptySharedWallet ctx
(,) <$> fixtureSharedWallet @n ctx <*> emptySharedWallet ctx
-- post tx
let amt = minUTxOValue (_mainEra ctx) :: Natural
rAddr <- request @[ApiAddress n] ctx
Expand Down Expand Up @@ -1565,7 +1563,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
\ctx -> runResourceT $ do

(wSrc, (ApiSharedWallet (Right walDest))) <-
(,) <$> fixtureSharedWallet ctx <*> emptySharedWallet ctx
(,) <$> fixtureSharedWallet @n ctx <*> emptySharedWallet ctx
-- post tx
let amt = minUTxOValue (_mainEra ctx) :: Natural
rAddr <- request @[ApiAddress n] ctx
Expand Down Expand Up @@ -1632,7 +1630,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do

fixtureSharedWalletWith ctx amt = do
(wSrc, wDest@(ApiSharedWallet (Right walDest))) <-
(,) <$> fixtureSharedWallet ctx <*> emptySharedWallet ctx
(,) <$> fixtureSharedWallet @n ctx <*> emptySharedWallet ctx

-- destination wallet
rAddr <- request @[ApiAddress n] ctx
Expand Down Expand Up @@ -1679,54 +1677,6 @@ spec = describe "SHARED_TRANSACTIONS" $ do
[ expectResponseCode HTTP.status202
]

fundSharedWallet ctx amt sharedWals = do
let wal = case NE.head sharedWals of
ApiSharedWallet (Right wal') -> wal'
_ -> error
"funding of shared wallet make sense only for active one"

rAddr <- request @[ApiAddress n] ctx
(Link.listAddresses @'Shared wal) Default Empty
expectResponseCode HTTP.status200 rAddr
let sharedAddrs = getFromResponse Prelude.id rAddr
let destination = (sharedAddrs !! 1) ^. #id

wShelley <- fixtureWallet ctx
let payloadTx = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}],
"passphrase": #{fixturePassphrase}
}|]
(_, ApiFee (Quantity _) (Quantity feeMax) _ _) <- unsafeRequest ctx
(Link.getTransactionFeeOld @'Shelley wShelley) payloadTx
let ep = Link.createTransactionOld @'Shelley
rTx <- request @(ApiTransaction n) ctx (ep wShelley) Default payloadTx
expectResponseCode HTTP.status202 rTx
eventually "wShelley balance is decreased" $ do
ra <- request @ApiWallet ctx
(Link.getWallet @'Shelley wShelley) Default Empty
expectField
(#balance . #available)
(`shouldBe` Quantity (faucetAmt - feeMax - amt)) ra

forM_ sharedWals $ \walShared -> do
rWal <- getSharedWallet ctx walShared
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField (traverse . #balance . #available)
(`shouldBe` Quantity amt)
]

fixtureSharedWallet ctx = do
walShared@(ApiSharedWallet (Right wal)) <- emptySharedWallet ctx
fundSharedWallet ctx faucetUtxoAmt (NE.fromList [walShared])
return wal

fixtureTwoPartySharedWallet ctx = do

let index = 30
Expand Down Expand Up @@ -1782,7 +1732,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let walShared2@(ApiSharedWallet (Right walB)) =
getFromResponse Prelude.id rPostB

fundSharedWallet
fundSharedWallet @n
ctx faucetUtxoAmt (NE.fromList [walShared1, walShared2])

return (walA, walB)
Expand Down Expand Up @@ -1869,7 +1819,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let walShared2@(ApiSharedWallet (Right walB)) =
getFromResponse Prelude.id rPatchB

fundSharedWallet ctx faucetUtxoAmt
fundSharedWallet @n ctx faucetUtxoAmt
(NE.fromList [walShared1, walShared2])

return
Expand Down Expand Up @@ -1951,7 +1901,7 @@ spec = describe "SHARED_TRANSACTIONS" $ do
let walShared3@(ApiSharedWallet (Right walC)) =
getFromResponse Prelude.id rPostC

fundSharedWallet ctx faucetUtxoAmt
fundSharedWallet @n ctx faucetUtxoAmt
(NE.fromList [walShared1, walShared2, walShared3])

return (walA, walB, walC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Cardano.Wallet.Api.Types
, ApiTransaction
, ApiUtxoStatistics
, ApiWallet
, ApiWalletUtxoSnapshot
, DecodeAddress
, DecodeStakeAddress
, EncodeAddress (..)
Expand Down Expand Up @@ -103,6 +104,7 @@ import Test.Integration.Framework.DSL
, expectWalletUTxO
, faucetAmt
, fixturePassphrase
, fixtureSharedWallet
, fixtureWallet
, genMnemonics
, genXPubsBech32
Expand Down Expand Up @@ -1552,6 +1554,25 @@ spec = describe "SHARED_WALLETS" $ do
r <- request @ApiUtxoStatistics ctx (Link.getUTxOsStatistics @'Shared w) headers Empty
verify r expectations

it "SHARED_WALLETS_UTXO_SNAPSHOT_01 - \
\Can generate UTxO snapshot of empty wallet" $
\ctx -> runResourceT $ do
(ApiSharedWallet (Right w)) <- emptySharedWallet ctx
rSnap <- request @ApiWalletUtxoSnapshot ctx
(Link.getWalletUtxoSnapshot @'Shared w) Default Empty
expectResponseCode HTTP.status200 rSnap
expectField #entries (`shouldBe` []) rSnap

it "SHARED_WALLETS_UTXO_SNAPSHOT_02 - \
\Can generate UTxO snapshot of pure-ada wallet" $
\ctx -> runResourceT $ do
w <- fixtureSharedWallet @n ctx
rSnap <- request @ApiWalletUtxoSnapshot ctx
(Link.getWalletUtxoSnapshot @'Shared w) Default Empty
expectResponseCode HTTP.status200 rSnap
let entries = getFromResponse #entries rSnap
length entries `shouldBe` 1

where
acctHrp = [Bech32.humanReadablePart|acct_shared_xvk|]
getAccountWallet name = do
Expand Down

0 comments on commit 8f29e01

Please sign in to comment.