Warning: Use of undefined constant _FILE_ - assumed '_FILE_' (this will throw an Error in a future version of PHP) in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/ad-blocker-stats-vip/ad-blocker-stats-vip.php on line 13

Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645

Sending new transfer (create transaction) to IOTA node using Java (aka sendTransfer)

Posted / Публикувана 2017-09-10 in category / в категория: IOTA Cryptocurrency, Java

Warning: count(): Parameter must be an array or an object that implements Countable in /home/bolyarco/www-ikratko/ogrelab/wp-content/plugins/microkids-related-posts/microkids-related-posts.php on line 645

Due to the lack of any official documentation it is hard to start using IOTA. Here you will find a short tutorial about sending transfer (i.e. creating transaction) and checking it's status.

I am assuming that you use Intellij IDEA.

Project setup

1. Create new gradle project

2. Add following dependencies to your build.gradle:

compile 'com.github.iotaledger:iota.lib.java:f43d606'
compile 'ch.qos.logback:logback-classic:1.2.3'

 

Building IotaAPI

In order to execute commands against IOTA node you have to create an IotaAPI instance. IotaAPI is the client that connects to a node and provides you the services of IOTA.

api = new IotaAPI.Builder().protocol(PROTOCOL).host(HOST).port(((Integer) PORT).toString()).build();

where PROTOCOL, HOST and PORT are constants defining the node's address.

Preparing and sending a transfer

For the sake of simplicity here we will send "empty" transfer, i.e. with value = 0.

The code:

List transfers = new ArrayList<>();  // (1)
Transfer t = new Transfer(ADDRESS_TO,  // (2)
        0, // (3)
        TrytesConverter.toTrytes("some msg"), // (4)
        "999999999999999999999999999"); // (5)
transfers.add(t);  // (6)
SendTransferResponse resp = api.sendTransfer(SEED, // (7)
        2, // (8)
        9, // (9)
        15, // (10)
        transfers, // (11)
        null,  // (12)
        ADDRESS_REMAINDER);  // (13)

First we need to prepare the transfer(s).

(1)  we are createing a list that will hold our transfer(s)

(2) ADDRESS_TO is the address of the recipient

(3) we set 0 for the value of the transfer, i.e. "empty" transfer

(4) this is the message. It is up to you what the message will contain but it must be encoded to trytes.

(5) optional tag, here we send empty tag, i.e. 999999999999999999999999999

(6) add the Transfer object to the list

(7) send the Transfer. Here SEED is your seed

(8)  2 is the security level (default value)

(9) 9 is the depth (default value)

(10) 15 is inWeightMagnitude which is basically the amount of PoW that is done for a transaction

(11) your transfer(s)

(12) prepared list of inputs. What an input really means is an address from which to take the iotas for the transfer. One transaction with value may use several inputs/address. We put null here because our value is 0

(13) address where the remainder of the transaction to be send. In our case not used because the value is 0. Remainder is difference between the sum of all inputs and the value of the transaction.

In the returned SendTransferResponse object you have a field successfully which is an array containing one boolean value per transaction which indicates if the transfer is send successfully. Please note that "send" differs from "confirmed". "Send" just means that the node received and accepted the transfer. Your transaction will now have "Pending" status.

If you send 2 transfers at once you will get 2 transactions. Those 2 transactions will be in the same bundle.

Now you may ask "Where is my transaction hash and how do I check if it is confirmed?". Well, oddly enough, SendTransferResponse does not contain the transaction hash(es) even though it is quite easy for it do to so because in the underlying code that information exist. It is probably just because the IOTA's java lib is still immature and this may change in the future.

In order to find your transaction's hash you will have to use:

FindTransactionResponse resp = api.findTransactions(new String[] {ADDRESS_FROM}, null, null, null);

In the response object you will get all transactions (their hashes) for this address and you will have to use the last. Then to check the status of the transaction:

GetInclusionStateResponse inclusionResp = api.getLatestInclusion(new String[] {transHash});

In inclusionResp there is a field 'states' which contain an array with the states of all matched transactions (in our case just one). If it is 'true' your transaction is approved.

Comments are closed.