Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 43x 43x 43x | import {
BlockfrostClient,
DEFAULT_BLOCKFROST_API_VERSION,
DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG,
DEFAULT_BLOCKFROST_URLS
} from '@cardano-sdk/cardano-services-client';
import Bottleneck from 'bottleneck';
let blockfrostClient: BlockfrostClient | undefined;
/**
* Gets the singleton blockfrost API instance.
*
* @returns The blockfrost API instance, this function always returns the same instance.
*/
export const getBlockfrostClient = (): BlockfrostClient => {
Iif (blockfrostClient !== undefined) return blockfrostClient;
// custom hosted instance
Iif (process.env.BLOCKFROST_CUSTOM_BACKEND_URL) {
blockfrostClient = new BlockfrostClient(
{ apiVersion: DEFAULT_BLOCKFROST_API_VERSION, baseUrl: process.env.BLOCKFROST_CUSTOM_BACKEND_URL },
{ rateLimiter: { schedule: (task) => task() } }
);
return blockfrostClient;
}
// instance hosted by Blockfrost
Iif (!process.env.BLOCKFROST_API_KEY)
throw new Error('BLOCKFROST_API_KEY or BLOCKFROST_CUSTOM_BACKEND_URL environment variable is required');
Iif (!process.env.NETWORK) throw new Error('NETWORK environment variable is required');
const baseUrl = DEFAULT_BLOCKFROST_URLS[process.env.NETWORK as keyof typeof DEFAULT_BLOCKFROST_URLS];
Iif (!baseUrl) throw new Error(`Unsupported NETWORK for blockfrost: ${process.env.NETWORK}`);
// network is not mandatory, we keep it for safety.
blockfrostClient = new BlockfrostClient(
{ apiVersion: DEFAULT_BLOCKFROST_API_VERSION, baseUrl, projectId: process.env.BLOCKFROST_API_KEY },
{
rateLimiter: new Bottleneck({
reservoir: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.size,
reservoirIncreaseAmount: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.increaseAmount,
reservoirIncreaseInterval: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.increaseInterval,
reservoirIncreaseMaximum: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.size
})
}
);
return blockfrostClient;
};
|