Skip to content

Commit 34017c2

Browse files
Add fee to transaction over separate argument
1 parent fa8adbe commit 34017c2

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module BotPlutusInterface.Balance (
44
balanceTxStep,
55
balanceTxIO,
6+
withFee,
67
) where
78

89
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
@@ -89,7 +90,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
8990
(balancedTx, minUtxos) <- loop utxoIndex privKeys [] preBalancedTx
9091

9192
-- Check if we have Ada change
92-
let adaChange = getAdaChange utxoIndex (lovelaceValue $ txFee balancedTx) balancedTx
93+
let adaChange = getAdaChange utxoIndex balancedTx
9394
-- If we have no change UTxO, but we do have change, we need to add an output for it
9495
-- We'll add a minimal output, run the loop again so it gets minUTxO, then update change
9596
balancedTxWithChange <-
@@ -107,7 +108,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
107108
else pure balancedTx
108109

109110
-- Get the updated change, add it to the tx
110-
let finalAdaChange = getAdaChange utxoIndex (lovelaceValue $ txFee balancedTxWithChange) balancedTxWithChange
111+
let finalAdaChange = getAdaChange utxoIndex balancedTxWithChange
111112
fullyBalancedTx = addAdaChange ownPkh finalAdaChange balancedTxWithChange
112113

113114
-- finally, we must update the signatories
@@ -131,7 +132,7 @@ balanceTxIO pabConf ownPkh unbalancedTx =
131132

132133
-- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
133134
txWithoutFees <-
134-
hoistEither $ balanceTxStep minUtxos 0 utxoIndex ownPkh tx
135+
hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
135136

136137
lift $ createDirectoryIfMissing @w False (Text.unpack pabConf.pcTxFileDir)
137138
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
@@ -140,13 +141,14 @@ balanceTxIO pabConf ownPkh unbalancedTx =
140141
lift $ printLog @w Debug $ "Fees: " ++ show fees
141142

142143
-- Rebalance the initial tx with the above fees
143-
balancedTx <- hoistEither $ balanceTxStep minUtxos fees utxoIndex ownPkh tx
144+
balancedTx <- hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` fees
144145

145-
let balanceTxWithFees = balancedTx {txFee = Ada.lovelaceValueOf fees}
146+
if balancedTx == tx
147+
then pure (balancedTx, minUtxos)
148+
else loop utxoIndex privKeys minUtxos balancedTx
146149

147-
if balanceTxWithFees == tx
148-
then pure (balanceTxWithFees, minUtxos)
149-
else loop utxoIndex privKeys minUtxos balanceTxWithFees
150+
withFee :: Tx -> Integer -> Tx
151+
withFee tx fee = tx {txFee = Ada.lovelaceValueOf fee}
150152

151153
calculateMinUtxos ::
152154
forall (w :: Type) (effs :: [Type -> Type]).
@@ -160,20 +162,20 @@ calculateMinUtxos pabConf datums txOuts =
160162

161163
balanceTxStep ::
162164
[(TxOut, Integer)] ->
163-
Integer ->
164165
Map TxOutRef TxOut ->
165166
PubKeyHash ->
166167
Tx ->
167168
Either Text Tx
168-
balanceTxStep minUtxos fees utxos ownPkh tx =
169+
balanceTxStep minUtxos utxos ownPkh tx =
169170
Right (addLovelaces minUtxos tx)
170-
>>= balanceTxIns utxos fees
171-
>>= handleNonAdaChange ownPkh utxos fees
171+
>>= balanceTxIns utxos
172+
>>= handleNonAdaChange ownPkh utxos
172173

173174
-- | Get change value of a transaction, taking inputs, outputs, mint and fees into account
174-
getChange :: Map TxOutRef TxOut -> Integer -> Tx -> Value
175-
getChange utxos fees tx =
176-
let txInRefs = map Tx.txInRef $ Set.toList $ txInputs tx
175+
getChange :: Map TxOutRef TxOut -> Tx -> Value
176+
getChange utxos tx =
177+
let fees = lovelaceValue $ txFee tx
178+
txInRefs = map Tx.txInRef $ Set.toList $ txInputs tx
177179
inputValue = mconcat $ map Tx.txOutValue $ mapMaybe (`Map.lookup` utxos) txInRefs
178180
outputValue = mconcat $ map Tx.txOutValue $ txOutputs tx
179181
nonMintedOutputValue = outputValue `minus` txMint tx
@@ -183,11 +185,11 @@ getChange utxos fees tx =
183185
lovelaceValue :: Value -> Integer
184186
lovelaceValue = flip Value.assetClassValueOf $ Value.assetClass "" ""
185187

186-
getAdaChange :: Map TxOutRef TxOut -> Integer -> Tx -> Integer
187-
getAdaChange utxos fees = lovelaceValue . getChange utxos fees
188+
getAdaChange :: Map TxOutRef TxOut -> Tx -> Integer
189+
getAdaChange utxos = lovelaceValue . getChange utxos
188190

189-
getNonAdaChange :: Map TxOutRef TxOut -> Integer -> Tx -> Value
190-
getNonAdaChange utxos fees = Ledger.noAdaValue . getChange utxos fees
191+
getNonAdaChange :: Map TxOutRef TxOut -> Tx -> Value
192+
getNonAdaChange utxos = Ledger.noAdaValue . getChange utxos
191193

192194
-- | Getting the necessary utxos to cover the fees for the transaction
193195
collectTxIns :: Set TxIn -> Map TxOutRef TxOut -> Value -> Either Text (Set TxIn)
@@ -245,13 +247,13 @@ addLovelaces minLovelaces tx =
245247
$ txOutputs tx
246248
in tx {txOutputs = lovelacesAdded}
247249

248-
balanceTxIns :: Map TxOutRef TxOut -> Integer -> Tx -> Either Text Tx
249-
balanceTxIns utxos fees tx = do
250+
balanceTxIns :: Map TxOutRef TxOut -> Tx -> Either Text Tx
251+
balanceTxIns utxos tx = do
250252
let txOuts = Tx.txOutputs tx
251253
nonMintedValue = mconcat (map Tx.txOutValue txOuts) `minus` txMint tx
252254
minSpending =
253255
mconcat
254-
[ Ada.lovelaceValueOf fees
256+
[ txFee tx
255257
, nonMintedValue
256258
]
257259
txIns <- collectTxIns (txInputs tx) utxos minSpending
@@ -274,10 +276,10 @@ addTxCollaterals utxos tx = do
274276
filterAdaOnly = Map.filter (isAdaOnly . txOutValue)
275277

276278
-- | Ensures all non ada change goes back to user
277-
handleNonAdaChange :: PubKeyHash -> Map TxOutRef TxOut -> Integer -> Tx -> Either Text Tx
278-
handleNonAdaChange ownPkh utxos fees tx =
279+
handleNonAdaChange :: PubKeyHash -> Map TxOutRef TxOut -> Tx -> Either Text Tx
280+
handleNonAdaChange ownPkh utxos tx =
279281
let changeAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
280-
nonAdaChange = getNonAdaChange utxos fees tx
282+
nonAdaChange = getNonAdaChange utxos tx
281283
outputs =
282284
case partition ((==) changeAddr . Tx.txOutAddress) $ txOutputs tx of
283285
([], txOuts) ->

test/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module Main (main) where
22

33
import Spec.BotPlutusInterface.Balance qualified
4-
import Spec.BotPlutusInterface.Server qualified
54
import Spec.BotPlutusInterface.Contract qualified
5+
import Spec.BotPlutusInterface.Server qualified
66
import Spec.BotPlutusInterface.UtxoParser qualified
77
import Test.Tasty (TestTree, defaultMain, testGroup)
88
import Prelude

test/Spec/BotPlutusInterface/Balance.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Spec.BotPlutusInterface.Balance (tests) where
22

3+
import BotPlutusInterface.Balance (withFee)
34
import BotPlutusInterface.Balance qualified as Balance
45
import Data.Map qualified as Map
56
import Data.Set qualified as Set
@@ -57,38 +58,35 @@ utxo4 = (txOutRef4, TxOut addr1 (Ada.lovelaceValueOf 800_000 <> Value.singleton
5758
addUtxosForFees :: Assertion
5859
addUtxosForFees = do
5960
let txout = TxOut addr2 (Ada.lovelaceValueOf 1_000_000) Nothing
60-
tx = mempty {txOutputs = [txout]}
61+
tx = mempty {txOutputs = [txout]} `withFee` 500_000
6162
minUtxo = [(txout, 1_000_000)]
62-
fees = 500_000
6363
utxoIndex = Map.fromList [utxo1, utxo2, utxo3]
6464
ownPkh = pkh1
6565
balancedTx =
66-
Balance.balanceTxStep minUtxo fees utxoIndex ownPkh tx
66+
Balance.balanceTxStep minUtxo utxoIndex ownPkh tx
6767

6868
txInputs <$> balancedTx @?= Right (Set.fromList [txIn1, txIn2])
6969

7070
addUtxosForNativeTokens :: Assertion
7171
addUtxosForNativeTokens = do
7272
let txout = TxOut addr2 (Value.singleton "11223344" "Token" 123) Nothing
73-
tx = mempty {txOutputs = [txout]}
73+
tx = mempty {txOutputs = [txout]} `withFee` 500_000
7474
minUtxo = [(txout, 1_000_000)]
75-
fees = 500_000
7675
utxoIndex = Map.fromList [utxo1, utxo2, utxo3, utxo4]
7776
ownPkh = pkh1
7877
balancedTx =
79-
Balance.balanceTxStep minUtxo fees utxoIndex ownPkh tx
78+
Balance.balanceTxStep minUtxo utxoIndex ownPkh tx
8079

8180
txInputs <$> balancedTx @?= Right (Set.fromList [txIn1, txIn2, txIn3, txIn4])
8281

8382
addUtxosForChange :: Assertion
8483
addUtxosForChange = do
8584
let txout = TxOut addr2 (Ada.lovelaceValueOf 1_600_000) Nothing
86-
tx = mempty {txOutputs = [txout]}
85+
tx = mempty {txOutputs = [txout]} `withFee` 500_000
8786
minUtxo = [(txout, 1_000_000)]
88-
fees = 500_000
8987
utxoIndex = Map.fromList [utxo1, utxo2, utxo3]
9088
ownPkh = pkh1
9189
balancedTx =
92-
Balance.balanceTxStep minUtxo fees utxoIndex ownPkh tx
90+
Balance.balanceTxStep minUtxo utxoIndex ownPkh tx
9391

9492
txInputs <$> balancedTx @?= Right (Set.fromList [txIn1, txIn2])

0 commit comments

Comments
 (0)