theref-sdk 0.1.0

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.
package/src/index.ts ADDED
@@ -0,0 +1,56 @@
1
+ export { TheRefClient } from "./client/TheRefClient";
2
+ export { normalizeMove } from "./adapters/normalizer";
3
+ export { NETWORKS, resolveNetwork, gidToNum } from "./utils/networks";
4
+ export type {
5
+ // Config
6
+ TheRefConfig,
7
+ NetworkId,
8
+ NetworkConfig,
9
+ ContractAddresses,
10
+ // Game
11
+ CreateGameOptions,
12
+ GameState,
13
+ GameStatus,
14
+ RoundResult,
15
+ JudgmentResult,
16
+ PlayerType,
17
+ // Move
18
+ AnyMove,
19
+ NormalizedMove,
20
+ // Tournament
21
+ CreateTournamentOptions,
22
+ Tournament,
23
+ BracketMatch,
24
+ // Leaderboard
25
+ LeaderboardEntry,
26
+ PlayerStats,
27
+ } from "./types";
28
+
29
+ // Convenience factory function
30
+ import { TheRefClient } from "./client/TheRefClient";
31
+ import { TheRefConfig } from "./types";
32
+
33
+ /**
34
+ * Create a TheRef client.
35
+ *
36
+ * @example
37
+ * // Agent (server-side)
38
+ * const ref = createTheRef({
39
+ * network: "bradbury",
40
+ * privateKey: process.env.AGENT_KEY as `0x${string}`,
41
+ * });
42
+ *
43
+ * @example
44
+ * // Browser wallet
45
+ * const ref = createTheRef({
46
+ * network: "bradbury",
47
+ * walletAddress: "0xYourWallet",
48
+ * });
49
+ *
50
+ * @example
51
+ * // Development (Studionet, no key needed)
52
+ * const ref = createTheRef({ network: "studionet" });
53
+ */
54
+ export function createTheRef(config: TheRefConfig): TheRefClient {
55
+ return new TheRefClient(config);
56
+ }
Binary file
@@ -0,0 +1,149 @@
1
+ // ── Network ───────────────────────────────────────────────────────────────────
2
+
3
+ export type NetworkId = "bradbury" | "studionet";
4
+
5
+ export interface NetworkConfig {
6
+ id: NetworkId;
7
+ rpc: string;
8
+ chainId: number;
9
+ addresses: ContractAddresses;
10
+ }
11
+
12
+ export interface ContractAddresses {
13
+ CORE: string;
14
+ LB: string;
15
+ ORG: string;
16
+ FEE: string;
17
+ TRN: string;
18
+ }
19
+
20
+ // ── TheRef Client Config ──────────────────────────────────────────────────────
21
+
22
+ export interface TheRefConfig {
23
+ network: NetworkId | NetworkConfig;
24
+ privateKey?: `0x${string}`; // for agents / server-side
25
+ walletAddress?: `0x${string}`; // for browser wallets
26
+ wsUrl?: string; // WebSocket URL (default: wss://ws.theref.fun)
27
+ retries?: number; // tx retries (default: 300)
28
+ pollInterval?: number; // ms between polls (default: 5000)
29
+ }
30
+
31
+ // ── Game ─────────────────────────────────────────────────────────────────────
32
+
33
+ export type GameStatus = "waiting" | "active" | "completed" | "draw";
34
+ export type PlayerType = "human" | "agent";
35
+
36
+ export interface CreateGameOptions {
37
+ name: string;
38
+ rules?: string;
39
+ player1: string;
40
+ player2?: string;
41
+ maxRounds?: number;
42
+ visibility?: "public" | "private";
43
+ agent1?: `0x${string}`;
44
+ agent2?: `0x${string}`;
45
+ }
46
+
47
+ export interface GameState {
48
+ gameId: string;
49
+ gameName: string;
50
+ status: GameStatus;
51
+ player1: string;
52
+ player2: string;
53
+ agent1: string;
54
+ agent2: string;
55
+ maxRounds: number;
56
+ roundCount: number;
57
+ judgedThrough: number;
58
+ rules: string;
59
+ winner: string;
60
+ score: Record<string, number>;
61
+ playerTypes: Record<string, PlayerType>;
62
+ rounds: RoundResult[];
63
+ caller: string;
64
+ }
65
+
66
+ export interface RoundResult {
67
+ roundNumber: number;
68
+ movePlayer1: string;
69
+ movePlayer2: string;
70
+ result: "player1" | "player2" | "draw" | "pending";
71
+ reasonType: "normal" | "invalid_move";
72
+ invalidPlayer: "none" | "player1" | "player2";
73
+ reasoning: string;
74
+ confidence: number;
75
+ status: string;
76
+ }
77
+
78
+ export interface JudgmentResult {
79
+ winner: string;
80
+ isDraw: boolean;
81
+ score: Record<string, number>;
82
+ rounds: RoundResult[];
83
+ txHash: string;
84
+ payload: string;
85
+ }
86
+
87
+ // ── Move ─────────────────────────────────────────────────────────────────────
88
+
89
+ export type AnyMove =
90
+ | string
91
+ | number
92
+ | boolean
93
+ | AnyMove[]
94
+ | { [key: string]: AnyMove };
95
+
96
+ export interface NormalizedMove {
97
+ raw: AnyMove;
98
+ text: string;
99
+ adapter: string;
100
+ }
101
+
102
+ // ── Tournament ────────────────────────────────────────────────────────────────
103
+
104
+ export interface CreateTournamentOptions {
105
+ name: string;
106
+ gameName: string;
107
+ format: "single_elimination" | "round_robin" | "swiss";
108
+ maxPlayers: number;
109
+ rules?: string;
110
+ roundsPerMatch?: number;
111
+ prizeSplit?: number[];
112
+ }
113
+
114
+ export interface Tournament {
115
+ tid: string;
116
+ name: string;
117
+ gameName: string;
118
+ format: string;
119
+ maxPlayers: number;
120
+ status: string;
121
+ players: string[];
122
+ bracket: BracketMatch[];
123
+ winner: string;
124
+ }
125
+
126
+ export interface BracketMatch {
127
+ matchId: number;
128
+ round: number;
129
+ player1: string;
130
+ player2: string;
131
+ gameId: string;
132
+ winner: string;
133
+ status: string;
134
+ }
135
+
136
+ // ── Leaderboard ───────────────────────────────────────────────────────────────
137
+
138
+ export interface LeaderboardEntry {
139
+ player: string;
140
+ wins: number;
141
+ losses: number;
142
+ draws: number;
143
+ score: number;
144
+ playerType: PlayerType;
145
+ }
146
+
147
+ export interface PlayerStats extends LeaderboardEntry {
148
+ games: number;
149
+ }
@@ -0,0 +1,39 @@
1
+ import { NetworkConfig, NetworkId } from "../types";
2
+
3
+ export const NETWORKS: Record<NetworkId, NetworkConfig> = {
4
+ bradbury: {
5
+ id: "bradbury",
6
+ rpc: "https://rpc-bradbury.genlayer.com",
7
+ chainId: 4221,
8
+ addresses: {
9
+ CORE: "0xA29CfFC83d32fe924cFf1F1bDCf21555CCC96206",
10
+ LB: "0x5D417F296b17656c9b950236feE66F63E22d8A54",
11
+ ORG: "0x440b28afc1804fc1E4AA8f5b559C18F7bCf43B3A",
12
+ FEE: "0x88A0A4d573fD9C63433E457e94d266D7904278C2",
13
+ TRN: "0xbcc0E82a17491297E0c4938606624Fa04e6abA1B",
14
+ },
15
+ },
16
+ studionet: {
17
+ id: "studionet",
18
+ rpc: "https://studio.genlayer.com/api",
19
+ chainId: 61999,
20
+ addresses: {
21
+ CORE: "0x88CAA18419714aA38CdF53c0E603141c48fa3238",
22
+ LB: "0x8A2d05Df048A64cc6B83682a431ade05030e4BBB",
23
+ ORG: "0x265ef96A5230F13836c553D7DD2B9D7c3fE14aE1",
24
+ FEE: "0x0000000000000000000000000000000000000000",
25
+ TRN: "0x44f7c1bDa293B9cdBD79a6dfb66bD45696dEa4A6",
26
+ },
27
+ },
28
+ };
29
+
30
+ export const DEFAULT_WS_URL = "wss://ws.theref.fun";
31
+
32
+ export function resolveNetwork(network: NetworkId | NetworkConfig): NetworkConfig {
33
+ if (typeof network === "string") return NETWORKS[network];
34
+ return network;
35
+ }
36
+
37
+ export function gidToNum(gid: string): number {
38
+ return parseInt(gid.replace(/^0+/, "") || "0", 36) || 1;
39
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "declaration": true,
8
+ "outDir": "dist",
9
+ "rootDir": "src",
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "lib": ["ES2020", "DOM"]
13
+ },
14
+ "include": ["src"],
15
+ "exclude": ["node_modules", "dist"]
16
+ }
Binary file