1
1
import {
2
- Provider ,
3
- IAgentRuntime ,
4
- Memory ,
5
- State ,
6
2
elizaLogger ,
7
3
} from "@elizaos/core" ;
8
4
9
5
10
6
import { Connection , PublicKey } from "@solana/web3.js" ;
7
+ const network = process . env . IQSOlRPC ;
8
+ const stringAddress = process . env . IQ_WALLET_ADDRESS ;
11
9
12
- let network = process . env . IQSOlRPC ;
13
- const iqHost = "https://solanacontractapi.uc.r.appspot.com" ;
14
-
15
- class OnChainJsonProvider {
16
- private connection : Connection ;
10
+ const connection = new Connection ( network , 'confirmed' ) ;
17
11
18
- constructor ( ) {
19
- this . connection = new Connection ( network , "confirmed" ) ;
20
- }
12
+ const iqHost = "https://solanacontractapi.uc.r.appspot.com" ;
21
13
22
- private async fetchDBPDA ( userKey : string ) : Promise < string > {
14
+ async function fetchDBPDA ( ) : Promise < string > {
23
15
try {
24
- const response = await fetch ( `${ iqHost } /getDBPDA/${ userKey } ` ) ;
16
+ elizaLogger . info ( "Your Address:" + stringAddress ) ;
17
+ const response = await fetch ( `${ iqHost } /getDBPDA/${ stringAddress } ` ) ;
25
18
const data = await response . json ( ) ;
26
19
if ( response . ok ) {
27
20
return data . DBPDA as string ;
@@ -34,26 +27,26 @@ class OnChainJsonProvider {
34
27
}
35
28
}
36
29
37
- private convertTextToEmoji ( text : string ) {
30
+ async function convertTextToEmoji ( text : string ) {
38
31
return text . replace ( / \/ u ( [ 0 - 9 A - F a - f ] { 4 , 6 } ) / g, ( match , code ) => {
39
32
return String . fromCodePoint ( parseInt ( code , 16 ) ) ;
40
33
} ) ;
41
34
}
42
35
43
- private async fetchTransactionInfo ( txId : string ) {
36
+ async function fetchTransactionInfo ( txId : string ) {
44
37
try {
45
38
const response = await fetch ( `${ iqHost } /get_transaction_info/${ txId } ` ) ;
46
39
if ( response . ok ) {
47
40
const data = await response . json ( ) ;
48
41
return data . argData ;
49
42
}
50
43
} catch ( error ) {
51
- console . error ( "Error fetching transaction info:" , error ) ;
44
+ elizaLogger . error ( "Error fetching transaction info:" , error ) ;
52
45
}
53
46
return null ;
54
47
}
55
48
56
- private async getTransactionData ( transactionData : {
49
+ async function getTransactionData ( transactionData : {
57
50
method : string ;
58
51
code : string ;
59
52
decode_break : number ;
@@ -76,8 +69,8 @@ class OnChainJsonProvider {
76
69
}
77
70
}
78
71
79
- private async extractCommitMessage ( dataTxid : string ) : Promise < string > {
80
- const txInfo = await this . fetchTransactionInfo ( dataTxid ) ;
72
+ async function extractCommitMessage ( dataTxid : string ) : Promise < string > {
73
+ const txInfo = await fetchTransactionInfo ( dataTxid ) ;
81
74
if ( ! txInfo ) return "null" ;
82
75
83
76
const type_field = txInfo . type_field || "null" ;
@@ -90,8 +83,8 @@ class OnChainJsonProvider {
90
83
}
91
84
}
92
85
93
- private async bringCode ( dataTxid : string ) {
94
- const txInfo = await this . fetchTransactionInfo ( dataTxid ) ;
86
+ async function bringCode ( dataTxid : string ) {
87
+ const txInfo = await fetchTransactionInfo ( dataTxid ) ;
95
88
if ( ! txInfo ) return {
96
89
json_data : "false" ,
97
90
commit_message : "false" ,
@@ -101,19 +94,24 @@ class OnChainJsonProvider {
101
94
const offset = txInfo . offset || "null" ;
102
95
let chunks = [ ] ;
103
96
let before_tx = tail_tx ;
97
+ if ( before_tx == "null" ) return {
98
+ json_data : "false" ,
99
+ commit_message : "false" ,
100
+ } ;
104
101
105
102
while ( before_tx !== "Genesis" ) {
106
103
if ( before_tx ) {
107
- const chunk = await this . fetchTransactionInfo ( before_tx ) ;
104
+ elizaLogger . info ( "Chunks: " + before_tx ) ;
105
+ const chunk = await fetchTransactionInfo ( before_tx ) ;
108
106
if ( ! chunk ) {
109
- console . log ( "No chunk found." ) ;
107
+ elizaLogger . error ( "No chunk found." ) ;
110
108
return {
111
109
json_data : "false" ,
112
110
commit_message : "false" ,
113
111
} ;
114
112
}
115
113
116
- const chunkData = await this . getTransactionData ( chunk ) ;
114
+ const chunkData = await getTransactionData ( chunk ) ;
117
115
if ( chunkData . data == "null" ) {
118
116
console . error ( "chunk data undefined" ) ;
119
117
return {
@@ -134,18 +132,19 @@ class OnChainJsonProvider {
134
132
}
135
133
136
134
const textList = chunks . reverse ( ) ;
137
- const textData = textList . join ( ) ;
135
+ const textData = textList . join ( "" ) ;
138
136
139
137
return {
140
- json_data : this . convertTextToEmoji ( textData ) ,
138
+ json_data : convertTextToEmoji ( textData ) ,
141
139
commit_message : offset ,
142
140
} ;
143
141
}
144
142
145
- private async fetchSignaturesForAddress ( dbAddress : PublicKey ) : Promise < string [ ] > {
143
+ async function fetchSignaturesForAddress ( dbAddress : PublicKey ) : Promise < string [ ] > {
146
144
try {
147
- const signatures = await this . connection . getSignaturesForAddress ( dbAddress , {
148
- limit : 30 ,
145
+ elizaLogger . info ( "Find Your Signature...(IQ6900)" ) ;
146
+ const signatures = await connection . getSignaturesForAddress ( dbAddress , {
147
+ limit : 20 ,
149
148
} ) ;
150
149
return signatures . map ( ( sig ) => sig . signature ) ;
151
150
} catch ( error ) {
@@ -154,48 +153,36 @@ class OnChainJsonProvider {
154
153
}
155
154
}
156
155
157
- private async findRecentJsonSignature ( stringAddress : string ) : Promise < string > {
158
- const dbAddress = await this . fetchDBPDA ( stringAddress ) ;
159
- const signatures = await this . fetchSignaturesForAddress ( new PublicKey ( dbAddress ) ) ;
156
+ async function findRecentJsonSignature ( ) : Promise < string > {
157
+ elizaLogger . info ( "FindRecentJsonSignature...(IQ6900)" ) ;
158
+ const dbAddress = await fetchDBPDA ( ) ;
159
+ const signatures = await fetchSignaturesForAddress ( new PublicKey ( dbAddress ) ) ;
160
160
161
161
for ( const signature of signatures ) {
162
- const commit = await this . extractCommitMessage ( signature ) ;
162
+ const commit = await extractCommitMessage ( signature ) ;
163
163
if ( commit !== "null" ) return signature ;
164
164
}
165
165
return "null" ;
166
166
}
167
167
168
- async bringAgentWithWalletAddress ( stringAddress : string ) {
168
+ export async function bringAgentWithWalletAddress ( ) {
169
169
elizaLogger . info ( "Connecting to Solana...(IQ6900)" ) ;
170
- const recent = await this . findRecentJsonSignature ( stringAddress ) ;
170
+ const recent = await findRecentJsonSignature ( ) ;
171
171
if ( recent === "null" ) {
172
- return { json_data : "null" , commit_message : "null" } ;
172
+
173
+ elizaLogger . error ( "Cannot found onchain data in this wallet." ) ;
174
+ return "null" ;
173
175
}
174
- const result = await this . bringCode ( recent ) ;
175
- elizaLogger . info ( "Your Json:" + result . json_data . slice ( 0 , 20 ) + "...." ) ;
176
- return result ;
176
+ const result = await bringCode ( recent ) ;
177
+
178
+ const json_string = await result . json_data ;
179
+
180
+ return await json_string ;
177
181
}
178
- }
179
182
180
183
181
184
182
185
183
- const onChainJsonProvider : Provider = {
184
- get : async (
185
- runtime : IAgentRuntime ,
186
- _message : Memory ,
187
- _state ?: State
188
- ) : Promise < string > => {
189
- const userWallet = process . env . IQ_WALLET_ADDRESS ;
190
- if ( userWallet != null ) {
191
- const provider = new OnChainJsonProvider ( ) ;
192
- const result = await provider . bringAgentWithWalletAddress ( userWallet ) ;
193
- return result . json_data ;
194
- }
195
- return "null" ;
196
- } ,
197
186
198
- } ;
199
187
200
- // Module exports
201
- export { onChainJsonProvider } ;
188
+
0 commit comments