Skip to main content

@radix-effects/gateway

@radix-effects/gateway wraps the Radix Babylon Gateway SDK in Effect services. It provides typed errors, retry behavior for rate limits, Gateway state helpers, transaction preview services, ROLA verification, and SBOR schemas.

Install

npm install @radix-effects/gateway effect

Configuration

GatewayApiClient.Default reads Effect config values:

ConfigDefaultDescription
NETWORK_ID1Radix network id.
GATEWAY_URLGateway SDK defaultOptional Gateway API base URL.
APPLICATION_NAME@radix-effects/gatewayApplication name sent to Gateway.
GATEWAY_BASIC_AUTHunsetOptional Basic auth value.

Some higher-level services also read package-specific config, such as GATEWAY_GET_FUNGIBLE_BALANCE_CONCURRENCY for batched balance reads.

Basic Effect Usage

import { Effect } from 'effect';
import {
GatewayApiClient,
StateEntityDetails,
} from '@radix-effects/gateway';

const program = Effect.gen(function* () {
const stateEntityDetails = yield* StateEntityDetails;

return yield* stateEntityDetails({
addresses: ['account_rdx...'],
aggregation_level: 'Global',
});
});

const result = await Effect.runPromise(
program.pipe(Effect.provide(StateEntityDetails.Default)),
);

Services that depend on GatewayApiClient provide it through their default dependency list, or you can provide GatewayApiClient.Default yourself in a larger layer graph.

Gateway API Client

GatewayApiClient exposes wrapped SDK areas:

  • state for state entity details, fungible pages, non-fungible pages, validators, and key-value stores.
  • transaction for transaction status and preview calls.
  • stream for stream transaction calls.
  • extensions for resource holder endpoints.
  • rawClient when a downstream SDK expects the original Gateway client.

Gateway SDK failures are converted into tagged Effect errors such as EntityNotFoundError, InvalidRequestError, InvalidTransactionError, TransactionNotFoundError, RateLimitExceededError, and UnknownGatewayError. Rate-limit responses are retried after the returned retry-after delay.

State Helpers

ServicePurpose
StateEntityDetailsCalls stateEntityDetails with typed ledger-state validation.
GetEntityDetailsVaultAggregatedReads entity details with vault aggregation.
EntityFungiblesPageFollows entity fungible pagination.
EntityNonFungiblesPageFollows entity non-fungible pagination.
EntityNonFungibleIdsPageReads paginated non-fungible ids for a resource.
NonFungibleDataReads non-fungible data for ids.
GetValidatorsReads current validator information.
GetLedgerStateServiceReads a ledger state from the stream API.

Balance And Resource Helpers

ServicePurpose
GetFungibleBalanceReturns positive fungible balances for one or more entities, using BigNumber amounts.
GetNonFungibleBalanceServiceReturns non-fungible balances for one or more entities.
GetResourceHoldersServiceReads and deduplicates all holders for a resource.
GetNftResourceManagersServiceLocates NFT resource managers.
GetAddressByNonFungibleServiceFinds an address by non-fungible id.
GetNonFungibleLocationServiceLocates non-fungibles on ledger.

Example:

import { Effect } from 'effect';
import { GetFungibleBalance } from '@radix-effects/gateway';

const balances = await Effect.runPromise(
Effect.gen(function* () {
const getFungibleBalance = yield* GetFungibleBalance;

return yield* getFungibleBalance({
addresses: ['account_rdx...'],
options: {
explicit_metadata: ['name', 'symbol'],
},
});
}).pipe(Effect.provide(GetFungibleBalance.Default)),
);

Key-Value Store Helpers

Use KeyValueStoreKeysService, KeyValueStoreDataService, and GetKeyValueStoreService for Gateway KVS pages and value reads. These helpers are useful when a component stores application state in Radix key-value stores.

Transaction Preview

import { Effect } from 'effect';
import { PreviewTransactionV2 } from '@radix-effects/gateway';

const result = await Effect.runPromise(
Effect.gen(function* () {
const preview = yield* PreviewTransactionV2;
return yield* preview({ payload: transactionPreviewV2Request });
}).pipe(Effect.provide(PreviewTransactionV2.Default)),
);

PreviewTransaction handles V1 preview requests and PreviewTransactionV2 handles V2 preview requests. Both fail with TransactionPreviewError when the receipt exists and is not successful.

ROLA Verification

The Rola service verifies account or persona proofs:

import { Effect, ConfigProvider, Layer } from 'effect';
import { Rola } from '@radix-effects/gateway';

const config = Layer.setConfigProvider(
ConfigProvider.fromJson({
NETWORK_ID: '1',
DAPP_DEFINITION_ADDRESS: 'account_rdx...',
ROLA_EXPECTED_ORIGIN: 'https://your-dapp.example',
}),
);

await Effect.runPromise(
Effect.gen(function* () {
const rola = yield* Rola;
yield* rola.verifySignedChallenge(proof);
}).pipe(Effect.provide(Rola.Default), Effect.provide(config)),
);

The proof must match either AccountProofSchema or PersonaProofSchema.