【问题标题】:BitGO-JS Exceed Transaction 250 limitBitGO-JS 超过 Transaction 250 限制
【发布时间】:2016-08-15 09:20:58
【问题描述】:

我正在尝试借助 test.bitgo.com 模拟超过 250 笔交易,这是为 API 设置的当前限制...我尝试并再次尝试不同的方法以达到相同的结果,一周后我仍然找不到一种正确的方法来一次获取所有交易数据 GO 。

他们的一位开发人员说,我可以使用具有嵌套 while 循环的 Promise 来做到这一点,该循环将 count:250 添加到 skip:0 并一次又一次地运行该函数,直到没有任何东西可以总结,因为 count最后变为 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
}

如您所见,我总共有“total”:852 个事务,skip 参数等于“start”:0,限制等于“count”:2

传说: limit:250 将仅显示总共 852 笔交易中的 250 笔交易 skip:250 将跳过前 250 个事务并开始显示 251 >= 500

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

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

【问题讨论】:

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


    【解决方案1】:

    你想要的是一个递归异步函数,它重复调用 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,此函数将重复调用 BitGo,并且每次将其收到的交易添加到 allTxs 数组的响应中。一旦该数组的大小等于钱包上的 tx 总数,它将打印出所有交易。将 console.log 调用替换为您想要对事务执行的任何处理,您将获得成功!

    【讨论】:

    • 谢谢你 almel ,你的代码很吸引人 :) 。这个问题已经有一个多星期了,简直快疯了......非常感谢你拯救了这一天!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2011-05-12
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多