@@ -5,6 +5,7 @@ import { TossStatus, type AgentWalletData, type CoinTossGame } from "./types";
5
5
6
6
const networkId = process . env . NETWORK_ID ;
7
7
export const WALLET_KEY_PREFIX = "wallet_data:" ;
8
+ export const WALLET_KEY_PREFIX_TOSS = "toss:" ;
8
9
export const WALLET_STORAGE_DIR = ".data/wallet_data" ;
9
10
export const XMTP_STORAGE_DIR = ".data/xmtp" ;
10
11
export const TOSS_STORAGE_DIR = ".data/tosses" ;
@@ -42,10 +43,10 @@ class StorageService {
42
43
*/
43
44
private async saveToFile (
44
45
directory : string ,
45
- inboxId : string ,
46
+ identifier : string ,
46
47
data : string ,
47
48
) : Promise < boolean > {
48
- const toRead = `${ WALLET_KEY_PREFIX } ${ inboxId } -${ networkId } ` ;
49
+ const toRead = `${ identifier } -${ networkId } ` ;
49
50
try {
50
51
const filePath = path . join ( directory , `${ toRead } .json` ) ;
51
52
await fs . writeFile ( filePath , data ) ;
@@ -61,10 +62,10 @@ class StorageService {
61
62
*/
62
63
private async readFromFile < T > (
63
64
directory : string ,
64
- inboxId : string ,
65
+ identifier : string ,
65
66
) : Promise < T | null > {
66
67
try {
67
- const key = `${ WALLET_KEY_PREFIX } ${ inboxId } -${ networkId } ` ;
68
+ const key = `${ identifier } -${ networkId } ` ;
68
69
const filePath = path . join ( directory , `${ key } .json` ) ;
69
70
const data = await fs . readFile ( filePath , "utf-8" ) ;
70
71
return JSON . parse ( data ) as T ;
@@ -87,15 +88,22 @@ class StorageService {
87
88
*/
88
89
public async saveGame ( toss : CoinTossGame ) : Promise < void > {
89
90
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
+ ) ;
91
96
}
92
97
93
98
/**
94
99
* Get a coin toss game by ID
95
100
*/
96
101
public async getGame ( tossId : string ) : Promise < CoinTossGame | null > {
97
102
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
+ ) ;
99
107
}
100
108
101
109
/**
@@ -143,15 +151,22 @@ class StorageService {
143
151
walletData : string ,
144
152
) : Promise < void > {
145
153
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
+ ) ;
147
159
}
148
160
149
161
/**
150
162
* Get user wallet data by user ID
151
163
*/
152
164
public async getUserWallet ( inboxId : string ) : Promise < AgentWalletData | null > {
153
165
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
+ ) ;
155
170
}
156
171
157
172
/**
0 commit comments