2016-04-21 120 views
0

我试图模拟与test.bitgo.com的帮助超过250个交易,这是目前的API限制设置...我试过并尝试了不同的方法来实现同样的结果和一周后,我仍然无法找到一个合适的方式获得一个GO中的所有交易数据。BitGO-JS超过交易250限制

他们的一位开发人员说我可以用一个带有嵌套while循环的Promise来实现,该Promise添加了count:250来跳过:0并一次又一次地运行该函数,直到没有什么可以总结为止,因为计数最终为0,并获得所有852个事务。

这就是我使用的https://www.bitgo.com/api/#list-wallet-transactions。它返回一个有250个事务的对象,并保持这样的计数。

var walletId = '2NB96fbwy8eoHttuZTtbwvvhEYrBwz494ov'; 
bitgo.wallets().get({ "id": walletId }, function callback(err, wallet) { 
    if (err) { throw err; } 
wallet.transactions({limit:2, skip:0}, function callback(err, transactions) { 
// handle transactions 
console.log(JSON.stringify(transactions, null, 4)); 
    }); 
}); 

// This is the result 

{ 
"transactions": [ 
    { 
     "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": 487, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    }, 
    { 
     "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": 487, 
     "pending": false, 
     "instant": false, 
     "blockhash": "000000000000020f526fe18af7536fa4e816694c4dec865e0d87d6b722b643d9", 
     "height": 786821 
    } 
], 
"start": 0, 
"count": 2, 
"total": 852 
} 

正如你可以看到我有一个总的“总”的:852个交易,跳过参数等于“开始”:0,上限等于“计数”:2

LEGEND : 限制:250只会显示总计852个250个交易 跳过:250会跳过前250个交易并开始显示251> = 500

主要问题是我最多只能获得250个交易一次,我试图将结果推送到一个数组,并在lodash的帮助下连接所有内容,但失败了。试图通过大量的请求跳过:250然后500然后750等工作,仍然无法清理和保存一切。

希望有人已经不得不爬上这座山,愿意花几分钟的时间指引我走向正确的方向。谢谢 !

回答

3

你想要的是一个递归的异步函数,它为“skip”重复调用wallet.transactions({skip:skip}),它允许你遍历钱包上的所有交易。看看下面的代码:

var BitGoJS = require('bitgo'); 

var user = '[email protected]'; 
var loginPassword = 'supersecretpassword'; 
var otp = '0000000'; 
var walletId = 'yourWalletId'; 

var bitgo = new BitGoJS.BitGo(); 


var printTxs = function() { 
    // Now get the wallet 
    bitgo.wallets().get({ id: walletId }, function(err, wallet) { 
    if (err) { console.log("Error getting wallet!"); console.dir(err); return process.exit(-1); } 

    var allTxs = []; 

    /** 
    * Fetch transactions from the wallet using skip as an index into the array of all 
    * transactions on the wallet 
    * @param skip {Number} the number of transactions we should skip ahead 
    */ 
    var getTransactionBatch = function(skip, callback) { 
     wallet.transactions({ skip: skip }, function(err, res) { 
     if (err) { console.log("Error getting transactions!"); console.dir(err); return process.exit(-1); } 

     res.transactions.forEach(function(tx) { 
      allTxs.push(tx); 
     }); 

     var totalTxCount = res.total; 

     if (totalTxCount && totalTxCount > allTxs.length) { 
      var newSkip = skip + res.count; // add the number of tx's we just fetched to the number we already skipped ahead 

      return getTransactionBatch(newSkip, callback); 
     } 

     return callback(); 
     }); 
    }; 

    getTransactionBatch(0, function() { 
     console.log('All Transactions\n'); 
     console.log(JSON.stringify(allTxs, null, 2)); 
    }) 
    }); 
}; 

// Authenticate first 
bitgo.authenticate({ username: user, password: loginPassword, otp: otp }, function(err, result) { 
    if (err) { console.dir(err); throw new Error("Could not authenticate!"); } 
    console.log("Unlocking account.."); 
    bitgo.unlock({ otp: otp }, function(err) { 
    if (err) { console.dir(err); throw new Error("Could not unlock!"); } 
    printTxs(); 
    }); 
}); 

一旦你登录凭据,walletId在适当的值上填写,此功能将重复调用BitG​​o每次添加它收到的交易响应allTxs阵列。一旦该数组的大小与钱包上的tx总数相等,它将打印出所有的交易。将console.log调用替换为您希望对事务执行的任何处理,并且您将成为金牌!

+0

谢谢** almel **,你的代码令人着迷:)。这个问题已经超过一个星期了,真的疯了...非常感谢你为拯救这一天! –