import React from "react";
import { TonClient, toNano } from "@ton/ton";
import { DEX, pTON } from "@ston-fi/sdk";
import { useTonConnectUI, useTonAddress } from "@tonconnect/ui-react";
const client = new TonClient({
endpoint: "https://toncenter.com/api/v2/jsonRPC",
});
const dex = client.open(new DEX.v1.Router());
export const Example = () => {
const wallet = useTonAddress();
const [tonConnectUI] = useTonConnectUI();
return (
<button
onClick={async () => {
const txParams = await dex.getSwapTonToJettonTxParams({
offerAmount: toNano("1"), // swap 1 TON
askJettonAddress: "EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO", // for a STON
minAskAmount: toNano("0.1"), // but not less than 0.1 STON
proxyTon: new pTON.v1(),
userWalletAddress: wallet,
});
await tonConnectUI.sendTransaction({
validUntil: Date.now() + 1000000,
messages: [
{
address: txParams.to.toString(),
amount: txParams.value.toString(),
payload: txParams.body?.toBoc().toString("base64"),
},
],
});
}}
>
Swap 1 TON to STON
</button>
);
};