/
Transactions.hs
2758 lines (2496 loc) · 113 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 DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.Integration.Scenario.API.Shared.Transactions
( spec
) where
import Prelude
import Cardano.Address.Script
( KeyHash (..), Script (..) )
import Cardano.Mnemonic
( MkSomeMnemonic (..) )
import Cardano.Wallet.Address.Derivation
( DerivationIndex (..), Index (..) )
import Cardano.Wallet.Address.Derivation.SharedKey
( replaceCosignersWithVerKeys )
import Cardano.Wallet.Address.Discovery.Shared
( CredentialType (..) )
import Cardano.Wallet.Api.Types
( ApiAddressWithPath
, ApiAnyCertificate (..)
, ApiCertificate (..)
, ApiConstructTransaction (..)
, ApiDecodedTransaction (..)
, ApiScriptTemplate (..)
, ApiSerialisedTransaction (..)
, ApiSharedWallet (..)
, ApiT (..)
, ApiTransaction
, ApiTxId (..)
, ApiTxInput (..)
, ApiTxInputGeneral (..)
, ApiTxMetadata (..)
, ApiTxOutputGeneral (..)
, ApiWallet
, ApiWalletOutput (..)
, Iso8601Time (..)
, WalletStyle (..)
, fromApiEra
, insertedAt
)
import Cardano.Wallet.Api.Types.Error
( ApiErrorInfo (..) )
import Cardano.Wallet.Api.Types.Transaction
( mkApiWitnessCount )
import Cardano.Wallet.Pools
( StakePool )
import Cardano.Wallet.Primitive.Passphrase
( Passphrase (..) )
import Cardano.Wallet.Primitive.Types
( SortOrder (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, TxMetadata (..)
, TxMetadataValue (..)
, TxScriptValidity (..)
, TxStatus (..)
, cardanoTxIdeallyNoLaterThan
)
import Cardano.Wallet.Read.NetworkId
( HasSNetworkId )
import Cardano.Wallet.Transaction
( AnyExplicitScript (..), ScriptReference (..), WitnessCount (..) )
import Control.Monad
( forM_ )
import Control.Monad.IO.Unlift
( MonadUnliftIO (..), liftIO )
import Control.Monad.Trans.Resource
( runResourceT )
import Data.Aeson
( toJSON )
import Data.Either.Combinators
( swapEither )
import Data.Function
( (&) )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Generics.Wrapped
( _Unwrapped )
import Data.Maybe
( isJust )
import Data.Quantity
( Quantity (..) )
import Data.Text.Class
( FromText (..), ToText (..) )
import Data.Time.Clock
( UTCTime, addUTCTime )
import Data.Time.Utils
( utcTimePred, utcTimeSucc )
import Numeric.Natural
( Natural )
import Test.Hspec
( SpecWith, describe )
import Test.Hspec.Expectations.Lifted
( shouldBe, shouldContain, shouldNotContain, shouldSatisfy )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
( Context (..)
, Headers (..)
, MnemonicLength (..)
, Payload (..)
, arbitraryStake
, decodeErrorInfo
, delegating
, deleteSharedWallet
, emptySharedWallet
, emptyWallet
, eventually
, expectErrorMessage
, expectField
, expectListField
, expectListSize
, expectResponseCode
, expectSuccess
, faucetUtxoAmt
, fixturePassphrase
, fixtureSharedWallet
, fixtureSharedWalletDelegating
, fundSharedWallet
, genMnemonics
, getFromResponse
, getSharedWallet
, json
, listAddresses
, minUTxOValue
, patchSharedWallet
, postSharedWallet
, request
, sharedAccPubKeyFromMnemonics
, signSharedTx
, submitSharedTxWithWid
, toQueryString
, unsafeGetTransactionTime
, unsafeRequest
, utcIso8601ToText
, verify
, waitForNextEpoch
, walletId
, (.>)
)
import Test.Integration.Framework.Request
( RequestException )
import Test.Integration.Framework.TestData
( errMsg400MinWithdrawalWrong
, errMsg400StartTimeLaterThanEndTime
, errMsg403EmptyUTxO
, errMsg403Fee
, errMsg403InvalidConstructTx
, errMsg403MinUTxOValue
, errMsg403MissingWitsInTransaction
, errMsg404CannotFindTx
, errMsg404NoWallet
)
import qualified Cardano.Address.Script as CA
import qualified Cardano.Address.Style.Shelley as CA
import qualified Cardano.Api as Cardano
import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Network.HTTP.Types as HTTP
data TestCase a = TestCase
{ query :: T.Text
, assertions :: [(HTTP.Status, Either RequestException a) -> IO ()]
}
spec
:: forall n
. HasSNetworkId n
=> SpecWith Context
spec = describe "SHARED_TRANSACTIONS" $ do
it "SHARED_TRANSACTIONS_CREATE_01 - \
\Cannot create tx for a pending shared wallet" $
\ctx -> runResourceT $ do
m15txt <- liftIO $ genMnemonics M15
m12txt <- liftIO $ genMnemonics M12
let (Right m15) = mkSomeMnemonic @'[ 15 ] m15txt
let (Right m12) = mkSomeMnemonic @'[ 12 ] m12txt
let passphrase =
Passphrase $ BA.convert $ T.encodeUtf8 fixturePassphrase
let index = 30
let accXPubDerived =
sharedAccPubKeyFromMnemonics m15 (Just m12) index passphrase
let payload = Json [json| {
"name": "Shared Wallet",
"mnemonic_sentence": #{m15txt},
"mnemonic_second_factor": #{m12txt},
"passphrase": #{fixturePassphrase},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": #{accXPubDerived} },
"template":
{ "all":
[ "cosigner#0",
"cosigner#1",
{ "active_from": 120 }
]
}
}
} |]
rPost <- postSharedWallet ctx Default payload
verify (fmap (swapEither . view #wallet) <$> rPost)
[ expectResponseCode HTTP.status201
]
let (ApiSharedWallet (Left wal)) =
getFromResponse Prelude.id rPost
let metadata =
Json [json|{ "metadata": { "1": { "string": "hello" } } }|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
verify rTx
[ expectResponseCode HTTP.status403
]
decodeErrorInfo rTx `shouldBe` SharedWalletIncomplete
it "SHARED_TRANSACTIONS_CREATE_01 - \
\Can create tx for an active shared wallet, typed metadata" $
\ctx -> runResourceT $ do
m15txt <- liftIO $ genMnemonics M15
m12txt <- liftIO $ genMnemonics M12
let payload = Json [json| {
"name": "Shared Wallet",
"mnemonic_sentence": #{m15txt},
"mnemonic_second_factor": #{m12txt},
"passphrase": #{fixturePassphrase},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "self" },
"template":
{ "all":
[ "cosigner#0" ]
}
}
} |]
rPost <- postSharedWallet ctx Default payload
verify (fmap (swapEither . view #wallet) <$> rPost)
[ expectResponseCode HTTP.status201
]
let walShared@(ApiSharedWallet (Right wal)) =
getFromResponse Prelude.id rPost
let metadata =
Json [json|{ "metadata": { "1": { "string": "hello" } } }|]
rTx1 <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
verify rTx1
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403EmptyUTxO
]
let amt = 10 * minUTxOValue (_mainEra ctx)
fundSharedWallet @n ctx amt (pure walShared)
rTx2 <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
verify rTx2
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldSatisfy` isJust)
, expectField (#fee . #getQuantity) (`shouldSatisfy` (>0))
]
let txCbor1 = getFromResponse #transaction rTx2
let decodePayload1 = Json (toJSON txCbor1)
rDecodedTx1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wal) Default decodePayload1
let expectedFee = getFromResponse (#fee . #getQuantity) rTx2
let metadata' =
ApiT (TxMetadata (Map.fromList [(1,TxMetaText "hello")]))
let decodedExpectations =
[ expectResponseCode HTTP.status202
, expectField (#fee . #getQuantity) (`shouldBe` expectedFee)
, expectField #withdrawals (`shouldBe` [])
, expectField #collateral (`shouldBe` [])
, expectField #metadata
(`shouldBe` (ApiTxMetadata (Just metadata')))
, expectField #scriptValidity
(`shouldBe` (Just $ ApiT TxScriptValid))
]
verify rDecodedTx1 decodedExpectations
let (ApiSerialisedTransaction apiTx _) =
getFromResponse #transaction rTx2
signedTx <-
signSharedTx ctx wal apiTx [ expectResponseCode HTTP.status202 ]
let decodePayload2 = Json (toJSON signedTx)
rDecodedTx2 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wal) Default decodePayload2
verify rDecodedTx2 decodedExpectations
-- Submit tx
submittedTx <- submitSharedTxWithWid ctx wal signedTx
verify submittedTx
[ expectSuccess
, expectResponseCode HTTP.status202
]
let txid = getFromResponse #id submittedTx
let queryTx = Link.getTransaction @'Shared wal (ApiTxId txid)
rGetTx <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx
[ expectResponseCode HTTP.status200
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
]
-- Make sure only fee is deducted from shared Wallet
eventually "Wallet balance is as expected" $ do
rWal <- getSharedWallet ctx walShared
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField
(traverse . #balance . #available . #getQuantity)
(`shouldBe` (amt - expectedFee))
]
eventually "Tx is in ledger finally" $ do
rGetTx' <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx'
[ expectResponseCode HTTP.status200
, expectField (#status . #getApiT) (`shouldBe` InLedger)
]
let listTxEp = Link.listTransactions @'Shared wal
request @[ApiTransaction n] ctx listTxEp Default Empty
>>= flip verify
[ expectListField 1
(#direction . #getApiT) (`shouldBe` Incoming)
, expectListField 1
(#status . #getApiT) (`shouldBe` InLedger)
]
it "SHARED_TRANSACTIONS_CREATE_01 - \
\Can create tx for an active shared wallet, untyped metadata" $
\ctx -> runResourceT $ do
m15txt <- liftIO $ genMnemonics M15
m12txt <- liftIO $ genMnemonics M12
let payload = Json [json| {
"name": "Shared Wallet",
"mnemonic_sentence": #{m15txt},
"mnemonic_second_factor": #{m12txt},
"passphrase": #{fixturePassphrase},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "self" },
"template":
{ "all":
[ "cosigner#0" ]
}
}
} |]
rPost <- postSharedWallet ctx Default payload
verify (fmap (swapEither . view #wallet) <$> rPost)
[ expectResponseCode HTTP.status201
]
let walShared@(ApiSharedWallet (Right wal)) =
getFromResponse Prelude.id rPost
let metadata = Json [json|{ "metadata": { "1": "hello" } }|]
let amt = 10 * minUTxOValue (_mainEra ctx)
fundSharedWallet @n ctx amt (pure walShared)
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wal) Default metadata
verify rTx
[ expectResponseCode HTTP.status202
, expectField (#coinSelection . #metadata) (`shouldSatisfy` isJust)
, expectField (#fee . #getQuantity) (`shouldSatisfy` (>0))
]
-- checking metadata before signing using decodeTransaction
let txCbor1 = getFromResponse #transaction rTx
let decodePayload1 = Json (toJSON txCbor1)
rDecodedTx1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wal) Default decodePayload1
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let metadata' =
ApiT (TxMetadata (Map.fromList [(1,TxMetaText "hello")]))
let decodedExpectations =
[ expectResponseCode HTTP.status202
, expectField (#fee . #getQuantity) (`shouldBe` expectedFee)
, expectField #withdrawals (`shouldBe` [])
, expectField #collateral (`shouldBe` [])
, expectField #metadata
(`shouldBe` (ApiTxMetadata (Just metadata')))
, expectField #scriptValidity
(`shouldBe` (Just $ ApiT TxScriptValid))
]
verify rDecodedTx1 decodedExpectations
-- checking metadata before signing via directly inspecting serialized
-- tx
let getMetadata (Cardano.InAnyCardanoEra _ tx) = Cardano.getTxBody tx &
\(Cardano.TxBody bodyContent) ->
Cardano.txMetadata bodyContent & \case
Cardano.TxMetadataNone ->
Nothing
Cardano.TxMetadataInEra _ (Cardano.TxMetadata m) ->
Just m
let era = fromApiEra $ _mainEra ctx
let txbinary1 = cardanoTxIdeallyNoLaterThan era $
getApiT (txCbor1 ^. #serialisedTxSealed)
case getMetadata txbinary1 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 (ApiSerialisedTransaction apiTx _) =
getFromResponse #transaction rTx
signedTx <-
signSharedTx ctx wal apiTx [ expectResponseCode HTTP.status202 ]
-- checking metadata after signing using decodeTransaction
let decodePayload2 = Json (toJSON signedTx)
rDecodedTx2 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wal) Default decodePayload2
verify rDecodedTx2 decodedExpectations
-- checking metadata after signing via directly inspecting serialized tx
let txbinary2 = cardanoTxIdeallyNoLaterThan era $
getApiT (signedTx ^. #serialisedTxSealed)
case getMetadata txbinary2 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"
-- Submit tx
submittedTx <- submitSharedTxWithWid ctx wal signedTx
verify submittedTx
[ expectSuccess
, expectResponseCode HTTP.status202
]
let txid = getFromResponse #id submittedTx
let queryTx = Link.getTransaction @'Shared wal (ApiTxId txid)
rGetTx <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx
[ expectResponseCode HTTP.status200
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
]
-- Make sure only fee is deducted from shared Wallet
eventually "Wallet balance is as expected" $ do
rWal <- getSharedWallet ctx walShared
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField
(traverse . #balance . #available . #getQuantity)
(`shouldBe` (amt - expectedFee))
]
eventually "Tx is in ledger finally" $ do
rGetTx' <- request @(ApiTransaction n) ctx queryTx Default Empty
verify rGetTx'
[ expectResponseCode HTTP.status200
, expectField (#status . #getApiT) (`shouldBe` InLedger)
]
let listTxEp = Link.listTransactions @'Shared wal
request @[ApiTransaction n] ctx listTxEp Default Empty
>>= flip verify
[ expectListField 1
(#direction . #getApiT) (`shouldBe` Incoming)
, expectListField 1
(#status . #getApiT) (`shouldBe` InLedger)
]
it "SHARED_TRANSACTIONS_CREATE_01a -\
\Empty payload is not allowed" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
let emptyPayload = Json [json|{}|]
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wa) Default emptyPayload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403InvalidConstructTx
]
it "SHARED_TRANSACTIONS_CREATE_01b - \
\Validity interval only is not allowed" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n 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 @'Shared wa)
Default validityInterval
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403InvalidConstructTx
]
it "SHARED_TRANSACTIONS_CREATE_04a - \
\Single Output Transaction with decode transaction - single party" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared 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 txCbor = getFromResponse #transaction rTx
let decodePayload = Json (toJSON txCbor)
rDecodedTxSource <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wa) Default decodePayload
rDecodedTxTarget <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
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))
]
verify rDecodedTxTarget sharedExpectationsBetweenWallets
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 2_147_485_500)
, ApiT (DerivationIndex 2_147_485_463)
, ApiT (DerivationIndex 2_147_483_648)
, ApiT (DerivationIndex 0)
, ApiT (DerivationIndex $ fromIntegral addrIx)
]
}
let isOutOurs out = case out of
WalletOutput _ -> False
ExternalOutput _ -> True
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)
]
let (ApiSerialisedTransaction apiTx _) =
getFromResponse #transaction rTx
signedTx <-
signSharedTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
let decodePayload1 = Json (toJSON signedTx)
rDecodedTxSource1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared wa) Default decodePayload1
rDecodedTxTarget1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload1
verify rDecodedTxTarget1 sharedExpectationsBetweenWallets
verify rDecodedTxSource1 $
sharedExpectationsBetweenWallets ++
[ expectField #inputs
(`shouldSatisfy` areOurs)
, expectField #outputs
(`shouldNotContain` [expectedTxOutTarget])
-- Check that the change output is there:
, expectField (#outputs)
((`shouldBe` 1) . length . filter isOutOurs)
]
-- Submit tx
submittedTx <- submitSharedTxWithWid ctx wa signedTx
verify submittedTx
[ expectSuccess
, expectResponseCode HTTP.status202
]
let walShared = ApiSharedWallet (Right wa)
eventually "Source wallet balance is decreased by amt + fee" $ do
rWal <- getSharedWallet ctx walShared
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField
(traverse . #balance . #available . #getQuantity)
(`shouldBe` (faucetUtxoAmt - expectedFee - amt))
]
eventually "Target wallet balance is increased by amt" $ do
rWa <- request @ApiWallet ctx
(Link.getWallet @'Shelley wb) Default Empty
verify rWa
[ expectSuccess
, expectField
(#balance . #available . #getQuantity)
(`shouldBe` amt)
]
it "SHARED_TRANSACTIONS_CREATE_04b - \
\Single Output Transaction Shared -> Shared" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
let srcWallet = ApiSharedWallet (Right wa)
destWallet@(ApiSharedWallet (Right wb)) <- emptySharedWallet ctx
-- prepare payload
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
rAddr <- request @[ApiAddressWithPath n] ctx
(Link.listAddresses @'Shared wb) Default Empty
let addrs = getFromResponse Prelude.id rAddr
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"payments": [{
"address": #{destination},
"amount": {
"quantity": #{amt},
"unit": "lovelace"
}
}]
}|]
-- Construct tx
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wa) Default payload
verify rTx
[ expectSuccess
, expectResponseCode HTTP.status202
, expectField (#fee . #getQuantity)
(`shouldSatisfy` (> 0))
]
let (ApiSerialisedTransaction apiTx _) =
getFromResponse #transaction rTx
-- Sign tx
signedTx <-
signSharedTx ctx wa apiTx [ expectResponseCode HTTP.status202 ]
-- Submit tx
submittedTx <- submitSharedTxWithWid ctx wa signedTx
verify submittedTx
[ expectSuccess
, expectResponseCode HTTP.status202
]
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let txid = getFromResponse #id submittedTx
let linkSrc = Link.getTransaction @'Shared wa (ApiTxId txid)
eventually "Source wallet balance is decreased by amt + fee" $ do
rWal <- getSharedWallet ctx srcWallet
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField
(traverse . #balance . #available . #getQuantity)
(`shouldBe` (faucetUtxoAmt - expectedFee - amt))
]
-- check outgoing tx in source wallet
rSrc <- request @(ApiTransaction n) ctx linkSrc Default Empty
verify rSrc
[ expectResponseCode HTTP.status200
, expectField (#amount . #getQuantity) (`shouldBe` amt + expectedFee)
, expectField (#direction . #getApiT) (`shouldBe` Outgoing)
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#fee . #getQuantity) (`shouldBe` expectedFee)
]
let linkDest = Link.getTransaction @'Shared wb (ApiTxId txid)
eventually "Target wallet balance is increased by amt" $ do
rWal <- getSharedWallet ctx destWallet
verify (fmap (view #wallet) <$> rWal)
[ expectResponseCode HTTP.status200
, expectField
(traverse . #balance . #available . #getQuantity)
(`shouldBe` amt)
]
-- check incoming tx in destination wallet
rDst <- request @(ApiTransaction n) ctx linkDest Default Empty
verify rDst
[ expectResponseCode HTTP.status200
, expectField (#amount . #getQuantity) (`shouldBe` amt)
, expectField (#direction . #getApiT) (`shouldBe` Incoming)
, expectField (#status . #getApiT) (`shouldBe` InLedger)
, expectField (#fee . #getQuantity) (`shouldBe` expectedFee)
]
it "SHARED_TRANSACTIONS_CREATE_04c - \
\Cannot spend less than minUTxOValue" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
let amt = minUTxOValue (_mainEra ctx) - 1
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403MinUTxOValue
]
it "SHARED_TRANSACTIONS_CREATE_04d - \
\Can't cover fee" $
\ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
payload <- liftIO $ mkTxPayload ctx wb faucetUtxoAmt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared wa) Default payload
verify rTx
[ expectResponseCode HTTP.status403
, expectErrorMessage errMsg403Fee
]
it "SHARED_TRANSACTIONS_CREATE_04e - \
\Multiple Output Tx to single wallet"
$ \ctx -> runResourceT $ do
wa <- fixtureSharedWallet @n ctx
wb <- emptyWallet ctx
addrs <- listAddresses @n ctx wb
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 @'Shared 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))
]
it "SHARED_TRANSACTIONS_CREATE_05a - \
\Single Output Transaction with decode transaction - multi party" $
\ctx -> runResourceT $ do
(sharedWal1, sharedWal2) <- fixtureTwoPartySharedWallet ctx
singleOutputTxTwoParty ctx sharedWal1 sharedWal2
it "SHARED_TRANSACTIONS_CREATE_05b - \
\Single Output Transaction with decode transaction - multi party" $
\ctx -> runResourceT $ do
(sharedWal1, sharedWal2, _, _, _, _) <-
fixtureTwoPartySharedWalletPatched ctx
singleOutputTxTwoParty ctx sharedWal1 sharedWal2
it "SHARED_TRANSACTIONS_CREATE_05c - \
\Single Output Transaction with decode transaction - multi party" $
\ctx -> runResourceT $ do
(sharedWal1, sharedWal2, sharedWal3) <-
fixtureThreePartySharedWallet ctx
-- check we see balance from two wallets
rSharedWal1 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal1))
rSharedWal2 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal2))
rSharedWal3 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal3))
let balanceExp =
[ expectResponseCode HTTP.status200
, expectField (traverse . #balance . #available)
(`shouldBe` Quantity faucetUtxoAmt)
]
verify (fmap (view #wallet) <$> rSharedWal1) balanceExp
verify (fmap (view #wallet) <$> rSharedWal2) balanceExp
verify (fmap (view #wallet) <$> rSharedWal3) balanceExp
wb <- emptyWallet ctx
let amt = (minUTxOValue (_mainEra ctx) :: Natural)
payload <- liftIO $ mkTxPayload ctx wb amt
rTx <- request @(ApiConstructTransaction n) ctx
(Link.createUnsignedTransaction @'Shared sharedWal1) Default payload
verify rTx
[ expectResponseCode HTTP.status202
]
let txCbor = getFromResponse #transaction rTx
let decodePayload1 = Json (toJSON txCbor)
rDecodedTx1Wal1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal1) Default decodePayload1
rDecodedTx1Wal2 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal2) Default decodePayload1
rDecodedTx1Wal3 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal3) Default decodePayload1
rDecodedTx1Target <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload1
let expectedFee = getFromResponse (#fee . #getQuantity) rTx
let (ApiScriptTemplate scriptTemplate) =
sharedWal1 ^. #paymentScriptTemplate
let paymentScript =
NativeExplicitScript
(replaceCosignersWithVerKeys
CA.UTxOExternal scriptTemplate (Index 1))
ViaSpending
let noVerKeyWitness = mkApiWitnessCount WitnessCount
{ verificationKey = 0
, scripts = [paymentScript]
, bootstrap = 0
}
let witsExp1 =
[ expectField (#witnessCount) (`shouldBe` noVerKeyWitness) ]
verify rDecodedTx1Wal1 witsExp1
verify rDecodedTx1Wal2 witsExp1
verify rDecodedTx1Wal3 witsExp1
-- for shelley wallet the script's verification key is unknown,
-- it only is aware of its policy verification key
let noVerKeyWitnessHex = mkApiWitnessCount WitnessCount
{ verificationKey = 0
, scripts = [changeRole CA.Unknown paymentScript]
, bootstrap = 0
}
let witsExp1hex =
[ expectField (#witnessCount) (`shouldBe` noVerKeyWitnessHex) ]
verify rDecodedTx1Target witsExp1hex
-- adding one witness
let (ApiSerialisedTransaction apiTx1 _) =
getFromResponse #transaction rTx
signedTx1 <-
signSharedTx ctx sharedWal1 apiTx1
[ expectResponseCode HTTP.status202 ]
let decodePayload2 = Json (toJSON signedTx1)
rDecodedTx2Wal1 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal1) Default decodePayload2
rDecodedTx2Wal2 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal2) Default decodePayload2
rDecodedTx2Wal3 <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shared sharedWal3) Default decodePayload2
rDecodedTx2Target <- request @(ApiDecodedTransaction n) ctx
(Link.decodeTransaction @'Shelley wb) Default decodePayload2
let oneVerKeyWitness = mkApiWitnessCount WitnessCount
{ verificationKey = 1
, scripts = [paymentScript]
, bootstrap = 0
}
let witsExp2 =
[ expectField (#witnessCount) (`shouldBe` oneVerKeyWitness) ]
verify rDecodedTx2Wal1 witsExp2
verify rDecodedTx2Wal2 witsExp2
verify rDecodedTx2Wal3 witsExp2
-- for shelley wallet the script's verification key is unknown,
-- it only is aware of its policy verification key
let oneVerKeyWitnessHex = mkApiWitnessCount WitnessCount
{ verificationKey = 1
, scripts = [changeRole CA.Unknown paymentScript]
, bootstrap = 0
}
let witsExp2hex =
[ expectField (#witnessCount) (`shouldBe` oneVerKeyWitnessHex) ]
verify rDecodedTx2Target witsExp2hex
submittedTx1 <- submitSharedTxWithWid ctx sharedWal1 signedTx1
verify submittedTx1
[ expectResponseCode HTTP.status403
, expectErrorMessage (errMsg403MissingWitsInTransaction 2 1)
]
-- adding the witness by the second participant make tx valid for
-- submission
let (ApiSerialisedTransaction apiTx2 _) = signedTx1
signedTx3 <-
signSharedTx ctx sharedWal2 apiTx2
[ expectResponseCode HTTP.status202 ]
-- now submission works
submittedTx3 <- submitSharedTxWithWid ctx sharedWal1 signedTx3
verify submittedTx3
[ expectResponseCode HTTP.status202
]
-- checking decreased balance of shared wallets
eventually "wShared balance is decreased" $ do
wal1 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal1))
wal2 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal2))
wal3 <- getSharedWallet ctx (ApiSharedWallet (Right sharedWal3))
let balanceExp1 =
[ expectResponseCode HTTP.status200
, expectField (traverse . #balance . #available)
(`shouldBe` Quantity
(faucetUtxoAmt - expectedFee - amt)
)
]
verify (fmap (view #wallet) <$> wal1) balanceExp1
verify (fmap (view #wallet) <$> wal2) balanceExp1
verify (fmap (view #wallet) <$> wal3) balanceExp1