Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sending IOTA transaction in more granular way: API calls Prepare_transfer() and Send_transfer()
// The snippet is a part of the IOTA Developer Essentials project. You can reach it at https://hribek25.github.io/IOTA101/
// Complete description and story behind the snippet is available at: https://hribek25.github.io/IOTA101/Allchapters_javascript.ipynb.html#07CFD43B146C
// Requirement: IOTA Javascript Library (!npm install iota.lib.js)
var IOTA = require('../../node_modules/iota.lib.js/lib/iota');
var NodeURL = "https://field.deviota.com:443";
var iota = new IOTA({
'provider': NodeURL
});
var MySeed = "HGW9HB9LJPYUGVHNGCPLFKKPNZAIIFHZBDHKSGMQKFMANUBASSMSV9TAJSSMPRZZU9SFZULXKJ9YLAIUA";
var TargetAddress1 = "CXDUYK9XGHC9DTSPDMKGGGXAIARSRVAFGHJOCDDHWADLVBBOEHLICHTMGKVDOGRU9TBESJNHAXYPVJ9R9";
var TargetAddress2 = "CYJV9DRIE9NCQJYLOYOJOGKQGOOELTWXVWUYGQSWCNODHJAHACADUAAHQ9ODUICCESOIVZABA9LTMM9RW";
var NowIs = new Date(); //get a actual date & time - just to have some meaningfull info
// preparing transactions
var pt = {
'address': TargetAddress1, //81 trytes long address
'value': 0,
'message': iota.utils.toTrytes('Here comes a first message. Now is ' + NowIs),
'tag': 'HRIBEK999IOTA999TUTORIAL' //Up to 27 trytes
};
var pt2 = {
'address': TargetAddress2, //81 trytes long address
'value': 0,
'message': iota.utils.toTrytes('Here comes a second message. Now is ' + NowIs),
'tag': 'HRIBEK999IOTA999TUTORIAL999' //Up to 27 trytes
};
// Creating bundle, preparing inputs and finalizing bundle. It returns trytes of prepared TXs
var trytes;
iota.api.prepareTransfers(
MySeed, // Seed
[pt, pt2], // List of transactions
{}, // Options - security level / Inputs in case of value TXs, etc
function (e, r) { // Callback
if (e) {
console.log("Something went wrong: %s", e);
}
else { // Bundle is prepared
trytes = r;
console.log("Almost prepared bundle - tips and POW are still missing");
console.log(trytes);
console.log("Searching for tips and performing POW... Wait please...");
iota.api.sendTrytes(
trytes, // Trytes of prepared bundle from previous API call
3, // Depth
14, // MWM
function (e, r) { // callback
if (e) {
console.log("Something went wrong: %s", e);
}
else { // if went OK, then it returns broadcasted TXs
console.log("Bundle was broadcasted.");
console.log("Final transactions were returned - including nonce (POW)");
console.log(r);
}
}
);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment