Skip to content

Commit 47b927b

Browse files
committed
tosses
1 parent b8a78b5 commit 47b927b

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

examples/cointoss/src/commands.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ async function handleExplicitCommand(
303303
}),
304304
);
305305

306-
let statusMessage = `🎮 TOSS #${tossId} STATUS 🎮\n\n`;
306+
let statusMessage = `TOSS #${tossId} 🪙\n\n`;
307307

308308
// Add toss topic if available
309309
if (toss.tossTopic) {

examples/cointoss/src/storage.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TossStatus, type AgentWalletData, type CoinTossGame } from "./types";
55

66
const networkId = process.env.NETWORK_ID;
77
export const WALLET_KEY_PREFIX = "wallet_data:";
8+
export const WALLET_KEY_PREFIX_TOSS = "toss:";
89
export const WALLET_STORAGE_DIR = ".data/wallet_data";
910
export const XMTP_STORAGE_DIR = ".data/xmtp";
1011
export const TOSS_STORAGE_DIR = ".data/tosses";
@@ -42,10 +43,10 @@ class StorageService {
4243
*/
4344
private async saveToFile(
4445
directory: string,
45-
inboxId: string,
46+
identifier: string,
4647
data: string,
4748
): Promise<boolean> {
48-
const toRead = `${WALLET_KEY_PREFIX}${inboxId}-${networkId}`;
49+
const toRead = `${identifier}-${networkId}`;
4950
try {
5051
const filePath = path.join(directory, `${toRead}.json`);
5152
await fs.writeFile(filePath, data);
@@ -61,10 +62,10 @@ class StorageService {
6162
*/
6263
private async readFromFile<T>(
6364
directory: string,
64-
inboxId: string,
65+
identifier: string,
6566
): Promise<T | null> {
6667
try {
67-
const key = `${WALLET_KEY_PREFIX}${inboxId}-${networkId}`;
68+
const key = `${identifier}-${networkId}`;
6869
const filePath = path.join(directory, `${key}.json`);
6970
const data = await fs.readFile(filePath, "utf-8");
7071
return JSON.parse(data) as T;
@@ -87,15 +88,22 @@ class StorageService {
8788
*/
8889
public async saveGame(toss: CoinTossGame): Promise<void> {
8990
if (!this.initialized) this.initialize();
90-
await this.saveToFile(TOSS_STORAGE_DIR, toss.id, JSON.stringify(toss));
91+
await this.saveToFile(
92+
TOSS_STORAGE_DIR,
93+
WALLET_KEY_PREFIX_TOSS + toss.id,
94+
JSON.stringify(toss),
95+
);
9196
}
9297

9398
/**
9499
* Get a coin toss game by ID
95100
*/
96101
public async getGame(tossId: string): Promise<CoinTossGame | null> {
97102
if (!this.initialized) this.initialize();
98-
return this.readFromFile<CoinTossGame>(TOSS_STORAGE_DIR, tossId);
103+
return this.readFromFile<CoinTossGame>(
104+
TOSS_STORAGE_DIR,
105+
WALLET_KEY_PREFIX_TOSS + tossId,
106+
);
99107
}
100108

101109
/**
@@ -143,15 +151,22 @@ class StorageService {
143151
walletData: string,
144152
): Promise<void> {
145153
if (!this.initialized) this.initialize();
146-
await this.saveToFile(WALLET_STORAGE_DIR, inboxId, walletData);
154+
await this.saveToFile(
155+
WALLET_STORAGE_DIR,
156+
WALLET_KEY_PREFIX + inboxId,
157+
walletData,
158+
);
147159
}
148160

149161
/**
150162
* Get user wallet data by user ID
151163
*/
152164
public async getUserWallet(inboxId: string): Promise<AgentWalletData | null> {
153165
if (!this.initialized) this.initialize();
154-
return this.readFromFile<AgentWalletData>(WALLET_STORAGE_DIR, inboxId);
166+
return this.readFromFile<AgentWalletData>(
167+
WALLET_STORAGE_DIR,
168+
WALLET_KEY_PREFIX + inboxId,
169+
);
155170
}
156171

157172
/**

0 commit comments

Comments
 (0)