BitGO - 获取帐号&来自对象数组的值

时间:2016-06-14 14:32:04

标签: javascript json node.js multidimensional-array javascript-objects

我在做什么:

  • 当新用户注册时,会在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
}, 

希望我找到能够帮助解释如何验证和提取发送到每日生成地址的用户地址和真实货币的人。

1 个答案:

答案 0 :(得分:2)

问题在于以下陈述

if (entry.account == allDBWallets[i] && entry.account == walletId) {

条件2( entry.account == walletId )仅在entry.account2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH时才为真,在这种情况下为条件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);
        }
    })
  }
});