bloxbean / cardano-client-lib

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

Native Assets transaction Amount don't support decimal #389

Open mackieq opened 3 months ago

mackieq commented 3 months ago

The Amount Object is:

public class Amount {

    private String unit;
    private BigInteger quantity;
}

When the transfer amount includes decimals, how should it be handled?

The Current Example code:


        PaymentTransaction paymentTransaction =
                PaymentTransaction.builder()
                        .sender(sender)
                        .receiver(receiver)
                        .amount(BigInteger.valueOf(3000))
                        .unit("57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522534245525259")
                        .build();
satran004 commented 3 months ago

@mackieq Cardano Native tokens are represented as whole number (BigInteger in Java). So during transfer you can't use decimal.

You can also get the decimal info of an existing mainnet token from token registry and convert it to a whole number using that. Please check info about token registry here https://developers.cardano.org/docs/native-tokens/cardano-token-registry/

Btw, PaymentTransaction api is now deprecated.

Please look into "QuickTx" api which is quite simple and flexible.

Here are some examples of QuickTx api.

https://github.com/bloxbean/cardano-client-examples/blob/main/src/test/java/com/bloxbean/cardano/client/example/quicktx/SimplePayment.java

Blog post: https://satran004.medium.com/introducing-new-quicktx-api-in-cardano-client-lib-0-5-0-beta1-5beb491282ce