pyre-world-kit 2.0.11 → 3.0.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.
Files changed (54) hide show
  1. package/.prettierrc.json +6 -0
  2. package/dist/actions.js +16 -0
  3. package/dist/index.d.ts +38 -4
  4. package/dist/index.js +100 -85
  5. package/dist/providers/action.provider.d.ts +46 -0
  6. package/dist/providers/action.provider.js +331 -0
  7. package/dist/providers/intel.provider.d.ts +29 -0
  8. package/dist/providers/intel.provider.js +363 -0
  9. package/dist/providers/mapper.provider.d.ts +197 -0
  10. package/dist/providers/mapper.provider.js +158 -0
  11. package/dist/providers/registry.provider.d.ts +25 -0
  12. package/dist/providers/registry.provider.js +229 -0
  13. package/dist/providers/state.provider.d.ts +42 -0
  14. package/dist/providers/state.provider.js +348 -0
  15. package/dist/pyre_world.json +34 -229
  16. package/dist/types/action.types.d.ts +41 -0
  17. package/dist/types/action.types.js +2 -0
  18. package/dist/types/intel.types.d.ts +20 -0
  19. package/dist/types/intel.types.js +2 -0
  20. package/dist/types/mapper.types.d.ts +27 -0
  21. package/dist/types/mapper.types.js +22 -0
  22. package/dist/types/registry.types.d.ts +0 -0
  23. package/dist/types/registry.types.js +1 -0
  24. package/dist/types/state.types.d.ts +112 -0
  25. package/dist/types/state.types.js +2 -0
  26. package/dist/types.d.ts +8 -24
  27. package/dist/util.d.ts +29 -0
  28. package/dist/util.js +144 -0
  29. package/dist/vanity.d.ts +3 -3
  30. package/dist/vanity.js +18 -15
  31. package/package.json +4 -2
  32. package/readme.md +134 -122
  33. package/src/index.ts +127 -92
  34. package/src/providers/action.provider.ts +443 -0
  35. package/src/providers/intel.provider.ts +383 -0
  36. package/src/providers/mapper.provider.ts +195 -0
  37. package/src/providers/registry.provider.ts +277 -0
  38. package/src/providers/state.provider.ts +357 -0
  39. package/src/pyre_world.json +35 -230
  40. package/src/types/action.types.ts +76 -0
  41. package/src/types/intel.types.ts +22 -0
  42. package/src/types/mapper.types.ts +84 -0
  43. package/src/types/registry.types.ts +0 -0
  44. package/src/types/state.types.ts +144 -0
  45. package/src/types.ts +329 -333
  46. package/src/util.ts +148 -0
  47. package/src/vanity.ts +27 -14
  48. package/tests/test_e2e.ts +339 -172
  49. package/src/actions.ts +0 -703
  50. package/src/intel.ts +0 -521
  51. package/src/mappers.ts +0 -302
  52. package/src/registry.ts +0 -317
  53. package/tests/test_devnet_e2e.ts +0 -401
  54. package/tests/test_sim.ts +0 -458
@@ -0,0 +1,348 @@
1
+ "use strict";
2
+ /**
3
+ * Pyre Kit State Provider
4
+ *
5
+ * Objective game state tracking for an agent.
6
+ * Owns holdings, action counts, vault resolution, tick counter.
7
+ * Injected into ActionProvider so every action automatically updates state.
8
+ */
9
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ var desc = Object.getOwnPropertyDescriptor(m, k);
12
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13
+ desc = { enumerable: true, get: function() { return m[k]; } };
14
+ }
15
+ Object.defineProperty(o, k2, desc);
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || (function () {
26
+ var ownKeys = function(o) {
27
+ ownKeys = Object.getOwnPropertyNames || function (o) {
28
+ var ar = [];
29
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
30
+ return ar;
31
+ };
32
+ return ownKeys(o);
33
+ };
34
+ return function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ })();
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.StateProvider = void 0;
44
+ const web3_js_1 = require("@solana/web3.js");
45
+ const vanity_1 = require("../vanity");
46
+ const util_1 = require("../util");
47
+ const EMPTY_COUNTS = {
48
+ join: 0,
49
+ defect: 0,
50
+ rally: 0,
51
+ launch: 0,
52
+ message: 0,
53
+ reinforce: 0,
54
+ war_loan: 0,
55
+ repay_loan: 0,
56
+ siege: 0,
57
+ ascend: 0,
58
+ raze: 0,
59
+ tithe: 0,
60
+ infiltrate: 0,
61
+ fud: 0,
62
+ };
63
+ class StateProvider {
64
+ connection;
65
+ publicKey;
66
+ registry;
67
+ _state = null;
68
+ checkpointConfig = null;
69
+ ticksSinceCheckpoint = 0;
70
+ constructor(connection, publicKey, registry) {
71
+ this.connection = connection;
72
+ this.publicKey = publicKey;
73
+ this.registry = registry;
74
+ }
75
+ // ─── Readonly accessors ─────────────────────────────────────────
76
+ get state() {
77
+ return this._state;
78
+ }
79
+ get vaultCreator() {
80
+ return this._state?.vaultCreator ?? null;
81
+ }
82
+ get initialized() {
83
+ return this._state?.initialized ?? false;
84
+ }
85
+ get tick() {
86
+ return this._state?.tick ?? 0;
87
+ }
88
+ // ─── Configuration ──────────────────────────────────────────────
89
+ /** Configure auto-checkpoint behavior */
90
+ setCheckpointConfig(config) {
91
+ this.checkpointConfig = config;
92
+ }
93
+ // ─── Initialization ─────────────────────────────────────────────
94
+ async init() {
95
+ if (this._state?.initialized)
96
+ return this._state;
97
+ const state = {
98
+ publicKey: this.publicKey,
99
+ vaultCreator: null,
100
+ stronghold: null,
101
+ tick: 0,
102
+ actionCounts: { ...EMPTY_COUNTS },
103
+ holdings: new Map(),
104
+ activeLoans: new Set(),
105
+ founded: [],
106
+ rallied: new Set(),
107
+ voted: new Set(),
108
+ sentiment: new Map(),
109
+ recentHistory: [],
110
+ personalitySummary: null,
111
+ totalSolSpent: 0,
112
+ totalSolReceived: 0,
113
+ initialized: false,
114
+ };
115
+ this._state = state;
116
+ // Resolve vault link
117
+ const { getVaultForWallet } = await Promise.resolve().then(() => __importStar(require('torchsdk')));
118
+ try {
119
+ const vault = await getVaultForWallet(this.connection, this.publicKey);
120
+ if (vault) {
121
+ state.vaultCreator = vault.creator;
122
+ state.stronghold = {
123
+ address: vault.address,
124
+ creator: vault.creator,
125
+ authority: vault.authority,
126
+ sol_balance: vault.sol_balance,
127
+ total_deposited: vault.total_deposited,
128
+ total_withdrawn: vault.total_withdrawn,
129
+ total_spent: vault.total_spent,
130
+ total_received: vault.total_received,
131
+ linked_agents: vault.linked_wallets,
132
+ created_at: vault.created_at,
133
+ };
134
+ }
135
+ }
136
+ catch { }
137
+ // Load registry profile — personality, action counts, SOL totals
138
+ try {
139
+ const profile = await this.registry.getProfile(this.publicKey);
140
+ if (profile) {
141
+ state.personalitySummary = profile.personality_summary || null;
142
+ state.totalSolSpent = profile.total_sol_spent;
143
+ state.totalSolReceived = profile.total_sol_received;
144
+ // Seed action counts from on-chain checkpoint
145
+ state.actionCounts.join = Math.max(state.actionCounts.join, profile.joins);
146
+ state.actionCounts.defect = Math.max(state.actionCounts.defect, profile.defects);
147
+ state.actionCounts.rally = Math.max(state.actionCounts.rally, profile.rallies);
148
+ state.actionCounts.launch = Math.max(state.actionCounts.launch, profile.launches);
149
+ state.actionCounts.message = Math.max(state.actionCounts.message, profile.messages);
150
+ state.actionCounts.reinforce = Math.max(state.actionCounts.reinforce, profile.reinforces);
151
+ state.actionCounts.fud = Math.max(state.actionCounts.fud, profile.fuds);
152
+ state.actionCounts.infiltrate = Math.max(state.actionCounts.infiltrate, profile.infiltrates);
153
+ state.actionCounts.war_loan = Math.max(state.actionCounts.war_loan, profile.war_loans);
154
+ state.actionCounts.repay_loan = Math.max(state.actionCounts.repay_loan, profile.repay_loans);
155
+ state.actionCounts.siege = Math.max(state.actionCounts.siege, profile.sieges);
156
+ state.actionCounts.ascend = Math.max(state.actionCounts.ascend, profile.ascends);
157
+ state.actionCounts.raze = Math.max(state.actionCounts.raze, profile.razes);
158
+ state.actionCounts.tithe = Math.max(state.actionCounts.tithe, profile.tithes);
159
+ // Set tick to total actions from checkpoint
160
+ const totalFromCheckpoint = Object.values(state.actionCounts).reduce((a, b) => a + b, 0);
161
+ state.tick = totalFromCheckpoint;
162
+ }
163
+ }
164
+ catch { }
165
+ // Load holdings (wallet + vault token accounts)
166
+ await this.refreshHoldings();
167
+ state.initialized = true;
168
+ return state;
169
+ }
170
+ // ─── Action Recording ───────────────────────────────────────────
171
+ async record(action, mint, description) {
172
+ if (!this._state)
173
+ throw new Error('State not initialized — call init() first');
174
+ this._state.tick++;
175
+ this._state.actionCounts[action]++;
176
+ this.ticksSinceCheckpoint++;
177
+ // Track founded factions
178
+ if (action === 'launch' && mint) {
179
+ this._state.founded.push(mint);
180
+ }
181
+ // Update sentiment for the target faction
182
+ if (mint) {
183
+ this.updateSentiment(action, mint);
184
+ }
185
+ // Track recent history for LLM memory block
186
+ if (description) {
187
+ this._state.recentHistory.push(description);
188
+ if (this._state.recentHistory.length > 20) {
189
+ this._state.recentHistory = this._state.recentHistory.slice(-20);
190
+ }
191
+ }
192
+ // Refresh holdings after any trade action
193
+ await this.refreshHoldings();
194
+ // Auto-checkpoint check
195
+ if (this.checkpointConfig && this.ticksSinceCheckpoint >= this.checkpointConfig.interval) {
196
+ this.ticksSinceCheckpoint = 0;
197
+ this.onCheckpointDue?.();
198
+ }
199
+ }
200
+ /** Update sentiment score for a faction based on action type */
201
+ updateSentiment(action, mint) {
202
+ if (!this._state)
203
+ return;
204
+ const current = this._state.sentiment.get(mint) ?? 0;
205
+ const SENTIMENT_DELTAS = {
206
+ join: 1,
207
+ reinforce: 1.5,
208
+ defect: -2,
209
+ rally: 3,
210
+ infiltrate: -5,
211
+ message: 0.5,
212
+ fud: -1.5,
213
+ war_loan: 1,
214
+ launch: 3,
215
+ };
216
+ const delta = SENTIMENT_DELTAS[action] ?? 0;
217
+ if (delta !== 0) {
218
+ this._state.sentiment.set(mint, Math.max(-10, Math.min(10, current + delta)));
219
+ }
220
+ }
221
+ /** Callback set by PyreKit to handle checkpoint triggers */
222
+ onCheckpointDue = null;
223
+ // ─── Holdings ───────────────────────────────────────────────────
224
+ async refreshHoldings() {
225
+ if (!this._state)
226
+ return;
227
+ const { TOKEN_2022_PROGRAM_ID } = await Promise.resolve().then(() => __importStar(require('@solana/spl-token')));
228
+ const walletPk = new web3_js_1.PublicKey(this.publicKey);
229
+ // Scan wallet token accounts
230
+ let walletValues = [];
231
+ try {
232
+ const walletAccounts = await this.connection.getParsedTokenAccountsByOwner(walletPk, {
233
+ programId: TOKEN_2022_PROGRAM_ID,
234
+ });
235
+ walletValues = walletAccounts.value;
236
+ }
237
+ catch { }
238
+ // Scan vault token accounts
239
+ let vaultValues = [];
240
+ if (this._state.stronghold) {
241
+ try {
242
+ const vaultPk = new web3_js_1.PublicKey(this._state.stronghold.address);
243
+ const vaultAccounts = await this.connection.getParsedTokenAccountsByOwner(vaultPk, {
244
+ programId: TOKEN_2022_PROGRAM_ID,
245
+ });
246
+ vaultValues = vaultAccounts.value;
247
+ }
248
+ catch { }
249
+ }
250
+ // Merge balances
251
+ const newHoldings = new Map();
252
+ for (const a of [...walletValues, ...vaultValues]) {
253
+ const mint = a.account.data.parsed.info.mint;
254
+ const balance = Number(a.account.data.parsed.info.tokenAmount.uiAmount ?? 0);
255
+ if (balance > 0 && (0, vanity_1.isPyreMint)(mint) && !(0, util_1.isBlacklistedMint)(mint)) {
256
+ newHoldings.set(mint, (newHoldings.get(mint) ?? 0) + balance);
257
+ }
258
+ }
259
+ // Update — clear stale, set fresh
260
+ this._state.holdings.clear();
261
+ for (const [mint, balance] of newHoldings) {
262
+ this._state.holdings.set(mint, balance);
263
+ }
264
+ }
265
+ getSentiment(mint) {
266
+ return this._state?.sentiment.get(mint) ?? 0;
267
+ }
268
+ get sentimentMap() {
269
+ return this._state?.sentiment ?? new Map();
270
+ }
271
+ get history() {
272
+ return this._state?.recentHistory ?? [];
273
+ }
274
+ getBalance(mint) {
275
+ return this._state?.holdings.get(mint) ?? 0;
276
+ }
277
+ // ─── Dedup Guards ───────────────────────────────────────────────
278
+ hasVoted(mint) {
279
+ return this._state?.voted.has(mint) ?? false;
280
+ }
281
+ hasRallied(mint) {
282
+ return this._state?.rallied.has(mint) ?? false;
283
+ }
284
+ markVoted(mint) {
285
+ this._state?.voted.add(mint);
286
+ }
287
+ markRallied(mint) {
288
+ this._state?.rallied.add(mint);
289
+ }
290
+ // ─── Serialization ──────────────────────────────────────────────
291
+ serialize() {
292
+ if (!this._state) {
293
+ return {
294
+ publicKey: this.publicKey,
295
+ vaultCreator: null,
296
+ tick: 0,
297
+ actionCounts: { ...EMPTY_COUNTS },
298
+ holdings: {},
299
+ activeLoans: [],
300
+ founded: [],
301
+ rallied: [],
302
+ voted: [],
303
+ sentiment: {},
304
+ recentHistory: [],
305
+ personalitySummary: null,
306
+ totalSolSpent: 0,
307
+ totalSolReceived: 0,
308
+ };
309
+ }
310
+ return {
311
+ publicKey: this._state.publicKey,
312
+ vaultCreator: this._state.vaultCreator,
313
+ tick: this._state.tick,
314
+ actionCounts: { ...this._state.actionCounts },
315
+ holdings: Object.fromEntries(this._state.holdings),
316
+ activeLoans: Array.from(this._state.activeLoans),
317
+ founded: [...this._state.founded],
318
+ rallied: Array.from(this._state.rallied),
319
+ voted: Array.from(this._state.voted),
320
+ sentiment: Object.fromEntries(this._state.sentiment),
321
+ recentHistory: this._state.recentHistory.slice(-20),
322
+ personalitySummary: this._state.personalitySummary,
323
+ totalSolSpent: this._state.totalSolSpent,
324
+ totalSolReceived: this._state.totalSolReceived,
325
+ };
326
+ }
327
+ hydrate(saved) {
328
+ this._state = {
329
+ publicKey: saved.publicKey,
330
+ vaultCreator: saved.vaultCreator,
331
+ stronghold: null, // will be resolved on next refreshHoldings or init
332
+ tick: saved.tick,
333
+ actionCounts: { ...EMPTY_COUNTS, ...saved.actionCounts },
334
+ holdings: new Map(Object.entries(saved.holdings)),
335
+ activeLoans: new Set(saved.activeLoans),
336
+ founded: [...saved.founded],
337
+ rallied: new Set(saved.rallied),
338
+ voted: new Set(saved.voted),
339
+ sentiment: new Map(Object.entries(saved.sentiment)),
340
+ recentHistory: [...saved.recentHistory],
341
+ personalitySummary: saved.personalitySummary,
342
+ totalSolSpent: saved.totalSolSpent,
343
+ totalSolReceived: saved.totalSolReceived,
344
+ initialized: true,
345
+ };
346
+ }
347
+ }
348
+ exports.StateProvider = StateProvider;