2

I am trying to retrieve all new confirmed transactions from the tangle and for each transaction the corresponding transactions that have been confirmed in order to be able to create that transaction. Here is the code I am using but I am not sure, if this is correct as I can not find much documentation or examples on it:

First I get all unconfirmed tips and cache them in a variable

var tipsHashCache;

this.Iota.api.getTips(function(error, hashes) {
    ...
    tipsHashCache = hashes;
});

Then later I use these unconfirmed tips I received to see, if there are any new confirmations for any of them

var confirmedHashes = [];

Iota.api.getLatestInclusion(tipsHashCache, (err, states) => {
    ...
    var confirmedHashes = [];
    for (let i in states) {
        ...
        if (states[i]) {
            confirmedHashes.push( tipsHashCache[i] );
        }
    }
}

Then after that in a callback I retrieve all the info about the confirmed transactions:

var confirmedTransactions = [];

Iota.api.getTransactionsObjects(confirmedHashes, (err, transactions) => {
    ...     
    confirmedTransactions = confirmedTransactions.concat(transactions);
});

Now from these transactionObjects I want to retrieve the two transactions that have been confirmed by that transaction. So can I just use confirmedTransactions[i].trunkTransaction AND confirmedTransaction[i].branchTransaction? Or am I missing something and do I first have to get all the info about the whole bundle of transactions before I can get the correct corresponding confirmed transactions?

2 Answers 2

1

As I'm new to Iota development I can not tell, if this is really the correct way to do it but I found the following works for me and the result looks correct and like what I expected it to look like:

After requesting all transactionObjects from the confirmed Tips (like I discribed in my question) I go ahead and get the bundle for each transaction:

self.Iota.api.getTransactionsObjects(confirmedHashes, (err, transactions) => {
    ...
    let tailHashes = [];
    for(var i in transactions) {
        ...
        if( transactions[i].currentIndex === 0 ){
            tailHashes.push(transactions[i].hash);
        }
    }

    for(var i in tailHashes) {
        ...
        let tailHash = tailHashes[i];
        self.Iota.api.getBundle(tailHash, function (err, bundle) {
            if (err) { return; }
            let nodeData = {
                hash: bundle[0].hash,
                trunkTransaction: bundle[bundle.length - 1].trunkTransaction,
                branchTransaction: bundle[bundle.length - 1].branchTransaction
            }
            ...
        });
    }
});

Now as you can see the hash of the first transaction from a bundle is the hash that other transactions within the network use to confirm. And the last transaction within a bundle will hold the two hashes from the other transaction bundles that had been confirmed.

This video explains it in more detail:

IOTA tutorial 10: Transaction and Bundle

0

You can let the node choose the two other transactions to approve.

There is a command for this in the official API (getTransactionsToApprove: IRI API Command).

To use this call in javascript, see here: iota.lib.js search: getTransactionsToApprove

I hope this is the answer to your question :)

1
  • Thanks for your suggestion! Actually I am not looking for tips to approve - I am rather trying to create visual representation of the existing network. This is why I need to to retrieve the confirmed transactions from already existing transactions. Actually I belief I already found a soultion myself. It looks like it is doing what I want but I am not sure, if it is really the correct way to do it - so I did not post an answer to my own question yet. But it perfectly works for what I need it for, though.
    – Eric Xyz
    Commented Mar 29, 2018 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.