magicblock-labs / Solana.Unity-SDK

Open-Source Unity-Solana SDK with Full RPC coverage, NFT support and more
https://solana.unity-sdk.gg
MIT License
164 stars 90 forks source link

Jupiter swap failed - Transaction failed to sanitize accounts offsets correctly #225

Open Zeddex opened 5 months ago

Zeddex commented 5 months ago

I'm getting error "invalid transaction: Transaction failed to sanitize accounts offsets correctly" making Jupiter swap.

string tokenIn = "SOL";
string tokenOut = "PONKE";
decimal amount = 0.001m;
public class JupiterSwap
{
    public IRpcClient RpcClient { get; } = ClientFactory.GetClient(Cluster.MainNet);
    public IDexAggregator Dex { get; }
    public Wallet Wallet { get; }

    public JupiterSwap(IRpcClient rpcClient, Wallet wallet) 
    {
        Wallet = wallet;
        RpcClient = rpcClient;
        Dex = new JupiterDexAg(Wallet.Account);
    }

    public async Task<string> MakeSwap(string tokenIn, string tokenOut, decimal inputAmount) 
    {
        TokenData tokenA = await Dex.GetTokenBySymbol(tokenIn);
        TokenData tokenB = await Dex.GetTokenBySymbol(tokenOut);

        SwapQuoteAg swapQuote = await Dex.GetSwapQuote(
            tokenA.MintAddress,
            tokenB.MintAddress,
            DecimalUtil.ToUlong(inputAmount, tokenA.Decimals)
        );

        var quoteOutput = DecimalUtil.FromBigInteger(swapQuote.OutputAmount, tokenB.Decimals);
        await Console.Out.WriteLineAsync($"Output Amount: {quoteOutput} {tokenOut}");
        await Console.Out.WriteLineAsync(string.Join(" -> ", swapQuote.RoutePlan.Select(p => p.SwapInfo.Label)));

        var swapTransaction = await Dex.Swap(swapQuote);
        swapTransaction.Sign(Wallet.Account);

        var txSignature = await RpcClient.SendTransactionAsync(swapTransaction.Serialize());

        return txSignature.Result;
    }
}
miaobei5555 commented 1 month ago

I had the same problem