/
Types.hs
4044 lines (3515 loc) · 148 KB
/
Types.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 DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
-- |
-- Copyright: © 2018-2020 IOHK
-- License: Apache-2.0
--
-- API type representations of various types. We define here pretty much all our
-- user-facing types that are mostly composed with internal / primitive types.
--
-- This module also define required API instances (JSON, HttpApiData...) for all
-- those types, making sure to match the specification document:
--
-- <https://github.com/input-output-hk/cardano-wallet/blob/master/specifications/api/swagger.yaml Wallet API Specification>
module Cardano.Wallet.Api.Types
(
-- * Wallet Styles
WalletStyle (..)
, ByronWalletStyle (..)
, StyleSymbol
, AllowedMnemonics
, fmtAllowedWords
-- * API Types
, ApiAsset (..)
, toApiAsset
, ApiAssetMetadata (..)
, toApiAssetMetadata
, ApiAddress (..)
, ApiCredential (..)
, ApiAddressData (..)
, ApiAddressDataPayload (..)
, AnyAddress (..)
, AnyAddressType (..)
, ApiCertificate (..)
, ApiDelegationAction (..)
, ApiEpochInfo (..)
, toApiEpochInfo
, ApiSelectCoinsData (..)
, ApiSelectCoinsPayments (..)
, ApiSelectCoinsAction (..)
, ApiMintBurnOperation (..)
, ApiMintData(..)
, ApiBurnData(..)
, ApiCoinSelection (..)
, ApiCoinSelectionChange (..)
, ApiCoinSelectionCollateral (..)
, ApiCoinSelectionOutput (..)
, ApiCoinSelectionWithdrawal (..)
, ApiBase64
, ApiMintBurnData (..)
, ApiStakePool (..)
, ApiStakePoolMetrics (..)
, ApiStakePoolFlag (..)
, ApiWallet (..)
, ApiWalletBalance (..)
, ApiWalletAssetsBalance (..)
, ApiWalletPassphrase (..)
, ApiWalletPassphraseInfo (..)
, ApiWalletUtxoSnapshot (..)
, ApiWalletUtxoSnapshotEntry (..)
, ApiUtxoStatistics (..)
, toApiUtxoStatistics
, WalletPostData (..)
, WalletPutData (..)
, SettingsPutData (..)
, WalletPutPassphraseData (..)
, ApiSignTransactionPostData (..)
, PostTransactionOldData (..)
, PostTransactionFeeOldData (..)
, ApiSerialisedTransaction (..)
, ApiTransaction (..)
, ApiMintedBurnedTransaction (..)
, ApiMintedBurnedInfo (..)
, ApiWithdrawalPostData (..)
, ApiMaintenanceAction (..)
, ApiMaintenanceActionPostData (..)
, MaintenanceAction (..)
, ApiFee (..)
, ApiTxCollateral (..)
, ApiTxId (..)
, ApiTxInput (..)
, ApiTxMetadata (..)
, AddressAmount (..)
, AddressAmountNoAssets (..)
, ApiAddressInspect (..)
, ApiAddressInspectData (..)
, ApiErrorCode (..)
, ApiNetworkInformation (..)
, ApiEra (..)
, ApiNtpStatus (..)
, NtpSyncingStatus (..)
, ApiNetworkClock (..)
, ApiSlotReference (..)
, ApiSlotId (..)
, ApiBlockReference (..)
, ApiBlockInfo (..)
, ApiStakeKeys (..)
, ApiOurStakeKey (..)
, ApiForeignStakeKey (..)
, ApiNullStakeKey (..)
, Iso8601Time (..)
, MinWithdrawal (..)
, ApiNetworkParameters (..)
, toApiNetworkParameters
, ApiEraInfo (..)
, ApiWalletDelegation (..)
, ApiWalletDelegationStatus (..)
, ApiWalletDelegationNext (..)
, ApiPoolId (..)
, ApiWalletMigrationPlanPostData (..)
, ApiWalletMigrationPostData (..)
, ApiWalletMigrationBalance (..)
, ApiWalletMigrationPlan (..)
, ApiWithdrawal (..)
, ApiWalletSignData (..)
, ApiVerificationKeyShelley (..)
, ApiVerificationKeyShared (..)
, ApiScriptTemplateEntry (..)
, XPubOrSelf (..)
, VerificationKeyHashing (..)
, ApiAccountKey (..)
, ApiAccountKeyShared (..)
, KeyFormat (..)
, ApiPostAccountKeyData (..)
, ApiPostAccountKeyDataWithPurpose (..)
, ApiConstructTransaction (..)
, ApiConstructTransactionData (..)
, ApiMultiDelegationAction (..)
, ApiStakeKeyIndex (..)
, ApiPaymentDestination (..)
, ApiValidityInterval (..)
, ApiValidityBound
, PostMintBurnAssetData(..)
, ApiBalanceTransactionPostData (..)
, ApiExternalInput (..)
, ApiRedeemer (..)
, ApiDecodedTransaction (..)
, ApiWalletInput (..)
, ApiTxInputGeneral (..)
, ResourceContext (..)
, ApiWithdrawalGeneral (..)
, ApiWalletOutput (..)
, ApiTxOutputGeneral (..)
, ApiAnyCertificate (..)
, ApiExternalCertificate (..)
, ApiRegisterPool (..)
, ApiDeregisterPool (..)
-- * API Types (Byron)
, ApiByronWallet (..)
, ApiByronWalletBalance (..)
, ByronWalletPostData (..)
, SomeByronWalletPostData (..)
, ByronWalletFromXPrvPostData (..)
, ByronWalletPutPassphraseData (..)
, ApiPostRandomAddressData (..)
, ApiWalletDiscovery (..)
, KnownDiscovery(..)
, ApiPutAddressesData (..)
-- * API Types (Hardware)
, AccountPostData (..)
, ApiAccountPublicKey (..)
, WalletOrAccountPostData (..)
-- * User-Facing Address Encoding/Decoding
, EncodeAddress (..)
, DecodeAddress (..)
, EncodeStakeAddress (..)
, DecodeStakeAddress (..)
-- * Shared Wallets
, ApiSharedWallet (..)
, ApiPendingSharedWallet (..)
, ApiActiveSharedWallet (..)
, ApiSharedWalletPostData (..)
, ApiSharedWalletPostDataFromMnemonics (..)
, ApiSharedWalletPostDataFromAccountPubX (..)
, ApiSharedWalletPatchData (..)
-- * Polymorphic Types
, ApiT (..)
, ApiMnemonicT (..)
, ApiBytesT (..)
-- * Type families
, ApiAddressT
, ApiStakeKeysT
, ApiPutAddressesDataT
, ApiAddressIdT
, ApiCoinSelectionT
, ApiSelectCoinsDataT
, ApiTransactionT
, ApiConstructTransactionT
, ApiConstructTransactionDataT
, PostTransactionOldDataT
, PostTransactionFeeOldDataT
, ApiMintedBurnedTransactionT
, ApiWalletMigrationPlanPostDataT
, ApiWalletMigrationPostDataT
, PostMintBurnAssetDataT
, ApiBalanceTransactionPostDataT
, ApiDecodedTransactionT
-- * API Type Conversions
, coinToQuantity
, coinFromQuantity
-- * Others
, defaultRecordTypeOptions
, strictRecordTypeOptions
, HealthStatusSMASH (..)
, HealthCheckSMASH (..)
, ApiHealthCheck (..)
-- * Re-exports
, Base (Base16, Base64)
) where
import Prelude
import Cardano.Address.Derivation
( XPrv, XPub, xpubFromBytes, xpubToBytes )
import Cardano.Address.Script
( Cosigner (..), KeyHash, Script, ScriptTemplate, ValidationLevel (..) )
import Cardano.Api
( StakeAddress
, TxMetadataJsonSchema (..)
, deserialiseFromBech32
, displayError
, metadataFromJson
, metadataToJson
, proxyToAsType
, serialiseToBech32
)
import Cardano.Mnemonic
( MkSomeMnemonic (..)
, MkSomeMnemonicError (..)
, SomeMnemonic (..)
, mnemonicToText
, natVals
)
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, DerivationIndex (..)
, DerivationType (..)
, Index (..)
, NetworkDiscriminant (..)
, Passphrase (..)
, PassphraseMaxLength (..)
, PassphraseMinLength (..)
, Role (..)
, fromHex
, hex
)
import Cardano.Wallet.Primitive.AddressDerivation.SharedKey
( purposeCIP1854 )
import Cardano.Wallet.Primitive.AddressDiscovery.Random
( RndState )
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap, SeqState, getAddressPoolGap, purposeCIP1852 )
import Cardano.Wallet.Primitive.Slotting
( Qry, timeOfEpoch )
import Cardano.Wallet.Primitive.SyncProgress
( SyncProgress (..) )
import Cardano.Wallet.Primitive.Types
( ActiveSlotCoefficient (..)
, DecentralizationLevel (..)
, EpochLength (..)
, EpochNo (..)
, ExecutionUnitPrices (..)
, GenesisParameters (..)
, MinimumUTxOValue (..)
, NetworkParameters (..)
, NonWalletCertificate (..)
, PoolId (..)
, PoolMetadataGCStatus (..)
, SlotInEpoch (..)
, SlotLength (..)
, SlotNo (..)
, SlottingParameters (..)
, SmashServer (..)
, StakePoolMetadata
, StartTime (..)
, WalletId (..)
, WalletName (..)
, decodePoolIdBech32
, encodePoolIdBech32
, unsafeEpochNo
)
import Cardano.Wallet.Primitive.Types.Address
( Address (..), AddressState (..) )
import Cardano.Wallet.Primitive.Types.Coin
( Coin (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Cardano.Wallet.Primitive.Types.Tx
( Direction (..)
, SealedTx (..)
, SerialisedTx (..)
, TxConstraints (..)
, TxIn (..)
, TxMetadata
, TxScriptValidity (..)
, TxStatus (..)
, coinIsValidForTxOut
, sealedTxFromBytes
, txMetadataIsNull
, txOutMaxCoin
)
import Cardano.Wallet.Primitive.Types.UTxO
( BoundType, HistogramBar (..), UTxOStatistics (..) )
import Cardano.Wallet.TokenMetadata
( TokenMetadataError (..) )
import Cardano.Wallet.Util
( ShowFmt (..) )
import Codec.Binary.Bech32
( dataPartFromBytes, dataPartToBytes )
import Codec.Binary.Bech32.TH
( humanReadablePart )
import "cardano-addresses" Codec.Binary.Encoding
( AbstractEncoding (..), detectEncoding, encode, fromBase16 )
import Control.Applicative
( optional, (<|>) )
import Control.Arrow
( left )
import Control.DeepSeq
( NFData (..) )
import Control.Monad
( guard, when, (<=<), (>=>) )
import Data.Aeson.Types
( FromJSON (..)
, Parser
, SumEncoding (..)
, ToJSON (..)
, Value (Object, String)
, camelTo2
, constructorTagModifier
, fieldLabelModifier
, genericParseJSON
, genericToJSON
, object
, omitNothingFields
, prependFailure
, rejectUnknownFields
, sumEncoding
, tagSingleConstructors
, withObject
, withText
, (.!=)
, (.:)
, (.:?)
, (.=)
)
import Data.Bifunctor
( bimap, first )
import Data.ByteArray
( ByteArray, ByteArrayAccess )
import Data.ByteArray.Encoding
( Base (..), convertFromBase, convertToBase )
import Data.ByteString
( ByteString )
import Data.Data
( Data )
import Data.Either.Combinators
( maybeToRight )
import Data.Either.Extra
( eitherToMaybe, maybeToEither )
import Data.Function
( (&) )
import Data.Generics.Internal.VL.Lens
( view, (^.) )
import Data.Hashable
( Hashable )
import Data.Kind
( Type )
import Data.List
( intercalate )
import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Map.Strict
( Map )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Percentage, Quantity (..) )
import Data.String
( IsString )
import Data.Text
( Text, split )
import Data.Text.Class
( CaseStyle (..)
, FromText (..)
, TextDecodingError (..)
, ToText (..)
, fromTextToBoundedEnum
, toTextFromBoundedEnum
)
import Data.Time.Clock
( NominalDiffTime, UTCTime )
import Data.Time.Clock.POSIX
( posixSecondsToUTCTime, utcTimeToPOSIXSeconds )
import Data.Time.Text
( iso8601, iso8601ExtendedUtc, utcTimeFromText, utcTimeToText )
import Data.Traversable
( for )
import Data.Typeable
( Typeable, typeRep )
import Data.Word
( Word16, Word32, Word64 )
import Data.Word.Odd
( Word31 )
import Fmt
( pretty )
import GHC.Generics
( Generic, Rep )
import GHC.TypeLits
( Nat, Symbol )
import Numeric.Natural
( Natural )
import Quiet
( Quiet (..) )
import Servant.API
( MimeRender (..), MimeUnrender (..), OctetStream )
import Web.HttpApiData
( FromHttpApiData (..), ToHttpApiData (..) )
import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet.Primitive.AddressDerivation as AD
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.RewardAccount as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W
import qualified Cardano.Wallet.Primitive.Types.TokenMap as TokenMap
import qualified Cardano.Wallet.Primitive.Types.TokenMap as W
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Codec.Binary.Bech32 as Bech32
import qualified Codec.Binary.Bech32.TH as Bech32
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as HM
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.Read as T
{-------------------------------------------------------------------------------
Styles of Wallets
-------------------------------------------------------------------------------}
data WalletStyle
= Shelley
| Byron
| Shared
data ByronWalletStyle
= Random
| Icarus
| Trezor
| Ledger
deriving (Show, Generic, Eq, Bounded, Enum)
instance FromText ByronWalletStyle where
fromText = fromTextToBoundedEnum SnakeLowerCase
instance ToText ByronWalletStyle where
toText = toTextFromBoundedEnum SnakeLowerCase
data SndFactor
= SndFactor
type family StyleSymbol (style :: ByronWalletStyle) :: Symbol where
StyleSymbol 'Random = "random"
StyleSymbol 'Icarus = "icarus"
StyleSymbol 'Trezor = "trezor"
StyleSymbol 'Ledger = "ledger"
type family AllowedMnemonics (style :: k) :: [Nat]
type instance AllowedMnemonics 'Random = '[12,15,18,21,24]
type instance AllowedMnemonics 'Icarus = '[12,15,18,21,24]
type instance AllowedMnemonics 'Trezor = '[12,15,18,21,24]
type instance AllowedMnemonics 'Ledger = '[12,15,18,21,24]
type instance AllowedMnemonics 'Shelley = '[15,18,21,24]
type instance AllowedMnemonics 'SndFactor = '[9,12]
fmtAllowedWords :: ByronWalletStyle -> String
fmtAllowedWords =
(++ " mnemonic words") . formatEnglishEnumeration . allowedWordLengths
where
allowedWordLengths = \case
Random -> map show $ natVals $ Proxy @(AllowedMnemonics 'Random)
Icarus -> map show $ natVals $ Proxy @(AllowedMnemonics 'Icarus)
Trezor -> map show $ natVals $ Proxy @(AllowedMnemonics 'Trezor)
Ledger -> map show $ natVals $ Proxy @(AllowedMnemonics 'Ledger)
-- >>> formatEnglishEnumeration ["a", "b", "c"]
-- "a, b or c"
--
-- >>> formatEnglishEnumeration ["a", "b"]
-- "a or b"
--
-- >>> formatEnglishEnumeration ["a"]
-- "a"
formatEnglishEnumeration = formatEnglishEnumerationRev . reverse
formatEnglishEnumerationRev [ult, penult]
= penult ++ " or " ++ ult
formatEnglishEnumerationRev (ult:penult:revBeginning)
= intercalate ", " (reverse revBeginning)
++ ", "
++ penult
++ " or "
++ ult
formatEnglishEnumerationRev xs = intercalate ", " (reverse xs)
{-------------------------------------------------------------------------------
API Types
-------------------------------------------------------------------------------}
data MaintenanceAction = GcStakePools
deriving (Eq, Generic, Show)
newtype ApiMaintenanceActionPostData = ApiMaintenanceActionPostData
{ maintenanceAction :: MaintenanceAction
}
deriving (Eq, Generic)
deriving Show via (Quiet ApiMaintenanceActionPostData)
newtype ApiMaintenanceAction = ApiMaintenanceAction
{ gcStakePools :: ApiT PoolMetadataGCStatus
}
deriving (Eq, Generic)
deriving Show via (Quiet ApiMaintenanceAction)
data ApiAsset = ApiAsset
{ policyId :: ApiT W.TokenPolicyId
, assetName :: ApiT W.TokenName
, fingerprint :: ApiT W.TokenFingerprint
, metadata :: Maybe ApiAssetMetadata
, metadataError :: Maybe ApiMetadataError
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiMetadataError = Fetch | Parse
deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiAssetMetadata = ApiAssetMetadata
{ name :: Text
, description :: Text
, ticker :: Maybe Text
, url :: Maybe (ApiT W.AssetURL)
, logo :: Maybe (ApiT W.AssetLogo)
, decimals :: Maybe (ApiT W.AssetDecimals)
} deriving (Eq, Generic, Ord, Show)
deriving anyclass NFData
toApiAsset
:: Either TokenMetadataError (Maybe W.AssetMetadata)
-> W.AssetId
-> ApiAsset
toApiAsset metadata_ (W.AssetId policyId_ assetName_) = ApiAsset
{ policyId = ApiT policyId_
, assetName = ApiT assetName_
, fingerprint = ApiT $ W.mkTokenFingerprint policyId_ assetName_
, metadata = either (const Nothing) (fmap toApiAssetMetadata) metadata_
, metadataError = either (Just . category) (const Nothing) metadata_
}
where
category = \case
TokenMetadataClientError _ -> Fetch
TokenMetadataFetchError _ -> Fetch
TokenMetadataJSONParseError _ _ -> Parse
toApiAssetMetadata :: W.AssetMetadata -> ApiAssetMetadata
toApiAssetMetadata W.AssetMetadata{name,description,ticker,url,logo,decimals} =
ApiAssetMetadata name description ticker
(ApiT <$> url) (ApiT <$> logo) (ApiT <$> decimals)
data ApiAddress (n :: NetworkDiscriminant) = ApiAddress
{ id :: !(ApiT Address, Proxy n)
, state :: !(ApiT AddressState)
, derivationPath :: NonEmpty (ApiT DerivationIndex)
} deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
data ApiCredential =
CredentialPubKey ByteString
| CredentialScript (Script KeyHash)
deriving (Eq, Generic, Show)
data ApiAddressData = ApiAddressData
{ address :: !ApiAddressDataPayload
, validationLevel :: !(Maybe (ApiT ValidationLevel))
} deriving (Eq, Generic, Show)
data ApiAddressDataPayload =
AddrEnterprise ApiCredential
| AddrRewardAccount ApiCredential
| AddrBase ApiCredential ApiCredential
deriving (Eq, Generic, Show)
data AnyAddressType =
EnterpriseDelegating
| RewardAccount
deriving (Eq, Show, Bounded, Enum)
data AnyAddress = AnyAddress
{ payload :: ByteString
, flavour :: AnyAddressType
, network :: Int
} deriving (Eq, Generic, Show)
data ApiEpochInfo = ApiEpochInfo
{ epochNumber :: !(ApiT EpochNo)
, epochStartTime :: !UTCTime
} deriving (Eq, Generic, Show)
deriving anyclass NFData
toApiEpochInfo :: EpochNo -> Qry ApiEpochInfo
toApiEpochInfo ep = ApiEpochInfo (ApiT ep) . fst <$> timeOfEpoch ep
data ApiSelectCoinsData (n :: NetworkDiscriminant)
= ApiSelectForPayment (ApiSelectCoinsPayments n)
| ApiSelectForDelegation ApiSelectCoinsAction
deriving (Eq, Generic, Show, Typeable)
data ApiSelectCoinsPayments (n :: NetworkDiscriminant) = ApiSelectCoinsPayments
{ payments :: NonEmpty (AddressAmount (ApiT Address, Proxy n))
, withdrawal :: !(Maybe ApiWithdrawalPostData)
, metadata :: !(Maybe (ApiT TxMetadata))
} deriving (Eq, Generic, Show, Typeable)
newtype ApiSelectCoinsAction = ApiSelectCoinsAction
{ delegationAction :: ApiDelegationAction
}
deriving (Eq, Generic)
deriving Show via (Quiet ApiSelectCoinsAction)
data ApiCertificate
= RegisterRewardAccount
{ rewardAccountPath :: NonEmpty (ApiT DerivationIndex)
}
| JoinPool
{ rewardAccountPath :: NonEmpty (ApiT DerivationIndex)
, pool :: ApiT PoolId
}
| QuitPool
{ rewardAccountPath :: NonEmpty (ApiT DerivationIndex)
}
deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiDelegationAction = Join (ApiT PoolId) | Quit
deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiCoinSelection (n :: NetworkDiscriminant) = ApiCoinSelection
{ inputs :: ![ApiWalletInput n]
, outputs :: ![ApiCoinSelectionOutput n]
, change :: ![ApiCoinSelectionChange n]
, collateral :: ![ApiCoinSelectionCollateral n]
, withdrawals :: ![ApiCoinSelectionWithdrawal n]
, certificates :: Maybe (NonEmpty ApiCertificate)
, deposits :: ![Quantity "lovelace" Natural]
, metadata :: !(Maybe ApiBase64)
} deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
data ApiCoinSelectionChange (n :: NetworkDiscriminant) = ApiCoinSelectionChange
{ address :: !(ApiT Address, Proxy n)
, amount :: !(Quantity "lovelace" Natural)
, assets :: !(ApiT W.TokenMap)
, derivationPath :: NonEmpty (ApiT DerivationIndex)
} deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
data ApiCoinSelectionOutput (n :: NetworkDiscriminant) = ApiCoinSelectionOutput
{ address :: !(ApiT Address, Proxy n)
, amount :: !(Quantity "lovelace" Natural)
, assets :: !(ApiT W.TokenMap)
} deriving (Eq, Ord, Generic, Show, Typeable)
deriving anyclass (NFData, Hashable)
data ApiCoinSelectionCollateral (n :: NetworkDiscriminant) =
ApiCoinSelectionCollateral
{ id :: !(ApiT (Hash "Tx"))
, index :: !Word32
, address :: !(ApiT Address, Proxy n)
, derivationPath :: NonEmpty (ApiT DerivationIndex)
, amount :: !(Quantity "lovelace" Natural)
}
deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
data ApiWallet = ApiWallet
{ id :: !(ApiT WalletId)
, addressPoolGap :: !(ApiT AddressPoolGap)
, balance :: !ApiWalletBalance
, assets :: !ApiWalletAssetsBalance
, delegation :: !ApiWalletDelegation
, name :: !(ApiT WalletName)
, passphrase :: !(Maybe ApiWalletPassphraseInfo)
, state :: !(ApiT SyncProgress)
, tip :: !ApiBlockReference
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiWalletBalance = ApiWalletBalance
{ available :: !(Quantity "lovelace" Natural)
, total :: !(Quantity "lovelace" Natural)
, reward :: !(Quantity "lovelace" Natural)
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiWalletAssetsBalance = ApiWalletAssetsBalance
{ available :: !(ApiT W.TokenMap)
, total :: !(ApiT W.TokenMap)
} deriving (Eq, Generic, Show)
deriving anyclass NFData
newtype ApiWalletPassphraseInfo = ApiWalletPassphraseInfo
{ lastUpdatedAt :: UTCTime
}
deriving (Eq, Generic)
deriving anyclass NFData
deriving Show via (Quiet ApiWalletPassphraseInfo)
data ApiWalletDelegation = ApiWalletDelegation
{ active :: !ApiWalletDelegationNext
, next :: ![ApiWalletDelegationNext]
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiWalletDelegationNext = ApiWalletDelegationNext
{ status :: !ApiWalletDelegationStatus
, target :: !(Maybe (ApiT PoolId))
, changesAt :: !(Maybe ApiEpochInfo)
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiWalletDelegationStatus
= NotDelegating
| Delegating
deriving (Eq, Generic, Show)
deriving anyclass NFData
newtype ApiWalletPassphrase = ApiWalletPassphrase
{ passphrase :: ApiT (Passphrase "lenient")
}
deriving (Eq, Generic)
deriving anyclass NFData
deriving Show via (Quiet ApiWalletPassphrase)
newtype ApiWalletUtxoSnapshot = ApiWalletUtxoSnapshot
{ entries :: [ApiWalletUtxoSnapshotEntry]
}
deriving (Eq, Generic)
deriving anyclass NFData
deriving Show via (Quiet ApiWalletUtxoSnapshot)
data ApiWalletUtxoSnapshotEntry = ApiWalletUtxoSnapshotEntry
{ ada :: !(Quantity "lovelace" Natural)
, adaMinimum :: !(Quantity "lovelace" Natural)
, assets :: !(ApiT W.TokenMap)
}
deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiStakePool = ApiStakePool
{ id :: !(ApiT PoolId)
, metrics :: !ApiStakePoolMetrics
, metadata :: !(Maybe (ApiT StakePoolMetadata))
, cost :: !(Quantity "lovelace" Natural)
, margin :: !(Quantity "percent" Percentage)
, pledge :: !(Quantity "lovelace" Natural)
, retirement :: !(Maybe ApiEpochInfo)
, flags :: ![ApiStakePoolFlag]
} deriving (Eq, Generic, Show)
data ApiStakePoolFlag
= Delisted
deriving stock (Eq, Generic, Show)
deriving anyclass NFData
data ApiStakePoolMetrics = ApiStakePoolMetrics
{ nonMyopicMemberRewards :: !(Quantity "lovelace" Natural)
, relativeStake :: !(Quantity "percent" Percentage)
, saturation :: !Double
, producedBlocks :: !(Quantity "block" Natural)
} deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiUtxoStatistics = ApiUtxoStatistics
{ total :: !(Quantity "lovelace" Natural)
, scale :: !(ApiT BoundType)
, distribution :: !(Map Word64 Word64)
} deriving (Eq, Generic, Show)
deriving anyclass NFData
toApiUtxoStatistics :: UTxOStatistics -> ApiUtxoStatistics
toApiUtxoStatistics (UTxOStatistics histo totalStakes bType) =
ApiUtxoStatistics
{ total = Quantity (fromIntegral totalStakes)
, scale = ApiT bType
, distribution = Map.fromList $ map (\(HistogramBar k v)-> (k,v)) histo
}
data WalletPostData = WalletPostData
{ addressPoolGap :: !(Maybe (ApiT AddressPoolGap))
, mnemonicSentence :: !(ApiMnemonicT (AllowedMnemonics 'Shelley))
, mnemonicSecondFactor :: !(Maybe (ApiMnemonicT (AllowedMnemonics 'SndFactor)))
, name :: !(ApiT WalletName)
, passphrase :: !(ApiT (Passphrase "raw"))
} deriving (Eq, Generic, Show)
data SomeByronWalletPostData
= RandomWalletFromMnemonic (ByronWalletPostData (AllowedMnemonics 'Random))
| RandomWalletFromXPrv ByronWalletFromXPrvPostData
| SomeIcarusWallet (ByronWalletPostData (AllowedMnemonics 'Icarus))
| SomeTrezorWallet (ByronWalletPostData (AllowedMnemonics 'Trezor))
| SomeLedgerWallet (ByronWalletPostData (AllowedMnemonics 'Ledger))
| SomeAccount AccountPostData
deriving (Eq, Generic, Show)
data ByronWalletPostData mw = ByronWalletPostData
{ mnemonicSentence :: !(ApiMnemonicT mw)
, name :: !(ApiT WalletName)
, passphrase :: !(ApiT (Passphrase "raw"))
} deriving (Eq, Generic, Show)
data ByronWalletFromXPrvPostData = ByronWalletFromXPrvPostData
{ name :: !(ApiT WalletName)
, encryptedRootPrivateKey :: !(ApiT XPrv)
-- ^ A root private key hex-encoded, encrypted using a given passphrase.
-- The underlying key should contain: private key, chain code, and public key
, passphraseHash :: !(ApiT (Hash "encryption"))
-- ^ A hash of master passphrase. The hash should be an output of a
-- Scrypt function with the following parameters:
-- - logN = 14
-- - r = 8
-- - p = 1
} deriving (Eq, Generic, Show)
deriving anyclass NFData
newtype ApiAccountPublicKey = ApiAccountPublicKey
{ key :: (ApiT XPub)
}
deriving (Eq, Generic)
deriving anyclass NFData
deriving Show via (Quiet ApiAccountPublicKey)
newtype WalletOrAccountPostData = WalletOrAccountPostData
{ postData :: Either WalletPostData AccountPostData
}
deriving (Eq, Generic)
deriving Show via (Quiet WalletOrAccountPostData)
data AccountPostData = AccountPostData
{ name :: !(ApiT WalletName)
, accountPublicKey :: !ApiAccountPublicKey
, addressPoolGap :: !(Maybe (ApiT AddressPoolGap))
} deriving (Eq, Generic, Show)
newtype WalletPutData = WalletPutData
{ name :: (Maybe (ApiT WalletName))
}
deriving (Eq, Generic)
deriving Show via (Quiet WalletPutData)
newtype SettingsPutData = SettingsPutData
{ settings :: (ApiT W.Settings)
}
deriving (Eq, Generic)
deriving Show via (Quiet SettingsPutData)
data WalletPutPassphraseData = WalletPutPassphraseData
{ oldPassphrase :: !(ApiT (Passphrase "raw"))
, newPassphrase :: !(ApiT (Passphrase "raw"))
} deriving (Eq, Generic, Show)
data ByronWalletPutPassphraseData = ByronWalletPutPassphraseData
{ oldPassphrase :: !(Maybe (ApiT (Passphrase "lenient")))
, newPassphrase :: !(ApiT (Passphrase "raw"))
} deriving (Eq, Generic, Show)
data ApiConstructTransaction (n :: NetworkDiscriminant) = ApiConstructTransaction
{ transaction :: !(ApiT SealedTx)
, coinSelection :: !(ApiCoinSelection n)
, fee :: !(Quantity "lovelace" Natural)
} deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
-- | Index of the stake key.
newtype ApiStakeKeyIndex = ApiStakeKeyIndex (ApiT DerivationIndex)
deriving (Eq, Generic, Show)
deriving anyclass NFData
-- | Stake pool delegation certificates.
data ApiMultiDelegationAction
= Joining !(ApiT PoolId) !ApiStakeKeyIndex
-- ^ Delegate given staking index to a pool, possibly registering the stake
-- key at the same time.
| Leaving !ApiStakeKeyIndex
-- ^ Undelegate the given staking index from its pool.
deriving (Eq, Generic, Show)
deriving anyclass NFData
-- | Input parameters for transaction construction.
data ApiConstructTransactionData (n :: NetworkDiscriminant) = ApiConstructTransactionData
{ payments :: !(Maybe (ApiPaymentDestination n))
, withdrawal :: !(Maybe ApiWithdrawalPostData)
, metadata :: !(Maybe (ApiT TxMetadata))
, mint :: !(Maybe (ApiT W.TokenMap))
, delegations :: !(Maybe (NonEmpty ApiMultiDelegationAction))
, validityInterval :: !(Maybe ApiValidityInterval)
} deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
data ApiPaymentDestination (n :: NetworkDiscriminant)
= ApiPaymentAddresses !(NonEmpty (AddressAmount (ApiAddressIdT n)))
-- ^ Pay amounts to one or more addresses.
| ApiPaymentAll !(NonEmpty (ApiT Address, Proxy n))
-- ^ Migrate all money to one or more addresses.
deriving (Eq, Generic, Show, Typeable)
deriving anyclass NFData
-- | Times where transactions are valid.
data ApiValidityInterval = ApiValidityInterval
{ invalidBefore :: !ApiValidityBound
-- ^ Tx is not valid before this time. Defaults to genesis.
, invalidHereafter :: !ApiValidityBound
-- ^ Tx is not valid at this time and after. Defaults to now + 2 hours.
} deriving (Eq, Generic, Show)
deriving anyclass NFData
-- | One side of the validity interval.
data ApiValidityBound
= ApiValidityBoundUnspecified
-- ^ Use the default.
| ApiValidityBoundAsTimeFromNow !(Quantity "second" NominalDiffTime)
-- ^ Time from transaction construction (not submission).
| ApiValidityBoundAsSlot !(Quantity "slot" Word64)
-- ^ Absolute slot number.
deriving (Eq, Generic, Show)
deriving anyclass NFData
data ApiSignTransactionPostData = ApiSignTransactionPostData
{ transaction :: !(ApiT SealedTx)
, passphrase :: !(ApiT (Passphrase "lenient"))
} deriving (Eq, Generic, Show)
-- | Legacy transaction API.
data PostTransactionOldData (n :: NetworkDiscriminant) = PostTransactionOldData
{ payments :: !(NonEmpty (AddressAmount (ApiT Address, Proxy n)))
, passphrase :: !(ApiT (Passphrase "lenient"))
, withdrawal :: !(Maybe ApiWithdrawalPostData)
, metadata :: !(Maybe (ApiT TxMetadata))
, timeToLive :: !(Maybe (Quantity "second" NominalDiffTime))
} deriving (Eq, Generic, Show, Typeable)
-- | Legacy transaction API.
data PostTransactionFeeOldData (n :: NetworkDiscriminant) = PostTransactionFeeOldData
{ payments :: (NonEmpty (AddressAmount (ApiT Address, Proxy n)))
, withdrawal :: !(Maybe ApiWithdrawalPostData)
, metadata :: !(Maybe (ApiT TxMetadata))
, timeToLive :: !(Maybe (Quantity "second" NominalDiffTime))
} deriving (Eq, Generic, Show, Typeable)
type ApiBase64 = ApiBytesT 'Base64 ByteString