Search code examples
swapsolana

Is possible SWAP sol to USDC?


I am trying to SWAP from SOL to USDC, this is what I have tried with web3js, Raydium and even Jupiter, none of these integrations have returned a positive result, on the other hand I do not find much documentation on the subject, I know how to send TOKEN -> TOKEN but no CRYPTO (SOL) -> STABLE-COIN (USDC)

What I am trying to do is make the SWAP work now with Jupiter, this has been impossible, there is a way to make SWAP between SOL and USDC?

Code Example

const jupiter = await Jupiter.load({
            connection: this.connection,
            cluster: "mainnet-beta",
            user: wallet,
        });

        const tokens = await this.getTokens();

        console.log(tokens);


        const routeMap: Map<string, string[]> = jupiter.getRouteMap();
        const outputToken = tokens.find((t) => t.address == "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
        const inputToken = tokens.find((t) => t.address == environment.swap.WrappedSolAddress);

        const routes = await jupiter.computeRoutes({
            inputMint: new PublicKey(inputToken.address),
            outputMint: new PublicKey(outputToken.address),
            inputAmount: LAMPORTS_PER_SOL * 1,
            slippage: 1,
        });

        const { execute } = await jupiter.exchange({
            routeInfo: routes.routesInfos[0],
        });

        const swapResult: any = await execute();

Error

TypeError: Cannot read properties of undefined (reading 'address')
at SolanaService.<anonymous> (/home/foo/GitHub/foo/dist/apps/foo/webpack:/doo/apps/foo-api/src/app/services/solana/solana.service.ts:208:55)

Solution

  • The way you're fetching tokens might be wrong

    import {Jupiter,TOKEN_LIST_URL} from "@jup-ag/core"
    
    const tokens = await (await fetch(TOKEN_LIST_URL["devnet"])).json();
    console.log(tokens.find((t)=> t.address == "7duMWSNdYMof6WKZHs5X1wdmmxUa6cDGqqKShhMSGkgg"))