Skip to content

Commit 2b11bbe

Browse files
committed
make it as function and retrun string in types
1 parent ba98d61 commit 2b11bbe

File tree

5 files changed

+1082
-1082
lines changed

5 files changed

+1082
-1082
lines changed

agent/src/index.ts

+21-14
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ import { abstractPlugin } from "@elizaos/plugin-abstract";
6565
import { avalanchePlugin } from "@elizaos/plugin-avalanche";
6666
import { webSearchPlugin } from "@elizaos/plugin-web-search";
6767
import { echoChamberPlugin } from "@elizaos/plugin-echochambers";
68-
import { elizaCodeinPlugin } from "@elizaos/plugin-iq6900";
68+
import { elizaCodeinPlugin, onchainJson } from "@elizaos/plugin-iq6900";
69+
70+
6971
import Database from "better-sqlite3";
7072
import fs from "fs";
7173
import path from "path";
@@ -123,13 +125,15 @@ function isAllStrings(arr: unknown[]): boolean {
123125
return Array.isArray(arr) && arr.every((item) => typeof item === "string");
124126
}
125127

126-
export async function loadCharacterFromOnchain(walletAddress:string): Promise<Character[]> {
127-
const jsonResult = await elizaCodeinPlugin.providers[0].get(undefined,undefined);
128-
console.log("JSON 데이터:", jsonResult);
129-
if (jsonResult == "null") return;
128+
export async function loadCharacterFromOnchain(): Promise<Character[]> {
129+
const jsonText = onchainJson;
130+
131+
console.log('JSON:', jsonText);
132+
if (jsonText == "null") return;
130133
const loadedCharacters = [];
131134
try {
132-
const character = JSON.parse(jsonResult);
135+
136+
const character = JSON.parse(jsonText);
133137
validateCharacterConfig(character);
134138

135139
// .id isn't really valid
@@ -165,16 +169,17 @@ export async function loadCharacterFromOnchain(walletAddress:string): Promise<Ch
165169

166170
loadedCharacters.push(character);
167171
elizaLogger.info(
168-
`Successfully loaded character from: ${walletAddress}`
172+
`Successfully loaded character from: ${process.env.IQ_WALLET_ADDRESS}`
169173
);
174+
return loadedCharacters;
170175
} catch (e) {
171176
elizaLogger.error(
172-
`Error parsing character from ${walletAddress}: ${e}`
177+
`Error parsing character from ${process.env.IQ_WALLET_ADDRESS}: ${e}`
173178
);
174179
process.exit(1);
175180
}
176-
177181
}
182+
178183
export async function loadCharacters(
179184
charactersArg: string
180185
): Promise<Character[]> {
@@ -578,6 +583,10 @@ export async function createAgent(
578583
character,
579584
// character.plugins are handled when clients are added
580585
plugins: [
586+
getSecret(character, "IQ_WALLET_ADDRESS")&&
587+
getSecret(character, "IQSOlRPC")
588+
? elizaCodeinPlugin
589+
: null,
581590
bootstrapPlugin,
582591
getSecret(character, "CONFLUX_CORE_PRIVATE_KEY")
583592
? confluxPlugin
@@ -811,13 +820,11 @@ const startAgents = async () => {
811820
const args = parseArguments();
812821
let charactersArg = args.characters || args.character;
813822
let characters = [defaultCharacter];
814-
let onchainJson = [];
815-
let iqWallet = process.env.IQ_WALLET_ADDRESS;
816823

817-
if (iqWallet!=null){
818-
onchainJson = await loadCharacterFromOnchain(iqWallet);
824+
if (process.env.IQ_WALLET_ADDRESS) {
825+
characters = await loadCharacterFromOnchain();
819826
}
820-
if (onchainJson.length === 0 && charactersArg) {
827+
if (onchainJson && charactersArg) {
821828
characters = await loadCharacters(charactersArg);
822829
}
823830

Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
import {
2-
Provider,
3-
IAgentRuntime,
4-
Memory,
5-
State,
62
elizaLogger,
73
} from "@elizaos/core";
84

95

106
import { Connection, PublicKey } from "@solana/web3.js";
7+
const network = process.env.IQSOlRPC;
8+
const stringAddress = process.env.IQ_WALLET_ADDRESS;
119

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');
1711

18-
constructor() {
19-
this.connection = new Connection(network, "confirmed");
20-
}
12+
const iqHost = "https://solanacontractapi.uc.r.appspot.com";
2113

22-
private async fetchDBPDA(userKey: string): Promise<string> {
14+
async function fetchDBPDA(): Promise<string> {
2315
try {
24-
const response = await fetch(`${iqHost}/getDBPDA/${userKey}`);
16+
elizaLogger.info("Your Address:"+stringAddress);
17+
const response = await fetch(`${iqHost}/getDBPDA/${stringAddress}`);
2518
const data = await response.json();
2619
if (response.ok) {
2720
return data.DBPDA as string;
@@ -34,26 +27,26 @@ class OnChainJsonProvider {
3427
}
3528
}
3629

37-
private convertTextToEmoji(text: string) {
30+
async function convertTextToEmoji(text: string) {
3831
return text.replace(/\/u([0-9A-Fa-f]{4,6})/g, (match, code) => {
3932
return String.fromCodePoint(parseInt(code, 16));
4033
});
4134
}
4235

43-
private async fetchTransactionInfo(txId: string) {
36+
async function fetchTransactionInfo(txId: string) {
4437
try {
4538
const response = await fetch(`${iqHost}/get_transaction_info/${txId}`);
4639
if (response.ok) {
4740
const data = await response.json();
4841
return data.argData;
4942
}
5043
} catch (error) {
51-
console.error("Error fetching transaction info:", error);
44+
elizaLogger.error("Error fetching transaction info:", error);
5245
}
5346
return null;
5447
}
5548

56-
private async getTransactionData(transactionData: {
49+
async function getTransactionData(transactionData: {
5750
method: string;
5851
code: string;
5952
decode_break: number;
@@ -76,8 +69,8 @@ class OnChainJsonProvider {
7669
}
7770
}
7871

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);
8174
if (!txInfo) return "null";
8275

8376
const type_field = txInfo.type_field || "null";
@@ -90,8 +83,8 @@ class OnChainJsonProvider {
9083
}
9184
}
9285

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);
9588
if (!txInfo) return {
9689
json_data: "false",
9790
commit_message: "false",
@@ -101,19 +94,24 @@ class OnChainJsonProvider {
10194
const offset = txInfo.offset || "null";
10295
let chunks = [];
10396
let before_tx = tail_tx;
97+
if (before_tx == "null") return {
98+
json_data: "false",
99+
commit_message: "false",
100+
};
104101

105102
while (before_tx !== "Genesis") {
106103
if (before_tx) {
107-
const chunk = await this.fetchTransactionInfo(before_tx);
104+
elizaLogger.info("Chunks: "+before_tx);
105+
const chunk = await fetchTransactionInfo(before_tx);
108106
if (!chunk) {
109-
console.log("No chunk found.");
107+
elizaLogger.error("No chunk found.");
110108
return {
111109
json_data: "false",
112110
commit_message: "false",
113111
};
114112
}
115113

116-
const chunkData = await this.getTransactionData(chunk);
114+
const chunkData = await getTransactionData(chunk);
117115
if (chunkData.data == "null"){
118116
console.error("chunk data undefined");
119117
return {
@@ -134,18 +132,19 @@ class OnChainJsonProvider {
134132
}
135133

136134
const textList = chunks.reverse();
137-
const textData = textList.join();
135+
const textData = textList.join("");
138136

139137
return {
140-
json_data: this.convertTextToEmoji(textData),
138+
json_data: convertTextToEmoji(textData),
141139
commit_message: offset,
142140
};
143141
}
144142

145-
private async fetchSignaturesForAddress(dbAddress: PublicKey): Promise<string[]> {
143+
async function fetchSignaturesForAddress(dbAddress: PublicKey): Promise<string[]> {
146144
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,
149148
});
150149
return signatures.map((sig) => sig.signature);
151150
} catch (error) {
@@ -154,48 +153,36 @@ class OnChainJsonProvider {
154153
}
155154
}
156155

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));
160160

161161
for (const signature of signatures) {
162-
const commit = await this.extractCommitMessage(signature);
162+
const commit = await extractCommitMessage(signature);
163163
if (commit !== "null") return signature;
164164
}
165165
return "null";
166166
}
167167

168-
async bringAgentWithWalletAddress(stringAddress: string) {
168+
export async function bringAgentWithWalletAddress() {
169169
elizaLogger.info("Connecting to Solana...(IQ6900)");
170-
const recent = await this.findRecentJsonSignature(stringAddress);
170+
const recent = await findRecentJsonSignature();
171171
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";
173175
}
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;
177181
}
178-
}
179182

180183

181184

182185

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-
},
197186

198-
};
199187

200-
// Module exports
201-
export { onChainJsonProvider };
188+

packages/plugin-iq6900/src/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import { Plugin } from "@elizaos/core";
2-
import { onChainJsonProvider } from "./providers/onChainJsonProvider";
2+
export { onchainJson } from "./types/iq";
33

44
export const elizaCodeinPlugin: Plugin = {
55
name: "eliza-codein",
66
description: "Plugin that interacts with the on-chain inscription method “Code-In",
77
actions: [
8-
/* custom actions */
98
],
109
providers: [
11-
onChainJsonProvider
10+
/* custom providers */
1211
],
1312
evaluators: [
1413
/* custom evaluators */
1514
],
1615
services: [],
1716
clients: [],
18-
};
1917

20-
export default elizaCodeinPlugin;
18+
};
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { bringAgentWithWalletAddress } from "../functions/bringIQData";
2+
3+
4+
const onchainJson = await (async () => {
5+
return await bringAgentWithWalletAddress();
6+
})();
7+
8+
export { onchainJson };

0 commit comments

Comments
 (0)