Skip to content

Commit

Permalink
use fee estimator in integration scenarios instead of hard-coded fees
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jun 26, 2019
1 parent fee392b commit 9f319f8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
Expand Up @@ -25,6 +25,7 @@ import Test.Integration.Framework.DSL
( Context
, Headers (..)
, Payload (..)
, TxDescription (..)
, amount
, balanceAvailable
, balanceTotal
Expand All @@ -39,6 +40,7 @@ import Test.Integration.Framework.DSL
, expectSuccess
, faucetAmt
, faucetUtxoAmt
, feeEstimator
, fixtureWallet
, fixtureWalletWith
, getWalletEp
Expand Down Expand Up @@ -89,7 +91,10 @@ spec = do
}],
"passphrase": "cardano-wallet"
}|]
let (feeMin, feeMax) = (168609, 168785)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 1
, nOutputs = 1
}

r <- request @(ApiTransaction t) ctx (postTxEp wa) Default payload
verify r
Expand Down Expand Up @@ -145,25 +150,26 @@ spec = do
}],
"passphrase": "cardano-wallet"
}|]
let (feeMin, feeMax) = (181487, 181839)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 2
, nOutputs = 2
}

r <- request @(ApiTransaction t) ctx (postTxEp wSrc) Default payload
ra <- request @ApiWallet ctx (getWalletEp wSrc) Default Empty
verify r
[ expectResponseCode HTTP.status202
, expectFieldBetween amount (feeMin + amt, feeMax + amt)
, expectFieldBetween amount (feeMin + (2*amt), feeMax + (2*amt))
, expectFieldEqual direction Outgoing
, expectFieldEqual status Pending
]

ra <- request @ApiWallet ctx (getWalletEp wSrc) Default Empty
verify ra
[ expectFieldBetween balanceTotal
( faucetAmt - feeMax - amt
, faucetAmt - feeMin - amt
( faucetAmt - feeMax - (2*amt)
, faucetAmt - feeMin - (2*amt)
)
, expectFieldEqual balanceAvailable (faucetAmt - 2 * faucetUtxoAmt)
]

rd <- request @ApiWallet ctx (getWalletEp wDest) Default Empty
verify rd
[ expectEventually ctx balanceAvailable (2*amt)
Expand Down Expand Up @@ -199,25 +205,26 @@ spec = do
],
"passphrase": "cardano-wallet"
}|]
let (feeMin, feeMax) = (181487, 181839)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 2
, nOutputs = 2
}

r <- request @(ApiTransaction t) ctx (postTxEp wSrc) Default payload
ra <- request @ApiWallet ctx (getWalletEp wSrc) Default Empty
verify r
[ expectResponseCode HTTP.status202
, expectFieldBetween amount (feeMin + amt, feeMax + amt)
, expectFieldBetween amount (feeMin + (2*amt), feeMax + (2*amt))
, expectFieldEqual direction Outgoing
, expectFieldEqual status Pending
]

ra <- request @ApiWallet ctx (getWalletEp wSrc) Default Empty
verify ra
[ expectFieldBetween balanceTotal
( faucetAmt - feeMax - amt
, faucetAmt - feeMin - amt
( faucetAmt - feeMax - (2*amt)
, faucetAmt - feeMin - (2*amt)
)
, expectFieldEqual balanceAvailable (faucetAmt - 2 * faucetUtxoAmt)
]

forM_ [wDest1, wDest2] $ \wDest -> do
rd <- request @ApiWallet ctx (getWalletEp wDest) Default payload
verify rd
Expand Down Expand Up @@ -260,7 +267,9 @@ spec = do
]

it "TRANS_CREATE_03 - 0 balance after transaction" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [168_434]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
let amt = 1
wSrc <- fixtureWalletWith ctx [feeMin+amt]
wDest <- emptyWallet ctx
addr:_ <- listAddresses ctx wDest

Expand All @@ -269,7 +278,7 @@ spec = do
"payments": [{
"address": #{destination},
"amount": {
"quantity": 1,
"quantity": #{amt},
"unit": "lovelace"
}
}],
Expand All @@ -278,7 +287,7 @@ spec = do
r <- request @(ApiTransaction t) ctx (postTxEp wSrc) Default payload
verify r
[ expectResponseCode HTTP.status202
, expectFieldEqual amount 168434
, expectFieldEqual amount (feeMin + amt)
, expectFieldEqual direction Outgoing
, expectFieldEqual status Pending
]
Expand All @@ -291,8 +300,8 @@ spec = do

rd <- request @ApiWallet ctx (getWalletEp wDest) Default Empty
verify rd
[ expectEventually ctx balanceAvailable 1
, expectEventually ctx balanceTotal 1
[ expectEventually ctx balanceAvailable amt
, expectEventually ctx balanceTotal amt
]

ra2 <- request @ApiWallet ctx (getWalletEp wSrc) Default Empty
Expand All @@ -302,7 +311,8 @@ spec = do
]

it "TRANS_CREATE_04 - Can't cover fee" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [100_000]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
wSrc <- fixtureWalletWith ctx [feeMin `div` 2]
wDest <- emptyWallet ctx
addr:_ <- listAddresses ctx wDest

Expand All @@ -324,7 +334,8 @@ spec = do
]

it "TRANS_CREATE_04 - Not enough money" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [100_000]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
wSrc <- fixtureWalletWith ctx [feeMin]
wDest <- emptyWallet ctx
addr:_ <- listAddresses ctx wDest

Expand All @@ -342,7 +353,8 @@ spec = do
r <- request @(ApiTransaction t) ctx (postTxEp wSrc) Default payload
verify r
[ expectResponseCode HTTP.status403
, expectErrorMessage (errMsg403NotEnoughMoney 100_000 1000_000)
, expectErrorMessage $
errMsg403NotEnoughMoney (fromIntegral feeMin) 1_000_000
]

it "TRANS_CREATE_04 - Wrong password" $ \ctx -> do
Expand Down
Expand Up @@ -39,6 +39,7 @@ import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain )
import Test.Integration.Framework.DSL
( Context (..)
, TxDescription (..)
, amount
, balanceAvailable
, balanceTotal
Expand All @@ -52,6 +53,7 @@ import Test.Integration.Framework.DSL
, expectValidJSON
, faucetAmt
, faucetUtxoAmt
, feeEstimator
, fixtureWallet
, fixtureWalletWith
, getWalletViaCLI
Expand Down Expand Up @@ -85,7 +87,10 @@ spec = do
let addrStr =
encodeAddress (Proxy @t) (getApiT $ fst $ addr ^. #id)
let amt = 14
let (feeMin, feeMax) = (168609, 168785)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 1
, nOutputs = 1
}
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addrStr
Expand Down Expand Up @@ -133,7 +138,10 @@ spec = do
let addr2 =
encodeAddress (Proxy @t) (getApiT $ fst $ addr !! 2 ^. #id)
let amt = 14
let (feeMin, feeMax) = (181487, 181839)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 2
, nOutputs = 2
}
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addr1
Expand All @@ -145,7 +153,7 @@ spec = do
err `shouldBe` "Please enter a passphrase: **************\nOk.\n"
txJson <- expectValidJSON (Proxy @(ApiTransaction t)) out
verify txJson
[ expectCliFieldBetween amount (feeMin + amt, feeMax + amt)
[ expectCliFieldBetween amount (feeMin + (2*amt), feeMax + (2*amt))
, expectCliFieldEqual direction Outgoing
, expectCliFieldEqual status Pending
]
Expand All @@ -156,8 +164,8 @@ spec = do
gJson <- expectValidJSON (Proxy @ApiWallet) gOutSrc
verify gJson
[ expectCliFieldBetween balanceTotal
( faucetAmt - feeMax - amt
, faucetAmt - feeMin - amt
( faucetAmt - feeMax - (2*amt)
, faucetAmt - feeMin - (2*amt)
)
, expectCliFieldEqual balanceAvailable (faucetAmt - 2*faucetUtxoAmt)
]
Expand All @@ -184,7 +192,10 @@ spec = do
let addr2' =
encodeAddress (Proxy @t) (getApiT $ fst $ addr2 ^. #id)
let amt = 14
let (feeMin, feeMax) = (181487, 181839)
let (feeMin, feeMax) = ctx ^. feeEstimator $ TxDescription
{ nInputs = 2
, nOutputs = 2
}
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addr1'
Expand All @@ -207,8 +218,8 @@ spec = do
gJson <- expectValidJSON (Proxy @ApiWallet) gOutSrc
verify gJson
[ expectCliFieldBetween balanceTotal
( faucetAmt - feeMax - amt
, faucetAmt - feeMin - amt
( faucetAmt - feeMax - (2*amt)
, faucetAmt - feeMin - (2*amt)
)
, expectCliFieldEqual balanceAvailable (faucetAmt - 2*faucetUtxoAmt)
]
Expand Down Expand Up @@ -246,22 +257,23 @@ spec = do
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_03 - 0 balance after transaction" $ \ctx -> do
let balance = 168434
wSrc <- fixtureWalletWith ctx [balance]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
let amt = 1
wSrc <- fixtureWalletWith ctx [feeMin+amt]
wDest <- emptyWallet ctx
addrs:_ <- listAddresses ctx wDest
let addr =
encodeAddress (Proxy @t) (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", "1@" <> addr
, "--payment", toText amt <> "@" <> addr
]

(c, out, err) <- postTransactionViaCLI ctx "Secure Passphrase" args
err `shouldBe` "Please enter a passphrase: *****************\nOk.\n"
txJson <- expectValidJSON (Proxy @(ApiTransaction t)) out
verify txJson
[ expectCliFieldEqual amount balance
[ expectCliFieldEqual amount (feeMin+amt)
, expectCliFieldEqual direction Outgoing
, expectCliFieldEqual status Pending
]
Expand All @@ -274,18 +286,19 @@ spec = do
, expectCliFieldEqual balanceAvailable 0
]

expectEventually' ctx balanceAvailable 1 wDest
expectEventually' ctx balanceTotal 1 wDest
expectEventually' ctx balanceAvailable amt wDest
expectEventually' ctx balanceTotal amt wDest

Stdout gOutDest <- getWalletViaCLI ctx (T.unpack (wDest ^. walletId))
destJson <- expectValidJSON (Proxy @ApiWallet) gOutDest
verify destJson
[ expectCliFieldEqual balanceAvailable 1
, expectCliFieldEqual balanceTotal 1
[ expectCliFieldEqual balanceAvailable amt
, expectCliFieldEqual balanceTotal amt
]

it "TRANS_CREATE_04 - Can't cover fee" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [100_000]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
wSrc <- fixtureWalletWith ctx [feeMin `div` 2]
wDest <- emptyWallet ctx
addrs:_ <- listAddresses ctx wDest
let addr =
Expand All @@ -301,7 +314,8 @@ spec = do
c `shouldBe` ExitFailure 1

it "TRANS_CREATE_04 - Not enough money" $ \ctx -> do
wSrc <- fixtureWalletWith ctx [100_000, 1000]
let (feeMin, _) = ctx ^. feeEstimator $ TxDescription 1 1
wSrc <- fixtureWalletWith ctx [feeMin]
wDest <- emptyWallet ctx
addrs:_ <- listAddresses ctx wDest
let addr =
Expand All @@ -312,7 +326,8 @@ spec = do
]

(c, out, err) <- postTransactionViaCLI ctx "Secure Passphrase" args
(T.unpack err) `shouldContain` (errMsg403NotEnoughMoney 101_000 1000_000)
(T.unpack err) `shouldContain`
errMsg403NotEnoughMoney (fromIntegral feeMin) 1_000_000
out `shouldBe` ""
c `shouldBe` ExitFailure 1

Expand Down

0 comments on commit 9f319f8

Please sign in to comment.