Skip to content

Commit

Permalink
Byron asset tx tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Stachyra committed Feb 8, 2021
1 parent 5a80689 commit c113c35
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 44 deletions.
134 changes: 133 additions & 1 deletion lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ module Test.Integration.Framework.DSL
, fixtureWalletWith
, fixtureWalletWithMnemonics
, fixtureMultiAssetWallet
, fixtureMultiAssetRandomWallet
, fixtureMultiAssetIcarusWallet
, faucetAmt
, faucetUtxoAmt
, proc'
, postTx
, pickAnAsset
, mkTxPayloadMA
, waitForServer
, for
, utcIso8601ToText
Expand Down Expand Up @@ -318,6 +322,8 @@ import Data.Set
( Set )
import Data.Text
( Text )
import Data.Text.Class
( ToText (..) )
import Data.Time
( NominalDiffTime, UTCTime )
import Data.Time.Text
Expand All @@ -342,7 +348,7 @@ import System.IO
import Test.Hspec
( Expectation, HasCallStack, expectationFailure )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain, shouldSatisfy )
( shouldBe, shouldContain, shouldNotBe, shouldSatisfy )
import Test.HUnit.Lang
( FailureReason (..), HUnitFailure (..) )
import Test.Integration.Faucet
Expand Down Expand Up @@ -379,6 +385,8 @@ import qualified Cardano.Wallet.Primitive.AddressDerivation.Icarus as Icarus
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Primitive.Types.TokenQuantity as TokenQuantity
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.Scrypt as Scrypt
Expand Down Expand Up @@ -618,6 +626,43 @@ defaultTxTTL = 7200
--
-- Helpers
--
pickAnAsset :: TokenMap.TokenMap -> ((Text, Text), Natural)
pickAnAsset tm = case TokenMap.toFlatList tm of
(TokenBundle.AssetId pid an, TokenQuantity.TokenQuantity q):_ ->
((toText pid, toText an), q)
_ -> error "pickAnAsset: empty TokenMap"

-- Like mkTxPayload, except that assets are included in the payment.
-- Asset amounts are specified by ((PolicyId Hex, AssetName Hex), amount).
mkTxPayloadMA
:: forall n m.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
, MonadUnliftIO m
)
=> (ApiT Address, Proxy n)
-> Natural
-> [((Text, Text), Natural)]
-> Text
-> m Payload
mkTxPayloadMA destination coin val passphrase = do
let assetJson ((pid, name), q) = [aesonQQ|{
"policy_id": #{pid},
"asset_name": #{name},
"quantity": #{q}
}|]
return $ Json [aesonQQ|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{coin},
"unit": "lovelace"
},
"assets": #{map assetJson val}
}],
"passphrase": #{passphrase}
}|]

postTx
:: forall n w m.
Expand Down Expand Up @@ -1171,6 +1216,93 @@ fixtureMultiAssetWallet
-> ResourceT m ApiWallet
fixtureMultiAssetWallet = fmap fst . fixtureWalletWithMnemonics (Proxy @"ma")

fixtureMultiAssetRandomWallet
:: forall n m.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
, MonadIO m
, MonadUnliftIO m
)
=> Context
-> ResourceT m ApiByronWallet
fixtureMultiAssetRandomWallet ctx = do
wMA <- fixtureMultiAssetWallet ctx
wB <- fixtureRandomWallet ctx

-- create Byron address
let p = Json [aesonQQ| { "passphrase": #{fixturePassphrase} }|]
r <- request @(ApiAddress n) ctx (Link.postRandomAddress wB) Default p
expectSuccess r

-- pick out assets to send
let assetsSrc = wMA ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue <$ pickAnAsset assetsSrc

rL <- request @[ApiAddress n] ctx (Link.listAddresses @'Byron wB) Default Empty
let addrs = getFromResponse id rL
let destination = (addrs !! 1) ^. #id
payload <- mkTxPayloadMA @n destination 0 [val] fixturePassphrase

-- send assets to Byron wallet
rtx <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Shelley wMA) Default payload
expectResponseCode HTTP.status202 rtx

eventually "Byron wallet has assets" $ do
rb <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wB) Default Empty
verify rb
[ expectField (#assets . #available . #getApiT)
(`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT)
(`shouldNotBe` TokenMap.empty)
, expectField (#state . #getApiT) (`shouldBe` Ready)
]
return (getFromResponse id rb)


fixtureMultiAssetIcarusWallet
:: forall n m.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
, MonadIO m
, MonadUnliftIO m
)
=> Context
-> ResourceT m ApiByronWallet
fixtureMultiAssetIcarusWallet ctx = do
wMA <- fixtureMultiAssetWallet ctx
wB <- fixtureIcarusWallet ctx

-- pick out assets to send
let assetsSrc = wMA ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue <$ pickAnAsset assetsSrc

rL <- request @[ApiAddress n] ctx (Link.listAddresses @'Byron wB) Default Empty
let addrs = getFromResponse id rL
let destination = (addrs !! 1) ^. #id
payload <- mkTxPayloadMA @n destination 0 [val] fixturePassphrase

-- send assets to Icarus wallet
rtx <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Shelley wMA) Default payload
expectResponseCode HTTP.status202 rtx

eventually "Icarus wallet has assets" $ do
rb <- request @ApiByronWallet ctx
(Link.getWallet @'Byron wB) Default Empty
verify rb
[ expectField (#assets . #available . #getApiT)
(`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT)
(`shouldNotBe` TokenMap.empty)
]
return (getFromResponse id rb)

fixtureRawTx
:: Context
-> (Address, Natural)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import Cardano.Wallet.Api.Types
)
import Cardano.Wallet.Primitive.AddressDerivation
( PaymentAddress )
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.Types.Tx
Expand All @@ -49,7 +51,7 @@ import Numeric.Natural
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe )
( shouldBe, shouldNotBe )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
Expand All @@ -70,13 +72,17 @@ import Test.Integration.Framework.DSL
, faucetAmt
, faucetUtxoAmt
, fixtureIcarusWallet
, fixtureMultiAssetIcarusWallet
, fixtureMultiAssetRandomWallet
, fixturePassphrase
, fixtureRandomWallet
, fixtureWallet
, getFromResponse
, json
, listAddresses
, minUTxOValue
, mkTxPayloadMA
, pickAnAsset
, postByronWallet
, postTx
, request
Expand All @@ -91,6 +97,7 @@ import Test.Integration.Framework.TestData
( errMsg400StartTimeLaterThanEndTime, errMsg404NoWallet )

import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Data.Text as T
import qualified Network.HTTP.Types.Status as HTTP

Expand All @@ -103,10 +110,94 @@ spec :: forall n.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
, PaymentAddress n ByronKey
, PaymentAddress n IcarusKey
) => SpecWith Context
spec = describe "BYRON_TRANSACTIONS" $ do

describe "BYRON_TRANS_ASSETS_CREATE_01 - Multi-asset transaction with ADA" $
forM_ [ (fixtureMultiAssetRandomWallet @n, "Byron wallet")
, (fixtureMultiAssetIcarusWallet @n, "Icarus wallet")] $
\(srcFixture, name) -> it name $ \ctx -> runResourceT $ do

wSrc <- srcFixture ctx
wDest <- emptyWallet ctx

-- pick out an asset to send
let assetsSrc = wSrc ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue <$ pickAnAsset assetsSrc

addrs <- listAddresses @n ctx wDest
let destination = (addrs !! 1) ^. #id
payload <- mkTxPayloadMA @n destination (minUTxOValue * 2) [val] fixturePassphrase

rtx <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Byron wSrc) Default payload
expectResponseCode HTTP.status202 rtx

eventually "Payee wallet balance is as expected" $ do
rb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wDest) Default Empty
verify rb
[ expectField (#assets . #available . #getApiT)
(`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT)
(`shouldNotBe` TokenMap.empty)
]

describe "BYRON_TRANS_ASSETS_CREATE_02 - Multi-asset transaction with too little ADA" $
forM_ [ (fixtureMultiAssetRandomWallet @n, "Byron wallet")
, (fixtureMultiAssetIcarusWallet @n, "Icarus wallet")] $
\(srcFixture, name) -> it name $ \ctx -> runResourceT $ do

wSrc <- srcFixture ctx
wDest <- emptyWallet ctx

-- pick out an asset to send
let assetsSrc = wSrc ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue <$ pickAnAsset assetsSrc

addrs <- listAddresses @n ctx wDest
let destination = (addrs !! 1) ^. #id
payload <- mkTxPayloadMA @n destination minUTxOValue [val] fixturePassphrase

rtx <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Byron wSrc) Default payload
expectResponseCode HTTP.status403 rtx

describe "BYRON_TRANS_ASSETS_CREATE_02a - Multi-asset transaction with no ADA" $
forM_ [ (fixtureMultiAssetRandomWallet @n, "Byron wallet")
, (fixtureMultiAssetIcarusWallet @n, "Icarus wallet")] $
\(srcFixture, name) -> it name $ \ctx -> runResourceT $ do

wSrc <- srcFixture ctx
wDest <- emptyWallet ctx

-- pick out an asset to send
let assetsSrc = wSrc ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue <$ pickAnAsset assetsSrc

addrs <- listAddresses @n ctx wDest
let destination = (addrs !! 1) ^. #id
payload <- mkTxPayloadMA @n destination 0 [val] fixturePassphrase

rtx <- request @(ApiTransaction n) ctx
(Link.createTransaction @'Byron wSrc) Default payload
expectResponseCode HTTP.status202 rtx

eventually "Payee wallet balance is as expected" $ do
rb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wDest) Default Empty
verify rb
[ expectField (#assets . #available . #getApiT)
(`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT)
(`shouldNotBe` TokenMap.empty)
]

describe "BYRON_TRANS_CREATE_01 - Single Output Transaction with non-Shelley witnesses" $
forM_ [(fixtureRandomWallet, "Byron wallet"), (fixtureIcarusWallet, "Icarus wallet")] $
\(srcFixture,name) -> it name $ \ctx -> runResourceT $ do
Expand Down
Loading

0 comments on commit c113c35

Please sign in to comment.