Skip to content

Commit 1f3f854

Browse files
committed
move network discriminant conversion to Compatibility module
1 parent 298f91d commit 1f3f854

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module Cardano.Wallet.Shelley.Compatibility
7777
, fromTip
7878
, fromTip'
7979
, fromPParams
80+
, fromNetworkDiscriminant
8081

8182
-- * Internal Conversions
8283
, decentralizationLevelFromPParams
@@ -132,12 +133,16 @@ import Data.Map.Strict
132133
( Map )
133134
import Data.Maybe
134135
( fromMaybe, isJust, mapMaybe )
136+
import Data.Proxy
137+
( Proxy )
135138
import Data.Quantity
136139
( Percentage, Quantity (..), mkPercentage )
137140
import Data.Text
138141
( Text )
139142
import Data.Text.Class
140143
( TextDecodingError (..) )
144+
import Data.Type.Equality
145+
( testEquality )
141146
import Data.Word
142147
( Word16, Word32, Word64 )
143148
import Fmt
@@ -175,6 +180,8 @@ import Ouroboros.Network.Point
175180
( WithOrigin (..) )
176181
import Shelley.Spec.Ledger.BaseTypes
177182
( strictMaybeToMaybe, urlToText )
183+
import Type.Reflection
184+
( Typeable, typeRep )
178185

179186
import qualified Cardano.Api as Cardano
180187
import qualified Cardano.Byron.Codec.Cbor as CBOR
@@ -741,6 +748,15 @@ fromUnitInterval x =
741748
, show x
742749
]
743750

751+
fromNetworkDiscriminant
752+
:: forall (n :: NetworkDiscriminant). (Typeable n)
753+
=> Proxy n
754+
-> SL.Network
755+
fromNetworkDiscriminant _ =
756+
case testEquality (typeRep @n) (typeRep @'Mainnet) of
757+
Just{} -> SL.Mainnet
758+
Nothing -> SL.Testnet
759+
744760
-- NOTE: Arguably breaks naming conventions. Perhaps fromCardanoSignedTx instead
745761
toSealed :: SL.Tx TPraosStandardCrypto -> (W.Tx, W.SealedTx)
746762
toSealed tx =

lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import Cardano.Wallet.Primitive.Types
7373
import Cardano.Wallet.Shelley.Compatibility
7474
( Shelley
7575
, TPraosStandardCrypto
76+
, fromNetworkDiscriminant
7677
, toCardanoLovelace
7778
, toCardanoTxIn
7879
, toCardanoTxOut
@@ -105,16 +106,14 @@ import Data.Proxy
105106
( Proxy (..) )
106107
import Data.Quantity
107108
( Quantity (..) )
108-
import Data.Type.Equality
109-
( testEquality )
110109
import Data.Word
111110
( Word16, Word64, Word8 )
112111
import Ouroboros.Consensus.Shelley.Protocol.Crypto
113112
( Crypto (..) )
114113
import Ouroboros.Network.Block
115114
( SlotNo )
116115
import Type.Reflection
117-
( Typeable, typeRep )
116+
( Typeable )
118117

119118
import qualified Cardano.Api as Cardano
120119
import qualified Cardano.Byron.Codec.Cbor as CBOR
@@ -478,19 +477,15 @@ mkWithdrawals
478477
-> ChimericAccount
479478
-> Word64
480479
-> Map (SL.RewardAcnt TPraosStandardCrypto) SL.Coin
481-
mkWithdrawals _ (ChimericAccount keyHash) amount
480+
mkWithdrawals proxy (ChimericAccount keyHash) amount
482481
| amount == 0 = mempty
483482
| otherwise = Map.fromList
484-
[ ( SL.RewardAcnt network keyHashObj
483+
[ ( SL.RewardAcnt (fromNetworkDiscriminant proxy) keyHashObj
485484
, SL.Coin $ fromIntegral amount
486485
)
487486
]
488487
where
489488
keyHashObj = SL.KeyHashObj $ SL.KeyHash $ Hash.UnsafeHash keyHash
490-
network =
491-
case testEquality (typeRep @n) (typeRep @'Mainnet) of
492-
Just{} -> SL.Mainnet
493-
Nothing -> SL.Testnet
494489

495490
-- TODO: The SlotId-SlotNo conversion based on epoch length would not
496491
-- work if the epoch length changed in a hard fork.

0 commit comments

Comments
 (0)