Skip to content

Commit

Permalink
Merge pull request #292 from input-output-hk/KtorZ/286/fix-change-cal…
Browse files Browse the repository at this point in the history
…culation

Fix Change Calculation (#286)
  • Loading branch information
KtorZ authored May 21, 2019
2 parents 624c035 + 252cd12 commit a015aff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
8 changes: 2 additions & 6 deletions lib/core/src/Cardano/Wallet/Primitive/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ import Control.Monad
( foldM, forM )
import Control.Monad.Trans.State.Strict
( State, evalState, runState, state )
import Data.Foldable
( fold )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Generics.Labels
Expand Down Expand Up @@ -348,10 +346,8 @@ changeUTxO
-> Set Tx
-> s
-> UTxO
changeUTxO proxy pending = evalState $ do
ourUtxo <- mapM (state . utxoOurs proxy) (Set.toList pending)
let ins = txIns pending
return $ fold ourUtxo `restrictedBy` ins
changeUTxO proxy pending = evalState $
mconcat <$> mapM (state . utxoOurs proxy) (Set.toList pending)

-- | Construct our _next_ UTxO (possible empty) from a transaction by selecting
-- outputs that are ours. It is important for the transaction outputs to be
Expand Down
20 changes: 14 additions & 6 deletions lib/http-bridge/test/integration/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -52,6 +53,7 @@ module Test.Integration.Framework.DSL
, json
, tearDown
, fixtureWallet
, oneMillionAda

-- * CLI
, cardanoWalletCLI
Expand Down Expand Up @@ -382,14 +384,14 @@ walletId =
_set :: HasType (ApiT WalletId) s => (s, Text) -> s
_set (s, v) = set typed (ApiT $ WalletId (unsafeCreateDigest v)) s

amount :: HasType (Quantity "lovelace" Natural) s => Lens' s Int
amount :: HasType (Quantity "lovelace" Natural) s => Lens' s Natural
amount =
lens _get _set
where
_get :: HasType (Quantity "lovelace" Natural) s => s -> Int
_get = fromIntegral . fromQuantity @"lovelace" @Natural . view typed
_set :: HasType (Quantity "lovelace" Natural) s => (s, Int) -> s
_set (s, v) = set typed (Quantity @"lovelace" @Natural $ fromIntegral v) s
_get :: HasType (Quantity "lovelace" Natural) s => s -> Natural
_get = fromQuantity @"lovelace" @Natural . view typed
_set :: HasType (Quantity "lovelace" Natural) s => (s, Natural) -> s
_set (s, v) = set typed (Quantity @"lovelace" @Natural v) s

direction :: HasType (ApiT Direction) s => Lens' s Direction
direction =
Expand Down Expand Up @@ -430,7 +432,7 @@ fixtureWallet ctx@(Context _ _ _ faucet) = do
Left _ -> fail "fixtureWallet: waited too long for initial transaction"
Right a -> return a
where
oneSecond = 1*1000*1000
oneSecond = 1_000_000
sixtySeconds = 60*oneSecond
checkBalance :: Text -> IO ApiWallet
checkBalance wid = do
Expand All @@ -439,6 +441,12 @@ fixtureWallet ctx@(Context _ _ _ faucet) = do
then return (getFromResponse id r)
else threadDelay oneSecond *> checkBalance wid

-- | One million ADA, in Lovelace, just like this.
oneMillionAda :: Natural
oneMillionAda = ada (1_000_000)
where
ada = (*) (1_000_000)

fromQuantity :: Quantity (u :: Symbol) a -> a
fromQuantity (Quantity a) = a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Test.Integration.Framework.DSL
, expectSuccess
, fixtureWallet
, json
, oneMillionAda
, request
, status
, unsafeRequest
Expand All @@ -55,35 +56,46 @@ spec = do
(wa, wb) <- (,) <$> fixtureWallet ctx <*> fixtureWallet ctx
(_, addrs) <-
unsafeRequest @[ApiAddress] ctx ("GET", getAddresses wb) Empty
let amt = 1
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": 1,
"quantity": #{amt},
"unit": "lovelace"
}
}],
"passphrase": "cardano-wallet"
}|]
let fee = 168653

r <- request @ApiTransaction ctx ("POST", postTx wa) Default payload
verify r
[ expectSuccess
, expectResponseCode HTTP.status202
, expectFieldEqual amount 168654
, expectFieldEqual amount (fee + amt)
, expectFieldEqual direction Outgoing
, expectFieldEqual status Pending
]

r' <- request @ApiWallet ctx ("GET", getWallet wb) Default payload
verify r'
ra <- request @ApiWallet ctx ("GET", getWallet wa) Default payload
verify ra
[ expectSuccess
, expectFieldEqual balanceTotal (oneMillionAda - fee - amt)
, expectFieldEqual balanceAvailable 0
]

rb <- request @ApiWallet ctx ("GET", getWallet wb) Default payload
verify rb
[ expectSuccess
, expectEventually ctx balanceAvailable (oneMillionAda + 1)
, expectEventually ctx balanceAvailable (oneMillionAda + amt)
]

verify ra
[ expectEventually ctx balanceAvailable (oneMillionAda - fee - amt)
]
where
oneMillionAda =
1 * 1000 * 1000 * 1000 * 1000
getAddresses (w :: ApiWallet) =
"v2/wallets/" <> w ^. walletId <> "/addresses"
postTx (w :: ApiWallet) =
Expand Down

0 comments on commit a015aff

Please sign in to comment.