@@ -7,13 +7,40 @@ import {
7
7
type Action ,
8
8
} from "../core/types.ts" ;
9
9
10
+ async function getTokenDecimals (
11
+ connection : Connection ,
12
+ mintAddress : string
13
+ ) : Promise < number > {
14
+ const mintPublicKey = new PublicKey ( mintAddress ) ;
15
+ const tokenAccountInfo =
16
+ await connection . getParsedAccountInfo ( mintPublicKey ) ;
17
+
18
+ // Check if the data is parsed and contains the expected structure
19
+ if (
20
+ tokenAccountInfo . value &&
21
+ typeof tokenAccountInfo . value . data === "object" &&
22
+ "parsed" in tokenAccountInfo . value . data
23
+ ) {
24
+ const parsedInfo = tokenAccountInfo . value . data . parsed ?. info ;
25
+ if ( parsedInfo && typeof parsedInfo . decimals === "number" ) {
26
+ return parsedInfo . decimals ;
27
+ }
28
+ }
29
+
30
+ throw new Error ( "Unable to fetch token decimals" ) ;
31
+ }
32
+
10
33
async function getQuote (
34
+ connection : Connection ,
11
35
baseToken : string ,
12
36
outputToken : string ,
13
37
amount : number
14
38
) : Promise < any > {
39
+ const decimals = await getTokenDecimals ( connection , baseToken ) ;
40
+ const adjustedAmount = amount * 10 ** decimals ;
41
+
15
42
const quoteResponse = await fetch (
16
- `https://quote-api.jup.ag/v6/quote?inputMint=${ baseToken } &outputMint=${ outputToken } &amount=${ amount * 10 ** 6 } &slippageBps=50`
43
+ `https://quote-api.jup.ag/v6/quote?inputMint=${ baseToken } &outputMint=${ outputToken } &amount=${ adjustedAmount } &slippageBps=50`
17
44
) ;
18
45
const swapTransaction = await quoteResponse . json ( ) ;
19
46
const swapTransactionBuf = Buffer . from ( swapTransaction , "base64" ) ;
@@ -100,6 +127,7 @@ export const executeSwap: Action = {
100
127
) ;
101
128
102
129
const quoteData = await getQuote (
130
+ connection as Connection ,
103
131
inputToken as string ,
104
132
outputToken as string ,
105
133
amount as number
0 commit comments