bloxbean / cardano-client-lib

Cardano client library in Java
https://cardano-client.dev
MIT License
120 stars 51 forks source link

(0.4.3 Output.builder() ) Balance 1.49ADA, transfer 1.2ADA failed.(I suspect that the remaining 0.29ADA has not been fully deducted from the handling fee. So, how about external settings for handling fees? After deducting the handling fees, all the remaining quantity will be transferred) #310

Open AdelaDella opened 1 year ago

AdelaDella commented 1 year ago

BigInteger amount = BigInteger.valueOf(1200000); Output output = Output.builder() .address(receiverAddress) .assetName(LOVELACE) .qty(amount) .build(); // Define TxBuilder TxBuilder txBuilder = output.outputBuilder() .buildInputs(createFromSender(senderAddress, senderAddress)) .andThen(balanceTx(senderAddress, 1));

    UtxoSupplier utxoSupplier = new DefaultUtxoSupplier(backendService.getUtxoService());
    ProtocolParamsSupplier protocolParamsSupplier = new DefaultProtocolParamsSupplier(backendService.getEpochService());

    //Build and sign the transaction
    Transaction signedTransaction = TxBuilderContext.init(utxoSupplier, protocolParamsSupplier).buildAndSign(txBuilder, signerFrom(senderAccount));
    //Submit the transaction
    Result<String> result = backendService.getTransactionService().submitTransaction(signedTransaction.serialize());

I suspect that the remaining 0.29ADA has not been fully deducted from the handling fee. So, how about external settings for handling fees? After deducting the handling fees, all the remaining quantity will be transferred

satran004 commented 1 year ago

@BlockByteMath I tried this scenario with 0.5.0-beta2. I have not tried it with 0.4.3 yet. I suspect I may face this https://github.com/bloxbean/cardano-client-lib/issues/285 with 0.4.3 .

Here's the code with 0.5.0-beta2 TxBuilder which works for me.

 BigInteger amount = BigInteger.valueOf(1200000);
        Output output = Output.builder()
                .address(receiver1Addr)
                .assetName(LOVELACE)
                .qty(amount)
                .build();

        TxBuilder txBuilder = output.outputBuilder()
                .buildInputs(createFromSender(sender1Addr, receiver1Addr))
                .andThen(balanceTx(receiver1Addr, 1));

        UtxoSupplier utxoSupplier = new DefaultUtxoSupplier(backendService.getUtxoService());
        ProtocolParamsSupplier protocolParamsSupplier = new DefaultProtocolParamsSupplier(backendService.getEpochService());

        //Build and sign the transaction
        Transaction signedTransaction = TxBuilderContext.init(utxoSupplier, protocolParamsSupplier).buildAndSign(txBuilder, signerFrom(sender1));
        //Submit the transaction
        Result<String> result = backendService.getTransactionService().submitTransaction(signedTransaction.serialize());

If you see in the above code, I am setting my changeAddress in createFromSender and balanceTx to receiver address. So the remaining balance goes to also receiver and fee is deducted from that.

With 0.5.0-beta2, the same transaction can also be written using new QuickTx api as below

       Tx tx1 = new Tx()
                .payToAddress(receiver1Addr, Amount.ada(1.2))
                .withChangeAddress(receiver1Addr)
                .from(sender1Addr);

        QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService);
        Result<String> result = quickTxBuilder
                .compose(tx1)
                .feePayer(receiver1Addr)
                .withSigner(signerFrom(sender1))
                .completeAndWait(System.out::println);

In the above code. changeAddress and feePayer are set to receiver.

As you are already upgrading your code to 0.4.3, you may try 0.5.0-beta2 to use new QuickTx api.

AdelaDella commented 1 year ago

285

Thank you. I will wait until 0.5.0 is officially released before upgrading QuickTxBuilder. I can also achieve full transfer using a PaymentTransaction of 0.4.3 first. But it is not possible to transfer all Ada while transferring tokens.