@@ -58,6 +58,10 @@ import qualified Cardano.Wallet.WalletLayer as WalletLayer
58
58
59
59
import Util.Buildable (ShowThroughBuild (.. ))
60
60
61
+ import qualified Cardano.Wallet.API.V1.Handlers.Transactions as Handlers
62
+ import Control.Monad.Except (runExceptT )
63
+ import Servant.Server
64
+
61
65
{-# ANN module ("HLint: ignore Reduce duplication" :: Text) #-}
62
66
63
67
-- | Do not pollute the test runner output with logs.
@@ -138,44 +142,62 @@ withFixture initialBalance toPay cc = do
138
142
walletSendTx = \ _tx -> return False
139
143
}
140
144
145
+ -- | Generate a fresh change 'Address', throwing an exception if the underlying
146
+ -- operation fails.
141
147
genChangeAddr :: AccountId -> PassiveWallet -> IO Address
142
148
genChangeAddr accountId pw = do
143
149
res <- Kernel. createAddress mempty accountId pw
144
150
case res of
145
151
Right addr -> pure addr
146
152
Left err -> throwM err
147
153
154
+ -- | A fake signer which doesn't check for 'Address' ownership.
148
155
fakeSigner :: PassPhrase
149
156
-> Maybe EncryptedSecretKey
150
157
-> Address
151
158
-> Either CoinSelHardErr SafeSigner
152
159
fakeSigner _ Nothing addr = Left (CoinSelHardErrAddressNotOwned (Proxy @ Cardano ) addr)
153
160
fakeSigner _ (Just esk) _ = Right (FakeSigner (encToSecret esk))
154
161
162
+ -- | A constant fee calculation.
155
163
constantFee :: Int -> NonEmpty Coin -> Coin
156
164
constantFee _ _ = mkCoin 10
157
165
166
+ -- | Helper function to facilitate payments via the Layer or Servant.
167
+ withPayment :: MonadIO n
168
+ => InitialBalance
169
+ -- ^ How big the wallet Utxo must be
170
+ -> Pay
171
+ -- ^ How big the payment must be
172
+ -> (ActiveWalletLayer n -> V1. Payment -> IO () )
173
+ -- ^ The action to run.
174
+ -> PropertyM IO ()
175
+ withPayment initialBalance toPay action = do
176
+ withFixture initialBalance toPay $ \ keystore activeLayer Fixture {.. } -> do
177
+ liftIO $ Keystore. insert (WalletIdHdRnd fixtureHdRootId) fixtureESK keystore
178
+ let (AccountIdHdRnd hdAccountId) = fixtureAccountId
179
+ let (HdRootId (InDb rootAddress)) = fixtureHdRootId
180
+ let sourceWallet = V1. WalletId (sformat build rootAddress)
181
+ let accountIndex = hdAccountId ^. hdAccountIdIx . to getHdAccountIx
182
+ let destinations =
183
+ fmap (\ (addr, coin) -> V1. PaymentDistribution (V1. V1 addr) (V1. V1 coin)
184
+ ) fixturePayees
185
+ let newPayment = V1. Payment {
186
+ pmtSource = V1. PaymentSource sourceWallet accountIndex
187
+ , pmtDestinations = destinations
188
+ , pmtGroupingPolicy = Nothing
189
+ , pmtSpendingPassword = Nothing
190
+ }
191
+ action activeLayer newPayment
192
+
158
193
spec :: Spec
159
194
spec = describe " NewPayment" $ do
160
195
161
196
describe " Generating a new payment (wallet layer)" $ do
162
197
163
198
prop " pay works (realSigner, SenderPaysFee)" $ withMaxSuccess 50 $ do
164
199
monadicIO $
165
- withFixture @ IO (InitialADA 10000 ) (PayLovelace 10 ) $ \ _ activeLayer Fixture {.. } -> do
166
- let (AccountIdHdRnd hdAccountId) = fixtureAccountId
167
- let (HdRootId (InDb rootAddress)) = fixtureHdRootId
168
- let sourceWallet = V1. WalletId (sformat build rootAddress)
169
- let accountIndex = hdAccountId ^. hdAccountIdIx . to getHdAccountIx
170
- let destinations =
171
- fmap (\ (addr, coin) -> V1. PaymentDistribution (V1. V1 addr) (V1. V1 coin)
172
- ) fixturePayees
173
- let newPayment = V1. Payment {
174
- pmtSource = V1. PaymentSource sourceWallet accountIndex
175
- , pmtDestinations = destinations
176
- , pmtGroupingPolicy = Nothing
177
- , pmtSpendingPassword = Nothing
178
- }
200
+ withPayment (InitialADA 10000 ) (PayLovelace 10 ) $ \ activeLayer newPayment -> do
179
201
res <- liftIO ((WalletLayer. pay activeLayer) mempty
180
202
IgnoreGrouping
181
203
SenderPaysFee
@@ -205,7 +227,6 @@ spec = describe "NewPayment" $ do
205
227
prop " newTransaction works (fake signer, ReceiverPaysFee)" $ withMaxSuccess 50 $ do
206
228
monadicIO $
207
229
withFixture @ IO (InitialADA 10000 ) (PayADA 1 ) $ \ _ _ Fixture {.. } -> do
208
- allWallets <- Kernel. hdWallets <$> Kernel. getWalletSnapshot fixturePw
209
230
let opts = (newOptions Kernel. cardanoFee) {
210
231
csoExpenseRegulation = ReceiverPaysFee
211
232
, csoInputGrouping = IgnoreGrouping
@@ -220,26 +241,21 @@ spec = describe "NewPayment" $ do
220
241
)
221
242
liftIO ((bimap STB STB res) `shouldSatisfy` isRight)
222
243
244
+ describe " Generating a new payment (Servant)" $ do
245
+
246
+ prop " works as expected in the happy path scenario" $ withMaxSuccess 50 $
247
+ monadicIO $
248
+ withPayment (InitialADA 1000 ) (PayADA 1 ) $ \ activeLayer newPayment -> do
249
+ res <- liftIO (runExceptT . runHandler' $ Handlers. newTransaction activeLayer newPayment)
250
+ liftIO ((bimap identity STB res) `shouldSatisfy` isRight)
251
+
223
252
describe " EstimateFees" $ do
224
253
225
254
describe " Estimating fees (wallet layer)" $ do
226
255
227
256
prop " estimating fees works (SenderPaysFee)" $ withMaxSuccess 50 $ do
228
257
monadicIO $
229
- withFixture @ IO (InitialADA 10000 ) (PayLovelace 10 ) $ \ _ activeLayer Fixture {.. } -> do
230
- let (AccountIdHdRnd hdAccountId) = fixtureAccountId
231
- let (HdRootId (InDb rootAddress)) = fixtureHdRootId
232
- let sourceWallet = V1. WalletId (sformat build rootAddress)
233
- let accountIndex = hdAccountId ^. hdAccountIdIx . to getHdAccountIx
234
- let destinations =
235
- fmap (\ (addr, coin) -> V1. PaymentDistribution (V1. V1 addr) (V1. V1 coin)
236
- ) fixturePayees
237
- let newPayment = V1. Payment {
238
- pmtSource = V1. PaymentSource sourceWallet accountIndex
239
- , pmtDestinations = destinations
240
- , pmtGroupingPolicy = Nothing
241
- , pmtSpendingPassword = Nothing
242
- }
258
+ withPayment (InitialADA 10000 ) (PayLovelace 10 ) $ \ activeLayer newPayment -> do
243
259
res <- liftIO ((WalletLayer. estimateFees activeLayer) mempty
244
260
IgnoreGrouping
245
261
SenderPaysFee
@@ -314,17 +330,10 @@ spec = describe "NewPayment" $ do
314
330
Left e -> fail (formatToString build e)
315
331
Right x -> x `shouldSatisfy` (> (Coin 0 ))
316
332
317
- {- -
318
- describe "Generating a new payment (Servant)" $ do
319
- prop "works as expected in the happy path scenario" $ do
320
- monadicIO $
321
- withFixture $ \keystore layer Fixture{..} -> do
322
- liftIO $ Keystore.insert (WalletIdHdRnd fixtureHdRootId) fixtureESK keystore
323
- let (HdRootId hdRoot) = fixtureHdRootId
324
- (AccountIdHdRnd myAccountId) = fixtureAccountId
325
- wId = sformat build (view fromDb hdRoot)
326
- accIdx = myAccountId ^. hdAccountIdIx . to getHdAccountIx
327
- req = V1.NewAddress Nothing accIdx (V1.WalletId wId)
328
- res <- liftIO (runExceptT . runHandler' $ Handlers.newAddress layer req)
329
- liftIO ((bimap identity STB res) `shouldSatisfy` isRight)
330
- --}
333
+ describe " Estimating fees (Servant)" $ do
334
+ prop " works as expected in the happy path scenario" $ withMaxSuccess 50 $
335
+ monadicIO $
336
+ withPayment (InitialADA 1000 ) (PayADA 1 ) $ \ activeLayer newPayment -> do
337
+ res <- liftIO (runExceptT . runHandler' $ Handlers. estimateFees activeLayer newPayment)
338
+ liftIO ((bimap identity STB res) `shouldSatisfy` isRight)
339
+
0 commit comments