ston-fi / sdk

SDK for building applications on STON.fi protocol

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting error while swapping tokens

oodles-prince opened this issue · comments

image

Below is my code, Please check i have tried several things but still same issue,

require("buffer");
const TonWeb = require("tonweb");
const { DEX, pTON } = require("@ston-fi/sdk");
const { TonClient, WalletContractV3R2, internal } = require("@ton/ton");
const { mnemonicToPrivateKey } = require("@ton/crypto");

async function swap() {
  const JETTON0 = "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs";
  const JETTON1 = "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO";
  try {
    const provider = new TonWeb.HttpProvider(
      "https://toncenter.com/api/v2/jsonRPC",
      {
        apiKey:
          "77da25e17ae08135df5518eedcc919eec30d3f5b18ae5b67122bc1ba1930d99a",
      }
    );
    const router = new DEX.v1.Router({
      tonApiClient: provider,
    });

    // Create Client
    const client = new TonClient({
      endpoint: "https://toncenter.com/api/v2/jsonRPC",
      apiKey:
        "77da25e17ae08135df5518eedcc919eec30d3f5b18ae5b67122bc1ba1930d99a",
    });
    
     const mnemonics = [// phrase];
     
      const keyPair = await mnemonicToPrivateKey(mnemonics);
    const workchain = 0;
    const wallet = WalletContractV3R2.create({
      workchain,
      publicKey: keyPair.publicKey,
    });
    const contract = client.open(wallet);

    const swapTxParams = await router.buildSwapTonToJettonTxParams({
      userWalletAddress: wallet.address.toString(),
      proxyTonAddress: pTON.v1.address,
      offerAmount: new TonWeb.utils.BN("1000000000"),
      askJettonAddress: "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs", // USDT
      minAskAmount: new TonWeb.utils.BN("1"),
      queryId: 9654790823,
    });

    const balance = await contract.getBalance();

    const seqno = await contract.getSeqno();
    console.log("swapTxParams-----------", swapTxParams);
    const transfer =  contract.createTransfer({
      seqno,
      secretKey: keyPair.secretKey,
      messages: [
        internal({
          to: swapTxParams.to ,
          value: swapTxParams.gasAmount,
          body: swapTxParams.payload,
        }),
      ],
    });

    console.log("Wallet Balance:---------------", balance,);
  } catch (error) {
    console.error("Error occurred during swap:", error);
  }
}

swap();

The error is in the missed mapping of the swapTxParams.to and swapTxParams.payload fields from the tonweb format to the format expected by the @ton/ton package.

Screenshot 2024-07-25 at 14 09 21

Please check this DOCs section with the example of sending swap via the @ton package

In the latest version of the SDK, we moved from the ton web package, so this transformation is no longer needed if you are using @ston-fi/sdk:1.0.0 or higher. Here is the migration guide if you want to migrate to the latest version https://docs.ston.fi/docs/developer-section/sdk/0.5-migration-guide