BitGO-JS超过事务250限制

时间:2016-04-21 23:16:34

标签: javascript node.js api while-loop promise

我在test.bitgo.com的帮助下尝试模拟超过250个交易,这是API的当前限制设置...我尝试并尝试了不同的方法来实现相同的结果,一周后我仍然找不到合适的方法将所有交易数据都放在一个GO中。

他们的一个开发人员说我可以使用一个具有嵌套while循环的Promise来实现它,该循环增加了计数: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个事务,skip参数等于“start”:0且限制等于“count”:2

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

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

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

1 个答案:

答案 0 :(得分:3)

你想要的是一个递归异步函数,它反复调用wallet.transactions({skip:skip}),其值越来越大,为" skip",这将允许你遍历所有的事务钱包。看看下面的代码:

var BitGoJS = require('bitgo');

var user = 'octavian@l.com';
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调用,你就会变得金黄!