@thru/programs 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,65 @@
1
+ import type { Account } from "@thru/sdk";
2
+ import {
3
+ OracleFeedAccount,
4
+ type OracleBooleanFeedData,
5
+ type OraclePriceFeedData,
6
+ } from "./abi/thru/program/oracle/types";
7
+ import { ORACLE_FEED_TYPE_BOOLEAN, ORACLE_FEED_TYPE_PRICE } from "./constants";
8
+ import { decodeCommonFeedData } from "./decode";
9
+ import type { OracleBooleanFeed, OracleFeed, OraclePriceFeed } from "./types";
10
+
11
+ const PRICE_FEED_ACCOUNT_LENGTH = 161;
12
+ const BOOLEAN_FEED_ACCOUNT_LENGTH = 146;
13
+
14
+ export function parseOracleFeedAccount(
15
+ account: Account | Uint8Array,
16
+ ): OracleFeed {
17
+ const data = account instanceof Uint8Array ? account : account.data?.data;
18
+ if (!data) throw new Error("Oracle feed account data is missing");
19
+
20
+ const feedType = data[0];
21
+ const expectedLength =
22
+ feedType === ORACLE_FEED_TYPE_PRICE
23
+ ? PRICE_FEED_ACCOUNT_LENGTH
24
+ : feedType === ORACLE_FEED_TYPE_BOOLEAN
25
+ ? BOOLEAN_FEED_ACCOUNT_LENGTH
26
+ : null;
27
+ if (expectedLength === null)
28
+ throw new Error(`Unknown Oracle feed type: ${feedType}`);
29
+ if (data.length !== expectedLength) {
30
+ throw new Error(
31
+ `Malformed Oracle feed account: expected ${expectedLength} bytes, got ${data.length}`,
32
+ );
33
+ }
34
+
35
+ const feed = OracleFeedAccount.from_array(data);
36
+ if (!feed) throw new Error("Malformed Oracle feed account");
37
+
38
+ if (feedType === ORACLE_FEED_TYPE_PRICE)
39
+ return parsePriceFeed(feed.data().asPrice());
40
+ return parseBooleanFeed(feed.data().asBoolean());
41
+ }
42
+
43
+ function parsePriceFeed(data: OraclePriceFeedData | null): OraclePriceFeed {
44
+ if (!data) throw new Error("Malformed Oracle price feed account");
45
+ return {
46
+ kind: "price",
47
+ common: decodeCommonFeedData(data.get_common()),
48
+ price: data.get_price(),
49
+ maxVarianceBps: data.get_max_variance_bps(),
50
+ exponent: data.get_exponent(),
51
+ };
52
+ }
53
+
54
+ function parseBooleanFeed(
55
+ data: OracleBooleanFeedData | null,
56
+ ): OracleBooleanFeed {
57
+ if (!data) throw new Error("Malformed Oracle boolean feed account");
58
+ const value = data.get_value();
59
+ if (value > 1) throw new Error(`Invalid Oracle boolean value: ${value}`);
60
+ return {
61
+ kind: "boolean",
62
+ common: decodeCommonFeedData(data.get_common()),
63
+ value: value === 1,
64
+ };
65
+ }
@@ -0,0 +1,7 @@
1
+ export const ORACLE_FEED_TYPE_PRICE = 1;
2
+ export const ORACLE_FEED_TYPE_BOOLEAN = 2;
3
+
4
+ export const ORACLE_EVENT_TYPE_PRICE_UPDATE = 1;
5
+ export const ORACLE_EVENT_TYPE_BOOLEAN_UPDATE = 2;
6
+
7
+ export const ORACLE_FEED_SEED_LENGTH = 32;
@@ -0,0 +1,32 @@
1
+ import { encodeAddress } from "@thru/sdk/helpers";
2
+ import type {
3
+ FeedName64,
4
+ OracleFeedCommonData,
5
+ } from "./abi/thru/program/oracle/types";
6
+ import type { OracleFeedCommon } from "./types";
7
+
8
+ const TEXT_DECODER = new TextDecoder("utf-8", { fatal: true });
9
+
10
+ export function decodeFeedName(name: FeedName64): string {
11
+ const bytes = name.asUint8Array();
12
+ const terminator = bytes.indexOf(0);
13
+ return TEXT_DECODER.decode(
14
+ terminator === -1 ? bytes : bytes.subarray(0, terminator),
15
+ );
16
+ }
17
+
18
+ export function decodeCommonFeedData(
19
+ common: OracleFeedCommonData,
20
+ ): OracleFeedCommon {
21
+ return {
22
+ maxStalenessNs: common.get_max_staleness_ns(),
23
+ lastUpdateNs: common.get_last_update_ns(),
24
+ adminAddress: encodeAddress(
25
+ Uint8Array.from(common.get_admin_address().get_bytes()),
26
+ ),
27
+ reporterAddress: encodeAddress(
28
+ Uint8Array.from(common.get_reporter_address().get_bytes()),
29
+ ),
30
+ feedName: decodeFeedName(common.get_feed_name()),
31
+ };
32
+ }
@@ -0,0 +1,25 @@
1
+ import type { Thru } from "@thru/sdk/client";
2
+ import { ORACLE_FEED_SEED_LENGTH } from "./constants";
3
+
4
+ const TEXT_ENCODER = new TextEncoder();
5
+
6
+ export function normalizeOracleFeedSeed(seed: string | Uint8Array): Uint8Array {
7
+ const source = typeof seed === "string" ? TEXT_ENCODER.encode(seed) : seed;
8
+ const normalized = new Uint8Array(ORACLE_FEED_SEED_LENGTH);
9
+ normalized.set(source.subarray(0, ORACLE_FEED_SEED_LENGTH));
10
+ return normalized;
11
+ }
12
+
13
+ export function deriveOracleFeedAddress(
14
+ thru: Thru,
15
+ oracleProgramAddress: string,
16
+ seed: string | Uint8Array,
17
+ ): { address: string; bytes: Uint8Array; seed: Uint8Array } {
18
+ const normalizedSeed = normalizeOracleFeedSeed(seed);
19
+ const address = thru.helpers.deriveProgramAddress({
20
+ programAddress: oracleProgramAddress,
21
+ seed: normalizedSeed,
22
+ ephemeral: false,
23
+ });
24
+ return { ...address, seed: normalizedSeed };
25
+ }
@@ -0,0 +1,23 @@
1
+ export enum OracleProgramError {
2
+ InvalidInstructionDataSize = 1,
3
+ InvalidInstruction = 2,
4
+ UnauthorizedOperation = 3,
5
+ FeedAlreadyExists = 4,
6
+ InvalidFeedAccount = 5,
7
+ InvalidAccountIndex = 6,
8
+ InvalidFeedNameLength = 7,
9
+ InvalidFeedType = 8,
10
+ UpdateTooStale = 9,
11
+ FeedTypeMismatch = 10,
12
+ InvalidBooleanValue = 11,
13
+ }
14
+
15
+ export function oracleProgramErrorFromCode(
16
+ code: number | bigint,
17
+ ): OracleProgramError | null {
18
+ const value = typeof code === "bigint" ? Number(code) : code;
19
+ return value >= OracleProgramError.InvalidInstructionDataSize &&
20
+ value <= OracleProgramError.InvalidBooleanValue
21
+ ? (value as OracleProgramError)
22
+ : null;
23
+ }
@@ -0,0 +1,81 @@
1
+ import { encodeAddress } from "@thru/sdk/helpers";
2
+ import {
3
+ OracleEvent as AbiOracleEvent,
4
+ type BooleanUpdateEventData,
5
+ type PriceUpdateEventData,
6
+ } from "./abi/thru/program/oracle/types";
7
+ import {
8
+ ORACLE_EVENT_TYPE_BOOLEAN_UPDATE,
9
+ ORACLE_EVENT_TYPE_PRICE_UPDATE,
10
+ } from "./constants";
11
+ import { decodeFeedName } from "./decode";
12
+ import type {
13
+ OracleBooleanUpdateEvent,
14
+ OracleEvent,
15
+ OraclePriceUpdateEvent,
16
+ } from "./types";
17
+
18
+ const PRICE_UPDATE_EVENT_LENGTH = 121;
19
+ const BOOLEAN_UPDATE_EVENT_LENGTH = 107;
20
+
21
+ export function parseOracleEvent(data: Uint8Array): OracleEvent {
22
+ const eventType = data[0];
23
+ const expectedLength =
24
+ eventType === ORACLE_EVENT_TYPE_PRICE_UPDATE
25
+ ? PRICE_UPDATE_EVENT_LENGTH
26
+ : eventType === ORACLE_EVENT_TYPE_BOOLEAN_UPDATE
27
+ ? BOOLEAN_UPDATE_EVENT_LENGTH
28
+ : null;
29
+ if (expectedLength === null)
30
+ throw new Error(`Unknown Oracle event type: ${eventType}`);
31
+ if (data.length !== expectedLength) {
32
+ throw new Error(
33
+ `Malformed Oracle event: expected ${expectedLength} bytes, got ${data.length}`,
34
+ );
35
+ }
36
+
37
+ const event = AbiOracleEvent.from_array(data);
38
+ if (!event) throw new Error("Malformed Oracle event");
39
+
40
+ if (eventType === ORACLE_EVENT_TYPE_PRICE_UPDATE)
41
+ return parsePriceEvent(event.data().asPriceUpdate());
42
+ return parseBooleanEvent(event.data().asBooleanUpdate());
43
+ }
44
+
45
+ function parsePriceEvent(
46
+ data: PriceUpdateEventData | null,
47
+ ): OraclePriceUpdateEvent {
48
+ if (!data) throw new Error("Malformed Oracle price update event");
49
+ return {
50
+ kind: "priceUpdate",
51
+ feedName: decodeFeedName(data.get_feed_name()),
52
+ feedAddress: encodeAddress(
53
+ Uint8Array.from(data.get_feed_address().get_bytes()),
54
+ ),
55
+ oldPrice: data.get_old_price(),
56
+ newPrice: data.get_new_price(),
57
+ timestampNs: data.get_timestamp_ns(),
58
+ };
59
+ }
60
+
61
+ function parseBooleanEvent(
62
+ data: BooleanUpdateEventData | null,
63
+ ): OracleBooleanUpdateEvent {
64
+ if (!data) throw new Error("Malformed Oracle boolean update event");
65
+ const oldValue = data.get_old_value();
66
+ const newValue = data.get_new_value();
67
+ if (oldValue > 1)
68
+ throw new Error(`Invalid Oracle boolean value: ${oldValue}`);
69
+ if (newValue > 1)
70
+ throw new Error(`Invalid Oracle boolean value: ${newValue}`);
71
+ return {
72
+ kind: "booleanUpdate",
73
+ feedName: decodeFeedName(data.get_feed_name()),
74
+ feedAddress: encodeAddress(
75
+ Uint8Array.from(data.get_feed_address().get_bytes()),
76
+ ),
77
+ oldValue: oldValue === 1,
78
+ newValue: newValue === 1,
79
+ timestampNs: data.get_timestamp_ns(),
80
+ };
81
+ }
@@ -0,0 +1,81 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { describe, expect, it } from "vitest";
3
+ import {
4
+ normalizeOracleFeedSeed,
5
+ OracleProgramError,
6
+ oracleProgramErrorFromCode,
7
+ parseOracleEvent,
8
+ parseOracleFeedAccount,
9
+ } from "./index";
10
+
11
+ function fixture(name: string): Uint8Array {
12
+ const url = new URL(
13
+ `../../../../../rpc/thru-oracle-sdk/tests/fixtures/${name}.hex`,
14
+ import.meta.url,
15
+ );
16
+ return Uint8Array.from(Buffer.from(readFileSync(url, "utf8").trim(), "hex"));
17
+ }
18
+
19
+ describe("Oracle read helpers", () => {
20
+ it("decodes shared price and boolean feed fixtures", () => {
21
+ const price = parseOracleFeedAccount(fixture("price_feed"));
22
+ expect(price.kind).toBe("price");
23
+ if (price.kind !== "price") throw new Error("expected price feed");
24
+ expect(price.common.feedName).toBe("BTC/USD");
25
+ expect(price.price).toBe(42_000n);
26
+ expect(price.maxVarianceBps).toBe(500);
27
+ expect(price.exponent).toBe(-8);
28
+
29
+ const boolean = parseOracleFeedAccount(fixture("boolean_feed"));
30
+ expect(boolean.kind).toBe("boolean");
31
+ if (boolean.kind !== "boolean") throw new Error("expected boolean feed");
32
+ expect(boolean.value).toBe(true);
33
+ });
34
+
35
+ it("decodes shared price and boolean event fixtures", () => {
36
+ const price = parseOracleEvent(fixture("price_event"));
37
+ expect(price.kind).toBe("priceUpdate");
38
+ if (price.kind !== "priceUpdate") throw new Error("expected price update");
39
+ expect(price.oldPrice).toBe(41_000n);
40
+ expect(price.newPrice).toBe(42_000n);
41
+
42
+ const boolean = parseOracleEvent(fixture("boolean_event"));
43
+ expect(boolean.kind).toBe("booleanUpdate");
44
+ if (boolean.kind !== "booleanUpdate")
45
+ throw new Error("expected boolean update");
46
+ expect(boolean.oldValue).toBe(false);
47
+ expect(boolean.newValue).toBe(true);
48
+ });
49
+
50
+ it("rejects malformed tags, lengths, and boolean values", () => {
51
+ expect(() => parseOracleFeedAccount(Uint8Array.of(99))).toThrow(
52
+ "Unknown Oracle feed type",
53
+ );
54
+ expect(() => parseOracleEvent(Uint8Array.of(99))).toThrow(
55
+ "Unknown Oracle event type",
56
+ );
57
+ expect(() =>
58
+ parseOracleFeedAccount(fixture("price_feed").subarray(0, 160)),
59
+ ).toThrow("expected 161 bytes");
60
+
61
+ const invalidBoolean = fixture("boolean_feed");
62
+ invalidBoolean[145] = 2;
63
+ expect(() => parseOracleFeedAccount(invalidBoolean)).toThrow(
64
+ "Invalid Oracle boolean value",
65
+ );
66
+ });
67
+
68
+ it("normalizes seeds and maps program errors", () => {
69
+ const seed = normalizeOracleFeedSeed(
70
+ "abcdefghijklmnopqrstuvwxyz0123456789",
71
+ );
72
+ expect(seed).toHaveLength(32);
73
+ expect(new TextDecoder().decode(seed)).toBe(
74
+ "abcdefghijklmnopqrstuvwxyz012345",
75
+ );
76
+ expect(oracleProgramErrorFromCode(3n)).toBe(
77
+ OracleProgramError.UnauthorizedOperation,
78
+ );
79
+ expect(oracleProgramErrorFromCode(99)).toBeNull();
80
+ });
81
+ });
@@ -0,0 +1,22 @@
1
+ export { parseOracleFeedAccount } from "./accounts";
2
+ export { deriveOracleFeedAddress, normalizeOracleFeedSeed } from "./derivation";
3
+ export { oracleProgramErrorFromCode, OracleProgramError } from "./errors";
4
+ export { parseOracleEvent } from "./events";
5
+
6
+ export {
7
+ ORACLE_EVENT_TYPE_BOOLEAN_UPDATE,
8
+ ORACLE_EVENT_TYPE_PRICE_UPDATE,
9
+ ORACLE_FEED_SEED_LENGTH,
10
+ ORACLE_FEED_TYPE_BOOLEAN,
11
+ ORACLE_FEED_TYPE_PRICE,
12
+ } from "./constants";
13
+
14
+ export type {
15
+ OracleBooleanFeed,
16
+ OracleBooleanUpdateEvent,
17
+ OracleEvent,
18
+ OracleFeed,
19
+ OracleFeedCommon,
20
+ OraclePriceFeed,
21
+ OraclePriceUpdateEvent,
22
+ } from "./types";
@@ -0,0 +1,43 @@
1
+ export interface OracleFeedCommon {
2
+ maxStalenessNs: bigint;
3
+ lastUpdateNs: bigint;
4
+ adminAddress: string;
5
+ reporterAddress: string;
6
+ feedName: string;
7
+ }
8
+
9
+ export interface OraclePriceFeed {
10
+ kind: "price";
11
+ common: OracleFeedCommon;
12
+ price: bigint;
13
+ maxVarianceBps: number;
14
+ exponent: number;
15
+ }
16
+
17
+ export interface OracleBooleanFeed {
18
+ kind: "boolean";
19
+ common: OracleFeedCommon;
20
+ value: boolean;
21
+ }
22
+
23
+ export type OracleFeed = OraclePriceFeed | OracleBooleanFeed;
24
+
25
+ export interface OraclePriceUpdateEvent {
26
+ kind: "priceUpdate";
27
+ feedName: string;
28
+ feedAddress: string;
29
+ oldPrice: bigint;
30
+ newPrice: bigint;
31
+ timestampNs: bigint;
32
+ }
33
+
34
+ export interface OracleBooleanUpdateEvent {
35
+ kind: "booleanUpdate";
36
+ feedName: string;
37
+ feedAddress: string;
38
+ oldValue: boolean;
39
+ newValue: boolean;
40
+ timestampNs: bigint;
41
+ }
42
+
43
+ export type OracleEvent = OraclePriceUpdateEvent | OracleBooleanUpdateEvent;
package/tsup.config.ts CHANGED
@@ -1,14 +1,15 @@
1
- import { defineConfig } from 'tsup';
1
+ import { defineConfig } from "tsup";
2
2
 
3
3
  export default defineConfig({
4
4
  entry: {
5
- 'token/index': 'src/token/index.ts',
6
- 'passkey-manager/index': 'src/passkey-manager/index.ts',
7
- 'multicall/index': 'src/multicall/index.ts',
8
- 'amm/index': 'src/amm/index.ts',
9
- 'clob/index': 'src/clob/index.ts',
5
+ "token/index": "src/token/index.ts",
6
+ "passkey-manager/index": "src/passkey-manager/index.ts",
7
+ "multicall/index": "src/multicall/index.ts",
8
+ "amm/index": "src/amm/index.ts",
9
+ "clob/index": "src/clob/index.ts",
10
+ "oracle/index": "src/oracle/index.ts",
10
11
  },
11
- format: ['cjs', 'esm'],
12
+ format: ["cjs", "esm"],
12
13
  dts: true,
13
14
  clean: true,
14
15
  sourcemap: true,