@vercora-protocol/sdk 0.2.2 → 0.2.4

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,70 @@
1
+ import type { Program } from '@coral-xyz/anchor';
2
+ import BN from 'bn.js';
3
+ import { PublicKey } from '@solana/web3.js';
4
+ /** Accepts any Anchor `Program` shape (e.g. `Program<Vercora>`) without fighting IDL generics. */
5
+ type AnyProgram = Program<any>;
6
+ /**
7
+ * Anchor `accounts[].discriminator` for `Resolver` from the loaded IDL.
8
+ * Used to tell "wrong account type at this PDA" from "layout decode failed".
9
+ */
10
+ export declare function resolverDiscriminatorFromIdl(program: AnyProgram): Buffer | null;
11
+ /**
12
+ * Read `resolver_pubkey` from raw account bytes.
13
+ *
14
+ * Matches on-chain layout (`programs/vercora/src/state/resolver.rs`):
15
+ * PDA seeds `[market, b"resolver", resolver_index]` (see `initialize_market_resolver`).
16
+ *
17
+ * Decode strategy (in order):
18
+ * 1) Try `coder.accounts.decode('Resolver', buf)` (handles padding / future fields).
19
+ * 2) If discriminator matches IDL `Resolver`, read pubkey at bytes `[8, 40)` (legacy / tolerant).
20
+ * 3) If `trustOwner === true` (caller has already checked `info.owner === programId`) AND
21
+ * the buffer is long enough, read pubkey at bytes `[8, 40)` even if discriminator doesn't
22
+ * match the current IDL. This recovers cases where the deployed program predates the
23
+ * current IDL (e.g. after a `declare_id!` update + redeploy on a fresh cluster, where the
24
+ * browser IDL cache lags the on-chain binary). The PDA seed + owner check is sufficient
25
+ * safety: only `initialize_market_resolver` can create a program-owned account at this PDA.
26
+ */
27
+ export declare function decodeResolverPubkeyFromAccountData(program: AnyProgram, data: Buffer | Uint8Array, options?: {
28
+ trustOwner?: boolean;
29
+ }): PublicKey | null;
30
+ /**
31
+ * Anchor `accounts[].discriminator` for `ResolutionVote` from the loaded IDL.
32
+ * Mirrors {@link resolverDiscriminatorFromIdl} so `fetchResolutionVote` can
33
+ * distinguish "wrong account type" from "layout decode failed".
34
+ */
35
+ export declare function resolutionVoteDiscriminatorFromIdl(program: AnyProgram): Buffer | null;
36
+ /** Shape returned by {@link decodeResolutionVoteFromAccountData}. */
37
+ export interface DecodedResolutionVote {
38
+ hasVoted: boolean;
39
+ outcomeIndex: number;
40
+ stakeAmount: BN;
41
+ claimed: boolean;
42
+ }
43
+ /**
44
+ * Read a `ResolutionVote` record from raw account bytes.
45
+ *
46
+ * Matches on-chain layout (`programs/vercora/src/state/resolution_vote.rs`):
47
+ * PDA seeds `[market, b"vote", resolver_index]` (see `vote_resolution`).
48
+ *
49
+ * Fixed offsets after the 8-byte Anchor discriminator:
50
+ * - `has_voted` : u8 @ 8 (bool)
51
+ * - `outcome_index` : u8 @ 9
52
+ * - `stake_amount` : u64 @ [10, 18) (little-endian)
53
+ * - `claimed` : u8 @ 18 (bool)
54
+ *
55
+ * Decode strategy (in order):
56
+ * 1) Try `coder.accounts.decode('ResolutionVote', buf)` (handles padding /
57
+ * future fields when the IDL layout matches the deployed program).
58
+ * 2) If discriminator matches IDL `ResolutionVote`, read the fixed offsets
59
+ * above (legacy / tolerant).
60
+ * 3) If `trustOwner === true` (caller has already checked
61
+ * `info.owner === programId`), read the fixed offsets even if the
62
+ * discriminator does not match the current IDL. The PDA seed + owner
63
+ * check is sufficient safety: only `vote_resolution` /
64
+ * `revoke_resolution_vote` can create a program-owned account at this PDA.
65
+ */
66
+ export declare function decodeResolutionVoteFromAccountData(program: AnyProgram, data: Buffer | Uint8Array, options?: {
67
+ trustOwner?: boolean;
68
+ }): DecodedResolutionVote | null;
69
+ export {};
70
+ //# sourceMappingURL=resolverDecode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolverDecode.d.ts","sourceRoot":"","sources":["../src/resolverDecode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,MAAM,OAAO,CAAC;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,kGAAkG;AAElG,KAAK,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;AAE/B;;;GAGG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAK/E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,mCAAmC,CACjD,OAAO,EAAE,UAAU,EACnB,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GACjC,SAAS,GAAG,IAAI,CA+BlB;AAED;;;;GAIG;AACH,wBAAgB,kCAAkC,CAChD,OAAO,EAAE,UAAU,GAClB,MAAM,GAAG,IAAI,CAKf;AAED,qEAAqE;AACrE,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,EAAE,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,mCAAmC,CACjD,OAAO,EAAE,UAAU,EACnB,IAAI,EAAE,MAAM,GAAG,UAAU,EACzB,OAAO,CAAC,EAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,GACjC,qBAAqB,GAAG,IAAI,CA8D9B"}
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.resolverDiscriminatorFromIdl = resolverDiscriminatorFromIdl;
7
+ exports.decodeResolverPubkeyFromAccountData = decodeResolverPubkeyFromAccountData;
8
+ exports.resolutionVoteDiscriminatorFromIdl = resolutionVoteDiscriminatorFromIdl;
9
+ exports.decodeResolutionVoteFromAccountData = decodeResolutionVoteFromAccountData;
10
+ const bn_js_1 = __importDefault(require("bn.js"));
11
+ const web3_js_1 = require("@solana/web3.js");
12
+ /**
13
+ * Anchor `accounts[].discriminator` for `Resolver` from the loaded IDL.
14
+ * Used to tell "wrong account type at this PDA" from "layout decode failed".
15
+ */
16
+ function resolverDiscriminatorFromIdl(program) {
17
+ const idl = program.idl;
18
+ const row = idl?.accounts?.find((a) => a.name === 'Resolver');
19
+ if (!row?.discriminator?.length)
20
+ return null;
21
+ return Buffer.from(row.discriminator);
22
+ }
23
+ /**
24
+ * Read `resolver_pubkey` from raw account bytes.
25
+ *
26
+ * Matches on-chain layout (`programs/vercora/src/state/resolver.rs`):
27
+ * PDA seeds `[market, b"resolver", resolver_index]` (see `initialize_market_resolver`).
28
+ *
29
+ * Decode strategy (in order):
30
+ * 1) Try `coder.accounts.decode('Resolver', buf)` (handles padding / future fields).
31
+ * 2) If discriminator matches IDL `Resolver`, read pubkey at bytes `[8, 40)` (legacy / tolerant).
32
+ * 3) If `trustOwner === true` (caller has already checked `info.owner === programId`) AND
33
+ * the buffer is long enough, read pubkey at bytes `[8, 40)` even if discriminator doesn't
34
+ * match the current IDL. This recovers cases where the deployed program predates the
35
+ * current IDL (e.g. after a `declare_id!` update + redeploy on a fresh cluster, where the
36
+ * browser IDL cache lags the on-chain binary). The PDA seed + owner check is sufficient
37
+ * safety: only `initialize_market_resolver` can create a program-owned account at this PDA.
38
+ */
39
+ function decodeResolverPubkeyFromAccountData(program, data, options) {
40
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
41
+ if (buf.length < 8 + 32)
42
+ return null;
43
+ const expected = resolverDiscriminatorFromIdl(program);
44
+ const actual = buf.subarray(0, 8);
45
+ const discriminatorMatches = !!(expected &&
46
+ expected.length === 8 &&
47
+ actual.equals(expected));
48
+ if (discriminatorMatches) {
49
+ try {
50
+ const acc = program.coder.accounts.decode('Resolver', buf);
51
+ const pk = (acc.resolverPubkey ?? acc.resolver_pubkey);
52
+ if (pk instanceof web3_js_1.PublicKey)
53
+ return pk;
54
+ }
55
+ catch {
56
+ /* fall through to byte-range read */
57
+ }
58
+ }
59
+ if (!discriminatorMatches && options?.trustOwner !== true) {
60
+ return null;
61
+ }
62
+ try {
63
+ return new web3_js_1.PublicKey(buf.subarray(8, 40));
64
+ }
65
+ catch {
66
+ return null;
67
+ }
68
+ }
69
+ /**
70
+ * Anchor `accounts[].discriminator` for `ResolutionVote` from the loaded IDL.
71
+ * Mirrors {@link resolverDiscriminatorFromIdl} so `fetchResolutionVote` can
72
+ * distinguish "wrong account type" from "layout decode failed".
73
+ */
74
+ function resolutionVoteDiscriminatorFromIdl(program) {
75
+ const idl = program.idl;
76
+ const row = idl?.accounts?.find((a) => a.name === 'ResolutionVote');
77
+ if (!row?.discriminator?.length)
78
+ return null;
79
+ return Buffer.from(row.discriminator);
80
+ }
81
+ /**
82
+ * Read a `ResolutionVote` record from raw account bytes.
83
+ *
84
+ * Matches on-chain layout (`programs/vercora/src/state/resolution_vote.rs`):
85
+ * PDA seeds `[market, b"vote", resolver_index]` (see `vote_resolution`).
86
+ *
87
+ * Fixed offsets after the 8-byte Anchor discriminator:
88
+ * - `has_voted` : u8 @ 8 (bool)
89
+ * - `outcome_index` : u8 @ 9
90
+ * - `stake_amount` : u64 @ [10, 18) (little-endian)
91
+ * - `claimed` : u8 @ 18 (bool)
92
+ *
93
+ * Decode strategy (in order):
94
+ * 1) Try `coder.accounts.decode('ResolutionVote', buf)` (handles padding /
95
+ * future fields when the IDL layout matches the deployed program).
96
+ * 2) If discriminator matches IDL `ResolutionVote`, read the fixed offsets
97
+ * above (legacy / tolerant).
98
+ * 3) If `trustOwner === true` (caller has already checked
99
+ * `info.owner === programId`), read the fixed offsets even if the
100
+ * discriminator does not match the current IDL. The PDA seed + owner
101
+ * check is sufficient safety: only `vote_resolution` /
102
+ * `revoke_resolution_vote` can create a program-owned account at this PDA.
103
+ */
104
+ function decodeResolutionVoteFromAccountData(program, data, options) {
105
+ const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
106
+ // 8 disc + 1 has_voted + 1 outcome_index + 8 stake_amount + 1 claimed = 19.
107
+ if (buf.length < 19)
108
+ return null;
109
+ const expected = resolutionVoteDiscriminatorFromIdl(program);
110
+ const actual = buf.subarray(0, 8);
111
+ const discriminatorMatches = !!(expected &&
112
+ expected.length === 8 &&
113
+ actual.equals(expected));
114
+ if (discriminatorMatches) {
115
+ try {
116
+ const acc = program.coder.accounts.decode('ResolutionVote', buf);
117
+ const stakeRaw = (acc.stakeAmount ?? acc.stake_amount);
118
+ const hasVoted = (acc.hasVoted ?? acc.has_voted);
119
+ const outcomeIndex = (acc.outcomeIndex ?? acc.outcome_index);
120
+ const claimed = acc.claimed;
121
+ return {
122
+ hasVoted: Boolean(hasVoted),
123
+ outcomeIndex: typeof outcomeIndex === 'number'
124
+ ? outcomeIndex
125
+ : Number(outcomeIndex ?? 0),
126
+ stakeAmount: stakeRaw instanceof bn_js_1.default
127
+ ? stakeRaw
128
+ : stakeRaw != null
129
+ ? new bn_js_1.default(stakeRaw.toString())
130
+ : new bn_js_1.default(0),
131
+ claimed: Boolean(claimed),
132
+ };
133
+ }
134
+ catch {
135
+ /* fall through to byte-range read */
136
+ }
137
+ }
138
+ if (!discriminatorMatches && options?.trustOwner !== true) {
139
+ return null;
140
+ }
141
+ try {
142
+ const hasVoted = buf.readUInt8(8) === 1;
143
+ const outcomeIndex = buf.readUInt8(9);
144
+ // `readBigUInt64LE` requires Node ≥ 12 / modern browsers; do it manually
145
+ // via a hex round-trip to stay portable and avoid BigInt leakage into the
146
+ // public API (`stakeAmount` is typed as `BN`).
147
+ const stakeAmount = new bn_js_1.default(buf.subarray(10, 18).toString('hex'), 16, 'le');
148
+ const claimed = buf.readUInt8(18) === 1;
149
+ return { hasVoted, outcomeIndex, stakeAmount, claimed };
150
+ }
151
+ catch {
152
+ return null;
153
+ }
154
+ }
155
+ //# sourceMappingURL=resolverDecode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolverDecode.js","sourceR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,CAAC;QAAC,MAAM,CAAC;YACP,qCAAqC;QACvC,CAAC;IACH,CAAC;IAED,IAAI,CAAC,oBAAoB,IAAI,OAAO,EAAE,UAAU,KAAK,IAAI,EAAE,CAAC;QAC1D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACxC,MAAM,YAAY,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACtC,yEAAyE;QACzE,0EAA0E;QAC1E,+CAA+C;QAC/C,MAAM,WAAW,GAAG,IAAI,eAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;QAC3E,MAAM,OAAO,GAAG,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC;IAC1D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercora-protocol/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "TypeScript SDK for the Vercora protocol on Solana",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",