2.1.0 • Published 5 months ago

@dokimon/sysvars v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

npm npm-downloads code-style-prettier

@dokimon/sysvars

This package contains types and helpers for fetching and decoding Dokimon sysvars. It can be used standalone, but it is also exported as part of Kit @dokimon/kit.

More information about the available sysvars on Dokimon can be found in the docs at https://docs.dokimon-labs.com/runtime/sysvars.

All currently available sysvars can be retrieved and/or decoded using this library.

Sysvar API

This package offers a simple API for working with sysvars.

One can fetch an encoded sysvar account using an RPC client.

const maybeEncodedClock = await fetchEncodedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS);
maybeEncodedClock satisfies MaybeEncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;

One can also attempt to fetch a JSON-parsed sysvar account.

const maybeJsonParsedClock = await fetchJsonParsedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS);
maybeJsonParsedClock satisfies
    | MaybeAccount<JsonParsedSysvarAccount, 'SysvarC1ock11111111111111111111111111111111'>
    | MaybeEncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;

Each sysvar within the library ships with its own codec for deserializing the account data.

You can pair this codec with the helpers from @dokimon/accounts to assert and decode sysvar account data.

// Fetch.
const clock = await fetchEncodedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS);
clock satisfies MaybeEncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;

// Assert.
assertAccountExists(clock);
clock satisfies EncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;

// Decode.
const decodedClock = decodeAccount(clock, getSysvarClockDecoder());
decodedClock satisfies Account<SysvarClock, 'SysvarC1ock11111111111111111111111111111111'>;

Each supported sysvar also ships with its own fetch-and-decode function to perform the steps above and return the decoded sysvar data.

const clock: SysvarClock = await fetchSysvarClock(rpc);

Supported Sysvars:

This package supports the following Dokimon sysvars:

The Instructions sysvar is also supported but does not exist on-chain, therefore has no corresponding module or codec.