@@ -971,13 +971,23 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
971
971
-- output and fee. Yes this means this current code will only work for
972
972
-- final fee of less than around 4000 ada (2^32-1 lovelace) and change output
973
973
-- of less than around 18 trillion ada (2^64-1 lovelace).
974
+ -- However, since at this point we know how much non-Ada change to give
975
+ -- we can use the true values for that.
976
+
977
+ let outgoingNonAda = mconcat [filterValue isNotAda v | (TxOut _ (TxOutValue _ v) _ _) <- txOuts txbodycontent]
978
+ let incomingNonAda = mconcat [filterValue isNotAda v | (TxOut _ (TxOutValue _ v) _ _) <- Map. elems $ unUTxO utxo]
979
+ let nonAdaChange = incomingNonAda <> negateValue outgoingNonAda
980
+
981
+ let changeTxOut = case multiAssetSupportedInEra cardanoEra of
982
+ Left _ -> lovelaceToTxOutValue $ Lovelace (2 ^ (64 :: Integer )) - 1
983
+ Right multiAsset -> TxOutValue multiAsset (lovelaceToValue (Lovelace (2 ^ (64 :: Integer )) - 1 ) <> nonAdaChange)
974
984
975
985
let (dummyCollRet, dummyTotColl) = maybeDummyTotalCollAndCollReturnOutput txbodycontent changeaddr
976
986
txbody1 <- first TxBodyError $ -- TODO: impossible to fail now
977
987
makeTransactionBody txbodycontent1 {
978
988
txFee = TxFeeExplicit explicitTxFees $ Lovelace (2 ^ (32 :: Integer ) - 1 ),
979
989
txOuts = TxOut changeaddr
980
- (lovelaceToTxOutValue $ Lovelace ( 2 ^ ( 64 :: Integer )) - 1 )
990
+ changeTxOut
981
991
TxOutDatumNone ReferenceScriptNone
982
992
: txOuts txbodycontent,
983
993
txReturnCollateral = dummyCollRet,
@@ -1009,13 +1019,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1009
1019
1010
1020
-- check if the balance is positive or negative
1011
1021
-- in one case we can produce change, in the other the inputs are insufficient
1012
- case balance of
1013
- TxOutAdaOnly _ _ -> balanceCheck balance
1014
- TxOutValue _ v ->
1015
- case valueToLovelace v of
1016
- Nothing -> Left $ TxBodyErrorNonAdaAssetsUnbalanced v
1017
- Just _ -> balanceCheck balance
1018
-
1022
+ balanceCheck balance
1019
1023
1020
1024
-- TODO: we could add the extra fee for the CBOR encoding of the change,
1021
1025
-- now that we know the magnitude of the change: i.e. 1-8 bytes extra.
@@ -1135,7 +1139,7 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1135
1139
1136
1140
balanceCheck :: TxOutValue era -> Either TxBodyErrorAutoBalance ()
1137
1141
balanceCheck balance
1138
- | txOutValueToLovelace balance == 0 = return ()
1142
+ | txOutValueToLovelace balance == 0 && onlyAda (txOutValueToValue balance) = return ()
1139
1143
| txOutValueToLovelace balance < 0 =
1140
1144
Left . TxBodyErrorAdaBalanceNegative $ txOutValueToLovelace balance
1141
1145
| otherwise =
@@ -1145,6 +1149,13 @@ makeTransactionBodyAutoBalance eraInMode systemstart history pparams
1145
1149
Left err -> Left err
1146
1150
Right _ -> Right ()
1147
1151
1152
+ isNotAda :: AssetId -> Bool
1153
+ isNotAda AdaAssetId = False
1154
+ isNotAda _ = True
1155
+
1156
+ onlyAda :: Value -> Bool
1157
+ onlyAda = null . valueToList . filterValue isNotAda
1158
+
1148
1159
checkMinUTxOValue
1149
1160
:: TxOut CtxTx era
1150
1161
-> ProtocolParameters
0 commit comments