Skip to content

Instantly share code, notes, and snippets.

@pasupulaphani
Last active January 7, 2019 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasupulaphani/45ac4989bacca6c79057bc40028e45bc to your computer and use it in GitHub Desktop.
Save pasupulaphani/45ac4989bacca6c79057bc40028e45bc to your computer and use it in GitHub Desktop.
new IOTA Java API (1.0.0-beta.5) transaction example
package iotaApi;
import java.util.ArrayList;
import java.util.List;
// jota - 1.0.0-beta1
import jota.IotaAPI;
import jota.dto.response.GetNewAddressResponse;
import jota.dto.response.GetNodeInfoResponse;
import jota.error.ArgumentException;
import jota.model.Input;
import jota.model.Transaction;
import jota.model.Transfer;
public class TxExample {
public static void main(String[] args) {
try {
String senderSeed = "<seed>";
String addressTo = "<address>";
String hash = makeTx(senderSeed, addressTo);
System.out.println("Published transaction with tail hash: " + hash);
System.out.println("Explorer link: https://devnet.thetangle.org/transaction/" + hash);
} catch (Throwable e) {
System.err.println("\nERROR: Something went wrong: " + e.getMessage());
e.printStackTrace();
}
}
public static String makeTx(String seed, String address) throws ArgumentException {
IotaAPI iotaApi = new IotaAPI.Builder()
.protocol("https")
.host("nodes.devnet.iota.org")
.port("443")
.build();
List < Transfer > transfers = new ArrayList < Transfer > ();
transfers.add(new Transfer(address, 1));
int minWeightMagnitude = 14;
int depth = 3;
int security = 2;
boolean validateInputs = true;
List < Input > inputs = new ArrayList < Input > ();
List < Transaction > tips = new ArrayList < Transaction > ();
// get remainderAddress
int index = 0;
boolean checksum = false; //Ensure remainder address doesn't have checksum. Due to a bug: https://github.com/iotaledger/iota-java/issues/135
int total = 1;
boolean returnAll = false;
GetNewAddressResponse getNewAddressResponse = iotaApi.getNewAddress(seed, security, index, checksum, total, returnAll);
String remainderAddress = getNewAddressResponse.getAddresses().get(0);
// bundle prep for all transfers
List < String > trytesBundle = iotaApi.prepareTransfers(seed, security, transfers, remainderAddress, inputs, tips, validateInputs);
// get latest known reference from nodeinfo
GetNodeInfoResponse nodeInfo = iotaApi.getNodeInfo();
String reference = nodeInfo.getLatestMilestone();
String[] trytes = trytesBundle.toArray(new String[0]);
List < Transaction > transactions = iotaApi.sendTrytes(trytes, depth, minWeightMagnitude, reference);
return transactions.get(0).getHash();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment