From 16476fbae2a1555718f84b8c78c175b6a067f97a Mon Sep 17 00:00:00 2001 From: sourabhxyz Date: Tue, 10 Dec 2024 17:16:15 +0530 Subject: [PATCH 1/2] feat(#368): avoid dependency upon `cardano-balance-tx:internal` --- atlas-cardano.cabal | 1 - src/GeniusYield/Transaction/CoinSelection.hs | 59 +++++++------------- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/atlas-cardano.cabal b/atlas-cardano.cabal index d204ad37..59acdba6 100644 --- a/atlas-cardano.cabal +++ b/atlas-cardano.cabal @@ -247,7 +247,6 @@ library cardano-addresses, cardano-api, cardano-api:internal, - cardano-balance-tx:internal, cardano-coin-selection, cardano-crypto-class, cardano-ledger-api, diff --git a/src/GeniusYield/Transaction/CoinSelection.hs b/src/GeniusYield/Transaction/CoinSelection.hs index 42980bf0..b723ceac 100644 --- a/src/GeniusYield/Transaction/CoinSelection.hs +++ b/src/GeniusYield/Transaction/CoinSelection.hs @@ -24,7 +24,6 @@ import Data.ByteString qualified as BS import Data.Default (Default (def)) import Data.Map qualified as Map import Data.Set qualified as S -import Data.Text qualified as Text import Data.Text.Class ( ToText (toText), fromText, @@ -50,11 +49,6 @@ import Cardano.Wallet.Primitive.Types.TokenMap qualified as CWTokenMap import Cardano.Wallet.Primitive.Types.TokenPolicyId qualified as CWallet import Cardano.Wallet.Primitive.Types.TokenQuantity qualified as CWallet import Cardano.Wallet.Primitive.Types.Tx.Constraints qualified as CWallet -import Cardano.Wallet.Primitive.Types.Tx.TxIn qualified as CWallet -import Internal.Cardano.Write.Tx.Balance.CoinSelection qualified as CBalanceInternal ( - SelectionBalanceError (..), - WalletUTxO (..), - ) import Cardano.Ledger.Conway.Core (eraProtVerHigh) import GeniusYield.Imports @@ -65,6 +59,12 @@ import GeniusYield.Utils type GYCoinSelectionContext :: PlutusVersion -> Type data GYCoinSelectionContext v +data WalletUTxO = WalletUTxO + { txIn :: !GYTxOutRef + , address :: !GYAddress + } + deriving (Eq, Generic, Ord, Show) + {- Note: The vast majority of partial functions in this module are fine since they are localized. Essentially, they are supposed to work on trusted inputs and outputs. As such, we should pay more @@ -78,7 +78,7 @@ TODO: -} instance CCoinSelection.SelectionContext (GYCoinSelectionContext v) where type Address (GYCoinSelectionContext v) = CWallet.Address - type UTxO (GYCoinSelectionContext v) = CBalanceInternal.WalletUTxO + type UTxO (GYCoinSelectionContext v) = WalletUTxO data GYCoinSelectionEnv v = GYCoinSelectionEnv { existingInputs :: ![GYTxInDetailed v] @@ -206,9 +206,9 @@ selectInputs map (\(fromTokenBundle -> tokenChange) -> GYTxOut changeAddr tokenChange Nothing Nothing) changeGenerated - foldHelper acc (CBalanceInternal.WalletUTxO {txIn}, _) - | fromCWalletTxIn txIn `S.member` inRefs = acc - | otherwise = case utxosLookup (fromCWalletTxIn txIn) ownUtxos of + foldHelper acc (WalletUTxO {txIn}, _) + | txIn `S.member` inRefs = acc + | otherwise = case utxosLookup txIn ownUtxos of {- Invariant: The balancer should only select inputs from 'existingInputs' or 'ownUtxos' Thus, if this txIn doesn't exist in ownUtxos, it must already be in 'existingInputs', so we don't need it in additional inputs -} @@ -403,10 +403,10 @@ toTokenBundle v = CTokenBundle.fromCoin coins `CTokenBundle.add` CTokenBundle.fr fromTokenBundle :: CTokenBundle.TokenBundle -> GYValue fromTokenBundle (CTokenBundle.TokenBundle (CWallet.Coin n) tkMap) = valueFromLovelace (toInteger n) <> fromTokenMap tkMap -utxosToUtxoIndex :: GYUTxOs -> CWallet.UTxOIndex CBalanceInternal.WalletUTxO +utxosToUtxoIndex :: GYUTxOs -> CWallet.UTxOIndex WalletUTxO utxosToUtxoIndex = CWallet.fromSequence . map utxoToTuple . utxosToList -utxoToTuple :: GYUTxO -> (CBalanceInternal.WalletUTxO, CTokenBundle.TokenBundle) +utxoToTuple :: GYUTxO -> (WalletUTxO, CTokenBundle.TokenBundle) utxoToTuple GYUTxO { utxoRef @@ -415,16 +415,16 @@ utxoToTuple } = (wUtxo, bundle) where wUtxo = - CBalanceInternal.WalletUTxO - { txIn = toCWalletTxIn utxoRef - , address = toCWalletAddress utxoAddress + WalletUTxO + { txIn = utxoRef + , address = utxoAddress } bundle = toTokenBundle utxoValue -txInDetailedToUtxoIndex :: [GYTxInDetailed v] -> CWallet.UTxOIndex CBalanceInternal.WalletUTxO +txInDetailedToUtxoIndex :: [GYTxInDetailed v] -> CWallet.UTxOIndex WalletUTxO txInDetailedToUtxoIndex = CWallet.fromSequence . map txInDetailedToTuple -txInDetailedToTuple :: GYTxInDetailed v -> (CBalanceInternal.WalletUTxO, CTokenBundle.TokenBundle) +txInDetailedToTuple :: GYTxInDetailed v -> (WalletUTxO, CTokenBundle.TokenBundle) txInDetailedToTuple GYTxInDetailed { gyTxInDet @@ -433,9 +433,9 @@ txInDetailedToTuple } = (wUtxo, bundle) where wUtxo = - CBalanceInternal.WalletUTxO - { txIn = toCWalletTxIn $ gyTxInTxOutRef gyTxInDet - , address = toCWalletAddress gyTxInDetAddress + WalletUTxO + { txIn = gyTxInTxOutRef gyTxInDet + , address = gyTxInDetAddress } bundle = toTokenBundle gyTxInDetValue @@ -447,24 +447,7 @@ fromCWalletAddress (CWallet.Address bs) = either customError addressFromApi $ Ap where customError e = error $ "fromCWalletAddress: unable to deserialize, error: " <> show e -toCWalletTxIn :: GYTxOutRef -> CWallet.TxIn -toCWalletTxIn ref = - CWallet.TxIn - { inputId = nTxId - , inputIx = fromIntegral txIx - } - where - (txId, txIx) = txOutRefToTuple ref - nTxId = either customError id $ fromText $ Text.pack $ show txId - customError = error "toCWalletTxIn: unable to deserialise" - -fromCWalletTxIn :: CWallet.TxIn -> GYTxOutRef -fromCWalletTxIn CWallet.TxIn {inputId, inputIx} = txOutRefFromTuple (txId, fromIntegral inputIx) - where - txId = fromMaybe customError . txIdFromHex . Text.unpack $ toText inputId - customError = error "fromCWalletTxIn: unable to deserialise txId" - -fromCWalletBalancingError :: CBalanceInternal.SelectionBalanceError ctx -> GYBalancingError +fromCWalletBalancingError :: CBalance.SelectionBalanceError ctx -> GYBalancingError fromCWalletBalancingError (CBalance.BalanceInsufficient (CBalance.BalanceInsufficientError _ _ delta)) = GYBalancingErrorInsufficientFunds $ fromTokenBundle delta fromCWalletBalancingError (CBalance.UnableToConstructChange (CBalance.UnableToConstructChangeError _ n)) = From 05c66246919b0468e065a20647217fb1200b2c71 Mon Sep 17 00:00:00 2001 From: sourabhxyz Date: Tue, 10 Dec 2024 17:18:48 +0530 Subject: [PATCH 2/2] chore(#368): handle versioning & changelog --- CHANGELOG.md | 4 ++++ atlas-cardano.cabal | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61036518..03977840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.3 + +* Avoid dependency upon `cardano-balance-tx:internal`. See [#368](https://github.com/geniusyield/atlas/issues/368) for more details. + ## 0.6.2 * Give `Eq`, `Semigroup`, `Monoid` instance for `GYTxWitness`. diff --git a/atlas-cardano.cabal b/atlas-cardano.cabal index 59acdba6..ce06ad14 100644 --- a/atlas-cardano.cabal +++ b/atlas-cardano.cabal @@ -1,6 +1,6 @@ cabal-version: 3.8 name: atlas-cardano -version: 0.6.2 +version: 0.6.3 synopsis: Application backend for Plutus smart contracts on Cardano description: Atlas is an all-in-one, Haskell-native application backend for writing off-chain code for on-chain Plutus smart contracts.