我在做什么:
当新用户注册时,将生成一个个人钱包并将其存储在Mongo中。
最近,我们提出了一个请求,并从Mongo获取所有用户钱包地址,并将其存储在allDBWallets
中。
每天都会生成一个新钱包,其地址存储在walletId
中。
人们从他们生成的唯一地址向walletId
汇款,我们开始记录和分析所有walletId
交易记录。
// Check if a wallet has new transactions
var allTransactions = [
{
"id": "09829738672b3aa5be23775ba7ee81fb2f8c99f386c8731bc9710d376698073b",
"normalizedHash": "453e4cb85bf5ad12afa13b017e82f3963a420e6b57c59e50098d0b0a8a850493",
"date": "2016-06-13T22:51:55.603Z",
"fee": 30000,
"inputs": [
{
"previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f",
"previousOutputIndex": 4
},
{
"previousHash": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f",
"previousOutputIndex": 6
},
{
"previousHash": "43e5068fddd8c5a2b340524b471aad74755f625ae9d9d6610d132f56c5acbedb",
"previousOutputIndex": 0
},
{
"previousHash": "c00bd520180c32687ecb9dc00e9d40f918d5b49c752acbe48a39b4b729fc0a03",
"previousOutputIndex": 0
}
],
"outputs": [
{
"vout": 0,
"account": "2Mu1BXKcWGxt1HnMdTTLdtPNVV9xmU94Vih",
"value": 22890000
},
{
"vout": 1,
"account": "2N2aohA3TyjLzAsjocHnAHW1zAGtBCvEbPn",
"value": 27025087
},
{
"vout": 2,
"account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH",
"value": 705400000,
"isMine": true,
"chain": 0,
"chainIndex": 0
},
{
"vout": 3,
"account": "2NBJFaWk6WVzwipvAAgQ9aMghhzLu8RL1Bi",
"value": 19670000
},
{
"vout": 4,
"account": "2NFbSredn8i7rGhwAY5vGuBzobkjQRKDy5n",
"value": 14420000
}
],
"entries": [
{
"account": "2NBJFaWk6WVzwipvAAgQ9aMghhzLu8RL1Bi",
"value": 19670000
},
{
"account": "2N6nyPBXnjUVQiktuAaRd6wkU1FkMgFLja5",
"value": -287741933
},
{
"account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY",
"value": -143298154
},
{
"account": "2NFbSredn8i7rGhwAY5vGuBzobkjQRKDy5n",
"value": 14420000
},
{
"account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3",
"value": -203125000
},
{
"account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH",
"value": 705400000
},
{
"account": "2N2aohA3TyjLzAsjocHnAHW1zAGtBCvEbPn",
"value": 27025087
},
{
"account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL",
"value": -155270000
},
{
"account": "2Mu1BXKcWGxt1HnMdTTLdtPNVV9xmU94Vih",
"value": 22890000
}
],
"confirmations": 183,
"pending": false,
"instant": false,
"blockhash": "0000000000771a83972ddc9d2f45051c5da52b40e3a4c20259ab700880708293",
"height": 870163
},
{
"id": "41cd8c04edd66415718eccd2353e903b95bc172931029667114dd28c6af5597f",
"normalizedHash": "ea8540f5e65d1faec9268586d35acfeed97419ac23888a2eef0ed5aa5940aed1",
"date": "2016-06-13T03:36:04.369Z",
"fee": 11846,
"inputs": [
{
"previousHash": "92f052103edacef17ae628ed435f64df5740c4a88dd889d57cc04135ace1ed4c",
"previousOutputIndex": 0
}
],
"outputs": [
{
"vout": 0,
"account": "2N2jZ3B39oPEVu85V9s74kKwyY4PkoUGuz6",
"value": 86370000
},
{
"vout": 1,
"account": "2NBCtYhx4i13F9xyWTU6WixkknYmdpfJaYw",
"value": 113000000
},
{
"vout": 2,
"account": "2MtBRRoisqfqKzVsvh6QdHWkcUFYaiDbK82",
"value": 98630000
},
{
"vout": 3,
"account": "2N9tXbXxY73KkQNybp5qqFTZu6qN6WRsm7H",
"value": 130620000
},
{
"vout": 4,
"account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL",
"value": 155270000
},
{
"vout": 5,
"account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH",
"value": 85300000,
"isMine": true,
"chain": 0,
"chainIndex": 0
},
{
"vout": 6,
"account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY",
"value": 143298154
}
],
"entries": [
{
"account": "2NBCtYhx4i13F9xyWTU6WixkknYmdpfJaYw",
"value": 113000000
},
{
"account": "2N9tXbXxY73KkQNybp5qqFTZu6qN6WRsm7H",
"value": 130620000
},
{
"account": "2MwgwnZV8sm9nBgjntHjhgEgCP18LtsfEGY",
"value": 143298154
},
{
"account": "2N2jZ3B39oPEVu85V9s74kKwyY4PkoUGuz6",
"value": 86370000
},
{
"account": "2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3",
"value": -812500000
},
{
"account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH",
"value": 85300000
},
{
"account": "2MtBRRoisqfqKzVsvh6QdHWkcUFYaiDbK82",
"value": 98630000
},
{
"account": "2MvSR1qPDc27w9knqgCkD3vkGw4svxb4fsL",
"value": 155270000
}
],
"confirmations": 370,
"pending": false,
"instant": true,
"instantId": "575e2a24b5bdf3f4069ae3f63b015023",
"blockhash": "00000000003c0e6cd52b31dac16dac5b85a3ea1ccb3637b62fb3930ce29f6c0f",
"height": 869976
}
]
// Daily Generated Wallet
var walletId = '2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH';
// Get all Users Addresses from Mongo and store them in allDBWallets
var allDBWallets=[];
User.find({}, (err, docs) => {
if (err) {console.log('Could not find Wallets in DB');}
// console.log(docs);
docs.forEach( (address) => {
// console.log(address);
allDBWallets.push(address.userWallet);
});
});
console.log(allDBWallets)
[ '2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3',
'2NFt8YfydBU5JD9U8Xq2ucbfUp2sP7BjUrh',];
// I'm sure the IF statement is wrong...
allTransactions.forEach(function(transaction){
// console.log(transaction);
transaction.entries.forEach(function(entry){
for (var i in allDBWallets){
// This is where I fail
if (entry.account == allDBWallets[i] && entry.account == walletId) {
console.log(entry.account, entry.value);
// best result would be allDBWallets[i] with walletId entry.value
}
}
})
});
*我希望实现的是*
//第一个TX OBJ示例
{
"account": "2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH", // bad address, i need the Users address (2MzppxEX7xMidjhoJGczFDYsHk5TQwFkjS3) not the walletId address
"value": 705400000 // real amount is perfect but can't properly compare and extract users address
},
希望我能找到可以帮助解释如何验证和提取发送到每日生成地址的用户地址和真实资金的人。
最佳答案
问题在于以下语句
if (entry.account == allDBWallets[i] && entry.account == walletId) {
条件2(entry.account == walletId)仅在
entry.account
为2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH
时为真,并且在这种情况下,条件1(entry.account == allDBWallets [i])仅在allDBWallets
数组具有2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH
的条目。并且根据上述数据,这两个条件永远不会同时满足,因此不会获得理想的结果。
因此,您需要更正条件/逻辑以获得所需的输出。
可能的解决方案
allTransactions.forEach(function(transaction){
// Check whether there is an entry from any of the wallets in allDBWallets
var wallet = transaction.entries.find(function(entry){
return allDBWallets.indexOf(entry.account) > -1;
});
// If an entry exists, then find entries for walletId and paint
if(wallet) {
transaction.entries.forEach(function(entry){
if (entry.account == walletId) {
console.log(wallet.account, entry.value);
}
})
}
});