Merge branch 'main' into feature/getBalanceOther

This commit is contained in:
aryan
2024-12-30 06:33:33 +05:30
25 changed files with 287 additions and 221 deletions

View File

@@ -9,7 +9,6 @@ import {
import { create_image } from "../tools/create_image";
import { BN } from "@coral-xyz/anchor";
import { FEE_TIERS } from "../tools";
import { toJSON } from "../utils/toJSON";
export class SolanaBalanceTool extends Tool {
name = "solana_balance";
@@ -361,7 +360,7 @@ export class SolanaRegisterDomainTool extends Tool {
protected async _call(input: string): Promise<string> {
try {
const parsedInput = toJSON(input);
const parsedInput = JSON.parse(input);
this.validateInput(parsedInput);
const tx = await this.solanaKit.registerDomain(
@@ -1298,7 +1297,7 @@ export class SolanaRockPaperScissorsTool extends Tool {
protected async _call(input: string): Promise<string> {
try {
const parsedInput = toJSON(input);
const parsedInput = JSON.parse(input);
this.validateInput(parsedInput);
const result = await this.solanaKit.rockPaperScissors(
Number(parsedInput['"amount"']),

View File

@@ -1,11 +1,7 @@
import {
VersionedTransaction,
PublicKey,
LAMPORTS_PER_SOL,
} from "@solana/web3.js";
import { VersionedTransaction, PublicKey } from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { TOKENS, DEFAULT_OPTIONS, JUP_API } from "../constants";
import { getMint } from "@solana/spl-token";
/**
* Swap tokens using Jupiter Exchange
* @param agent SolanaAgentKit instance
@@ -23,12 +19,23 @@ export async function trade(
slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS,
): Promise<string> {
try {
// Check if input token is native SOL
const isNativeSol = inputMint.equals(TOKENS.SOL);
// For native SOL, we use LAMPORTS_PER_SOL, otherwise fetch mint info
const inputDecimals = isNativeSol
? 9 // SOL always has 9 decimals
: (await getMint(agent.connection, inputMint)).decimals;
// Calculate the correct amount based on actual decimals
const scaledAmount = inputAmount * Math.pow(10, inputDecimals);
const quoteResponse = await (
await fetch(
`${JUP_API}/quote?` +
`inputMint=${inputMint.toString()}` +
`inputMint=${isNativeSol ? TOKENS.SOL.toString() : inputMint.toString()}` +
`&outputMint=${outputMint.toString()}` +
`&amount=${inputAmount * LAMPORTS_PER_SOL}` +
`&amount=${scaledAmount}` +
`&slippageBps=${slippageBps}` +
`&onlyDirectRoutes=true` +
`&maxAccounts=20`,

View File

@@ -1,26 +0,0 @@
export const toJSON = (str: string): any => {
try {
// Remove curly braces and split by comma
const pairs = str.trim().slice(1, -1).split(",");
// Convert to object with explicit type
const obj: Record<string, any> = {};
pairs.forEach((pair) => {
const [key, value] = pair
.trim()
.split(":")
.map((s) => s.trim());
if (!key || value === undefined) {
throw new Error("Invalid key-value pair format");
}
obj[key] = isNaN(Number(value)) ? value : Number(value);
});
return JSON.parse(JSON.stringify(obj));
} catch (error) {
throw new Error(`Failed to parse string to JSON: ${error}`);
}
};