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

Confirm how to do mint transaction with additional Ada- and MultiAsset- outputs (i.e. refunds)

Open woofpool opened this issue 3 years ago • 2 comments

Hello Satya, This is not an issue per se, but I'd like to confirm my approach for building a transaction with function builders. I'm trying to build a transaction that includes an Ada refund output and an MultiAsset refund output along with a mint MultiAsset output. I'm attaching a file with some groovy test code to build a Transaction with function helpers. The code appears to be working just fine, but I have a few questions about it.

TxBuilderSpec.txt

Questions

  • The code below creates a transaction with two outputs. The first output is sent to receiverAddr and it includes Ada coin value with a refund amount factored into it, a MultiAsset refund (e.g. Hosky coin), and the minted NFT. The second output is the change that goes back to the sender address. In order to produce the correct outputs, I needed to invoke the function builders with refunds first followed by the mint output. Does this look correct? It appears you need to always call the createFromMintOutput last to make the Ada calculations work.
  • In the code, where it creates the mint TransactionOutput, I set the Ada coin value to 1 Ada arbitrarily. It adjusted the Ada value appropriately base on the refund amt. Is there any strategy for setting the Ada coin value or will the function builders correct as necessary based on context?
  • Using function helpers, I do not see a way to set the TTL (invalid-hereafter) parameter. Can the TTL be set with function helpers? If not, how can we set the TTL parameter?
  • Does a mint transaction require a collateral UTxO? In the function example, cardano-client-examples MintToken example, I am not seeing use of the CollateralBuilders function calls. Is this an oversight?

Thanks

woofpool avatar Feb 23 '22 22:02 woofpool

It looks like I can do something like the following to set the TTL, and I will experiment whether collateral is needed.

TxOutputBuilder txOutputBuilder = <build tx output here>;

TxBuilder txBuilder = txOutputBuilder
                .buildInputs(createFromSender(faucetAddress, faucetAddress))
                .andThen(mintCreator(mintingPolicy.getPolicyScript(), multiAsset))
                .andThen(metadataProvider(nftMetadata))
                .andThen(feeCalculator(faucetAddress, 2))
                .andThen(adjustChangeOutput(faucetAddress, 2));

// set the TTL
Transaction transaction = TxBuilderContext.init(backendService).build(txBuilder);
transaction.getBody().setTtl(getTtl());

// sign the transaction
Transaction outputTxn = faucetAccount.sign(transaction);
for (SecretKey sk : mintingPolicy.getPolicyKeys()) {
        outputTxn = TransactionSigner.INSTANCE.sign(outputTxn, sk);
}

// submit the transaction
return transactionService.submitTransaction(outputTxn.serialize());

woofpool avatar Feb 24 '22 00:02 woofpool

In order to produce the correct outputs, I needed to invoke the function builders with refunds first followed by the mint output. Does this look correct? It appears you need to always call the createFromMintOutput last to make the Ada calculations work.

If the receiver is same for refund ada/existing token and the newly minted token, for accurate min ada calculation, you need to call createMintOutput() at the end for that receiver. Also, I noticed you have created two separate outputs for refund ada & tokens for the same receiver. You can just use one TransactionOutput for ada & existing token and another output put for minted token.

If you are using Output.class instead of TransactionOutput, then Output(Ada), Output(existing token), Output(minted token). But that complexity is not required in your case.

The output passed in the createMintOutput( ) is not considered during input building.

In the code, where it creates the mint TransactionOutput, I set the Ada coin value to 1 Ada arbitrarily. It adjusted the Ada value appropriately base on the refund amt. Is there any strategy for setting the Ada coin value or will the function builders correct as necessary based on context?

The function will automatically calculate the minimum required ada in Utxo and add if you set "0" ADA in the TransactionOutput. Ideally you should set "0" ada in the mint TransactionOutput if you just want to send min ada & minted token, otherwise refund ADA amount can also be set in the mint output. But you can't mix mint token & existing token in the same output and call createMintToken().

Using function helpers, I do not see a way to set the TTL (invalid-hereafter) parameter. Can the TTL be set with function helpers? If not, how can we set the TTL parameter?

You can write your own TxBuilder function to set TTL parameter or do any other transformation. It's quite flexible.

createFromMintOutput(mintOutput1)
                        .and(createFromMintOutput(mintOutput2))
                        .and(createFromMintOutput(output3))
                        .buildInputs(createFromSender(senderAddress, senderAddress))
                        .andThen(mintCreator(policy.getPolicyScript(), mergeMultiAsset))
                        .andThen(metadataProvider(nftMetadata))
                        .andThen((txBuilderContext, transaction) -> {
                            transaction.getBody().setTtl(getTtl());
                        })
                        .andThen(feeCalculator(senderAddress, 2))

Does a mint transaction require a collateral UTxO?

No, collateral is only required for plutus call. If you are minting using plutus script, you need collateral, otherwise no.

Hope it helps.

satran004 avatar Feb 24 '22 12:02 satran004