Skip to content

Instantly share code, notes, and snippets.

@Hribek25
Last active September 24, 2019 10:30
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 Hribek25/32a8bc8bc8e27cb1211fd42b2562bf92 to your computer and use it in GitHub Desktop.
Save Hribek25/32a8bc8bc8e27cb1211fd42b2562bf92 to your computer and use it in GitHub Desktop.
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: A core module of IOTA Javascript Library (!npm install @iota/core)
var iotalib = require('@iota/core');
var NodeURL = "https://nodes.thetangle.org:443";
var iota = iotalib.composeAPI({
'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, //zero transaction - only data in the message field will be sent
'message': Converter.asciiToTrytes('Here comes a first message. Now is ' + NowIs), //data needs to be encoded in trytes
'tag': 'HRIBEK999IOTA999TUTORIAL' //Up to 27 trytes
}
var pt2 = {
'address': TargetAddress2, //81 trytes long address
'value': 0, //zero transaction - only data in the message field will be sent
'message': Converter.asciiToTrytes('Here comes a second message. Now is ' + NowIs), //data needs to be encoded in trytes
'tag': 'HRIBEK999IOTA999TUTORIAL' //Up to 27 trytes
}
var transfers = [pt,pt2]; // a list of transactions to be sent
var depth = 3;
var minWeightMagnitude = 14;
console.log("Preparing/Broadcasting... Wait please...");
var promise = iota.prepareTransfers(MySeed, transfers) //prepare a bundle in trytes
.then(trytes => {
console.log("Almost prepared bundle - tips and POW are still missing");
console.log(trytes);
console.log("Searching for tips and performing POW... Wait please...");
return iota.sendTrytes(trytes, depth, minWeightMagnitude); //perform GTTA + POW + BROADCASTING
})
.then(bundle => {
console.log("Bundle was broadcasted.");
console.log("Final transactions were returned - including nonce (POW)");
bundle.map(tx => console.log(tx))
})
.catch(err => {
console.log("Something went wrong: %s", err);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment