Skip to content

Commit 60d1190

Browse files
committed
fixup! feat: browser compatible BlockfrostAssetProvider
1 parent b630d7f commit 60d1190

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

packages/cardano-services-client/src/blockfrost/BlockfrostClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export type RateLimiter = {
1313
};
1414

1515
export type BlockfrostClientDependencies = {
16+
/**
17+
* Rate limiter from npm: https://www.npmjs.com/package/bottleneck
18+
*
19+
* new Bottleneck({
20+
* reservoir: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.size,
21+
* reservoirIncreaseAmount: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.increaseAmount,
22+
* reservoirIncreaseInterval: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.increaseInterval,
23+
* reservoirIncreaseMaximum: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.size
24+
* })
25+
*/
1626
rateLimiter: RateLimiter;
1727
};
1828

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Milliseconds } from '@cardano-sdk/core';
2+
3+
export const DEFAULT_BLOCKFROST_API_VERSION = 'v0';
4+
5+
export const DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG = {
6+
increaseAmount: 10,
7+
increaseInterval: Milliseconds(1000),
8+
size: 500
9+
};
10+
11+
export const DEFAULT_BLOCKFROST_URLS = {
12+
mainnet: 'https://cardano-mainnet.blockfrost.io',
13+
preprod: 'https://cardano-preprod.blockfrost.io',
14+
preview: 'https://cardano-preview.blockfrost.io',
15+
sanchonet: 'https://cardano-sanchonet.blockfrost.io'
16+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './BlockfrostProvider';
22
export * from './BlockfrostClient';
3+
export * from './const';

packages/cardano-services-client/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ export * from './RewardsProvider';
99
export * from './HandleProvider';
1010
export * from './version';
1111
export * from './WebSocket';
12-
export { BlockfrostClient, BlockfrostError } from './blockfrost';
12+
export {
13+
BlockfrostClient,
14+
BlockfrostError,
15+
DEFAULT_BLOCKFROST_API_VERSION,
16+
DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG,
17+
DEFAULT_BLOCKFROST_URLS
18+
} from './blockfrost';
1319
export type { BlockfrostClientConfig, BlockfrostClientDependencies, RateLimiter } from './blockfrost';

packages/cardano-services/src/util/BlockfrostProvider/BlockfrostClientFactory.ts

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
import { AvailableNetworks } from '../../Program';
22
import { BlockFrostAPI } from '@blockfrost/blockfrost-js';
3-
import { BlockfrostClient } from '@cardano-sdk/cardano-services-client';
4-
import { Milliseconds } from '@cardano-sdk/core';
3+
import {
4+
BlockfrostClient,
5+
DEFAULT_BLOCKFROST_API_VERSION,
6+
DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG,
7+
DEFAULT_BLOCKFROST_URLS
8+
} from '@cardano-sdk/cardano-services-client';
59
import Bottleneck from 'bottleneck';
610

711
let blockfrostApi: BlockFrostAPI | undefined;
812
let blockfrostClient: BlockfrostClient | undefined;
913

10-
const DEFAULT_API_VERSION = 'v0';
11-
12-
const DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG = {
13-
increaseAmount: 10,
14-
increaseInterval: Milliseconds(1000),
15-
size: 500
16-
};
17-
18-
const DEFAULT_BLOCKFROST_URLS = {
19-
mainnet: 'https://cardano-mainnet.blockfrost.io',
20-
preprod: 'https://cardano-preprod.blockfrost.io',
21-
preview: 'https://cardano-preview.blockfrost.io',
22-
sanchonet: 'https://cardano-sanchonet.blockfrost.io'
23-
};
24-
2514
/**
2615
* Gets the singleton blockfrost API instance.
2716
*
@@ -67,7 +56,7 @@ export const getBlockfrostClient = (): BlockfrostClient => {
6756
// custom hosted instance
6857
if (process.env.BLOCKFROST_CUSTOM_BACKEND_URL) {
6958
blockfrostClient = new BlockfrostClient(
70-
{ apiVersion: DEFAULT_API_VERSION, baseUrl: process.env.BLOCKFROST_CUSTOM_BACKEND_URL },
59+
{ apiVersion: DEFAULT_BLOCKFROST_API_VERSION, baseUrl: process.env.BLOCKFROST_CUSTOM_BACKEND_URL },
7160
{ rateLimiter: { schedule: (task) => task() } }
7261
);
7362

@@ -84,7 +73,7 @@ export const getBlockfrostClient = (): BlockfrostClient => {
8473

8574
// network is not mandatory, we keep it for safety.
8675
blockfrostClient = new BlockfrostClient(
87-
{ apiVersion: DEFAULT_API_VERSION, baseUrl, projectId: process.env.BLOCKFROST_API_KEY },
76+
{ apiVersion: DEFAULT_BLOCKFROST_API_VERSION, baseUrl, projectId: process.env.BLOCKFROST_API_KEY },
8877
{
8978
rateLimiter: new Bottleneck({
9079
reservoir: DEFAULT_BLOCKFROST_RATE_LIMIT_CONFIG.size,

0 commit comments

Comments
 (0)