From 506ad8fbb6a3872e8864e03a4bcde86c1a4ce275 Mon Sep 17 00:00:00 2001 From: aryan Date: Mon, 18 Nov 2024 03:37:21 +0700 Subject: [PATCH] fix: json template --- src/langchain/index.ts | 5 ++++- src/tools/deploy_token.ts | 27 +++++++++++++++++++++------ src/tools/get_balance.ts | 4 ++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/langchain/index.ts b/src/langchain/index.ts index 36885ef..7a9c6ca 100644 --- a/src/langchain/index.ts +++ b/src/langchain/index.ts @@ -53,7 +53,10 @@ export class SolanaDeployTokenTool extends Tool { async _call(input: string): Promise { try { - const { decimals = 9, initialSupply } = JSON.parse(input); + const validJson = input + .replace(/([a-zA-Z0-9_]+):/g, '"$1":') // Add quotes around keys + .trim(); + const { decimals = 9, initialSupply } = JSON.parse(validJson); const result = await this.solanaKit.deployToken(decimals, initialSupply); return `Token deployed successfully. Mint address: ${result.mint.toString()}`; } catch (error: any) { diff --git a/src/tools/deploy_token.ts b/src/tools/deploy_token.ts index c67f9c5..c3a40b1 100644 --- a/src/tools/deploy_token.ts +++ b/src/tools/deploy_token.ts @@ -1,10 +1,10 @@ import { SolanaAgentKit } from "../index"; -import { +import { createMint, getOrCreateAssociatedTokenAccount, mintTo, TOKEN_PROGRAM_ID, - Account + Account, } from "@solana/spl-token"; /** @@ -23,15 +23,19 @@ export async function deploy_token( // Create new token mint const mint = await createMint( agent.connection, - agent.wallet, // Payer + agent.wallet, // Payer agent.wallet_address, // Mint authority agent.wallet_address, // Freeze authority (optional) decimals, - undefined, // Optional keypair - undefined, // Confirmation options + undefined, // Optional keypair + { + commitment: "confirmed", + }, // Confirmation options TOKEN_PROGRAM_ID ); + console.log("Token deployed successfully. Mint address: ", mint.toString()); + // If initial supply is specified, mint tokens let tokenAccount: Account | undefined = undefined; if (initialSupply) { @@ -43,6 +47,11 @@ export async function deploy_token( agent.wallet_address ); + console.log( + "Token account created successfully. Address: ", + tokenAccount.address.toString() + ); + // Mint the initial supply await mintTo( agent.connection, @@ -52,13 +61,19 @@ export async function deploy_token( agent.wallet_address, initialSupply * Math.pow(10, decimals) ); + + console.log( + "Tokens minted successfully. Total supply: ", + initialSupply * Math.pow(10, decimals) + ); } return { mint: mint, - tokenAccount: tokenAccount?.address + tokenAccount: tokenAccount?.address, }; } catch (error: any) { + console.log(error); throw new Error(`Token deployment failed: ${error.message}`); } } diff --git a/src/tools/get_balance.ts b/src/tools/get_balance.ts index 1817515..2bdf3f2 100644 --- a/src/tools/get_balance.ts +++ b/src/tools/get_balance.ts @@ -1,4 +1,4 @@ -import { PublicKey } from "@solana/web3.js"; +import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; import { SolanaAgentKit } from "../index"; /** @@ -12,7 +12,7 @@ export async function get_balance( token_address?: PublicKey ) { if (!token_address) - return await agent.connection.getBalance(agent.wallet_address); + return await agent.connection.getBalance(agent.wallet_address) / LAMPORTS_PER_SOL const token_account = await agent.connection.getTokenAccountBalance(token_address); return token_account.value.uiAmount;