mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-17 15:10:27 +00:00
Fix: if token is sol
This commit is contained in:
@@ -22,20 +22,26 @@ export async function trade(
|
||||
slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS,
|
||||
): Promise<string> {
|
||||
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user