-
Notifications
You must be signed in to change notification settings - Fork 211
/
TransactionsNew.hs
2535 lines (2260 loc) · 113 KB
/
TransactionsNew.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{- HLINT ignore "Use head" -}
module Test.Integration.Scenario.API.Shelley.TransactionsNew
( spec
) where
import Prelude
import Cardano.Address.Derivation
( XPub, xpubPublicKey )
import Cardano.Api
( CardanoEra (..), InAnyCardanoEra (..) )
import Cardano.Crypto.DSIGN.Class
( rawDeserialiseVerKeyDSIGN )
import Cardano.Mnemonic
( SomeMnemonic (..), mnemonicToText )
import Cardano.Wallet.Api.Types
( AddressAmount (..)
, ApiAddress (..)
, ApiAnyCertificate (..)
, ApiCoinSelection (withdrawals)
, ApiConstructTransaction (..)
, ApiDecodedTransaction
, ApiDeregisterPool (..)
, ApiExternalCertificate (..)
, ApiRegisterPool (..)
, ApiSerialisedTransaction (..)
, ApiStakePool
, ApiT (..)
, ApiTransaction
, ApiTxId (..)
, ApiTxInputGeneral (..)
, ApiTxMetadata (..)
, ApiTxOutputGeneral (..)
, ApiWallet
, ApiWalletInput (..)
, ApiWalletOutput (..)
, DecodeAddress
, DecodeStakeAddress
, EncodeAddress (..)
, EncodeStakeAddress
, ResourceContext (..)
, WalletStyle (..)
)
import Cardano.Wallet.Primitive.AddressDerivation
( DerivationIndex (..)
, HardDerivation (..)
, PaymentAddress (..)
, Role (..)
, WalletKey (..)
, hex
)
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.Types
( EpochNo (..)
, NonWalletCertificate (..)
, PoolId (..)
, PoolOwner (..)
, StakePoolMetadataHash (..)
, StakePoolMetadataUrl (..)
)
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.RewardAccount
( RewardAccount (..) )
import Cardano.Wallet.Primitive.Types.TokenBundle
( AssetId (..) )
import Cardano.Wallet.Primitive.Types.TokenPolicy
( TokenName (..), TokenPolicyId (..) )
import Cardano.Wallet.Primitive.Types.TokenQuantity
( TokenQuantity (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, SealedTx (..)
, TxIn (..)
, TxMetadata (..)
, TxMetadataValue (..)
, TxScriptValidity (..)
, TxStatus (..)
, cardanoTx
, getSealedTxBody
, sealedTxFromCardanoBody
)
import Cardano.Wallet.Unsafe
( unsafeFromHex, unsafeMkMnemonic )
import Control.Arrow
( second )
import Control.Monad
( foldM_, forM_ )
import Control.Monad.IO.Unlift
( MonadIO (..), MonadUnliftIO (..), liftIO )
import Control.Monad.Trans.Resource
( runResourceT )
import Data.Aeson
( toJSON, (.=) )
import Data.Function
( (&) )
import Data.Functor
( void )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Generics.Sum
( _Ctor )
import Data.Maybe
( fromJust, isJust )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..), mkPercentage )
import Data.Ratio
( (%) )
import Data.Text
( Text )
import Numeric.Natural
( Natural )
import Test.Hspec
( SpecWith, describe, pendingWith, shouldContain, shouldNotContain )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldNotBe, shouldNotSatisfy, shouldSatisfy )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, Payload (..)
, arbitraryStake
, delegating
, emptyWallet
, eventually
, expectErrorMessage
, expectField
, expectResponseCode
, expectSuccess
, fixtureMultiAssetWallet
, fixturePassphrase
, fixtureWallet
, fixtureWalletWith
, fixtureWalletWithMnemonics
, getFromResponse
, getSomeVerificationKey
, json
, listAddresses
, minUTxOValue
, pickAnAsset
, request
, rewardWallet
, selectCoins
, signTx
, submitTx
, submitTxWithWid
, unsafeRequest
, verify
, waitForNextEpoch
, waitForTxImmutability
)
import Test.Integration.Framework.TestData
( errMsg403Collateral
, errMsg403Fee
, errMsg403InvalidConstructTx
, errMsg403MinUTxOValue
, errMsg403NotDelegating
, errMsg403NotEnoughMoney
, errMsg404NoSuchPool
)
import UnliftIO.Exception
( fromEither )
import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Crypto as Ledger
import qualified Cardano.Ledger.Keys as Ledger
import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Data.Aeson as Aeson
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types.Status as HTTP
import qualified Test.Integration.Plutus as PlutusScenario
spec :: forall n.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeStakeAddress n
, EncodeAddress n
, PaymentAddress n IcarusKey
) => SpecWith Context
spec = describe "NEW_SHELLEY_TRANSACTIONS" $ do
it "TRANS_NEW_CREATE_01a - Empty payload is not allowed" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
let emptyPayload = Json [json|{}|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default emptyPayload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403InvalidConstructTx
]
it "TRANS_NEW_CREATE_01b - Validity interval only is not allowed" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
let validityInterval = Json [json|{
"validity_interval": {
"invalid_before": {
"quantity": 10,
"unit": "second"
},
"invalid_hereafter": {
"quantity": 50,
"unit": "second"
}
}
}|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default validityInterval
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403InvalidConstructTx
]
it "TRANS_NEW_CREATE_01c - No payload is bad request" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default Empty
verify rTx
[ expectResponseCode HTTP.status400
]
it "TRANS_NEW_CREATE_02 - Only metadata" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
let metadata = Json [json|{ "metadata": { "1": { "string": "hello" } } }|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default metadata
verify rTx
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldSatisfy` isJust)
, expectField (#fee . #getQuantity) (`shouldSatisfy` (>0))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
-- Check for the presence of metadata on signed transaction
let
getMetadata (InAnyCardanoEra _ tx) = Cardano.getTxBody tx
& (\(Cardano.TxBody bodyContent) ->
Cardano.txMetadata bodyContent
& \case Cardano.TxMetadataNone ->
Nothing
Cardano.TxMetadataInEra _ (Cardano.TxMetadata m) ->
Just m
)
case getMetadata (cardanoTx $ getApiT (signedTx ^. #transaction)) of
Nothing -> error "Tx doesn't include metadata"
Just m -> case Map.lookup 1 m of
Nothing -> error "Tx doesn't include metadata"
Just (Cardano.TxMetaText "hello") -> pure ()
Just _ -> error "Tx metadata incorrect"
let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx)
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
let expMetadata = ApiT (TxMetadata (Map.fromList [(1,TxMetaText "hello")]))
rDecodedTx <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload
verify rDecodedTx
[ expectResponseCode HTTP.status202
, expectField #metadata (`shouldBe` (ApiTxMetadata (Just expMetadata)))
]
-- Submit tx
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
-- Make sure only fee is deducted from fixtureWallet
eventually "Wallet balance is as expected" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` (fromIntegral oneMillionAda - expectedFee))
]
it "TRANS_NEW_CREATE_03a - Withdrawal from self, 0 rewards" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
let initialBalance = wa ^. #balance . #available . #getQuantity
let withdrawal = Json [json|{ "withdrawal": "self" }|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default withdrawal
verify rTx
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldBe` Nothing)
, expectField (#coinSelection . #withdrawals) (`shouldSatisfy` (not . null))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx)
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
rDecodedTx <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload
verify rDecodedTx
[ expectResponseCode HTTP.status202
, expectField #withdrawals (`shouldBe` [])
]
-- Submit tx
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
-- Make sure wallet balance is decreased by fee, since rewards = 0
eventually "Wallet balance is decreased by fee" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` initialBalance - fromIntegral expectedFee)
, expectField
(#balance . #reward . #getQuantity)
(`shouldBe` 0)
]
it "TRANS_NEW_CREATE_03a - Withdrawal from self" $ \ctx -> runResourceT $ do
(wa, _) <- rewardWallet ctx
let withdrawal = Json [json|{ "withdrawal": "self" }|]
let withdrawalAmt = 1_000_000_000_000
let rewardInitialBalance = 100_000_000_000
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default withdrawal
verify rTx
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldBe` Nothing)
, expectField (#fee . #getQuantity) (`shouldSatisfy` (>0))
, expectField (#coinSelection . #withdrawals) (`shouldSatisfy` (not . null))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
let txCbor = getFromResponse #transaction (HTTP.status202, Right signedTx)
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
let withdrawalWith ownership wdrls = case wdrls of
[wdrl] ->
wdrl ^. #amount == Quantity withdrawalAmt &&
wdrl ^. #context == ownership
_ -> False
rDecodedTx <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload
verify rDecodedTx
[ expectResponseCode HTTP.status202
, expectField #withdrawals (`shouldSatisfy` (withdrawalWith Our))
]
-- Submit tx
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
-- Make sure wallet balance is increased by withdrawalAmt - fee
eventually "Wallet balance is increased by withdrawalAmt - fee" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` rewardInitialBalance + (withdrawalAmt - fromIntegral expectedFee))
]
eventually "Reward balance is 0" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #reward . #getQuantity)
(`shouldBe` 0)
]
wb <- fixtureWallet ctx
rDecodedTx' <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload
verify rDecodedTx'
[ expectResponseCode HTTP.status202
, expectField #withdrawals (`shouldSatisfy` (withdrawalWith External))
]
it "TRANS_NEW_CREATE_03b - Withdrawal from external wallet" $ \ctx -> runResourceT $ do
liftIO $ pendingWith "ADP-1189: Issues with withdrawals from external wallets"
(wr, mw) <- rewardWallet ctx
wa <- fixtureWallet ctx
let withdrawal = Json [json|{ "withdrawal": #{mnemonicToText mw} }|]
let withdrawalAmt = 1000000000000
let rewardInitialBalance = 100_000_000_000
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default withdrawal
verify rTx
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldBe` Nothing)
, expectField (#fee . #getQuantity) (`shouldSatisfy` (>0))
, expectField (#coinSelection . #withdrawals) (`shouldSatisfy` (not . null))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
-- Make sure wallet balance is increased by withdrawalAmt - fee
eventually "Wallet balance is increased by withdrawalAmt - fee" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` rewardInitialBalance + (withdrawalAmt - fromIntegral expectedFee))
]
-- Check that reward account is 0 on external rewardWallet,
eventually "Reward wallet balance is zero" $ do
rWr <- request @ApiWallet ctx
(Link.getWallet @'Shelley wr) Default Empty
verify rWr
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` rewardInitialBalance)
]
it "TRANS_NEW_CREATE_04a - Single Output Transaction with decode transaction" $ \ctx -> runResourceT $ do
let initialAmt = 3 * minUTxOValue (_mainEra ctx)
wa <- fixtureWalletWith @n ctx [initialAmt]
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #change) (`shouldSatisfy` (not . null))
, expectField (#fee . #getQuantity) (`shouldSatisfy` (> 0))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let txCbor = getFromResponse #transaction rTx
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
let sharedExpectationsBetweenWallets =
[ expectResponseCode HTTP.status202
, expectField (#fee . #getQuantity) (`shouldBe` expectedFee)
, expectField #withdrawals (`shouldBe` [])
, expectField #collateral (`shouldBe` [])
, expectField #metadata (`shouldBe` (ApiTxMetadata Nothing))
, expectField #scriptValidity (`shouldBe` (Just $ ApiT TxScriptValid))
]
-- After constructing tx the cbor is as expected, both wallets share common information
-- source wallet sees inputs as his, target wallet sees them as external
let isInpOurs inp = case inp of
ExternalInput _ -> False
WalletInput _ -> True
let areOurs = all isInpOurs
addrs <- listAddresses @n ctx wb
let addrIx = 1
let addrDest = (addrs !! addrIx) ^. #id
let expectedTxOutTarget = WalletOutput $ ApiWalletOutput
{ address = addrDest
, amount = Quantity amt
, assets = ApiT TokenMap.empty
, derivationPath = NE.fromList
[ ApiT (DerivationIndex 2147485500)
, ApiT (DerivationIndex 2147485463)
, ApiT (DerivationIndex 2147483648)
, ApiT (DerivationIndex 0)
, ApiT (DerivationIndex $ fromIntegral addrIx)
]
}
let isOutOurs out = case out of
WalletOutput _ -> False
ExternalOutput _ -> True
rDecodedTxSource <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload
verify rDecodedTxSource $
sharedExpectationsBetweenWallets ++
[ expectField #inputs (`shouldSatisfy` areOurs)
, expectField #outputs (`shouldNotContain` [expectedTxOutTarget])
-- Check that the change output is there:
, expectField (#outputs) ((`shouldBe` 1) . length . filter isOutOurs)
]
rDecodedTxTarget <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload
verify rDecodedTxTarget $
sharedExpectationsBetweenWallets ++
[ expectField #inputs (`shouldNotSatisfy` areOurs)
, expectField #outputs (`shouldContain` [expectedTxOutTarget])
]
let filterInitialAmt =
filter $ \(ApiWalletInput _ _ _ _ amt' _) ->
amt' == Quantity initialAmt
let coinSelInputs = filterInitialAmt $
getFromResponse (#coinSelection . #inputs) rTx
length coinSelInputs `shouldBe` 1
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
eventually "Target wallet balance is decreased by amt + fee" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` amt)
]
eventually "Source wallet balance is decreased by (amt + expectedFee)" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` (initialAmt - amt - expectedFee))
]
-- After signing tx the cbor is as before modulo added wtinesses,
-- and in line what was there after construction. Also as we tx was
-- accommodated in ledger output change in amount for target wallet
let expectedTxOutTarget' = WalletOutput $ ApiWalletOutput
{ address = addrDest
, amount = Quantity amt
, assets = ApiT TokenMap.empty
, derivationPath = NE.fromList
[ ApiT (DerivationIndex 2147485500)
, ApiT (DerivationIndex 2147485463)
, ApiT (DerivationIndex 2147483648)
, ApiT (DerivationIndex 0)
, ApiT (DerivationIndex $ fromIntegral addrIx)
]
}
addrsSourceAll <- listAddresses @n ctx wa
--we expect change address here with x=0 as this wallet does not participated in outcoming tx before this one
let derPath = NE.fromList
[ ApiT (DerivationIndex 2147485500)
, ApiT (DerivationIndex 2147485463)
, ApiT (DerivationIndex 2147483648)
, ApiT (DerivationIndex 1)
, ApiT (DerivationIndex 0)
]
let addrSourceChange:_ =
filter (\(ApiAddress _ _ derPath') -> derPath == derPath') addrsSourceAll
let addrSrc = addrSourceChange ^. #id
let expectedTxOutSource = WalletOutput $ ApiWalletOutput
{ address = addrSrc
, amount = Quantity $ initialAmt - (amt + fromIntegral expectedFee)
, assets = ApiT TokenMap.empty
, derivationPath = derPath
}
let txCbor' = getFromResponse #transaction (HTTP.status202, Right signedTx)
let decodePayload' = Json (toJSON $ ApiSerialisedTransaction txCbor')
rDecodedTxSource' <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload'
verify rDecodedTxSource' $
sharedExpectationsBetweenWallets ++
[ expectField #inputs (`shouldNotSatisfy` areOurs) -- the input is not anymore belonging to wallet
, expectField #outputs (`shouldNotContain` [expectedTxOutTarget'])
, expectField #outputs (`shouldContain` [expectedTxOutSource])
]
rDecodedTxTarget' <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload'
verify rDecodedTxTarget' $
sharedExpectationsBetweenWallets ++
[ expectField #inputs (`shouldNotSatisfy` areOurs)
, expectField #outputs (`shouldContain` [expectedTxOutTarget'])
, expectField #outputs (`shouldNotContain` [expectedTxOutSource])
]
it "TRANS_NEW_CREATE_04ab - Constructed inputs = Decoded inputs" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx [ expectSuccess ]
let expectedInputs = getFromResponse (#coinSelection . #inputs) rTx
let txCbor = getFromResponse #transaction rTx
let decodePayload = Json (toJSON $ ApiSerialisedTransaction txCbor)
rDecodedTxSource <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wa) Default decodePayload
let decodedInputs = getFromResponse #inputs rDecodedTxSource
WalletInput <$> expectedInputs `shouldBe` decodedInputs
it "TRANS_NEW_CREATE_04b - Cannot spend less than minUTxOValue" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
wb <- emptyWallet ctx
let amt = minUTxOValue (_mainEra ctx) - 1
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403MinUTxOValue
]
it "TRANS_NEW_CREATE_04c - Can't cover fee" $ \ctx -> runResourceT $ do
wa <- fixtureWalletWith @n ctx [minUTxOValue (_mainEra ctx) + 1]
wb <- emptyWallet ctx
payload <- liftIO $ mkTxPayload ctx wb (minUTxOValue (_mainEra ctx))
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403Fee
]
it "TRANS_NEW_CREATE_04d - Not enough money" $ \ctx -> runResourceT $ do
let minUTxOValue' = minUTxOValue (_mainEra ctx)
let (srcAmt, reqAmt) = (minUTxOValue', 2 * minUTxOValue')
wa <- fixtureWalletWith @n ctx [srcAmt]
wb <- emptyWallet ctx
payload <- liftIO $ mkTxPayload ctx wb reqAmt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403NotEnoughMoney
]
it "TRANS_NEW_CREATE_04d - Not enough money emptyWallet" $ \ctx -> runResourceT $ do
wa <- emptyWallet ctx
wb <- emptyWallet ctx
payload <- liftIO $ mkTxPayload ctx wb (minUTxOValue (_mainEra ctx))
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403NotEnoughMoney
]
it "TRANS_NEW_CREATE_04e- Multiple Output Tx to single wallet" $ \ctx -> runResourceT $ do
wa <- fixtureWallet ctx
wb <- emptyWallet ctx
addrs <- listAddresses @n ctx wb
initialAmt <- getFromResponse (#balance . #available . #getQuantity) <$>
request @ApiWallet ctx (Link.getWallet @'Shelley wa) Default Empty
let amt = minUTxOValue (_mainEra ctx) :: Natural
let destination1 = (addrs !! 1) ^. #id
let destination2 = (addrs !! 2) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination1},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
},
{
"address": #{destination2},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}]
}|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #change) (`shouldSatisfy` (not . null))
, expectField (#fee . #getQuantity) (`shouldSatisfy` (> 0))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
eventually "Target wallet balance is increased by 2*amt" $ do
rWb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWb
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` 2*amt)
]
eventually "Source wallet balance is decreased by (2*amt + expectedFee)" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` (initialAmt - 2*amt - expectedFee))
]
it "TRANS_NEW_CREATE_04a - Single Output Transaction with submitWithWid" $ \ctx -> runResourceT $ do
let initialAmt = 3 * minUTxOValue (_mainEra ctx)
wa <- fixtureWalletWith @n ctx [initialAmt]
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #change) (`shouldSatisfy` (not . null))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
submittedTx <- submitTxWithWid ctx wa signedTx
verify submittedTx
[ expectSuccess
, expectResponseCode HTTP.status202
]
let txid = getFromResponse (#id) submittedTx
let queryTx = Link.getTransaction @'Shelley wa (ApiTxId txid)
rGetTx <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx
[ expectResponseCode HTTP.status200
, expectField
(#amount . #getQuantity)
(`shouldBe` initialAmt - (amt + fromIntegral expectedFee))
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` Pending)
]
eventually "Source wallet balance is decreased by amt + fee" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` initialAmt - (amt + fromIntegral expectedFee))
]
eventually "transaction is eventually in ledger after submitting" $ do
let queryTx' = Link.getTransaction @'Shelley wa (ApiTxId txid)
rSrc <- request @(ApiTransaction n) ctx queryTx' Default Empty
verify rSrc
[ expectResponseCode HTTP.status200
, expectField (#status . #getApiT) (`shouldBe` InLedger)
]
eventually "Target wallet balance is amt" $ do
rWr <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWr
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` amt)
]
it "TRANS_NEW_ASSETS_CREATE_01a - Multi-asset tx with Ada" $ \ctx -> runResourceT $ do
wa <- fixtureMultiAssetWallet ctx
wb <- emptyWallet ctx
initialAmt <- getFromResponse (#balance . #available . #getQuantity) <$>
request @ApiWallet ctx (Link.getWallet @'Shelley wa) Default Empty
-- pick out an asset to send
let assetsSrc = wa ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue (_mainEra ctx) <$ pickAnAsset assetsSrc
-- create payload
addrs <- listAddresses @n ctx wb
let destination = (addrs !! 1) ^. #id
let amt = 2 * minUTxOValue (_mainEra ctx)
payload <- mkTxPayloadMA @n destination amt [val]
--construct transaction
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #change) (`shouldSatisfy` (not . null))
, expectField (#fee . #getQuantity) (`shouldSatisfy` (> 0))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
void $ submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
eventually "Target wallet balance is increased by amt and assets" $ do
rWb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWb
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` amt)
, expectField (#assets . #available . #getApiT) (`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT) (`shouldNotBe` TokenMap.empty)
]
eventually "Source wallet balance is decreased by (amt + expectedFee)" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` (initialAmt - amt - expectedFee))
]
it "TRANS_NEW_ASSETS_CREATE_01b - Multi-asset tx with not enough Ada" $ \ctx -> runResourceT $ do
wa <- fixtureMultiAssetWallet ctx
wb <- emptyWallet ctx
ra <- request @ApiWallet ctx (Link.getWallet @'Shelley wa) Default Empty
let (_, Right wal) = ra
-- pick out an asset to send
let assetsSrc = wal ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue (_mainEra ctx) <$ pickAnAsset assetsSrc
-- create payload
addrs <- listAddresses @n ctx wb
let destination = (addrs !! 1) ^. #id
let amt = minUTxOValue (_mainEra ctx)
payload <- mkTxPayloadMA @n destination amt [val]
--construct transaction
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage "Some outputs have ada values that are too small."
]
it "TRANS_NEW_ASSETS_CREATE_01c - Multi-asset tx without Ada" $ \ctx -> runResourceT $ do
wa <- fixtureMultiAssetWallet ctx
wb <- emptyWallet ctx
initialAmt <- getFromResponse (#balance . #available . #getQuantity) <$>
request @ApiWallet ctx (Link.getWallet @'Shelley wa) Default Empty
-- pick out an asset to send
let assetsSrc = wa ^. #assets . #total . #getApiT
assetsSrc `shouldNotBe` mempty
let val = minUTxOValue (_mainEra ctx) <$ pickAnAsset assetsSrc
-- create payload
addrs <- listAddresses @n ctx wb
let destination = (addrs !! 1) ^. #id
let amt = 0
payload <- mkTxPayloadMA @n destination amt [val]
--construct transaction
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shelley wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#coinSelection . #inputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #outputs) (`shouldSatisfy` (not . null))
, expectField (#coinSelection . #change) (`shouldSatisfy` (not . null))
, expectField (#fee . #getQuantity) (`shouldSatisfy` (> 0))
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let apiTx = getFromResponse #transaction rTx
signedTx <- signTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
txId <- submitTx ctx signedTx [ expectResponseCode HTTP.status202 ]
outTxAmt <- eventually "Transactions is in ledger" $ do
let linkSrc = Link.getTransaction @'Shelley wa txId
r1 <- request @(ApiTransaction n) ctx linkSrc Default Empty
verify r1
[ expectResponseCode HTTP.status200
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` InLedger)
]
pure $ getFromResponse (#amount . #getQuantity) r1
let inTxAmt = outTxAmt - expectedFee
eventually "Target wallet balance is increased by inTxAmt and assets" $ do
rWb <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWb
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` inTxAmt)
, expectField (#assets . #available . #getApiT) (`shouldNotBe` TokenMap.empty)
, expectField (#assets . #total . #getApiT) (`shouldNotBe` TokenMap.empty)
]
eventually "Source wallet balance is decreased by outTxAmt" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wa) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` (initialAmt - outTxAmt))
]
it "TRANS_NEW_ASSETS_CREATE_01d - Multi-asset tx with not enough assets" $ \ctx -> runResourceT $ do
wa <- fixtureMultiAssetWallet ctx
wb <- emptyWallet ctx
ra <- request @ApiWallet ctx (Link.getWallet @'Shelley wa) Default Empty
let (_, Right wal) = ra
let minUTxOValue' = minUTxOValue (_mainEra ctx)