Skip to content

API ReferencePricing and oraclesFunction

getUsdPrices()

function getUsdPrices(ctx, params): Promise<UsdPriceOutcome[]>;

Fetches USD display prices for a set of ERC-20 addresses and/or direct Pyth feed entries in one batched Hermes read.

Addresses, resolved feed IDs, and feed entries’ feed IDs are all deduplicated for the request, while the result keeps caller order and duplicates. Resolving an address entry’s feed can still cost one Hermes symbol lookup per token that is absent from the on-chain PythPriceOracle; a UsdPriceFeedEntry skips that lookup entirely. An entry’s stale, unsupported, or malformed state is represented in its own outcome and never rejects its neighbors; aside from argument validation, which rejects before any read, only a failure of the Hermes request itself rejects, so callers keep their last good prices instead of receiving a batch of blanks.

Parameters

ParameterTypeDescription
ctxSDKContextThe SDK context.
paramsGetUsdPricesParamsThe token addresses and/or feed entries to price.

Returns

Promise<UsdPriceOutcome[]>

One discriminated UsdPriceOutcome per supplied entry, in the supplied order.

Throws

PriceServerError when the batched Hermes request itself fails; the underlying error is preserved as cause.

Throws

InvalidArgumentError when params.tokens is not an array, or a feed entry’s decimals is not an integer.

Examples

const outcomes = await sdk.pricing.getUsdPrices({
tokens: [WETH, USDC],
});
for (const outcome of outcomes) {
// Narrow on `kind` before reading identity: an address entry's outcome
// carries `token: Address`, a feed entry's carries `key` instead (see
// the feed-entry example below).
if (outcome.kind !== 'address') continue;
if (outcome.status === 'fresh' || outcome.status === 'stale') {
console.log(outcome.token, outcome.status, outcome.price);
} else {
console.warn(outcome.token, outcome.reason);
}
}
const outcomes = await sdk.pricing.getUsdPrices({
tokens: [WETH, { feedId: ETH_USD_FEED_ID, decimals: 18, key: 'ETH' }],
});
for (const outcome of outcomes) {
// A mixed request returns a mix of kinds: narrow on `kind` before
// reading identity. Address entries carry `token: Address`; feed
// entries carry `key` (here, 'ETH') and have no `token` field.
const id = outcome.kind === 'address' ? outcome.token : outcome.key;
if (outcome.status === 'fresh' || outcome.status === 'stale') {
console.log(id, outcome.status, outcome.price);
} else {
console.warn(id, outcome.reason);
}
}