Thursday, April 25, 2024
HomeJavaName Plutus V2 contract from off-chain Java code utilizing Cardano Shopper Lib...

Name Plutus V2 contract from off-chain Java code utilizing Cardano Shopper Lib – Java Code Geeks


On this publish, we’ll undergo the steps required to invoke a Plutus sensible contract from a Java utility utilizing Cardano Shopper Lib.

Cardano-client-lib is a Java shopper library for Cardano blockchain. It simplifies the interplay with Cardano blockchain from Java purposes. Utilizing this library, you’ll be able to carry out numerous kinds of transactions together with sensible contract calls in a Java utility.

Appropriate model: Cardano Shopper Lib 0.3.0-beta2 and above

Plutus Contract Instance

We are going to use a easy contract referred to as “Sum Contract” for example on this publish. To validate, the sum contract checks the sum of 0..n, the place n is the datum worth. The caller can unlock the fund in a script output by guessing the right sum.

Whereas this can be a quite simple plutus contract, however by this instance we’ll attempt to use among the new thrilling options obtainable in Babbage period (after Vasil Hardfork).

  1. Reference Inputs (CIP 31)
  2. Inline Datums (CIP 32)
  3. Reference Scripts (CIP 33)
  4. Collateral Output (CIP 40)

So, in excessive degree we can be going by the next steps :

  1. Create a reference script output to connect a script to the output. That is a part of CIP 33. With reference script output, we don’t must ship the script physique within the spending transaction.
  2. Ship some fund to script deal with with inline datum. The fund can be locked within the script output. Inline datum is outlined in CIP 32. So as an alternative of storing datum hash, we will now retailer datum worth instantly within the output.
  3. Lastly, create a spending transaction to unlock fund in script output. In spending transaction, we’ll check with the reference script output (step-1) as a reference enter. That is a part of CIP 31. Additionally, we’re going to use collateral outputs (CIP 40) within the spending transaction.

Earlier than going by our instance, let’s focus on some primary ideas in Cardano Shopper Lib. For extra particulars, you’ll be able to examine my earlier posts.

Alternatively, you’ll be able to soar to the part “Invoke Guess Sum Contract” if you’re already conscious of those ideas.

A set of FunctionalInterface which can be utilized to implement composable capabilities. These capabilities are used to construct numerous various kinds of transactions. The library supplies many helpful out-of-box implementations of those capabilities. A lot of the generally used capabilities are already there. If required, utility builders can write their very own capabilities and use them with different out of field capabilities.

The followings are the principle FunctionalInterface

  1. TxBuilder
  2. TxOutputBuilder
  3. TxInputBuilder
  4. TxSigner

TxBuilder : This purposeful interface helps to rework a transaction object. The apply technique on this interface takes a TxBuilderContext and a Transaction object as enter parameters. The function of this perform is to rework the enter transaction object with extra attributes or replace present attributes.

TxOutputBuilder : This purposeful interface helps to construct a TransactionOutput object and provides that to the transaction output listing. The settle for technique on this interface takes a TxBuilderContext and an inventory of TransactionOutput.

TxInputBuilder : This purposeful interface is accountable to construct inputs for the anticipated outputs.

TxSigner : This interface is accountable to supply transaction signing functionality.

The library supplies a backend service layer, which is required to construct and submit transaction to Cardano blockchain. By default, it supplies backend service implementations for Blockfrost, Koios and so on.

However, if you wish to use another suppliers to construct and submit transaction, you’ll be able to simply accomplish that by implementing following three easy interfaces :

  1. ProtocolParameterSupplier :- Implement this purposeful interface to supply present protocol parameters. Protocol parameters are required to construct the transaction.
public interface ProtocolParamsSupplier {     ProtocolParams getProtocolParams(); }

2. UtxoSupplier:- This interface has just one technique. Implement this interface to supply utxos that are required throughout transaction constructing.

3. TransactionProcessor :- Implement this interface to submit transaction to the Cardano blockchain.

public interface TransactionProcessor {     Outcome<String> submitTransaction(byte[] cborData) throws ApiException; }

Observe: If you’re utilizing a supported backend supplier like Blockfrost, you’ll be able to simply use the out-of-box backend service implementation.

On this part, we’ll undergo the instance step-by-step. It’s also possible to discover the total supply code of the instance right here.

Now, let’s begin with some initialization steps.

Account setup

To submit any transaction to Cardano blockchain, we’d like an account to signal and pay transaction price.

So, let’s see the way to create a sender account object from a mnemonic phrase. We can be utilizing this account to ship fund to the script output and likewise to unlock the fund.

String senderMnemonic = "<24 phrases mnemonic phrase>";
Account sender = new Account(Networks.testnet(), senderMnemonic);
String senderAddress = sender.baseAddress();

Plutus V2 Script Object and Script deal with

We have to use the compiled model of our Sum contract script. The next code snippet exhibits the way to initialize the PlutusV2Script occasion and discover the script deal with.

//Sum Script
PlutusV2Script sumScript =
PlutusV2Script.builder()
.cborHex("5907a65907a3010000323322323232323232323232323232323322323232323222232325335323232333573466e1ccc07000d200000201e01d3333573466e1cd55cea80224000466442466002006004646464646464646464646464646666ae68cdc39aab9d500c480008cccccccccccc88888888888848cccccccccccc00403403002c02802402001c01801401000c008cd405c060d5d0a80619a80b80c1aba1500b33501701935742a014666aa036eb94068d5d0a804999aa80dbae501a35742a01066a02e0446ae85401cccd5406c08dd69aba150063232323333573466e1cd55cea801240004664424660020060046464646666ae68cdc39aab9d5002480008cc8848cc00400c008cd40b5d69aba15002302e357426ae8940088c98c80c0cd5ce01881801709aab9e5001137540026ae854008c8c8c8cccd5cd19b8735573aa004900011991091980080180119a816bad35742a004605c6ae84d5d1280111931901819ab9c03103002e135573ca00226ea8004d5d09aba2500223263202c33573805a05805426aae7940044dd50009aba1500533501775c6ae854010ccd5406c07c8004d5d0a801999aa80dbae200135742a00460426ae84d5d1280111931901419ab9c029028026135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d5d1280089aba25001135744a00226ae8940044d55cf280089baa00135742a00860226ae84d5d1280211931900d19ab9c01b01a018375a00a6eb4014405c4c98c805ccd5ce24810350543500017135573ca00226ea800448c88c008dd6000990009aa80b911999aab9f0012500a233500930043574200460066ae880080508c8c8cccd5cd19b8735573aa004900011991091980080180118061aba150023005357426ae8940088c98c8050cd5ce00a80a00909aab9e5001137540024646464646666ae68cdc39aab9d5004480008cccc888848cccc00401401000c008c8c8c8cccd5cd19b8735573aa0049000119910919800801801180a9aba1500233500f014357426ae8940088c98c8064cd5ce00d00c80b89aab9e5001137540026ae854010ccd54021d728039aba150033232323333573466e1d4005200423212223002004357426aae79400c8cccd5cd19b875002480088c84888c004010dd71aba135573ca00846666ae68cdc3a801a400042444006464c6403666ae7007006c06406005c4d55cea80089baa00135742a00466a016eb8d5d09aba2500223263201533573802c02a02626ae8940044d5d1280089aab9e500113754002266aa002eb9d688

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments