2016-04-21 156 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 **,你的代碼令人着迷:)。這個問題已經超過一個星期了,真的瘋了...非常感謝你爲拯救這一天! –