-
Notifications
You must be signed in to change notification settings - Fork 211
/
Transactions.hs
1047 lines (956 loc) · 43.2 KB
/
Transactions.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 FlexibleContexts #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Test.Integration.Scenario.CLI.Shelley.Transactions
( spec
) where
import Prelude
import Cardano.CLI
( Port )
import Cardano.Wallet.Api.Types
( ApiFee (..)
, ApiT (..)
, ApiTransaction
, ApiWallet
, DecodeAddress
, DecodeStakeAddress
, EncodeAddress (..)
, getApiT
)
import Cardano.Wallet.Api.Types.SchemaMetadata
( TxMetadataSchema (..), detailedMetadata, noSchemaMetadata )
import Cardano.Wallet.Primitive.Types
( SortOrder (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..), TxMetadata (..), TxMetadataValue (..), TxStatus (..) )
import Control.Monad
( forM_, join )
import Control.Monad.IO.Class
( MonadIO )
import Control.Monad.IO.Unlift
( MonadUnliftIO (..) )
import Control.Monad.Trans.Resource
( ResourceT, runResourceT )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Generics.Product.Typed
( typed )
import Data.List.Extra
( enumerate )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Data.Text.Class
( showT )
import Data.Time.Utils
( utcTimePred, utcTimeSucc )
import GHC.Stack
( HasCallStack )
import Numeric.Natural
( Natural )
import System.Command
( Exit (..), Stderr (..), Stdout (..) )
import System.Exit
( ExitCode (..) )
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain, shouldSatisfy )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context (..)
, between
, cardanoWalletCLI
, deleteTransactionViaCLI
, deleteWalletViaCLI
, emptyRandomWallet
, emptyWallet
, eventually
, expectCliField
, expectCliListField
, expectValidJSON
, faucetAmt
, fixturePassphrase
, fixtureWallet
, fixtureWalletWith
, getTransactionViaCLI
, getTxId
, getWalletViaCLI
, listAddresses
, listAllTransactions
, listTransactionsViaCLI
, minUTxOValue
, postTransactionFeeViaCLI
, postTransactionViaCLI
, unsafeGetTransactionTime
, utcIso8601ToText
, verify
, walletId
, (.>=)
)
import Test.Integration.Framework.TestData
( arabicWalletName
, errMsg403AlreadyInLedger
, errMsg403WrongPass
, errMsg404CannotFindTx
, errMsg404NoWallet
, falseWalletIds
, invalidByronBase58
, kanjiWalletName
, polishWalletName
, wildcardsWalletName
)
import UnliftIO.Exception
( throwString )
import qualified Data.Map as Map
import qualified Data.Text as T
spec :: forall n.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
) => SpecWith Context
spec = describe "SHELLEY_CLI_TRANSACTIONS" $ do
it "TRANS_CREATE_01 - Can create transaction via CLI" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
let amt = fromIntegral . minUTxOValue . _mainEra $ ctx
args <- postTxArgs ctx wSrc wDest amt Nothing Nothing
Stdout feeOut <- postTransactionFeeViaCLI ctx args
ApiFee (Quantity feeMin) (Quantity feeMax) _ (Quantity 0) <-
expectValidJSON Proxy feeOut
txJson <- postTxViaCLI ctx wSrc wDest amt Nothing Nothing
verify txJson
[ expectCliField (#amount . #getQuantity)
(between (feeMin + amt, feeMax + amt))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
, expectCliField #metadata (`shouldBe` Nothing)
]
-- verify balance on src wallet
Stdout gOutSrc <- getWalletViaCLI ctx (T.unpack (wSrc ^. walletId))
gJson <- expectValidJSON (Proxy @ApiWallet) gOutSrc
verify gJson
[ expectCliField
(#balance . #total)
(.>= Quantity (faucetAmt - feeMax - amt))
]
eventually "balance on dest wallet is OK" $ do
Stdout gOutDest <- getWalletViaCLI ctx
(T.unpack (wDest ^. walletId))
destJson <- expectValidJSON (Proxy @ApiWallet) gOutDest
verify destJson
[ expectCliField
(#balance . #available) (`shouldBe` Quantity amt)
, expectCliField
(#balance . #total) (`shouldBe` Quantity amt)
]
it "TRANS_CREATE_02 - Multiple Output Tx to single wallet via CLI" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addr <- listAddresses @n ctx wDest
let addr1 = encodeAddress @n (getApiT $ fst $ addr !! 1 ^. #id)
let addr2 = encodeAddress @n (getApiT $ fst $ addr !! 2 ^. #id)
let amt = fromIntegral . minUTxOValue . _mainEra $ ctx
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addr1
, "--payment", T.pack (show amt) <> "@" <> addr2
]
Stdout feeOut <- postTransactionFeeViaCLI ctx args
ApiFee (Quantity feeMin) (Quantity feeMax) _ _ <- expectValidJSON Proxy feeOut
-- post transaction
(c, out, err) <- postTransactionViaCLI ctx "cardano-wallet" args
err `shouldBe` "Please enter your passphrase: **************\nOk.\n"
txJson <- expectValidJSON (Proxy @(ApiTransaction n)) out
verify txJson
[ expectCliField
(#amount . #getQuantity)
(between (feeMin + (2*amt), feeMax + (2*amt)))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
]
c `shouldBe` ExitSuccess
-- verify balance on src wallet
Stdout gOutSrc <- getWalletViaCLI ctx (T.unpack (wSrc ^. walletId))
gJson <- expectValidJSON (Proxy @ApiWallet) gOutSrc
verify gJson
[ expectCliField
(#balance . #total)
(.>= Quantity (faucetAmt - feeMax - (2*amt)))
]
eventually "balance on dest wallet is OK" $ do
Stdout gOutDest <- getWalletViaCLI ctx
(T.unpack (wDest ^. walletId))
destJson <- expectValidJSON (Proxy @ApiWallet) gOutDest
verify destJson
[ expectCliField
(#balance . #available) (`shouldBe` Quantity (2*amt))
, expectCliField
(#balance . #total) (`shouldBe` Quantity (2*amt))
]
it "TRANS_CREATE_04 - Wrong password" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let amt = T.pack . show . minUTxOValue . _mainEra $ ctx
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", amt <> "@" <> addr
]
(c, out, err) <- postTransactionViaCLI ctx "This password is wrong" args
(T.unpack err) `shouldContain` errMsg403WrongPass
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_CREATE_05 - Invalid addresses" $ do
forM_ matrixInvalidAddrs $ \(title, addr, errMsg) -> it title $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", "12@" <> (T.pack addr)
]
(c, out, err) <- postTransactionViaCLI ctx
(T.unpack fixturePassphrase) args
(T.unpack err) `shouldContain` errMsg
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_CREATE_06 - Invalid amount" $ do
forM_ matrixInvalidAmt $ \(title, amt, errMsg) -> it title $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", amt <> "@" <> addr
]
(c, out, err) <- postTransactionViaCLI ctx
(T.unpack fixturePassphrase) args
(T.unpack err) `shouldContain` errMsg
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_CREATE_07 - False wallet ids" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> runResourceT $ do
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let port = show $ ctx ^. typed @(Port "wallet")
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let args =
[ "transaction", "create", "--port", port
, walId, "--payment", "12@" ++ (T.unpack addr)
]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
err `shouldContain` "I couldn't find a wallet with \
\the given id: 1111111111111111111111111111111111111111"
else
err `shouldContain` "wallet id should be a \
\hex-encoded string of 40 characters"
it "TRANS_CREATE_07 - 'almost' valid walletId" $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let port = T.pack $ show $ ctx ^. typed @(Port "wallet")
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let amt = T.pack . show . minUTxOValue . _mainEra $ ctx
let args = T.unpack <$>
[ "transaction", "create", "--port", port
, T.append (wSrc ^. walletId) "0", "--payment", amt <> "@" <> addr
]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
err `shouldContain` "wallet id should be a hex-encoded\
\ string of 40 characters"
out `shouldBe` ""
c `shouldBe` ExitFailure 1
it "TRANS_CREATE_07 - Deleted wallet" $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
Exit ex <- deleteWalletViaCLI ctx (T.unpack ( wSrc ^. walletId ))
ex `shouldBe` ExitSuccess
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let port = T.pack $ show $ ctx ^. typed @(Port "wallet")
let amt = T.pack . show . minUTxOValue . _mainEra $ ctx
let args = T.unpack <$>
[ "transaction", "create", "--port", port
, wSrc ^. walletId, "--payment", amt <> "@" <> addr
]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
err `shouldContain` "I couldn't find a wallet with \
\the given id: " ++ T.unpack ( wSrc ^. walletId )
out `shouldBe` ""
c `shouldBe` ExitFailure 1
it "TRANSMETA_CREATE_01a - \
\Transaction with metadata via CLI" $
\ctx -> runResourceT $ do
(wSrc, wDest) <- (,) <$> fixtureWallet ctx <*> emptyWallet ctx
let amt = 10_000_000
let md = Just "{ \"1\": { \"string\": \"hello\" } }"
let expected =
Just $
detailedMetadata $
TxMetadata $
Map.singleton 1 (TxMetaText "hello")
args <- postTxArgs ctx wSrc wDest amt md Nothing
Stdout feeOut <- postTransactionFeeViaCLI ctx args
ApiFee (Quantity feeMin) (Quantity feeMax) _ _ <- expectValidJSON Proxy feeOut
txJson <- postTxViaCLI ctx wSrc wDest amt md Nothing
verify txJson
[ expectCliField
(#amount . #getQuantity)
(between (feeMin + amt, feeMax + amt))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
, expectCliField #metadata (`shouldBe` expected)
]
eventually "metadata is confirmed in transaction list" $ do
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema
[T.unpack $ wSrc ^. walletId]
err `shouldBe` "Ok.\n"
code `shouldBe` ExitSuccess
outJson <- expectValidJSON (Proxy @([ApiTransaction n])) out
verify
outJson
[ expectCliListField 0 #metadata (`shouldBe` expected)
, expectCliListField 0
(#status . #getApiT)
(`shouldBe` InLedger)
]
it "TRANSMETA_CREATE_01b - \
\Transaction with metadata via CLI with simple metadata" $
\ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
let amt = 10_000_000
let md = Just "{ \"1\": { \"string\": \"hello\" } }"
let expected = Just $
detailedMetadata $
TxMetadata $
Map.singleton 1 (TxMetaText "hello")
args <- postTxArgs ctx wSrc wDest amt md Nothing
Stdout feeOut <- postTransactionFeeViaCLI ctx args
ApiFee (Quantity feeMin) (Quantity feeMax) _ _ <-
expectValidJSON Proxy feeOut
txJson <- postTxViaCLI ctx wSrc wDest amt md Nothing
verify txJson
[ expectCliField
(#amount . #getQuantity)
(between (feeMin + amt, feeMax + amt))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
, expectCliField #metadata (`shouldBe` expected)
]
let wSrcId = T.unpack (wSrc ^. walletId)
let txId = getTxId txJson
(Exit code, Stdout out, Stderr err) <-
getTransactionViaCLI ctx wSrcId txId TxMetadataNoSchema
err `shouldBe` "Ok.\n"
code `shouldBe` ExitSuccess
outJson <- expectValidJSON (Proxy @(ApiTransaction n)) out
let expectedNoSchema = Just $
noSchemaMetadata $
TxMetadata $
Map.singleton 1 (TxMetaText "hello")
verify outJson
[ expectCliField
(#amount . #getQuantity)
(between (feeMin + amt, feeMax + amt))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
, expectCliField #metadata (`shouldBe` expectedNoSchema)
]
eventually "metadata is confirmed in transaction list" $ do
(Exit codeL, Stdout outL, Stderr errL) <-
listTransactionsViaCLI ctx TxMetadataNoSchema
[T.unpack $ wSrc ^. walletId]
errL `shouldBe` "Ok.\n"
codeL `shouldBe` ExitSuccess
outJsonL <- expectValidJSON (Proxy @([ApiTransaction n])) outL
verify
outJsonL
[ expectCliListField 0 #metadata (`shouldBe` expectedNoSchema)
, expectCliListField 0 (#status . #getApiT) (`shouldBe` InLedger)
]
it "TRANSTTL_CREATE_01 - Transaction with TTL via CLI" $ \ctx -> runResourceT $ do
(wSrc, wDest) <- (,) <$> fixtureWallet ctx <*> emptyWallet ctx
let amt = 10_000_000
let ttl = Just "30s"
args <- postTxArgs ctx wSrc wDest amt Nothing ttl
Stdout feeOut <- postTransactionFeeViaCLI ctx args
ApiFee (Quantity feeMin) (Quantity feeMax) _ _ <- expectValidJSON Proxy feeOut
txJson <- postTxViaCLI ctx wSrc wDest amt Nothing ttl
verify txJson
[ expectCliField (#amount . #getQuantity)
(between (feeMin + amt, feeMax + amt))
, expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
]
eventually "transaction with ttl is confirmed in transaction list" $ do
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema
[T.unpack $ wDest ^. walletId]
err `shouldBe` "Ok.\n"
code `shouldBe` ExitSuccess
outJson <- expectValidJSON (Proxy @([ApiTransaction n])) out
verify outJson
[ expectCliListField 0 (#status . #getApiT) (`shouldBe` InLedger) ]
describe "TRANS_ESTIMATE_08 - Invalid addresses" $ do
forM_ matrixInvalidAddrs $ \(title, addr, errMsg) -> it title $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
let amt = T.pack . show . minUTxOValue . _mainEra $ ctx
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", amt <> "@" <> (T.pack addr)
]
(Exit c, Stdout out, Stderr err) <- postTransactionFeeViaCLI ctx args
err `shouldContain` errMsg
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_ESTIMATE_09 - Invalid amount" $ do
forM_ matrixInvalidAmt $ \(title, amt, errMsg) -> it title $ \ctx -> runResourceT $ do
wSrc <- emptyWallet ctx
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", amt <> "@" <> addr
]
(c, out, err) <- postTransactionViaCLI ctx "cardano-wallet" args
(T.unpack err) `shouldContain` errMsg
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_LIST_01 - Listing transactions for an empty wallet" $
forM_ timeRangeMatrix $ \(mStart, mEnd) -> do
forM_ sortOrderMatrix $ \mOrder -> do
let title = mempty
<> "listing transactions from "
<> show mStart
<> " to "
<> show mEnd
<> " in "
<> show mOrder
<> " order "
it title $ \ctx -> runResourceT $ do
wallet <- emptyWallet ctx
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema $
join
[ [T.unpack $ wallet ^. walletId]
, maybe [] (\t -> ["--start", t]) mStart
, maybe [] (\t -> ["--end" , t]) mEnd
, maybe [] (\o -> ["--order", showT o]) mOrder
]
err `shouldBe` "Ok.\n"
out `shouldBe` "[]\n"
code `shouldBe` ExitSuccess
it "TRANS_LIST_01 - Can list Incoming and Outgoing transactions" $ \ctx -> runResourceT $ do
-- Make tx from fixtureWallet
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addr:_ <- listAddresses @n ctx wDest
let addrStr = encodeAddress @n (getApiT $ fst $ addr ^. #id)
let amt = minUTxOValue (_mainEra ctx) :: Natural
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addrStr
]
-- post transaction
(c, _, _) <- postTransactionViaCLI ctx "cardano-wallet" args
c `shouldBe` ExitSuccess
eventually "Balance on wallet is as expected" $ do
Stdout gOutDest <- getWalletViaCLI ctx
(T.unpack (wDest ^. walletId))
destJson <- expectValidJSON (Proxy @ApiWallet) gOutDest
verify destJson
[ expectCliField
(#balance . #available) (`shouldBe` Quantity amt)
, expectCliField
(#balance . #total) (`shouldBe` Quantity amt)
]
-- Verify Tx list contains Incoming and Outgoing
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema
[T.unpack $ wSrc ^. walletId]
err `shouldBe` "Ok.\n"
code `shouldBe` ExitSuccess
outJson <- expectValidJSON (Proxy @([ApiTransaction n])) out
verify outJson
[ expectCliListField 0 (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliListField 1 (#direction . #getApiT) (`shouldBe` Incoming)
]
describe "TRANS_LIST_02 - Start time shouldn't be later than end time" $
forM_ sortOrderMatrix $ \mOrder -> do
let startTime = max validTime1 validTime2
let endTime = min validTime1 validTime2
let title = mempty
<> "listing transactions from "
<> show startTime
<> " to "
<> show endTime
<> " in "
<> show mOrder
<> " order "
it title $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema $ join
[ [ wid ]
, [ "--start", startTime ]
, [ "--end" , endTime ]
, maybe [] (\o -> ["--order", showT o]) mOrder
]
err `shouldBe` mconcat
[ "The specified start time '"
, startTime
, "' is later than the specified end time '"
, endTime
, "'.\n"
]
out `shouldBe` mempty
code `shouldBe` ExitFailure 1
it "TRANS_LIST_03 - Can order results" $ \ctx -> runResourceT $ do
let a1 = Quantity $ sum $ replicate 10 (minUTxOValue (_mainEra ctx))
let a2 = Quantity $ sum $ replicate 10 (2 * (minUTxOValue (_mainEra ctx)))
w <- fixtureWalletWith @n ctx $ mconcat
[ replicate 10 (minUTxOValue (_mainEra ctx))
, replicate 10 (2 * (minUTxOValue (_mainEra ctx)))
]
let orderings =
[ ( mempty
, [ expectCliListField 0 #amount (`shouldBe` a2)
, expectCliListField 1 #amount (`shouldBe` a1)
]
)
, ( [ "--order", "ascending" ]
, [ expectCliListField 0 #amount (`shouldBe` a1)
, expectCliListField 1 #amount (`shouldBe` a2)
]
)
, ( [ "--order", "descending" ]
, [ expectCliListField 0 #amount (`shouldBe` a2)
, expectCliListField 1 #amount (`shouldBe` a1)
]
)
]
forM_ orderings $ \(order, expects) -> do
let args = T.unpack <$> w ^. walletId : order
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema args
err `shouldBe` "Ok.\n"
code `shouldBe` ExitSuccess
outJson <- expectValidJSON (Proxy @([ApiTransaction n])) out
length outJson `shouldBe` 2
verify outJson expects
describe "TRANS_LIST_02,03 - Faulty start, end, order values" $ do
let orderErr = "Please specify one of the following values:\
\ ascending, descending."
let startEndErr = "Expecting ISO 8601 date-and-time format\
\ (basic or extended), e.g. 2012-09-25T10:15:00Z."
let queries =
[ ( [ "--start", "2009" ]
, startEndErr
)
,
( [ "--start", "2012-09-25T10:15:00Z", "--end", "2016-11-21" ]
, startEndErr
)
,
( [ "--start", "2012-09-25", "--end", "2016-11-21T10:15:00Z" ]
, startEndErr
)
,
( [ "--end", "2012-09-25T10:15:00Z", "--start", "2016-11-21" ]
, startEndErr
)
,
( [ "--order", "scending" ]
, orderErr
)
,
( [ "--start", "2012-09-25T10:15:00Z", "--order", "asc" ]
, orderErr
)
]
forM_ queries $ \(q, errorMess) -> it (unwords q) $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
let args = wid : q
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema args
out `shouldBe` mempty
code `shouldBe` ExitFailure 1
err `shouldContain` errorMess
it "TRANS_LIST_04 - 'almost' valid walletId" $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
let invalidWid = wid ++ "0"
(Exit code, Stdout out, Stderr err) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema [invalidWid]
err `shouldContain` "wallet id should be a hex-encoded\
\ string of 40 characters"
code `shouldBe` ExitFailure 1
out `shouldBe` mempty
it "TRANS_LIST_04 - Deleted wallet" $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
Exit d <- deleteWalletViaCLI ctx wid
d `shouldBe` ExitSuccess
(Exit c, Stdout o, Stderr e) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema [wid]
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
c `shouldBe` ExitFailure 1
describe "TRANS_LIST_04 - False wallet ids" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> runResourceT $ do
(Exit c, Stdout o, Stderr e) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema [walId]
o `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
e `shouldContain`
errMsg404NoWallet "1111111111111111111111111111111111111111"
else
e `shouldContain`
"wallet id should be a hex-encoded string of 40 characters"
it "TRANS_LIST_RANGE_01 - \
\Transaction at time t is SELECTED by small ranges that cover it" $
\ctx -> runResourceT $ do
w <- fixtureWalletWith @n ctx [(minUTxOValue (_mainEra ctx))]
let walId = w ^. walletId
t <- unsafeGetTransactionTime =<< listAllTransactions @n ctx w
let (te, tl) = (utcTimePred t, utcTimeSucc t)
let query t1 t2 =
[ "--start", utcIso8601ToText t1
, "--end", utcIso8601ToText t2
]
Stdout o1 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> walId : (query t t))
Stdout o2 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> walId : (query te t))
Stdout o3 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> walId : (query t tl))
Stdout o4 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> walId : (query te tl))
oJson1 <- expectValidJSON (Proxy @([ApiTransaction n])) o1
oJson2 <- expectValidJSON (Proxy @([ApiTransaction n])) o2
oJson3 <- expectValidJSON (Proxy @([ApiTransaction n])) o3
oJson4 <- expectValidJSON (Proxy @([ApiTransaction n])) o4
length <$> [oJson1, oJson2, oJson3, oJson4] `shouldSatisfy` all (== 1)
it "TRANS_LIST_RANGE_02 - \
\Transaction at time t is NOT selected by range [t + 𝛿t, ...)" $
\ctx -> runResourceT $ do
w <- fixtureWalletWith @n ctx [(minUTxOValue (_mainEra ctx))]
let walId = w ^. walletId
t <- unsafeGetTransactionTime =<< listAllTransactions @n ctx w
let tl = utcIso8601ToText $ utcTimeSucc t
Stdout o1 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> [walId, "--start", tl])
Stdout o2 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> [walId, "--start", tl, "--end", tl])
oJson1 <- expectValidJSON (Proxy @([ApiTransaction n])) o1
oJson2 <- expectValidJSON (Proxy @([ApiTransaction n])) o2
length <$> [oJson1, oJson2] `shouldSatisfy` all (== 0)
it "TRANS_LIST_RANGE_03 - \
\Transaction at time t is NOT selected by range (..., t - 𝛿t]" $
\ctx -> runResourceT $ do
w <- fixtureWalletWith @n ctx [(minUTxOValue (_mainEra ctx))]
let walId = w ^. walletId
t <- unsafeGetTransactionTime =<< listAllTransactions @n ctx w
let te = utcIso8601ToText $ utcTimePred t
Stdout o1 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> [walId, "--end", te])
Stdout o2 <- listTransactionsViaCLI ctx TxMetadataDetailedSchema
(T.unpack <$> [walId, "--start", te, "--end", te])
oJson1 <- expectValidJSON (Proxy @([ApiTransaction n])) o1
oJson2 <- expectValidJSON (Proxy @([ApiTransaction n])) o2
length <$> [oJson1, oJson2] `shouldSatisfy` all (== 0)
it "TRANS_GET_01 - Can get Incoming and Outgoing transaction" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addr:_ <- listAddresses @n ctx wDest
let addrStr = encodeAddress @n (getApiT $ fst $ addr ^. #id)
let amt = (minUTxOValue (_mainEra ctx)) :: Natural
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addrStr
]
(c, out, err) <- postTransactionViaCLI ctx "cardano-wallet" args
err `shouldBe` "Please enter your passphrase: **************\nOk.\n"
txJson <- expectValidJSON (Proxy @(ApiTransaction n)) out
verify txJson
[ expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
]
c `shouldBe` ExitSuccess
eventually "Balance on wallet is as expected" $ do
Stdout gOutDest <- getWalletViaCLI ctx
(T.unpack (wDest ^. walletId))
destJson <- expectValidJSON (Proxy @ApiWallet) gOutDest
verify destJson
[ expectCliField
(#balance . #available) (`shouldBe` Quantity amt)
, expectCliField
(#balance . #total) (`shouldBe` Quantity amt)
]
eventually "Transactions are available and in ledger" $ do
let wSrcId = T.unpack (wSrc ^. walletId)
let txId = getTxId txJson
-- Verify Tx in source wallet is Outgoing and InLedger
(Exit code1, Stdout out1, Stderr err1) <-
getTransactionViaCLI ctx wSrcId txId TxMetadataDetailedSchema
err1 `shouldBe` "Ok.\n"
code1 `shouldBe` ExitSuccess
outJson1 <- expectValidJSON (Proxy @(ApiTransaction n)) out1
verify outJson1
[ expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` InLedger)
]
let wDestId = T.unpack (wDest ^. walletId)
-- Verify Tx in destination wallet is Incoming and InLedger
(Exit code2, Stdout out2, Stderr err2) <-
getTransactionViaCLI ctx wDestId txId TxMetadataDetailedSchema
err2 `shouldBe` "Ok.\n"
code2 `shouldBe` ExitSuccess
outJson2 <- expectValidJSON (Proxy @(ApiTransaction n)) out2
verify outJson2
[ expectCliField (#direction . #getApiT) (`shouldBe` Incoming)
, expectCliField (#status . #getApiT) (`shouldBe` InLedger)
]
it "TRANS_GET_02 - Deleted wallet" $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
Exit d <- deleteWalletViaCLI ctx wid
d `shouldBe` ExitSuccess
let txId = "3e6ec12da4414aa0781ff8afa9717ae53ee8cb4aa55d622f65bc62619a4f7b12"
(Exit c, Stdout o, Stderr e) <-
getTransactionViaCLI ctx wid txId TxMetadataDetailedSchema
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
c `shouldBe` ExitFailure 1
it "TRANS_GET_03 - Using wrong transaction id" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
addr:_ <- listAddresses @n ctx wDest
let addrStr = encodeAddress @n (getApiT $ fst $ addr ^. #id)
let amt = (minUTxOValue (_mainEra ctx)) :: Natural
let args = T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addrStr
]
(c1, o1, e1) <- postTransactionViaCLI ctx "cardano-wallet" args
e1 `shouldBe` "Please enter your passphrase: **************\nOk.\n"
txJson <- expectValidJSON (Proxy @(ApiTransaction n)) o1
verify txJson
[ expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
]
c1 `shouldBe` ExitSuccess
let wid = T.unpack (wSrc ^. walletId)
let txId = "3e6ec12da4414aa0781ff8afa9717ae53ee8cb4aa55d622f65bc62619a4f7b12"
(Exit c2, Stdout o2, Stderr e2) <-
getTransactionViaCLI ctx wid txId TxMetadataDetailedSchema
e2 `shouldContain` errMsg404CannotFindTx (T.pack txId)
o2 `shouldBe` mempty
c2 `shouldBe` ExitFailure 1
it "TRANS_DELETE_01 - Cannot forget pending transaction when not pending anymore via CLI" $ \ctx -> runResourceT $ do
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
let wSrcId = T.unpack (wSrc ^. walletId)
-- post transaction
txJson <- postTxViaCLI ctx wSrc wDest (minUTxOValue (_mainEra ctx)) Nothing Nothing
verify txJson
[ expectCliField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliField (#status . #getApiT) (`shouldBe` Pending)
]
let txId = getTxId txJson
eventually "Tx is in ledger" $ do
listTransactionsViaCLI ctx TxMetadataDetailedSchema [wSrcId]
>>= expectValidJSON (Proxy @([ApiTransaction n])) . fromStdout
>>= flip verify
[ expectCliListField 0
(#direction . #getApiT) (`shouldBe` Outgoing)
, expectCliListField 0
(#status . #getApiT) (`shouldBe` InLedger)
]
-- Try Forget transaction once it's no longer pending
(Exit c2, Stdout out2, Stderr err2) <-
deleteTransactionViaCLI ctx wSrcId txId
err2 `shouldContain` errMsg403AlreadyInLedger (T.pack txId)
out2 `shouldBe` ""
c2 `shouldBe` ExitFailure 1
it "TRANS_DELETE_03 - Cannot forget tx that is not found via CLI" $ \ctx -> runResourceT $ do
wid <- fixtureWallet' ctx
let txId = "3e6ec12da4414aa0781ff8afa9717ae53ee8cb4aa55d622f65bc62619a4f7b12"
-- forget transaction
(Exit c, Stdout out, Stderr err) <-
deleteTransactionViaCLI ctx wid (T.unpack txId)
err `shouldContain` errMsg404CannotFindTx txId
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_DELETE_04 - False wallet ids via CLI" $ do
forM_ falseWalletIds $ \(title, walId) -> it title $ \ctx -> runResourceT $ do
let txId = "3e6ec12da4414aa0781ff8afa9717ae53ee8cb4aa55d622f65bc62619a4f7b12"
-- forget transaction once again
(Exit c, Stdout out, Stderr err) <-
deleteTransactionViaCLI ctx walId (T.unpack txId)
out `shouldBe` ""
c `shouldBe` ExitFailure 1
if (title == "40 chars hex") then
err `shouldContain` "I couldn't find a wallet with \
\the given id: 1111111111111111111111111111111111111111"
else
err `shouldContain` "wallet id should be a \
\hex-encoded string of 40 characters"
it "TRANS_DELETE_06 -\
\ Cannot forget tx that is performed from different wallet via CLI"
$ \ctx -> runResourceT $ do
-- post tx
wSrc <- fixtureWallet ctx
wDest <- emptyWallet ctx
txJson <- postTxViaCLI ctx wSrc wDest (minUTxOValue (_mainEra ctx)) Nothing Nothing
-- try to forget from different wallet
widDiff <- emptyWallet' ctx
let txId = getTxId txJson
(Exit c, Stdout out, Stderr err) <-
deleteTransactionViaCLI ctx widDiff txId
err `shouldContain` errMsg404CannotFindTx (T.pack txId)
out `shouldBe` ""
c `shouldBe` ExitFailure 1
describe "TRANS_DELETE_07 - invalid tx id via CLI" $ do
let txIds =
[ replicate 63 '1'
, replicate 65 '1'
, replicate 64 'ś'
]
forM_ txIds $ \tid -> it (show tid) $ \ctx -> runResourceT $ do
wid <- emptyWallet' ctx
(Exit c, Stdout out, Stderr err) <-
deleteTransactionViaCLI ctx wid tid
err `shouldContain`
"should be a hex-encoded string of 64 characters"
out `shouldBe` ""
c `shouldBe` ExitFailure 1
it "BYRON_TX_LIST_03 -\
\ Shelley CLI does not list Byron wallet transactions" $ \ctx -> runResourceT $ do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout o, Stderr e) <-
listTransactionsViaCLI ctx TxMetadataDetailedSchema [wid]
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
c `shouldBe` ExitFailure 1
it "BYRON_TRANS_DELETE -\
\ Cannot delete tx on Byron wallet using shelley CLI" $ \ctx -> runResourceT $ do
wid <- emptyRandomWallet' ctx
(Exit c, Stdout o, Stderr e)
<- deleteTransactionViaCLI ctx wid (replicate 64 '1')
e `shouldContain` errMsg404NoWallet (T.pack wid)
o `shouldBe` mempty
c `shouldBe` ExitFailure 1
describe "BYRON_TRANS_CREATE / BYRON_TRANS_ESTIMATE -\
\ Cannot create/estimate tx on Byron wallet using shelley CLI" $ do
forM_ ["create", "fees"] $ \action -> it action $ \ctx -> runResourceT $ do
wSrc <- emptyRandomWallet ctx
wDest <- emptyWallet ctx
addrs:_ <- listAddresses @n ctx wDest
let addr = encodeAddress @n (getApiT $ fst $ addrs ^. #id)
let port = T.pack $ show $ ctx ^. typed @(Port "wallet")
let args = T.unpack <$>
[ "transaction", T.pack action, "--port", port
, wSrc ^. walletId, "--payment", T.pack (show . minUTxOValue . _mainEra $ ctx) <> "@" <> addr
]
-- make sure CLI returns error before asking for passphrase
(Exit c, Stdout out, Stderr err) <- cardanoWalletCLI args
err `shouldContain` "I couldn't find a wallet with \
\the given id: " ++ T.unpack ( wSrc ^. walletId )
out `shouldBe` ""
c `shouldBe` ExitFailure 1
where
postTxViaCLI
:: MonadUnliftIO m
=> Context
-> ApiWallet
-> ApiWallet
-> Natural
-> Maybe Text
-> Maybe Text
-> m (ApiTransaction n)
postTxViaCLI ctx wSrc wDest amt md ttl = do
args <- postTxArgs ctx wSrc wDest amt md ttl
-- post transaction
(c, out, err) <- postTransactionViaCLI ctx "cardano-wallet" args
err `shouldBe` "Please enter your passphrase: **************\nOk.\n"
c `shouldBe` ExitSuccess
expectValidJSON (Proxy @(ApiTransaction n)) out
postTxArgs
:: MonadUnliftIO m
=> Context
-> ApiWallet
-> ApiWallet
-> Natural
-> Maybe Text
-> Maybe Text
-> m [String]
postTxArgs ctx wSrc wDest amt md ttl = do
addr <- headMayIO =<< listAddresses @n ctx wDest
let addrStr = encodeAddress @n (getApiT $ fst $ addr ^. #id)
return $ T.unpack <$>
[ wSrc ^. walletId
, "--payment", T.pack (show amt) <> "@" <> addrStr
] ++ maybe [] (\json -> ["--metadata", json]) md
++ maybe [] (\s -> ["--ttl", s]) ttl
fixtureWallet' :: Context -> ResourceT IO String
fixtureWallet' = fmap (T.unpack . view walletId) . fixtureWallet
emptyWallet' :: Context -> ResourceT IO String