Skip to content

Commit

Permalink
Merge pull request #161 from blockfrost/chore/misc-balance-history
Browse files Browse the repository at this point in the history
Chore/misc balance history
  • Loading branch information
vladimirvolek committed Jan 10, 2022
2 parents d161cb3 + 14444c8 commit 2aed397
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
28 changes: 25 additions & 3 deletions scripts/performance/index.ts
Expand Up @@ -22,7 +22,7 @@ class TestSuite {
addUser = async (xpubs: string[]) => {
console.log(`Connecting user with ${xpubs.length} accounts... `);
// each user has its own websocket client
const c = new TestClient(this.wsServer);
const c = new TestClient(this.wsServer, false);
await c.waitForConnection();

// retrieve basic info and subscribe to block notifications
Expand All @@ -43,7 +43,7 @@ class TestSuite {
descriptor: xpub,
});
// after each added account call resubscribe with updated addresses
c.send('SUBSCRIBE_ADDRESS', {
await c.sendAndWait('SUBSCRIBE_ADDRESS', {
addresses: addresses,
});

Expand All @@ -55,8 +55,24 @@ class TestSuite {
details: 'basic',
});
}, 20000);

// refetch balance history (could happen after receiving/sending tx)
setInterval(() => {
c.send('GET_BALANCE_HISTORY', {
descriptor: xpub,
groupBy: 86400, // 1 day
});
}, randomIntFromInterval(600, 1200) * 1000); // every 10-20 mins
this.metrics.accounts += 1;
}
for (const xpub of xpubs) {
// fetch history for all xpubs of the user
c.send('GET_BALANCE_HISTORY', {
descriptor: xpub,
groupBy: 86400, // 1 day
});
}

this.metrics.users += 1;
console.log(
`Current total users: ${this.metrics.users}, total accounts: ${this.metrics.accounts}`,
Expand All @@ -66,7 +82,13 @@ class TestSuite {
addRandomUserInInterval = (interval: number) => {
setInterval(() => {
console.log('Adding new user');
this.addUser(generateRandomAccounts(randomIntFromInterval(2, 4)));
// 50% change it will be "all seed" user otherwise there will be only empty accounts
const r = randomIntFromInterval(0, 10);
if (r > 5) {
this.addUser(allSeed);
} else {
this.addUser(generateRandomAccounts(randomIntFromInterval(2, 4)));
}
}, interval);
};
}
Expand Down
6 changes: 5 additions & 1 deletion src/constants/config.ts
Expand Up @@ -4,12 +4,16 @@ export const ADDRESS_GAP_LIMIT = 20;
// Skip emitting of `newBlock` event for missed blocks if we missed more than defined number of them
export const EMIT_MAX_MISSED_BLOCKS = 3;

// List of coingecko-compatible proxies to retrieve historical fiat rates for balance history endpoint
// List of coingecko-compatible proxies to retrieve historical fiat rates for getBalanceHistory endpoint
// Set env variable BLOCKFROST_FIAT_RATES_PROXY to provide additional proxies (comma separated values)
// eg. BLOCKFROST_FIAT_RATES_PROXY="https://example.com/api/v3/coins/cardano/history,https://example2.com/history"
export const FIAT_RATES_PROXY = ['https://api.coingecko.com/api/v3/coins/cardano/history'];

// Max number of requests per second sent to FIAT_RATES_PROXY, additional requests will be queued
export const FIAT_RATES_REQUESTS_PER_SEC = 100;

// Request timeout for fetching single rate for a given day
export const FIAT_RATES_REQUESTS_TIMEOUT = 1000;

// Whether to return fiat rates in getBalanceHistory on cardano testnet network
export const FIAT_RATES_ENABLE_ON_TESTNET = false;
11 changes: 9 additions & 2 deletions src/methods/getBalanceHistory.ts
Expand Up @@ -7,6 +7,8 @@ import { sumAssetBalances } from '../utils/asset';
import { getRatesForDate } from '../utils/rates';
import { prepareErrorMessage, prepareMessage } from '../utils/message';
import { txIdsToTransactions } from '../utils/transaction';
import { FIAT_RATES_ENABLE_ON_TESTNET } from '../constants/config';
import { blockfrostAPI } from '../utils/blockfrostAPI';

interface BalanceHistoryBin {
from: number;
Expand Down Expand Up @@ -131,19 +133,24 @@ export const getAccountBalanceHistory = async (

const bins = await aggregateTransactions(txs, addresses, groupBy);

if (blockfrostAPI.options.isTestnet && !FIAT_RATES_ENABLE_ON_TESTNET) {
// fiat rates for testnet are disabled
return bins;
}

// fetch fiat rate for each bin
const binRatesPromises = bins.map(bin => getRatesForDate(bin.time));
const binRates = await Promise.allSettled(binRatesPromises);

const result = bins.map((bin, index) => {
const binsWithRates = bins.map((bin, index) => {
const rateForBin = binRates[index];
return {
...bin,
rates: rateForBin.status === 'fulfilled' ? rateForBin.value : {},
};
});

return result;
return binsWithRates;
};

export default async (
Expand Down
2 changes: 1 addition & 1 deletion src/types/response.ts
Expand Up @@ -69,5 +69,5 @@ export interface BalanceHistoryData {
received: string;
sent: string;
sentToSelf: string;
rates: { [k: string]: number | undefined };
rates?: { [k: string]: number | undefined };
}

0 comments on commit 2aed397

Please sign in to comment.