fix: json template

This commit is contained in:
aryan
2024-11-18 03:37:21 +07:00
parent 0cdfa04978
commit 506ad8fbb6
3 changed files with 27 additions and 9 deletions

View File

@@ -53,7 +53,10 @@ export class SolanaDeployTokenTool extends Tool {
async _call(input: string): Promise<string> {
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) {

View File

@@ -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}`);
}
}

View File

@@ -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;