Skip to content

API ReferencePricing and oraclesInterface

PricingModule

Pricing operations: oracle prices, price feed IDs, and update fees.

Reached as sdk.pricing on a constructed AnvilSDK instance, which creates its own modules — consumers never call new PricingModule(...). The class is exported type-only so its methods appear in the API reference and so integrators can name the type without reaching into package internals.

Methods

getMaxPriceUpdateSecondsAgo()

getMaxPriceUpdateSecondsAgo(): Promise<number>;

Returns how many seconds old an oracle price update may be and still be accepted by the LetterOfCredit contract.

The value includes a UX buffer, so it is deliberately shorter than the contract’s own window and will not match it exactly. It is the single place that buffer is applied — treat the result as the effective freshness window rather than subtracting a margin again.

Returns

Promise<number>

The effective freshness window, in seconds.


getOraclePriceUpdate()

getOraclePriceUpdate(params): Promise<OraclePriceUpdate>;

Returns the Pyth oracle price update that must accompany any LetterOfCredit call whose outcome depends on a price — creating or modifying a dynamic LOC, most notably.

The result carries the fee Pyth charges to process the update on chain. That fee must be passed as the transaction’s value, so treat the update and its fee as a unit. Fetch it immediately before sending: an update goes stale within PricingModule.getMaxPriceUpdateSecondsAgo seconds.

Parameters

ParameterTypeDescription
paramsGetOraclePriceUpdateParamsThe token pair the update must cover.

Returns

Promise<OraclePriceUpdate>

The price update bytes and the fee required to submit them.

Throws

StalePythPriceError If Hermes returns a price outside oracleConfig.maxPriceAgeSeconds.


getOraclePriceUpdateFee()

getOraclePriceUpdateFee(params): Promise<bigint>;

Returns the fee Pyth charges to submit the given price update bytes on chain. PricingModule.getOraclePriceUpdate already reports this for the update it returns; call this only when you hold update bytes from elsewhere.

Parameters

ParameterTypeDescription
paramsGetOraclePriceUpdateFeeParamsThe price update bytes to be submitted.

Returns

Promise<bigint>

The fee, in wei, to send as the transaction value.


getPrice()

getPrice(params): Promise<Price>;

Returns the current Pyth exchange rate from inputToken to outputToken as a Price in smallest units of output per smallest unit of input, computed with the same integer math the on-chain PythPriceOracle uses. Both tokens need a Pyth feed.

Because both tokens’ decimals are already folded into the result, it can be handed to contract math as-is; rescale only to display a human-readable rate.

Parameters

ParameterTypeDescription
paramsGetPriceParamsThe token being priced and the token to price it in.

Returns

Promise<Price>

The output-per-unit-input price between the two tokens.

Example

const { price, exponent } = await sdk.pricing.getPrice({
inputToken: WETH,
outputToken: USDC,
});
// price * 10**exponent = USDC smallest units per 1 wei; both tokens'
// decimals are already folded in, so on-chain math uses it as-is. Rescale
// by (18 - 6) only to show USDC per whole WETH.
const usdcPerWholeWeth =
Number(price) * 10 ** (Number(exponent) + 18 - 6);

Throws

StalePythPriceError If Hermes returns a price outside oracleConfig.maxPriceAgeSeconds. This unsafe-data failure does not use the on-chain fallback reserved for PriceServerError.


getPriceFeedId()

getPriceFeedId(params): Promise<string>;

Returns the Pyth price feed ID for a token. The on-chain PythPriceOracle is consulted first, falling back to the Pyth Hermes API when the oracle does not know the token.

Most integrations never need this directly — PricingModule.getPrice and PricingModule.getOraclePriceUpdate resolve feed IDs themselves. Reach for it when you are driving Pyth yourself.

Parameters

ParameterTypeDescription
paramsGetPriceFeedIdParamsThe token to look up.

Returns

Promise<string>

The Pyth price feed ID.


getTokenPairPrice()

getTokenPairPrice(params): Promise<TokenPairPrice>;

Reads the token-pair price with the actual source used, including a permitted chain fallback.

Parameters

ParameterTypeDescription
paramsGetPriceParamsInput and output tokens in the required price direction.

Returns

Promise<TokenPairPrice>

Price in smallest units together with pair identity and source.

Throws

StalePythPriceError If the selected price fails freshness validation.


getUsdPrice()

getUsdPrice(params): Promise<Price>;

Returns the USD price of a token, expressed as cents per most granular unit of the token. This is not how a price is usually displayed — scale by the token’s decimals before showing it to a person.

Parameters

ParameterTypeDescription
paramsGetUsdPriceParamsThe token being priced.

Returns

Promise<Price>

The USD price of one smallest unit, in cents.

Throws

StalePythPriceError If Hermes returns a price outside oracleConfig.maxPriceAgeSeconds.


getUsdPrices()

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

Returns best-effort USD display prices for a set of ERC-20 addresses and/or direct Pyth feed entries. After resolving address entries’ feed IDs (which may cost one Hermes symbol lookup per token absent from the on-chain PythPriceOracle), the SDK batches the normalized set — address entries and feed entries together — into one Hermes latest-price request, while outcomes retain caller order and identify stale or unavailable entries individually. Only a failure of that one request rejects, so callers keep their last good prices. A feed entry (for a value with no ERC-20 address, such as native ETH) is priced directly by feed ID with no token resolution.

Parameters

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

Returns

Promise<UsdPriceOutcome[]>

One discriminated outcome per supplied entry.

Throws

PriceServerError when the batched Hermes request itself fails.

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);
}
}