1
1
import { elizaLogger } from "@elizaos/core" ;
2
-
3
2
import { Connection , PublicKey } from "@solana/web3.js" ;
4
- const network = process . env . IQSOlRPC || "https://api.mainnet-beta.solana.com" ;
5
- const stringAddress = process . env . IQ_WALLET_ADDRESS ;
6
3
7
- const connection = new Connection ( network , "confirmed" ) ;
4
+ // Types
5
+ interface TransactionInfo {
6
+ argData ?: {
7
+ type_field ?: string ;
8
+ offset ?: string ;
9
+ tail_tx ?: string ;
10
+ } ;
11
+ }
12
+
13
+ interface TransactionData {
14
+ method : string ;
15
+ code : string ;
16
+ decode_break : number ;
17
+ before_tx : string ;
18
+ }
19
+
20
+ interface CodeResult {
21
+ json_data : string ;
22
+ commit_message : string ;
23
+ }
8
24
9
- const iqHost = "https://solanacontractapi.uc.r.appspot.com" ;
25
+ // Constants
26
+ const NETWORK = process . env . IQSOlRPC || "https://api.mainnet-beta.solana.com" ;
27
+ const WALLET_ADDRESS = process . env . IQ_WALLET_ADDRESS ;
28
+ const IQ_HOST = "https://solanacontractapi.uc.r.appspot.com" ;
29
+ const GENESIS_TX = "Genesis" ;
30
+ const ERROR_RESULT : CodeResult = {
31
+ json_data : "false" ,
32
+ commit_message : "false" ,
33
+ } ;
34
+
35
+ // Initialize connection
36
+ const connection = new Connection ( NETWORK , "confirmed" ) ;
37
+
38
+ async function fetchDBPDA ( ) : Promise < string | null > {
39
+ if ( ! WALLET_ADDRESS ) {
40
+ elizaLogger . error ( "Wallet address not provided" ) ;
41
+ return null ;
42
+ }
10
43
11
- async function fetchDBPDA ( ) : Promise < string > {
12
44
try {
13
- if ( stringAddress ) {
14
- elizaLogger . info ( "Connecting to Solana...(IQ6900)" ) ;
15
- elizaLogger . info ( "Your Address:" + stringAddress ) ;
16
- const response = await fetch ( `${ iqHost } /getDBPDA/${ stringAddress } ` ) ;
17
- const data = await response . json ( ) ;
18
- if ( response . ok ) {
19
- return data . DBPDA as string ;
20
- }
45
+ elizaLogger . info ( "Connecting to Solana...(IQ6900)" ) ;
46
+ elizaLogger . info ( `Your Address: ${ WALLET_ADDRESS } ` ) ;
47
+
48
+ const response = await fetch ( `${ IQ_HOST } /getDBPDA/${ WALLET_ADDRESS } ` ) ;
49
+ if ( ! response . ok ) {
50
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
21
51
}
52
+
53
+ const data = await response . json ( ) ;
54
+ return data . DBPDA || null ;
22
55
} catch ( error ) {
23
- console . error ( "Error fetching PDA:" , error ) ;
24
- return " null" ;
56
+ elizaLogger . error ( "Error fetching PDA:" , error ) ;
57
+ return null ;
25
58
}
26
59
}
27
60
28
- async function convertTextToEmoji ( text : string ) {
29
- return text . replace ( / \/ u ( [ 0 - 9 A - F a - f ] { 4 , 6 } ) / g, ( match , code ) => {
30
- return String . fromCodePoint ( parseInt ( code , 16 ) ) ;
31
- } ) ;
61
+ async function convertTextToEmoji ( text : string ) : Promise < string > {
62
+ return text . replace ( / \/ u ( [ 0 - 9 A - F a - f ] { 4 , 6 } ) / g, ( _ , code ) =>
63
+ String . fromCodePoint ( parseInt ( code , 16 ) )
64
+ ) ;
32
65
}
33
66
34
- async function fetchTransactionInfo ( txId : string ) {
67
+ async function fetchTransactionInfo (
68
+ txId : string
69
+ ) : Promise < TransactionInfo [ "argData" ] | null > {
35
70
try {
36
- const response = await fetch ( `${ iqHost } /get_transaction_info/${ txId } ` ) ;
37
- if ( response . ok ) {
38
- const data = await response . json ( ) ;
39
- return data . argData ;
71
+ const response = await fetch ( `${ IQ_HOST } /get_transaction_info/${ txId } ` ) ;
72
+ if ( ! response . ok ) {
73
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
40
74
}
75
+
76
+ const data = await response . json ( ) ;
77
+ return data . argData || null ;
41
78
} catch ( error ) {
42
79
elizaLogger . error ( "Error fetching transaction info:" , error ) ;
80
+ return null ;
43
81
}
44
- return null ;
45
82
}
46
83
47
- async function getTransactionData ( transactionData : {
48
- method : string ;
49
- code : string ;
50
- decode_break : number ;
84
+ async function getTransactionData ( transactionData : TransactionData ) : Promise < {
85
+ data : any ;
51
86
before_tx : string ;
52
- } ) : Promise < { data : any ; before_tx : string } > {
53
- if ( "code" in transactionData ) {
54
- return {
55
- data : {
56
- code : transactionData . code ,
57
- method : transactionData . method ,
58
- decode_break : transactionData . decode_break ,
59
- } ,
60
- before_tx : transactionData . before_tx ,
61
- } ;
62
- } else {
87
+ } > {
88
+ if ( ! transactionData || ! ( "code" in transactionData ) ) {
63
89
return {
64
90
data : "fail" ,
65
91
before_tx : "fail" ,
66
92
} ;
67
93
}
94
+
95
+ return {
96
+ data : {
97
+ code : transactionData . code ,
98
+ method : transactionData . method ,
99
+ decode_break : transactionData . decode_break ,
100
+ } ,
101
+ before_tx : transactionData . before_tx ,
102
+ } ;
68
103
}
69
104
70
- async function extractCommitMessage ( dataTxid : string ) : Promise < string > {
105
+ async function extractCommitMessage ( dataTxid : string ) : Promise < string | null > {
71
106
const txInfo = await fetchTransactionInfo ( dataTxid ) ;
72
- if ( ! txInfo ) return "null" ;
73
-
74
- const type_field = txInfo . type_field || "null" ;
107
+ if ( ! txInfo ) return null ;
75
108
76
- if ( type_field === "json" ) {
77
- const offset = txInfo . offset ;
78
- return offset . split ( "commit: " ) [ 1 ] ;
79
- } else {
80
- return "null" ;
109
+ const type_field = txInfo . type_field || null ;
110
+ if ( type_field === "json" && txInfo . offset ) {
111
+ const [ , commitMessage ] = txInfo . offset . split ( "commit: " ) ;
112
+ return commitMessage || null ;
81
113
}
114
+
115
+ return null ;
82
116
}
83
117
84
- async function bringCode ( dataTxid : string ) {
118
+ async function bringCode ( dataTxid : string ) : Promise < CodeResult > {
85
119
const txInfo = await fetchTransactionInfo ( dataTxid ) ;
86
- if ( ! txInfo )
87
- return {
88
- json_data : "false" ,
89
- commit_message : "false" ,
90
- } ;
120
+ if ( ! txInfo || ! txInfo . tail_tx ) return ERROR_RESULT ;
91
121
92
- const tail_tx = txInfo . tail_tx || "null" ;
93
- const offset = txInfo . offset || "null" ;
94
- let chunks = [ ] ;
95
- let before_tx = tail_tx ;
96
- if ( before_tx == "null" )
97
- return {
98
- json_data : "false" ,
99
- commit_message : "false" ,
100
- } ;
122
+ let chunks : string [ ] = [ ] ;
123
+ let before_tx = txInfo . tail_tx ;
124
+
125
+ if ( before_tx === null ) return ERROR_RESULT ;
101
126
102
- while ( before_tx !== "Genesis" ) {
103
- if ( before_tx ) {
104
- elizaLogger . info ( "Chunks: " + before_tx ) ;
127
+ try {
128
+ while ( before_tx !== GENESIS_TX ) {
129
+ if ( ! before_tx ) {
130
+ elizaLogger . error ( "Before transaction undefined" ) ;
131
+ return ERROR_RESULT ;
132
+ }
133
+
134
+ elizaLogger . info ( `Chunks: ${ before_tx } ` ) ;
105
135
const chunk = await fetchTransactionInfo ( before_tx ) ;
136
+
106
137
if ( ! chunk ) {
107
- elizaLogger . error ( "No chunk found." ) ;
108
- return {
109
- json_data : "false" ,
110
- commit_message : "false" ,
111
- } ;
138
+ elizaLogger . error ( "No chunk found" ) ;
139
+ return ERROR_RESULT ;
112
140
}
113
141
114
- const chunkData = await getTransactionData ( chunk ) ;
115
- if ( chunkData . data == "null" ) {
116
- console . error ( "chunk data undefined" ) ;
117
- return {
118
- json_data : "false" ,
119
- commit_message : "false" ,
120
- } ;
121
- } else {
122
- chunks . push ( chunkData . data . code ) ;
123
- before_tx = chunkData . before_tx ;
142
+ const chunkData = await getTransactionData (
143
+ chunk as TransactionData
144
+ ) ;
145
+ if ( ! chunkData . data || chunkData . data === null ) {
146
+ elizaLogger . error ( "Chunk data undefined" ) ;
147
+ return ERROR_RESULT ;
124
148
}
125
- } else {
126
- console . error ( "before data undefined" ) ;
127
- return {
128
- json_data : "false" ,
129
- commit_message : "false" ,
130
- } ;
131
- }
132
- }
133
149
134
- const textList = chunks . reverse ( ) ;
135
- const textData = textList . join ( "" ) ;
150
+ chunks . push ( chunkData . data . code ) ;
151
+ before_tx = chunkData . before_tx ;
152
+ }
136
153
137
- return {
138
- json_data : await convertTextToEmoji ( textData ) ,
139
- commit_message : offset ,
140
- } ;
154
+ const textData = chunks . reverse ( ) . join ( "" ) ;
155
+ return {
156
+ json_data : await convertTextToEmoji ( textData ) ,
157
+ commit_message : txInfo . offset || "false" ,
158
+ } ;
159
+ } catch ( error ) {
160
+ elizaLogger . error ( "Error in bringCode:" , error ) ;
161
+ return ERROR_RESULT ;
162
+ }
141
163
}
142
164
143
165
async function fetchSignaturesForAddress (
@@ -150,32 +172,41 @@ async function fetchSignaturesForAddress(
150
172
} ) ;
151
173
return signatures . map ( ( sig ) => sig . signature ) ;
152
174
} catch ( error ) {
153
- console . error ( "Error fetching signatures:" , error ) ;
175
+ elizaLogger . error ( "Error fetching signatures:" , error ) ;
154
176
return [ ] ;
155
177
}
156
178
}
157
179
158
- async function findRecentJsonSignature ( ) : Promise < string > {
180
+ async function findRecentJsonSignature ( ) : Promise < string | null > {
159
181
const dbAddress = await fetchDBPDA ( ) ;
160
- if ( ! dbAddress ) return ;
182
+ if ( ! dbAddress ) {
183
+ elizaLogger . error ( "Failed to fetch DBPDA" ) ;
184
+ return null ;
185
+ }
186
+
161
187
const signatures = await fetchSignaturesForAddress (
162
188
new PublicKey ( dbAddress )
163
189
) ;
190
+ if ( signatures . length === 0 ) {
191
+ elizaLogger . error ( "No signatures found" ) ;
192
+ return null ;
193
+ }
164
194
165
195
for ( const signature of signatures ) {
166
196
const commit = await extractCommitMessage ( signature ) ;
167
- if ( commit !== "null" ) return signature ;
197
+ if ( commit ) return signature ;
168
198
}
169
- return ;
199
+
200
+ return null ;
170
201
}
171
202
172
- export async function bringAgentWithWalletAddress ( ) {
203
+ export async function bringAgentWithWalletAddress ( ) : Promise < string | null > {
173
204
const recent = await findRecentJsonSignature ( ) ;
174
205
if ( ! recent ) {
175
206
elizaLogger . error ( "Cannot found onchain data in this wallet." ) ;
176
- return ;
207
+ return null ;
177
208
}
209
+
178
210
const result = await bringCode ( recent ) ;
179
- const json_string = result . json_data ;
180
- return await json_string ;
211
+ return result . json_data === "false" ? null : result . json_data ;
181
212
}
0 commit comments