cardano-client-lib icon indicating copy to clipboard operation
cardano-client-lib copied to clipboard

QuickTx: Add ScriptTx.collectFrom(List<Utxo>) without redeemer

Open satran004 opened this issue 1 year ago • 0 comments

Currently, the ScriptTx.collectUtxo method accepts utxo/List<Utxo> and redeemer data as parameters. However, in some scenarios, the redeemer is not required, but a specific utxo is needed as input.

For example,

  • A minting script only mints if certain utxo is present in inputs

Currently, to include a utxo, we must also pass the redeemer. However, when redeemer data is provided as an argument, the QuickTx builder creates a redeemer in the witness. As a result, we need to remove that extra redeemer (spend) in the preBalance method.

This is a workaround for now.

ScriptTx scriptTx = new ScriptTx()
               .collectFrom(utxo, PlutusData.unit())
               .mintAsset(giftPlutusScript, new Asset(tokenName, BigInteger.valueOf(1)), mintAction, senderAddress);

Result<String> result = new QuickTxBuilder(backendService)
               .compose(scriptTx)
               .feePayer(senderAddress)
               .withSigner(SignerProviders.signerFrom(sender))
               .withTxEvaluator(new AikenTransactionEvaluator(backendService))
               .preBalanceTx((context, txn) -> {
                   txn.getWitnessSet().getRedeemers().removeIf(redeemer -> redeemer.getTag() == RedeemerTag.Spend);
               }).completeAndWait(System.out::println);

Solution

ScriptTx should introduce a new method, collectUtxo(List<Utxo>), that doesn't require redeemer data. In this scenario, the builder should simply balance the transaction.

satran004 avatar Aug 23 '23 14:08 satran004