On this put up, we are going to see how one can create and put up a transaction with metadata to Cardano community in a Java utility utilizing cardano-client-lib.
Cardano-client-lib is a Java library which gives help to create, signal and submit transaction to the Cardano community. Test Half-I of this collection to know extra about Cardano-client-lib and the opposite functionalities offered by this library.
On this put up we can be utilizing Blockfrost backend Api.
Cardano Metadata as outlined on this weblog :-
Metadata tells the story of a transaction and there are numerous methods to work together with this story. Builders can make the most of metadata by embedding particulars immediately right into a transaction, and ada customers can seek for particular info within the Cardano Explorer. The information may be added immediately, or, for bigger quantities, it’s potential to create a Merkle tree of the info and put the basis hash of the Merkle tree on the blockchain. As soon as that is achieved, it may be proved that the info existed at a selected level of time and that it stays completely on the chain for future reference.
Cardano-client-lib gives easy apis to create metadata. It additionally gives helpers to transform metadata to JSON and vice versa.
Let’s create a cost transaction and add some metadata to it.
- Mission and account setup
You probably have not setup your venture but, please comply with the step-1 to step-6 from Half I.
2. Create a metadata object
To create a metadata object, it’s a must to begin with CBORMetadata class. You may as well use CBORMetadataMap and CBORMetadataList if you wish to set the worth as Map or Checklist.
CBORMetadataMap productDetailsMap = new CBORMetadataMap() .put("code", "PROD-800") .put("slno", "SL20000039484"); CBORMetadataList tagList = new CBORMetadataList() .add("laptop computer") .add("laptop"); Metadata metadata = new CBORMetadata() .put(new BigInteger("670001"), productDetailsMap) .put(new BigInteger("670002"), tagList);
Within the above instance, we’re first making a Map which comprises code and serial no. We’re then making a Checklist to carry a number of tags.
Lastly, we’re making a Metadata object and setting productDetailsMap and tagList with metadata labels 670001, 670002 respectively.
You’ll be able to create a multi-level nested construction, however the top-level object must be Metadata.
3. Create a transaction request object
As defined within the Half I, let’s create a transaction with PaymentTransaction class.
PaymentTransaction paymentTransaction = PaymentTransaction.builder() .sender(sender) .receiver(receiver) .quantity(BigInteger.valueOf(20000000)) .unit(LOVELACE) .construct();
4. Calculate TTL (Time-to-Stay)
Use BlockService to get the most recent slot quantity after which calculate ttl.
lengthy ttl = blockService.getLastestBlock().getValue().getSlot() + 1000; TransactionDetailsParams detailsParams = TransactionDetailsParams.builder() .ttl(ttl) .construct();
5. Calculate Charge
Use FeeCalculatorService to calculate charge. Ensure that to ship all three parameters “PaymentTransaction, TransactionDetailsParams and Metadata” to FeeCalculatorService.
Set the calculated charge in PaymentTransaction request.
BigInteger charge = feeCalculationService.calculateFee(paymentTransaction, detailsParams, metadata); paymentTransaction.setFee(charge);
6. Submit Transaction with Metadata
Now submit the transaction request to the community utilizing TransactionHelperService.
Word: Ensure that to cross “metadata” parameter within the switch name.
Outcomeconsequence = transactionHelperService.switch(paymentTransaction, detailsParams, metadata); if(consequence.isSuccessful()) System.out.println("Transaction Id: " + consequence.getValue()); else System.out.println("Transaction failed: " + consequence);
7. Test transaction particulars in Cardano Explorer
Now go to Cardano Testnet Explorer and test the transaction particulars.
It could take a while for the transaction to be mined.
Now you’ll be able to see the metadata in Cardano explorer.
Metadata may be added to a Native token switch in related methods.
Metadata Helper Class
There are few helper courses which can be utilized to transform a JSON (No Schema) object to metadata format and vice-versa.
However the Json payload has to comply with the format talked about on this wiki.
JsonNoSchemaToMetadataConverter MetadataToJsonNoSchemaConverter
Test additionally:
Full Supply code
public void transferWithMetadata() throws ApiException, AddressExcepion, CborSerializationException { BackendService backendService = BackendFactory.getBlockfrostBackendService(Constants.BLOCKFROST_TESTNET_URL, Fixed.BF_PROJECT_KEY); FeeCalculationService feeCalculationService = backendService.getFeeCalculationService(); TransactionHelperService transactionHelperService = backendService.getTransactionHelperService(); BlockService blockService = backendService.getBlockService(); String senderMnemonic = "equipment coloration frog trick communicate make use of swimsuit type bomb goddess jewel main spoil fade individual ineffective measure handle warfare scale back few scrub past period"; Account sender = new Account(Networks.testnet(), senderMnemonic); String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82"; CBORMetadataMap productDetailsMap = new CBORMetadataMap() .put("code", "PROD-800") .put("slno", "SL20000039484"); CBORMetadataList tagList = new CBORMetadataList() .add("laptop computer") .add("laptop"); Metadata metadata = new CBORMetadata() .put(new BigInteger("670001"), productDetailsMap) .put(new BigInteger("670002"), tagList); PaymentTransaction paymentTransaction = PaymentTransaction.builder() .sender(sender) .receiver(receiver) .quantity(BigInteger.valueOf(20000000)) .unit(LOVELACE) .construct(); lengthy ttl = blockService.getLastestBlock().getValue().getSlot() + 1000; TransactionDetailsParams detailsParams = TransactionDetailsParams.builder() .ttl(ttl) .construct(); BigInteger charge = feeCalculationService.calculateFee(paymentTransaction, detailsParams, metadata); paymentTransaction.setFee(charge); Outcome<String> consequence = transactionHelperService.switch(paymentTransaction, detailsParams, metadata); if(consequence.isSuccessful()) System.out.println("Transaction Id: " + consequence.getValue()); else System.out.println("Transaction failed: " + consequence); }