Bitgo将所有事务作为对象提供,并始终保存在一个大数组中。在嵌套的.entries中,我们看到第一个TX对象有两个负值-312084680和-4254539,我只需要最低值。我的代码工作并记录所有交易,但只有从一个地址发送资金才能正确。如果存在两个减值,则记录3个事务而不是仅记录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++) {
if(allTxs[i].confirmations >= 7 ) {
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));
}
}
}
}
这就是魔术发生的地方:
if (tx.entries[receivedFrom].value < 0){
// both values are less than 0
希望有人发现这很容易并伸出援助之手!
答案 0 :(得分:1)
我做了一些更改以提高代码的可读性,更改变量的名称,并使用forEach而不是传统的for循环。还创建了一个名为minEntry的对象,它存储具有最低值的条目。希望这有助于你
var allTransactions = [
{
"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
}
];
allTransactions.forEach(function (transaction){
var value = 0;
var minEntry = { value : 0 };
transaction.entries.forEach(function (entry) {
//if (entry.account === wallet.id()) {
value += entry.value;
//}
});
var verb = (value > 0) ? 'Received' : 'Sent';
transaction.entries.forEach(function (entry) {
if (entry.value < minEntry.value){
minEntry = entry;
}
});
if(minEntry.value){
console.log(minEntry.account + ' : ' + verb + ' ' + (value / 1e8).toFixed(8));
}
});