Introduction to the

This article is a very useful step-by-step tutorial that aims to show Java developers (in particular) a very simple way to interact with blockchain smart contracts. Understand how easy it is to use the library to interact with the blockchain.

Environmental requirements

We will use Tezos blockchain. To enable our Java code to interact with existing, deployed smart contracts, we need the TeZOSJ library from TezosRio. There are two versions of the library, one written specifically for Android application development (TezosJ_SDK) and one for general-purpose Java applications written using the Eclipse IDE (TezosJ_plainJava), which we’ll use here. You don’t need to download the entire library source code. The V1.1.0 JAR file alone is sufficient. Eclipse will be our development IDE of choice.

The article goal

We want to call intelligent basic registration is a simple client contract, can be in Tezos testnet found, address is as follows: KT18pK2MGrnTZqyTafUe1sWp2ubJ75eYT86t

You can easily view the details of smart contracts through one of Tezos block browsers such as Tezblock. Or by using the better-call.dev tool. Another option is to check the smart contract with SmartPy resource Manager. Remember, remember, to specify a test web server, such as https://tezos-dev.cryptonomic-infra.tech.

Our smart contract is called “Customer,” and it has four entry points (or methods) : addCustomer, removeCustomer, Transfer, and updateBalance. Basically insert, delete, transfer funds and update basic functionality. This was created for educational purposes only. Its role is to maintain a list of “customers” in the store of the Tezos blockchain.

A method is called

TezosJ is probably one of the easiest ways to invoke Tezos smart contracts today. Basically, our call can be done with a command:



The code above adds a new customer to a new customer Bob by adding his balance to 10ꜩ with ID 123456789 and phone number 98769985. This is done by calling the callContractEntryPoint method, which basically requires the same parameters as the normal tezos.send () method, but with a few extra features: the smart contract entry point name and a String Array containing the expected entry point parameters

Step by step, please!

If you have not done so, download and install Eclipse. Open it. The first step is to create a new project and select the File-> New -> Project option as follows:



The next step is to select the Project type: select Gradle Project:



Set the name of the new project to “callSmartContract” and click the “Finish” button. Wait for Eclipse to build the project file structure. Now, we will create a Main class so that we can run and test the project. Right click on the project name “SmartContract” and select New->Class:



Then, specify “Main” as the name of the new class and make sure “Public void static Main (String args[])” is selected to create the box. Then click the “Finish” button:



You’ll notice that the Main class has been created. Right-click Main Class and choose Run as-> Java Application. This is just to configure how the project will run from now on when the Play button is clicked:



In the console, you will see that the project has run successfully, but of course there is no response yet. Therefore, you will see the “terminated” message:



Now we will add the TezosJ library JAR file. Go to the Tezos.rio Github repository and download the file tezosj-SDK-plain-java-1.1.0.jar

In the file browser, copy the tezosj-SDK-plain-java-1.1.0.jar file (CTRL + C).

Back in Eclipse, click on the project name, then use CTRL + V to paste.

The file will appear as added to the project structure:



The next step is to add the JAR to your project’s build path. Right-click on the library file in the project browser and select Build Path-> Add to Build Path (or Build Path-> Configure Build Path) :



Select the library file, then click the Apply and Close button:



Now that we have everything ready, let’s first build a Tezos wallet:



The red “X” at the top (left) reminds us that we need to import the class so our code knows what TezosWallet is:



Now add some console output to check that our wallet was created successfully. We will print information about this: the public key hash of the wallet (Tezos address), its mnemonic and the current balance. After adding the system.out.println command, run the project and you get something like the following (check the console) :



Good! We have a fully functional Tezos wallet. Now we can invoke the customer Tezos smart contract.

One thing about smart contracts… .

We will use smartpy. IO online IDE to create and deploy the customer smart contracts we will use on the Tezos blockchain test network. Check out the smart contract source code below:



The source code for the SmartPy smart contract shown here is for illustrative purposes only and we do not need to compile and deploy it again. It has located in Tezos block chain test online, address is: KT18pK2MGrnTZqyTafUe1sWp2ubJ75eYT86t

Now we have everything we need to invoke the contract from Java. First we will add new customers using the smart contract’s “addCustomer” entry point. Let’s consider the customer’s name “Bob”, whose ID will be “98765” and phone number “99876787”. Bob’s balance is 10 pounds (10 million Mutez). Therefore, our smart contract invocation command will look like this:



First, we need to inform TezosJ that we will be using TezosTestNet (where the smart contract is actually published). We also need to set transaction amounts and fees. This is done with the following Java code:



Copy the code and paste it into our Main class so it looks like this:



Note that for this call to work, you first need to inject capital and display your Tezos account. Otherwise, you may receive the following error:

There were errors: kind 'branch' ID 'proto.005- PSbabym1.implicit. empty_implicit_contract'Copy the code

Run the project! If all is well, you will get the operation hash shown in the console:



This indicates that we have successfully sent the callContract transaction to the Tezos Testnet blockchain (because we have an operation hash). Now let’s check if the transaction is subject to Tezos block chaining. We will do this using the TezBlock explorer. Open https://tezblock.io in your favorite browser, and first, in the upper right corner of the screen, change “Mainnet” to “Balylonnet” (the same as Testnet). Then, paste the action hash obtained from the Java execution into the search box and press Enter:



This will show the results of our smart contract as well as the operational details. By clicking ‘display’, you will be able to check the parameters passed to the Tezos blockchain from our Java code:



Parameters:

Left (Left (Left (Pair (Pair (Pair 1000000 "98765") "Bob"99876787))))Copy the code

This is the Micheline format parameter that we generated from the Java application call. When deployed, Tezos smart contracts are written in the Michelson language and they expect input parameters to be sent in this format. One of the benefits of using the TezosJ library is that it can generate Micheline format parameters in real time, without you having to worry about how it is done.

So far, we can ensure that the smart contract is invoked correctly, the parameters are passed, and the operation is accepted by the Tezos block link. Now the last thing we need to do is check to see if the new customer Bob has been inserted into the smart contract store. To do this, we will use the better-call.dev tool. Simply open your browser and point it to https://better-call.dev. In the search field Enter our customer intelligence contract address (KT18pK2MGrnTZqyTafUe1sWp2ubJ75eYT86t), the network changes for babylonnet, then press Enter:



If will be a list of all operations ever sent to the smart contract address and the current state of the smart contract store. Here you will check the parameters passed from Java and whether they are correctly stored in storage:



further

You can try other entry points for smart contracts, such as removeCustomer, Transfer, and updateBalance. Remember to always check the number of parameters required for each entry point.

When you run the code, if you encounter a Java error, the transaction was not sent. In this case, you must recheck the parameters, their order, account balance, fees, and the network you are using (main network or test network). On the other hand, if you ever get an action hash, it means that your Java application has successfully sent the transaction to the blockchain. However, you must also check for possible blockchain-related errors. In this case, when you check with the TezBlock Explorer, it will show errors with a salmon background, which means that errors occurred in the blockchain context (e.g., wrong number of arguments passed, wrong Micheline format of arguments, or even conditions not accepted by the contract — for example: You invoked the transfer entry point, but the Sender client does not have sufficient funds to send). Also check the gasLimit and storageLimit parameters.

conclusion

It has never been easier for developers to use blockchain smart contracts. Tezos, in particular, offers many excellent tools and numerous computer language libraries that facilitate integration with most systems. The TezosJ library innovates by providing an easy-to-use callContractEntryPoint method that creates Micheline format parameters on the go. It has all the tools Java developers need to thrive in application development that seamlessly interact with blockchain.

Bitoken.world /? p=690