Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error while swapping tokens #15

Closed
oodles-prince opened this issue May 6, 2024 · 1 comment
Closed

Getting error while swapping tokens #15

oodles-prince opened this issue May 6, 2024 · 1 comment

Comments

@oodles-prince
Copy link

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();
@pavel-rakhmanov
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants