BitGO - 从对象数组中获取帐户和值

时间:2022-05-25 22:55:34

What i'm doing :

我在做什么:

  • When a new Users register a personal wallet is generated and stored for them inside Mongo.
  • 当新用户注册时,会在Mongo中为他们生成并存储个人钱包。

  • Latter we make a request and get all User Wallet addresses from Mongo and store them in allDBWallets.
  • 后来我们提出请求并从Mongo获取所有用户钱包地址并将它们存储在allDBWallets中。

  • Every day a new wallet is generated and it's address is stored in walletId.
  • 每天都会生成一个新钱包,它的地址存储在walletId中。

  • People send money to walletId from their unique generated address and we start logging and analyzing all walletId transaction history.
  • 人们从他们唯一生成的地址向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
        }
      }
    })
});

* What I hope to achieve is this*

*我希望实现的是*

// First TX OBJ Example

//第一个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
}, 

Hope I find someone that can help explain how to validate and extract the Users Address and Real money sent to the Daily Generated address.

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

1 个解决方案

#1


2  

The problem is with the following statement

问题在于以下陈述

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

Condition 2 (entry.account == walletId) will be true only when entry.account will be 2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH and in that case condition 1 (entry.account == allDBWallets[i]) will be true only when allDBWallets array has an entry for 2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH.

条件2(entry.account == walletId)仅当entry.account为2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH时才为真,在这种情况下条件1(entry.account == allDBWallets [i])仅当allDBWallets数组具有2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH的条目时才为真。

And as per above data, both conditions are never true at same time, hence, not getting desired result.

并且根据上述数据,两个条件在任何时候都不是真的,因此,没有得到期望的结果。

Hence, you need to correct the condition/logic to get the desired output.

因此,您需要更正条件/逻辑以获得所需的输出。

A possible solution

可能的解决方案

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);
        }
    })
  }
});

#1


2  

The problem is with the following statement

问题在于以下陈述

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

Condition 2 (entry.account == walletId) will be true only when entry.account will be 2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH and in that case condition 1 (entry.account == allDBWallets[i]) will be true only when allDBWallets array has an entry for 2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH.

条件2(entry.account == walletId)仅当entry.account为2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH时才为真,在这种情况下条件1(entry.account == allDBWallets [i])仅当allDBWallets数组具有2N1mqFS5nDJkPHTvKz1xBFs2LP8LwoKmcBH的条目时才为真。

And as per above data, both conditions are never true at same time, hence, not getting desired result.

并且根据上述数据,两个条件在任何时候都不是真的,因此,没有得到期望的结果。

Hence, you need to correct the condition/logic to get the desired output.

因此,您需要更正条件/逻辑以获得所需的输出。

A possible solution

可能的解决方案

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);
        }
    })
  }
});