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

Swap Jetton to Ton #8

Open
kasahh opened this issue Jul 14, 2024 · 4 comments
Open

Swap Jetton to Ton #8

kasahh opened this issue Jul 14, 2024 · 4 comments

Comments

@kasahh
Copy link

kasahh commented Jul 14, 2024

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

@ybzy-xkd
Copy link

ybzy-xkd commented Aug 1, 2024

Have you found a solution?

@kasahh
Copy link
Author

kasahh commented Aug 1, 2024

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

@ybzy-xkd
Copy link

ybzy-xkd commented Aug 1, 2024

I also encountered this problem and was returned inexplicably.

@ncsft
Copy link

ncsft commented Aug 1, 2024

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));

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

3 participants