Skip to content

Commit

Permalink
feat: remove GYEra, abstract out api era type
Browse files Browse the repository at this point in the history
Related to #326.
  • Loading branch information
sourabhxyz committed Aug 14, 2024
1 parent b73bcd1 commit a8dc9c0
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 164 deletions.
16 changes: 3 additions & 13 deletions src/GeniusYield/CardanoApi/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module GeniusYield.CardanoApi.Query (
-- * Low-level query runners
queryCardanoMode,
queryConwayEra,
queryBabbageEra,
queryUTxO,
-- * Exception
CardanoQueryException (..),
Expand Down Expand Up @@ -44,21 +43,12 @@ queryCardanoMode info q = do
Left err -> throwIO $ CardanoQueryException $ show err
Right x -> return x

queryConwayEra :: Api.LocalNodeConnectInfo -> Api.QueryInShelleyBasedEra Api.ConwayEra a -> IO a
queryConwayEra :: Api.LocalNodeConnectInfo -> Api.QueryInShelleyBasedEra ApiEra a -> IO a
queryConwayEra info q = do
e <- queryCardanoMode info $ Api.QueryInEra $ Api.QueryInShelleyBasedEra Api.ShelleyBasedEraConway q
case e of
Left err -> throwIO $ CardanoQueryException $ show err
Right x -> return x

queryBabbageEra :: Api.LocalNodeConnectInfo -> Api.QueryInShelleyBasedEra Api.BabbageEra a -> IO a
queryBabbageEra info q = do
e <- queryCardanoMode info $ Api.QueryInEra $ Api.QueryInShelleyBasedEra Api.ShelleyBasedEraBabbage q
case e of
Left err -> throwIO $ CardanoQueryException $ show err
Right x -> return x


queryUTxO :: GYEra -> Api.S.LocalNodeConnectInfo -> Api.QueryUTxOFilter -> IO GYUTxOs
queryUTxO GYBabbage info q = fmap utxosFromApi $ queryBabbageEra info $ Api.QueryUTxO q
queryUTxO GYConway info q = fmap utxosFromApi $ queryConwayEra info $ Api.QueryUTxO q
queryUTxO :: Api.S.LocalNodeConnectInfo -> Api.QueryUTxOFilter -> IO GYUTxOs
queryUTxO info q = fmap utxosFromApi $ queryConwayEra info $ Api.QueryUTxO q
1 change: 0 additions & 1 deletion src/GeniusYield/GYConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ withCfgProviders
(gyGetParameters, gySlotActions', gyQueryUTxO', gyLookupDatum, gySubmitTx, gyAwaitTxConfirmed, gyGetStakeAddressInfo) <- case cfgCoreProvider of
GYNodeKupo path kupoUrl -> do
let info = nodeConnectInfo path cfgNetworkId
era = networkIdToEra cfgNetworkId
kEnv <- KupoApi.newKupoApiEnv $ Text.unpack kupoUrl
nodeSlotActions <- makeSlotActions slotCachingTime $ Node.nodeGetSlotOfCurrentBlock info
pure
Expand Down
12 changes: 6 additions & 6 deletions src/GeniusYield/Providers/Node/AwaitTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ created since the tx - thus, there have been at least k confirmations.
See: https://docs.cardano.org/about-cardano/learn/chain-confirmation-versus-transaction-confirmation/
-}
nodeAwaitTxConfirmed :: GYEra -> Api.LocalNodeConnectInfo -> GYAwaitTx
nodeAwaitTxConfirmed era info p@GYAwaitTxParameters{..} txId = go 0
nodeAwaitTxConfirmed :: Api.LocalNodeConnectInfo -> GYAwaitTx
nodeAwaitTxConfirmed info p@GYAwaitTxParameters{..} txId = go 0
where
go attempt
| attempt >= maxAttempts = throwIO $ GYAwaitTxException p
Expand All @@ -50,14 +50,14 @@ nodeAwaitTxConfirmed era info p@GYAwaitTxParameters{..} txId = go 0
However, this is an extreme edge case that is unlikely to ever exist in
privnet tests (where this module is meant to be used, exclusively).
-}
utxos <- nodeUtxosFromTx era info txId
utxos <- nodeUtxosFromTx info txId
-- FIXME: This doesn't actually wait for confirmations.
unless (utxosSize utxos /= 0) $
threadDelay checkInterval >> go (attempt + 1)

-- | Obtain UTxOs created by a transaction.
nodeUtxosFromTx :: GYEra -> Api.LocalNodeConnectInfo -> GYTxId -> IO GYUTxOs
nodeUtxosFromTx era info txId = do
nodeUtxosFromTx :: Api.LocalNodeConnectInfo -> GYTxId -> IO GYUTxOs
nodeUtxosFromTx info txId = do
{- We don't have a way to obtain utxos produced by a TxId. As an alternative, we could
obtain the whole UTxO set and filter from there, but there's a faster way.
Expand All @@ -75,7 +75,7 @@ nodeUtxosFromTx era info txId = do
go mempty startIx uptoIx
where
go acc startIx uptoIx = do
utxos <- nodeUtxosAtTxOutRefs era info $ curry txOutRefFromTuple txId <$> [startIx .. uptoIx]
utxos <- nodeUtxosAtTxOutRefs info $ curry txOutRefFromTuple txId <$> [startIx .. uptoIx]
let acc' = acc <> utxos
if utxosSize utxos == 0
then pure acc'
Expand Down
52 changes: 26 additions & 26 deletions src/GeniusYield/Providers/Node/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,57 @@ import GeniusYield.Types
-- UTxO query
-------------------------------------------------------------------------------

nodeUtxosAtAddress :: GYEra -> Api.LocalNodeConnectInfo -> GYAddress -> Maybe GYAssetClass -> IO GYUTxOs
nodeUtxosAtAddress era info addr mAssetClass = do
utxos <- nodeUtxosAtAddresses era info [addr]
nodeUtxosAtAddress :: Api.LocalNodeConnectInfo -> GYAddress -> Maybe GYAssetClass -> IO GYUTxOs
nodeUtxosAtAddress info addr mAssetClass = do
utxos <- nodeUtxosAtAddresses info [addr]
pure $ case mAssetClass of
Nothing -> utxos
Just assetClass -> filterUTxOs (\GYUTxO {utxoValue} -> valueAssetClass utxoValue assetClass /= 0) utxos

nodeUtxosAtAddresses :: GYEra -> Api.LocalNodeConnectInfo -> [GYAddress] -> IO GYUTxOs
nodeUtxosAtAddresses era info addrs = do
queryUTxO era info $ Api.QueryUTxOByAddress $ Set.fromList $ addressToApi <$> addrs
nodeUtxosAtAddresses :: Api.LocalNodeConnectInfo -> [GYAddress] -> IO GYUTxOs
nodeUtxosAtAddresses info addrs = do
queryUTxO info $ Api.QueryUTxOByAddress $ Set.fromList $ addressToApi <$> addrs

nodeUtxoAtTxOutRef :: GYEra -> Api.LocalNodeConnectInfo -> GYTxOutRef -> IO (Maybe GYUTxO)
nodeUtxoAtTxOutRef era info ref = do
utxos <- nodeUtxosAtTxOutRefs era info [ref]
nodeUtxoAtTxOutRef :: Api.LocalNodeConnectInfo -> GYTxOutRef -> IO (Maybe GYUTxO)
nodeUtxoAtTxOutRef info ref = do
utxos <- nodeUtxosAtTxOutRefs info [ref]
case utxosToList utxos of
[x] | utxoRef x == ref -> return (Just x)
_ -> return Nothing -- we return Nothing also in "should never happen" cases.

nodeUtxosAtTxOutRefs :: GYEra -> Api.LocalNodeConnectInfo -> [GYTxOutRef] -> IO GYUTxOs
nodeUtxosAtTxOutRefs era info refs = queryUTxO era info $ Api.QueryUTxOByTxIn $ Set.fromList $ txOutRefToApi <$> refs
nodeUtxosAtTxOutRefs :: Api.LocalNodeConnectInfo -> [GYTxOutRef] -> IO GYUTxOs
nodeUtxosAtTxOutRefs info refs = queryUTxO info $ Api.QueryUTxOByTxIn $ Set.fromList $ txOutRefToApi <$> refs

-- NOTE: This is extremely inefficient and only viable for a small private testnet. It queries the whole UTxO set.
nodeUtxosAtPaymentCredential :: GYEra -> Api.LocalNodeConnectInfo -> GYPaymentCredential -> Maybe GYAssetClass -> IO GYUTxOs
nodeUtxosAtPaymentCredential era info cred mAssetClass = do
utxos <- nodeUtxosAtPaymentCredentials era info [cred]
nodeUtxosAtPaymentCredential :: Api.LocalNodeConnectInfo -> GYPaymentCredential -> Maybe GYAssetClass -> IO GYUTxOs
nodeUtxosAtPaymentCredential info cred mAssetClass = do
utxos <- nodeUtxosAtPaymentCredentials info [cred]
pure $ case mAssetClass of
Nothing -> utxos
Just assetClass -> filterUTxOs (\GYUTxO {utxoValue} -> valueAssetClass utxoValue assetClass /= 0) utxos

-- NOTE: This is extremely inefficient and only viable for a small private testnet. It queries the whole UTxO set.
nodeUtxosAtPaymentCredentials :: GYEra -> Api.LocalNodeConnectInfo -> [GYPaymentCredential] -> IO GYUTxOs
nodeUtxosAtPaymentCredentials era info creds = do
allUtxos <- queryUTxO era info Api.QueryUTxOWhole
nodeUtxosAtPaymentCredentials :: Api.LocalNodeConnectInfo -> [GYPaymentCredential] -> IO GYUTxOs
nodeUtxosAtPaymentCredentials info creds = do
allUtxos <- queryUTxO info Api.QueryUTxOWhole
pure $ filterUTxOs (\GYUTxO {utxoAddress} -> matchesCred $ addressToPaymentCredential utxoAddress) allUtxos
where
credSet = Set.fromList creds
matchesCred Nothing = False
matchesCred (Just cred) = cred `Set.member` credSet

nodeQueryUTxO :: GYEra -> Api.S.LocalNodeConnectInfo -> GYQueryUTxO
nodeQueryUTxO era info = GYQueryUTxO
nodeQueryUTxO :: Api.S.LocalNodeConnectInfo -> GYQueryUTxO
nodeQueryUTxO info = GYQueryUTxO
{ gyQueryUtxosAtTxOutRefsWithDatums' = Nothing
, gyQueryUtxosAtTxOutRefs' = nodeUtxosAtTxOutRefs era info
, gyQueryUtxosAtTxOutRefs' = nodeUtxosAtTxOutRefs info
, gyQueryUtxosAtPaymentCredsWithDatums' = Nothing
, gyQueryUtxosAtPaymentCredentials' = nodeUtxosAtPaymentCredentials era info
, gyQueryUtxosAtPaymentCredential' = nodeUtxosAtPaymentCredential era info
, gyQueryUtxosAtPaymentCredentials' = nodeUtxosAtPaymentCredentials info
, gyQueryUtxosAtPaymentCredential' = nodeUtxosAtPaymentCredential info
, gyQueryUtxosAtPaymentCredWithDatums' = Nothing
, gyQueryUtxosAtAddressesWithDatums' = Nothing
, gyQueryUtxosAtAddresses' = nodeUtxosAtAddresses era info
, gyQueryUtxosAtAddresses' = nodeUtxosAtAddresses info
, gyQueryUtxosAtAddressWithDatums' = Nothing
, gyQueryUtxosAtAddress' = nodeUtxosAtAddress era info
, gyQueryUtxoRefsAtAddress' = gyQueryUtxoRefsAtAddressDefault $ nodeUtxosAtAddress era info
, gyQueryUtxoAtTxOutRef' = nodeUtxoAtTxOutRef era info
, gyQueryUtxosAtAddress' = nodeUtxosAtAddress info
, gyQueryUtxoRefsAtAddress' = gyQueryUtxoRefsAtAddressDefault $ nodeUtxosAtAddress info
, gyQueryUtxoAtTxOutRef' = nodeUtxoAtTxOutRef info
}
7 changes: 3 additions & 4 deletions src/GeniusYield/Test/Clb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ import GeniusYield.TxBuilder.Common
import GeniusYield.TxBuilder.Errors
import GeniusYield.TxBuilder.User
import GeniusYield.Types
import GeniusYield.Types.ProtocolParameters (protocolParametersFromApi)
import Ouroboros.Consensus.HardFork.History.EraParams (EraParams (eraGenesisWin))
import GeniusYield.Types.ProtocolParameters (protocolParametersFromApi)

deriving newtype instance Num EpochSize
deriving newtype instance Num EpochNo

type Clb = ClbT Api.ConwayEra Identity
type Clb = ClbT ApiEra Identity

newtype GYTxRunEnv = GYTxRunEnv { runEnvWallet :: User }

Expand Down Expand Up @@ -158,7 +158,7 @@ mkTestFor name action =
(mkSimpleWallet (Clb.intToKeyPair 9))

-- | Helper for building tests
testNoErrorsTraceClb :: GYValue -> GYValue -> Clb.MockConfig Api.ConwayEra -> String -> Clb a -> Tasty.TestTree
testNoErrorsTraceClb :: GYValue -> GYValue -> Clb.MockConfig ApiEra -> String -> Clb a -> Tasty.TestTree
testNoErrorsTraceClb funds walletFunds cfg msg act =
testCaseInfo msg
$ maybe (pure mockLog) assertFailure
Expand Down Expand Up @@ -228,7 +228,6 @@ instance GYTxQueryMonad GYTxMonadClb where
pure . GYPrivnet $ GYNetworkInfo
{ gyNetworkMagic = Api.S.unNetworkMagic $ Api.S.toNetworkMagic magic
, gyNetworkEpochSlots = 500
, gyNetworkEra = GYConway
}

lookupDatum :: GYDatumHash -> GYTxMonadClb (Maybe GYDatum)
Expand Down
15 changes: 4 additions & 11 deletions src/GeniusYield/Test/Privnet/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ withPrivnet testnetOpts setupUser = do
, testnetMagic
} <- cardanoTestnetDefault testnetOpts conf

era <- case cardanoNodeEra testnetOpts of
Api.AnyCardanoEra Api.BabbageEra -> pure GYBabbage
Api.AnyCardanoEra Api.ConwayEra -> pure GYConway
Api.AnyCardanoEra x -> liftIO . die $ printf "Unsupported era: %s" (show x)
liftIO . STM.atomically
$ STM.writeTMVar tmvRuntime PrivnetRuntime
-- TODO: Consider obtaining everything here from shelleyGenesis rather than testnetOpts.
Expand All @@ -155,9 +151,8 @@ withPrivnet testnetOpts setupUser = do
. poolRuntime
$ head poolNodes
, runtimeNetworkInfo = GYNetworkInfo
{ gyNetworkEra = era
-- TODO: Conway support.
, gyNetworkEpochSlots = fromIntegral $ cardanoEpochLength testnetOpts
{ -- TODO: Conway support.
gyNetworkEpochSlots = fromIntegral $ cardanoEpochLength testnetOpts
, gyNetworkMagic = fromIntegral testnetMagic
}
, runtimeWallets = wallets
Expand Down Expand Up @@ -217,16 +212,14 @@ withPrivnet testnetOpts setupUser = do
debug $ printf "slotOfCurrentBlock = %s\n" slot

withLCIClient info [] $ \lci -> do
let era = gyNetworkEra runtimeNetworkInfo

let localLookupDatum :: GYLookupDatum
localLookupDatum = lciLookupDatum lci

let localAwaitTxConfirmed :: GYAwaitTx
localAwaitTxConfirmed = nodeAwaitTxConfirmed era info
localAwaitTxConfirmed = nodeAwaitTxConfirmed info

let localQueryUtxo :: GYQueryUTxO
localQueryUtxo = nodeQueryUTxO era info
localQueryUtxo = nodeQueryUTxO info

let localGetParams :: GYGetParameters
localGetParams = nodeGetParameters info
Expand Down
30 changes: 15 additions & 15 deletions src/GeniusYield/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ balanceTxStep
isWdrlScriptWitness GYTxWdrlWitnessScript{} = True
isWdrlScriptWitness _ = False

retColSup :: Api.BabbageEraOnwards Api.ConwayEra
retColSup :: Api.BabbageEraOnwards ApiEra
retColSup = Api.BabbageEraOnwardsConway

finalizeGYBalancedTx :: GYBuildTxEnv -> GYBalancedTx v -> Int -> Either GYBuildTxError GYTxBody
Expand Down Expand Up @@ -369,7 +369,7 @@ finalizeGYBalancedTx
GYInReferenceSimpleScript _ s -> getTotalKeysInSimpleScript s <> acc
estimateKeyWitnessesFromNativeScripts acc _ = acc

inRefs :: Api.TxInsReference Api.BuildTx Api.ConwayEra
inRefs :: Api.TxInsReference Api.BuildTx ApiEra
inRefs = case inRefs' of
[] -> Api.TxInsReferenceNone
_ -> Api.TxInsReference Api.BabbageEraOnwardsConway inRefs'
Expand All @@ -388,33 +388,33 @@ finalizeGYBalancedTx
outs' :: [Api.S.TxOut Api.S.CtxTx Api.S.ConwayEra]
outs' = txOutToApi <$> outs

ins' :: [(Api.TxIn, Api.BuildTxWith Api.BuildTx (Api.Witness Api.WitCtxTxIn Api.ConwayEra))]
ins' :: [(Api.TxIn, Api.BuildTxWith Api.BuildTx (Api.Witness Api.WitCtxTxIn ApiEra))]
ins' = [ txInToApi (isInlineDatum $ gyTxInDetDatum i) (gyTxInDet i) | i <- ins ]

collaterals' :: Api.TxInsCollateral Api.ConwayEra
collaterals' :: Api.TxInsCollateral ApiEra
collaterals' = case utxosRefs collaterals of
[] -> Api.TxInsCollateralNone
orefs -> Api.TxInsCollateral Api.AlonzoEraOnwardsConway $ txOutRefToApi <$> orefs

-- will be filled by makeTransactionBodyAutoBalance
fee :: Api.TxFee Api.ConwayEra
fee :: Api.TxFee ApiEra
fee = Api.TxFeeExplicit Api.ShelleyBasedEraConway $ Ledger.Coin 0

lb' :: Api.TxValidityLowerBound Api.ConwayEra
lb' :: Api.TxValidityLowerBound ApiEra
lb' = maybe
Api.TxValidityNoLowerBound
(Api.TxValidityLowerBound Api.AllegraEraOnwardsConway . slotToApi)
lb

ub' :: Api.TxValidityUpperBound Api.ConwayEra
ub' :: Api.TxValidityUpperBound ApiEra
ub' = Api.TxValidityUpperBound Api.ShelleyBasedEraConway $ slotToApi <$> ub

extra :: Api.TxExtraKeyWitnesses Api.ConwayEra
extra :: Api.TxExtraKeyWitnesses ApiEra
extra = case toList signers of
[] -> Api.TxExtraKeyWitnessesNone
pkhs -> Api.TxExtraKeyWitnesses Api.AlonzoEraOnwardsConway $ pubKeyHashToApi <$> pkhs

mint :: Api.TxMintValue Api.BuildTx Api.ConwayEra
mint :: Api.TxMintValue Api.BuildTx ApiEra
mint = case mmint of
Nothing -> Api.TxMintNone
Just (v, xs) -> Api.TxMintValue Api.MaryEraOnwardsConway (valueToApi v) $ Api.BuildTxWith $ Map.fromList
Expand All @@ -427,7 +427,7 @@ finalizeGYBalancedTx
]

-- Putting `TxTotalCollateralNone` & `TxReturnCollateralNone` would have them appropriately calculated by `makeTransactionBodyAutoBalance` but then return collateral it generates is only for ada. To support multi-asset collateral input we therefore calculate correct values ourselves and put appropriate entries here to have `makeTransactionBodyAutoBalance` calculate appropriate overestimated fees.
(dummyTotCol :: Api.TxTotalCollateral Api.ConwayEra, dummyRetCol :: Api.TxReturnCollateral Api.CtxTx Api.ConwayEra) =
(dummyTotCol :: Api.TxTotalCollateral ApiEra, dummyRetCol :: Api.TxReturnCollateral Api.CtxTx ApiEra) =
if mempty == collaterals then
(Api.TxTotalCollateralNone, Api.TxReturnCollateralNone)
else
Expand All @@ -441,14 +441,14 @@ finalizeGYBalancedTx
collateralTotalValue :: GYValue
collateralTotalValue = foldMapUTxOs utxoValue collaterals

txMetadata :: Api.TxMetadataInEra Api.ConwayEra
txMetadata :: Api.TxMetadataInEra ApiEra
txMetadata = maybe Api.TxMetadataNone toMetaInEra mbTxMetadata
where
toMetaInEra :: GYTxMetadata -> Api.TxMetadataInEra Api.ConwayEra
toMetaInEra :: GYTxMetadata -> Api.TxMetadataInEra ApiEra
toMetaInEra gymd = let md = txMetadataToApi gymd in
if md == mempty then Api.TxMetadataNone else Api.TxMetadataInEra Api.ShelleyBasedEraConway md

wdrls' :: Api.TxWithdrawals Api.BuildTx Api.ConwayEra
wdrls' :: Api.TxWithdrawals Api.BuildTx ApiEra
wdrls' = if wdrls == mempty then Api.TxWithdrawalsNone else Api.TxWithdrawals Api.ShelleyBasedEraConway $ map txWdrlToApi wdrls

certs' =
Expand All @@ -466,7 +466,7 @@ finalizeGYBalancedTx

unregisteredStakeCredsMap = Map.fromList [ (stakeCredentialToApi sc, fromIntegral amt) | GYStakeAddressDeregistrationCertificate amt sc <- map gyTxCertCertificate' certs]

body :: Api.TxBodyContent Api.BuildTx Api.ConwayEra
body :: Api.TxBodyContent Api.BuildTx ApiEra
body =
Api.TxBodyContent {
Api.txIns = ins',
Expand Down Expand Up @@ -655,4 +655,4 @@ collapseExtraOut apiOut@(Api.TxOut _ outVal _ _) bodyContent@Api.TxBodyContent {
where
(skeletonOuts, changeOuts) = splitAt numSkeletonOuts txOuts

type ShelleyBasedConwayEra = Api.S.ShelleyLedgerEra Api.ConwayEra
type ShelleyBasedConwayEra = Api.S.ShelleyLedgerEra ApiEra
5 changes: 3 additions & 2 deletions src/GeniusYield/Types/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import GeniusYield.Types.Credential (GYPaymentCredential,
stakeCredentialFromApi,
stakeCredentialToApi,
stakeCredentialToHexText)
import GeniusYield.Types.Era
import GeniusYield.Types.Ledger
import GeniusYield.Types.NetworkId
import GeniusYield.Types.PaymentKeyHash (GYPaymentKeyHash,
Expand Down Expand Up @@ -147,11 +148,11 @@ instance Hashable GYAddress where
addressToApi :: GYAddress -> Api.AddressAny
addressToApi = coerce

addressToApi' :: GYAddress -> Api.AddressInEra Api.ConwayEra
addressToApi' :: GYAddress -> Api.AddressInEra ApiEra
addressToApi' = coerce addrAnyToConwayEra

-- not exported
addrAnyToConwayEra :: Api.AddressAny -> Api.AddressInEra Api.ConwayEra
addrAnyToConwayEra :: Api.AddressAny -> Api.AddressInEra ApiEra
addrAnyToConwayEra (Api.AddressByron addr) = Api.AddressInEra Api.ByronAddressInAnyEra addr
addrAnyToConwayEra (Api.AddressShelley addr) = Api.AddressInEra (Api.ShelleyAddressInEra Api.ShelleyBasedEraConway) addr

Expand Down
Loading

0 comments on commit a8dc9c0

Please sign in to comment.