@emurgo/yoroi-lib
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -45,3 +45,4 @@ import { Axios } from 'axios'; | ||
private _base; | ||
constructor(base: UtxoApiContract); | ||
private _maxAddresses; | ||
constructor(base: UtxoApiContract, maxAddresses?: number); | ||
getSafeBlock(): Promise<string>; | ||
@@ -57,3 +58,4 @@ getBestBlock(): Promise<string>; | ||
private _throwRequestErrors; | ||
constructor(axios: Axios, apiUrl: string, throwRequestErrors: boolean); | ||
private _pageSize; | ||
constructor(axios: Axios, apiUrl: string, throwRequestErrors: boolean, pageSize?: number); | ||
getSafeBlock(): Promise<string>; | ||
@@ -60,0 +62,0 @@ getBestBlock(): Promise<string>; |
@@ -59,4 +59,5 @@ "use strict"; | ||
class BatchedEmurgoUtxoApi { | ||
constructor(base) { | ||
constructor(base, maxAddresses = 500) { | ||
this._base = base; | ||
this._maxAddresses = maxAddresses; | ||
} | ||
@@ -81,3 +82,3 @@ getSafeBlock() { | ||
try { | ||
const addressChunks = (0, js_1.chunk)(req.addresses, 50); | ||
const addressChunks = (0, js_1.chunk)(req.addresses, this._maxAddresses); | ||
const promises = addressChunks.map((addresses) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -103,3 +104,3 @@ return yield this._base.getUtxoAtPoint({ | ||
try { | ||
const addressChunks = (0, js_1.chunk)(req.addresses, 50); | ||
const addressChunks = (0, js_1.chunk)(req.addresses, this._maxAddresses); | ||
const promises = addressChunks.map((addresses) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -142,9 +143,8 @@ return yield this._base.getUtxoDiffSincePoint({ | ||
exports.BatchedEmurgoUtxoApi = BatchedEmurgoUtxoApi; | ||
const UTXO_AT_POINT_PAGE_SIZE = 10; | ||
const UTXO_DIFF_SINCE_POINT_PAGE_SIZE = 10; | ||
class EmurgoUtxoApi { | ||
constructor(axios, apiUrl, throwRequestErrors) { | ||
constructor(axios, apiUrl, throwRequestErrors, pageSize = 50) { | ||
this._axios = axios; | ||
this._apiUrl = apiUrl; | ||
this._throwRequestErrors = throwRequestErrors; | ||
this._pageSize = pageSize; | ||
} | ||
@@ -179,3 +179,3 @@ getSafeBlock() { | ||
lastFoundBestBlock: resp.data.reference.lastFoundBestBlock, | ||
lastFoundSafeBlock: resp.data.reference.lastFoundSafeBlock | ||
lastFoundSafeBlock: resp.data.reference.lastFoundSafeBlock, | ||
} | ||
@@ -213,3 +213,3 @@ } | ||
allUtxos = allUtxos.concat(utxosAtPointPage); | ||
while (utxosAtPointPage.length === UTXO_AT_POINT_PAGE_SIZE) { | ||
while (utxosAtPointPage.length === this._pageSize) { | ||
page++; | ||
@@ -250,3 +250,3 @@ utxosAtPointPage = yield this.getUtxoAtPointPage(req, page); | ||
afterBestblocks: req.afterBestBlocks, | ||
diffLimit: UTXO_DIFF_SINCE_POINT_PAGE_SIZE | ||
diffLimit: this._pageSize, | ||
}); | ||
@@ -261,3 +261,3 @@ if (response.data.lastFoundBestblock == null) { | ||
let allDiffItems = [...response.data.diffItems]; | ||
while (response.data.diffItems.length === UTXO_DIFF_SINCE_POINT_PAGE_SIZE) { | ||
while (response.data.diffItems.length === this._pageSize) { | ||
response = yield this._axios.post(url, { | ||
@@ -267,3 +267,3 @@ addresses: req.addresses, | ||
afterPoint: response.data.lastDiffPointSelected, | ||
diffLimit: UTXO_DIFF_SINCE_POINT_PAGE_SIZE | ||
diffLimit: this._pageSize, | ||
}); | ||
@@ -319,3 +319,3 @@ allDiffItems = allDiffItems.concat(response.data.diffItems); | ||
page: page, | ||
pageSize: UTXO_AT_POINT_PAGE_SIZE, | ||
pageSize: this._pageSize, | ||
}); | ||
@@ -322,0 +322,0 @@ return resp.data; |
@@ -23,2 +23,15 @@ import { UtxoApiContract } from './api'; | ||
} | ||
export declare const init: (utxoStorage: UtxoStorage, apiUrl: string) => UtxoService; | ||
/** | ||
* It builds the UtxoService | ||
* | ||
* @param {UtxoStorage} utxoStorage An instance of the UtxoStorage | ||
* @param {string} apiUrl A base endpoint URL ending with `/` | ||
* @param {number} [pageSize=50] It caps the number of the records per response | ||
* @param {number} [maxAddresses=500] It caps the number of addresses per request | ||
* | ||
* @returns {UtxoService} | ||
* | ||
* @example init(utxoService, 'https://cardano-api.emurgo.com/') | ||
* @example init(utxoService, 'https://cardano-api.emurgo.com/', 200, 200) | ||
*/ | ||
export declare const init: (utxoStorage: UtxoStorage, apiUrl: string, pageSize?: number, maxAddresses?: number) => UtxoService; |
@@ -22,3 +22,3 @@ "use strict"; | ||
to be extremely low. | ||
best block: | ||
@@ -32,3 +32,3 @@ current block on tip of the chain | ||
and therefore needs to be removed | ||
lastFoundSafeBlock: | ||
@@ -230,7 +230,20 @@ used for us to merge the diff items we have locally which are now considered to be safe. | ||
exports.UtxoService = UtxoService; | ||
const init = (utxoStorage, apiUrl) => { | ||
const utxoApi = new emurgo_api_1.EmurgoUtxoApi(axios_1.default, apiUrl, true); | ||
const batchedUtxoApi = new emurgo_api_1.BatchedEmurgoUtxoApi(utxoApi); | ||
/** | ||
* It builds the UtxoService | ||
* | ||
* @param {UtxoStorage} utxoStorage An instance of the UtxoStorage | ||
* @param {string} apiUrl A base endpoint URL ending with `/` | ||
* @param {number} [pageSize=50] It caps the number of the records per response | ||
* @param {number} [maxAddresses=500] It caps the number of addresses per request | ||
* | ||
* @returns {UtxoService} | ||
* | ||
* @example init(utxoService, 'https://cardano-api.emurgo.com/') | ||
* @example init(utxoService, 'https://cardano-api.emurgo.com/', 200, 200) | ||
*/ | ||
const init = (utxoStorage, apiUrl, pageSize = 50, maxAddresses = 500) => { | ||
const utxoApi = new emurgo_api_1.EmurgoUtxoApi(axios_1.default, apiUrl, true, pageSize); | ||
const batchedUtxoApi = new emurgo_api_1.BatchedEmurgoUtxoApi(utxoApi, maxAddresses); | ||
return new UtxoService(batchedUtxoApi, utxoStorage); | ||
}; | ||
exports.init = init; |
{ | ||
"name": "@emurgo/yoroi-lib", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
230039
4113