Bitgo provides all transactions as objects and are always kept inside a big array. Inside the nested .entries we see that the first TX object has two minus values -312084680 and -4254539 from which I only need the lowest value. My code works and logs all transactions but it's only correct if the money are sent from only one address. If two minus values are present, I log 3 transactions instead of just 2.
var allTxs = [
{
"id": "e5216ffaaa2a37bcc14380db07f06c85a65bcdc4e1fcab2bd5523f0b8a11bc15",
"normalizedHash": "0709c99097386a3c0130f3d6b002acf6a4e37978406704268fc9d308eec4c2b8",
"date": "2016-04-17T20:07:03.700Z",
"fee": 7440,
"inputs": [
{
"previousHash": "6d043a06ade4eac5315967c463fcd65deb4ed9bff23ee3e73ff82c9cf72360e9",
"previousOutputIndex": 1
},
{
"previousHash": "b6e566cbee0f23bee7b321eda7f6159a165101e77e7f1e75bd9eb6e31540b391",
"previousOutputIndex": 0
}
],
"outputs": [
{
"vout": 0,
"account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q",
"value": 312500000,
"isMine": true,
"chain": 0,
"chainIndex": 0
},
{
"vout": 1,
"account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe",
"value": 3831779
}
],
"entries": [
{
"account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q",
"value": 312500000
},
{
"account": "muEePZzkRWX3RnLWHxTx6r8T3MMruTgMgg",
"value": -312084680
},
{
"account": "mmRuajWq2xPYQw4gjXz8pQ2fUfJTF7fvYe",
"value": 3831779
},
{
"account": "n47gD5D3XfBG41tWKX4YHNc9gboyWU9yJg",
"value": -4254539
}
],
"confirmations": 677,
"pending": false,
"instant": false,
"blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9",
"height": 786821
},
{
"id": "71fb53e7d70ce27dced2eb327ac544b8f046e66480342ba81533046f3267e6f4",
"normalizedHash": "80116b194b58b494d85b2a831815a978ec6f0fe617cfd020880ff1ad76b2bacc",
"date": "2016-04-17T20:06:56.474Z",
"fee": 4480,
"inputs": [
{
"previousHash": "1f4145b615f5d067160184a3e9660396f826614c3fcae9abdcb7192c615b843a",
"previousOutputIndex": 0
}
],
"outputs": [
{
"vout": 0,
"account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q",
"value": 625000000,
"isMine": true,
"chain": 0,
"chainIndex": 0
},
{
"vout": 1,
"account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt",
"value": 390110612
}
],
"entries": [
{
"account": "2N5Jr87jhTuAHab37VKWNPhoH1WUEHkVg1Q",
"value": 625000000
},
{
"account": "mqRsJr8szT5XTSLm3CU7i9ePa7kWnC2VWs",
"value": -1015115092
},
{
"account": "mpntSJWk116JF58VRDxeMMwr4gC7afVEKt",
"value": 390110612
}
],
"confirmations": 689,
"pending": false,
"instant": false,
"blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9",
"height": 786821
}
];
for (var i=0; i<allTxs.length; i++) {
var tx = allTxs[i];
var value = 0;
for (var entriesIndex = 0; entriesIndex < tx.entries.length; ++entriesIndex) {
if (tx.entries[entriesIndex].account === wallet.id()) {
value += tx.entries[entriesIndex].value;
}
}
for ( var receivedFrom = 0; receivedFrom < tx.entries.length; ++receivedFrom ){
if (tx.entries[receivedFrom].value < 0){
var verb = (value > 0) ? 'Received' : 'Sent';
console.log(tx.entries[receivedFrom].account + ' : ' + verb + ' ' + (value / 1e8).toFixed(8));
}
}
}
This is where the magic happens:
if (tx.entries[receivedFrom].value < 0){
// both values are less than 0
Hope someone finds this easy and points the mob to the right direction ! Cheers
Aucun commentaire:
Enregistrer un commentaire