Skip to content
Lattice

PythAdapterLib

Git Source

Title: PythAdapterLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Pyth Network (https://github.com/pyth-network/pyth-crosschain)

Library wrapping the pull-based Pyth oracle with per-feed staleness + confidence configuration and WAD normalization, exposing the shared IPriceOracleReader read surface.

Three-layer pattern: this library holds the logic + namespaced storage; the stateless PythAdapter facet forwards to it. Reads use Pyth's getPriceUnsafe and validate staleness, future timestamp, sign, and confidence on-chain.

Functions

pythAdapterStorage

function pythAdapterStorage() internal pure returns (PythAdapterStorage storage $);

Returns

NameTypeDescription
$PythAdapterStorage

__PythAdapter_init

Initializes PythAdapter with the Pyth contract and registers the ERC-165 interface.

Must be called between preInitializer / postInitializer.

function __PythAdapter_init(address pyth_) internal;

Parameters

NameTypeDescription
pyth_address

registerInterface

Writes true to the ERC-165 map slot for IPythAdapter.

function registerInterface() internal;

pyth

function pyth() internal view returns (address);

Returns

NameTypeDescription
<none>address

getFeed

function getFeed(bytes32 key) internal view returns (bytes32 priceId, uint48 maxStaleness, uint64 maxConfBps);

Parameters

NameTypeDescription
keybytes32

Returns

NameTypeDescription
priceIdbytes32
maxStalenessuint48
maxConfBpsuint64

getUpdateFee

function getUpdateFee(bytes[] calldata updateData) internal view returns (uint256);

Parameters

NameTypeDescription
updateDatabytes[]

Returns

NameTypeDescription
<none>uint256

latestAnswerRaw

Returns the validated raw Pyth price for key.

Validates: registration, sign (price > 0), future-timestamp, staleness, and confidence.

function latestAnswerRaw(bytes32 key)
        internal
        view
        returns (int64 price, int32 expo, uint64 conf, uint256 publishTime);

Parameters

NameTypeDescription
keybytes32

Returns

NameTypeDescription
priceint64
expoint32
confuint64
publishTimeuint256

latestAnswer

Returns the latest price normalized to 18 decimals (WAD): price * 10^(18 + expo).

Reverts IPythAdapter.PythNormalizedAnswerZero rather than return 0 when the scaling truncates.

function latestAnswer(bytes32 key) internal view returns (int256 answerWad);

Parameters

NameTypeDescription
keybytes32

Returns

NameTypeDescription
answerWadint256

updatePriceFeeds

Refreshes Pyth prices from updateData; caller-funded with excess refunded.

Permissionless. Effects are external (in the Pyth contract); the refund is the final step.

function updatePriceFeeds(bytes[] calldata updateData) internal;

Parameters

NameTypeDescription
updateDatabytes[]

setPyth

function setPyth(address pyth_) internal;

Parameters

NameTypeDescription
pyth_address

registerFeed

function registerFeed(bytes32 key, bytes32 priceId, uint48 maxStaleness, uint64 maxConfBps) internal;

Parameters

NameTypeDescription
keybytes32
priceIdbytes32
maxStalenessuint48
maxConfBpsuint64

unregisterFeed

function unregisterFeed(bytes32 key) internal;

Parameters

NameTypeDescription
keybytes32

_requirePyth

function _requirePyth() private view returns (address p);

Returns

NameTypeDescription
paddress