sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Swap Jetton to Ton

Open kasahh opened this issue 1 year ago • 8 comments

I am trying to swap jetton to ton programmatically using the "Swapping Jettons" example here: https://docs.dedust.io/docs/swaps. For some reasons it doesn't work and I have no idea why. 0.3Ton gets sent out and 0.2999Ton is immediately returned.

This is my code:

const tonClient = new TonClient4({ endpoint: "https://mainnet-v4.tonhubapi.com" });

const publicKey = keyPair.publicKey;
const privateKey = keyPair.secretKey;

const walletContract = tonClient.open(
    WalletContractV4.create({
        workchain: 0,
        publicKey,
    }),
)

const sender = walletContract.sender(privateKey)


const JETTON_ADDRESS = Address.parse('EQBlqsm144Dq6SjbPI4jjZvA1hqTIP3CvHovbIfW_t-SCALE');

const factory = tonClient.open(Factory.createFromAddress(MAINNET_FACTORY_ADDR));

const jettonRoot = tonClient.open(JettonRoot.createFromAddress(JETTON_ADDRESS));
const jettonWallet = tonClient.open(await jettonRoot.getWallet(sender.address));

const jettonVault = tonClient.open(await factory.getJettonVault(JETTON_ADDRESS));
const TON = Asset.native();
const JETTON = Asset.jetton(JETTON_ADDRESS);
const pool = tonClient.open(await factory.getPool(PoolType.VOLATILE, [JETTON, TON]));

if ((await pool.getReadinessStatus()) !== ReadinessStatus.READY) {
    throw new Error("Pool (TON, JETTON) does not exist.");
}

const amountIn = BigInt(Math.floor(jettonIn)); // 50 SCALE

await jettonWallet.sendTransfer(sender, toNano(0.3), {
amount: amountIn,
destination: jettonVault.address,
responseAddress: sender.address, // return gas to user
forwardAmount: toNano(gasAmount),
forwardPayload: VaultJetton.createSwapPayload({ poolAddress: pool.address }),
});

`

This is the transaction on chain: https://tonviewer.com/transaction/b229c7b02616f8b698fa1efa602fb89070f9fd77fa7c49fde6c11d00d01f0d8e

kasahh avatar Jul 14 '24 20:07 kasahh

Have you found a solution?

ybzy-xkd avatar Aug 01 '24 07:08 ybzy-xkd

No I didn't, I settled for the python version

kasahh avatar Aug 01 '24 08:08 kasahh

I also encountered this problem and was returned inexplicably.

ybzy-xkd avatar Aug 01 '24 08:08 ybzy-xkd

Debug variable sender.address, u will be surprised Then change const jettonWallet = tonClient.open(await jettonRoot.getWallet(sender.address)); to const jettonWallet = tonClient.open(await jettonRoot.getWallet(walletContract.address));

ncsft avatar Aug 01 '24 21:08 ncsft

Debug variable sender.address, u will be surprised

It worked after changing: const jettonWallet = tonClient.open(await jettonRoot.getWallet(sender.address)); to const jettonWallet = tonClient.open(await jettonRoot.getWallet(walletContract.address)); Thank you!

laptrinhbockchain avatar Oct 09 '24 13:10 laptrinhbockchain

i solved this by ensuring forward_amount < attached_amount

await jettonWallet.sendTransfer(
    sender,
    toNano("0.3"), // attached_amount
    {
      amount: tokenInAmount,
      destination: vault.address,
      responseAddress: wallet.address, // return gas
      forwardAmount: toNano("0.25"), // forward_amount has to be less than attached_amount
      forwardPayload: VaultJetton.createSwapPayload({
        poolAddress: pool.address,
        limit: minAmountOut,
      }),
    },
  );

jessepinkman9900 avatar Oct 13 '24 16:10 jessepinkman9900

with WalletContractV4 sendTransfer method returns error. i changed WalletContractV4 to WalletContractV5R1 and the error is gone. the send API on the network returns 200 OK status now. but still no swap happens in the wallet. any idea?

Unknown47Dream avatar Nov 10 '24 08:11 Unknown47Dream

replace the sender. address with the wallet. add this function to estimate the gas do not pass the calculated estimated gas like this const { amountOut: expectedAmountOut } = await pool.getEstimatedSwapOut({ assetIn, amountIn, });

await jettonWallet.sendTransfer(
    sender,
    expectedAmountOut,    {
      amount: tokenInAmount,
      destination: vault.address,
      responseAddress: wallet.address, // return gas
      forwardPayload: VaultJetton.createSwapPayload({
        poolAddress: pool.address,
        limit: minAmountOut,
      }),
    },
  );
``` this is worked I have done with live project

asadullah097 avatar Nov 22 '24 07:11 asadullah097