From 3552ffe41c9e501fce783fcc37ca4060ef0c25f2 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Sun, 29 Dec 2024 06:01:19 -0300 Subject: [PATCH 1/2] Fix: change lamports by getting mintInfo --- src/tools/trade.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/tools/trade.ts b/src/tools/trade.ts index b42cc2a..0fc3ff0 100644 --- a/src/tools/trade.ts +++ b/src/tools/trade.ts @@ -1,11 +1,10 @@ import { VersionedTransaction, - PublicKey, - LAMPORTS_PER_SOL, + 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 +22,17 @@ export async function trade( slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS, ): Promise { try { + const mintInfo = await getMint(agent.connection, inputMint); + const inputDecimals = mintInfo.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()}` + `&outputMint=${outputMint.toString()}` + - `&amount=${inputAmount * LAMPORTS_PER_SOL}` + + `&amount=${scaledAmount}` + `&slippageBps=${slippageBps}` + `&onlyDirectRoutes=true` + `&maxAccounts=20`, From 6b9a347f56dda671aeaeb6841286cf92a6649102 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Sun, 29 Dec 2024 16:03:40 -0300 Subject: [PATCH 2/2] Fix: if token is sol --- src/tools/trade.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/tools/trade.ts b/src/tools/trade.ts index 0fc3ff0..3704308 100644 --- a/src/tools/trade.ts +++ b/src/tools/trade.ts @@ -22,20 +22,26 @@ export async function trade( slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS, ): Promise { try { - const mintInfo = await getMint(agent.connection, inputMint); - const inputDecimals = mintInfo.decimals; + // 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()}` + - `&outputMint=${outputMint.toString()}` + - `&amount=${scaledAmount}` + - `&slippageBps=${slippageBps}` + - `&onlyDirectRoutes=true` + - `&maxAccounts=20`, + `inputMint=${isNativeSol ? TOKENS.SOL.toString() : inputMint.toString()}` + + `&outputMint=${outputMint.toString()}` + + `&amount=${scaledAmount}` + + `&slippageBps=${slippageBps}` + + `&onlyDirectRoutes=true` + + `&maxAccounts=20`, ) ).json();