bloxbean / cardano-client-lib

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

CBOR deserialization failing on NFT transfer #227

Closed MSchiavi closed 1 year ago

MSchiavi commented 1 year ago

Hi I haven't sent an NFT with the API in a couple months and it seems like code I have written is no longer working for NFT transfers but works for ADA. I didn't see any NFT examples so figured I would post here in case it is a bug.

My Code:

` BackendService backendService = new BFBackendService(cardanoBaseUrl, cardanoProjectId);

    Account account = new Account(Networks.preprod(), mintMnemonic);
    String unitName = " " // This is the policyId + assetName; 

    PaymentTransaction paymentTransaction = 
    PaymentTransaction.builder()
    .sender(account)
    .receiver(receiverAddress)
    .amount(BigInteger.valueOf(1))
    .unit(unitName)
    .build();

    BlockService blockService = backendService.getBlockService();
    FeeCalculationService feeCalculationService = backendService.getFeeCalculationService();
    TransactionHelperService transactionHelperService = backendService.getTransactionHelperService();

    try{
        long ttl = blockService.getLatestBlock().getValue().getSlot() + 1000;
        TransactionDetailsParams detailsParams = TransactionDetailsParams.builder().ttl(ttl).build();

        BigInteger fee = feeCalculationService.calculateFee(paymentTransaction, detailsParams, null); 
        paymentTransaction.setFee(fee);

        Result<TransactionResult> result = transactionHelperService.transfer(paymentTransaction, detailsParams);

        if(result.isSuccessful()){
            log.info("Transaction Id: {}",result.getValue());
        }else{
            log.info("Transaction failed: {}", result);
        }

    }catch(...){...}`

Here is the exception that is being thrown

com.bloxbean.cardano.client.exception.CborRuntimeException: com.bloxbean.cardano.client.exception.CborDeserializationException: CBOR deserialization failed at com.bloxbean.cardano.client.transaction.util.TransactionUtil.createCopy(TransactionUtil.java:26) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.transaction.TransactionSigner.sign(TransactionSigner.java:33) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.account.Account.sign(Account.java:362) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.api.helper.TransactionBuilder.createSignedTransaction(TransactionBuilder.java:127) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.api.helper.impl.FeeCalculationServiceImpl.calculateFee(FeeCalculationServiceImpl.java:71) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.api.helper.impl.FeeCalculationServiceImpl.calculateFee(FeeCalculationServiceImpl.java:57) ~[cardano-client-core-0.4.3.jar:na] at com.example.sockpalzapi.service.SockMusicService.sendSockMusic(SockMusicService.java:370) ~[main/:na] at com.example.sockpalzapi.service.SockMusicService.findMintedSockMusicAndSend(SockMusicService.java:242) ~[main/:na] at com.example.sockpalzapi.service.SockMusicService$$FastClassBySpringCGLIB$$219fda15.invoke(<generated>) ~[main/:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.22.jar:5.3.22] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.22.jar:5.3.22] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.22.jar:5.3.22] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.22.jar:5.3.22] at com.example.sockpalzapi.service.SockMusicService$$EnhancerBySpringCGLIB$$5d637ce3.findMintedSockMusicAndSend(<generated>) ~[main/:na] at com.example.sockpalzapi.tasks.sockmusic.SockMusicSendRunner.run(SockMusicSendRunner.java:29) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na] at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.3.22.jar:5.3.22] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.22.jar:5.3.22] at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na] at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na] at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na] at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na] Caused by: com.bloxbean.cardano.client.exception.CborDeserializationException: CBOR deserialization failed at com.bloxbean.cardano.client.transaction.spec.Transaction.deserialize(Transaction.java:171) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.transaction.util.TransactionUtil.createCopy(TransactionUtil.java:23) ~[cardano-client-core-0.4.3.jar:na] ... 32 common frames omitted Caused by: java.lang.ClassCastException: class co.nstant.in.cbor.model.SimpleValue cannot be cast to class co.nstant.in.cbor.model.ByteString (co.nstant.in.cbor.model.SimpleValue and co.nstant.in.cbor.model.ByteString are in unnamed module of loader 'app') at com.bloxbean.cardano.client.transaction.spec.TransactionOutput.deserializeAlonzo(TransactionOutput.java:199) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.transaction.spec.TransactionOutput.deserialize(TransactionOutput.java:138) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.transaction.spec.TransactionBody.deserialize(TransactionBody.java:203) ~[cardano-client-core-0.4.3.jar:na] at com.bloxbean.cardano.client.transaction.spec.Transaction.deserialize(Transaction.java:166) ~[cardano-client-core-0.4.3.jar:na] ... 33 common frames omitted

satran004 commented 1 year ago

@MSchiavi Similar code in integration test seems to be working. Here's the link

https://github.com/bloxbean/cardano-client-lib/blob/8fdf949a27d84b95f4b58ec95b7a4df117bc4f43/integration-test/src/it/java/com/bloxbean/cardano/client/backend/api/helper/TransactionHelperServiceIT.java#L178

If it still doesn't work, could you please provide some sample code with token name?

Btw, current recommended way of building transaction is through composable function. Here's a simple ada transfer example using composable function. But you can easily change it to transfer multi asset. https://cardano-client.bloxbean.com/docs/gettingstarted/simple-transfer

You can also find other examples under function package here https://github.com/bloxbean/cardano-client-examples/blob/main/src/test/java/com/bloxbean/cardano/client/example/function/

MSchiavi commented 1 year ago

Thanks I got it working appreciate it Satran