Skip to content

Commit

Permalink
Add type ApiCoinSelectionChange.
Browse files Browse the repository at this point in the history
Add type `ApiCoinSelectionChange`, along with JSON instances and
roundtrip tests.

Values of this type will be used to encode change outputs, within
`ApiCoinSelection` objects.
  • Loading branch information
jonathanknowles committed Oct 19, 2020
1 parent fd2b175 commit 6640baa
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -48,6 +48,7 @@ module Cardano.Wallet.Api.Types
, ApiSelectCoinsPayments (..)
, ApiSelectCoinsAction (..)
, ApiCoinSelection (..)
, ApiCoinSelectionChange (..)
, ApiCoinSelectionInput (..)
, ApiStakePool (..)
, ApiStakePoolMetrics (..)
Expand Down Expand Up @@ -414,6 +415,12 @@ data ApiCoinSelection (n :: NetworkDiscriminant) = ApiCoinSelection
, certificates :: Maybe (NonEmpty ApiCertificate)
} deriving (Eq, Generic, Show)

data ApiCoinSelectionChange (n :: NetworkDiscriminant) = ApiCoinSelectionChange
{ address :: !(ApiT Address, Proxy n)
, amount :: !(Quantity "lovelace" Natural)
, derivationPath :: NonEmpty (ApiT DerivationIndex)
} deriving (Eq, Generic, Show)

data ApiCoinSelectionInput (n :: NetworkDiscriminant) = ApiCoinSelectionInput
{ id :: !(ApiT (Hash "Tx"))
, index :: !Word32
Expand Down Expand Up @@ -1085,6 +1092,11 @@ instance ToJSON (ApiT DelegationAction) where
toJSON (ApiT (Join pid)) = object [ "action" .= String "join", "pool" .= (ApiT pid) ]
toJSON (ApiT Quit) = object [ "action" .= String "quit" ]

instance DecodeAddress n => FromJSON (ApiCoinSelectionChange n) where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance EncodeAddress n => ToJSON (ApiCoinSelectionChange n) where
toJSON = genericToJSON defaultRecordTypeOptions

instance DecodeAddress n => FromJSON (ApiCoinSelectionInput n) where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance EncodeAddress n => ToJSON (ApiCoinSelectionInput n) where
Expand Down
@@ -0,0 +1,246 @@
{
"seed": -8132152044144507405,
"samples": [
{
"amount": {
"quantity": 141,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"852",
"13597",
"11458",
"22421",
"21106",
"26839",
"25413",
"4795",
"24010",
"15277",
"25381",
"27752",
"3988",
"27219",
"28927",
"5860",
"20155",
"31643",
"18256",
"21059"
]
},
{
"amount": {
"quantity": 23,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"11665",
"15069",
"18615",
"15382",
"24988",
"25011",
"26605",
"16166",
"12632",
"15123",
"14126",
"8442",
"30703",
"16135"
]
},
{
"amount": {
"quantity": 134,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"19126",
"7199",
"15884",
"27377"
]
},
{
"amount": {
"quantity": 96,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"4056",
"19030"
]
},
{
"amount": {
"quantity": 181,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"12385",
"20949",
"20575",
"14941",
"337",
"26321",
"14876",
"13361",
"15097",
"12163",
"20729",
"26193",
"11931",
"23619",
"19733",
"13144",
"15636",
"17164",
"14447"
]
},
{
"amount": {
"quantity": 37,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"1388",
"25003",
"12595",
"13690",
"5100",
"9206",
"5149",
"20797",
"22585",
"20120",
"23015",
"1072",
"15182",
"12693",
"22588",
"6557",
"727",
"31672",
"20403",
"5335",
"9216",
"12309",
"6537",
"8107",
"8520",
"8216",
"2071",
"8673",
"21194"
]
},
{
"amount": {
"quantity": 13,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"1854"
]
},
{
"amount": {
"quantity": 20,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"18343",
"13012",
"13333",
"11303",
"15402",
"28235",
"6016",
"13367",
"4861",
"17945",
"25861",
"30290",
"19896",
"27940",
"9264",
"20206",
"27381",
"14735",
"18547",
"24626",
"27896",
"13277"
]
},
{
"amount": {
"quantity": 103,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"18387",
"23841",
"6757",
"17536",
"27510",
"8045",
"5359",
"25791",
"24524",
"1155",
"27704",
"9057",
"2032",
"8471",
"8329",
"21112",
"18407",
"9226"
]
},
{
"amount": {
"quantity": 229,
"unit": "lovelace"
},
"address": "<addr>",
"derivation_path": [
"10787",
"16842",
"14843",
"20424",
"28409",
"974",
"29142",
"30947",
"1892",
"29095",
"23095",
"6255",
"21037",
"26751",
"19270",
"6415",
"31235",
"20766",
"15900",
"8107",
"26036",
"19717"
]
}
]
}
21 changes: 21 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Expand Up @@ -50,6 +50,7 @@ import Cardano.Wallet.Api.Types
, ApiByronWalletBalance (..)
, ApiCertificate (..)
, ApiCoinSelection (..)
, ApiCoinSelectionChange (..)
, ApiCoinSelectionInput (..)
, ApiEpochInfo (..)
, ApiFee (..)
Expand Down Expand Up @@ -322,6 +323,7 @@ spec = do
jsonRoundtripAndGolden $ Proxy @ApiEpochInfo
jsonRoundtripAndGolden $ Proxy @(ApiSelectCoinsData ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @(ApiCoinSelection ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @(ApiCoinSelectionChange ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @(ApiCoinSelectionInput ('Testnet 0))
jsonRoundtripAndGolden $ Proxy @ApiBlockReference
jsonRoundtripAndGolden $ Proxy @ApiSlotReference
Expand Down Expand Up @@ -623,6 +625,18 @@ spec = do
}
in
x' === x .&&. show x' === show x
it "ApiCoinSelectionChange" $ property $ \x ->
let
x' = ApiCoinSelectionChange
{ address = address
(x :: ApiCoinSelectionChange ('Testnet 0))
, amount = amount
(x :: ApiCoinSelectionChange ('Testnet 0))
, derivationPath = derivationPath
(x :: ApiCoinSelectionChange ('Testnet 0))
}
in
x' === x .&&. show x' === show x
it "ApiCoinSelectionInput" $ property $ \x ->
let
x' = ApiCoinSelectionInput
Expand Down Expand Up @@ -955,6 +969,13 @@ instance Arbitrary (ApiCoinSelection n) where
arbitrary = ApiCoinSelection <$> arbitrary <*> arbitrary <*> arbitrary
shrink = genericShrink

instance Arbitrary (ApiCoinSelectionChange n) where
arbitrary = ApiCoinSelectionChange
<$> fmap (, Proxy @n) arbitrary
<*> arbitrary
<*> arbitrary
shrink _ = []

instance Arbitrary (ApiCoinSelectionInput n) where
arbitrary = ApiCoinSelectionInput
<$> arbitrary
Expand Down

0 comments on commit 6640baa

Please sign in to comment.