From a911ad222f9319ed0ff8cb2dce5e533ac7cbfa63 Mon Sep 17 00:00:00 2001 From: ChefEric <173023571+chef-eric@users.noreply.github.com> Date: Fri, 7 Feb 2025 16:44:55 +0800 Subject: [PATCH 1/3] fix: integrate swap to sc --- apps/ton/src/ton/context/TonContext.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/ton/src/ton/context/TonContext.ts b/apps/ton/src/ton/context/TonContext.ts index 98a62c283db89..7f51cd8a3b962 100644 --- a/apps/ton/src/ton/context/TonContext.ts +++ b/apps/ton/src/ton/context/TonContext.ts @@ -7,12 +7,15 @@ import { Emiter } from 'ton/utils/Emiter' export class TonContext extends Emiter { private tonClient?: TonClient + private endpoint?: string + constructor() { super() const { network } = tonState getHttpEndpoint({ network: network === TonNetworks.Mainnet ? 'mainnet' : 'testnet' }).then((endpoint) => { + this.endpoint = endpoint this.tonClient = new TonClient({ endpoint }) }) } @@ -21,5 +24,9 @@ export class TonContext extends Emiter { return this.tonClient! } + public getEndPoint() { + return this.endpoint + } + public static instance = new TonContext() } From 856500fd46147fb9a86639b7bcfad3941b413628 Mon Sep 17 00:00:00 2001 From: ChefEric <173023571+chef-eric@users.noreply.github.com> Date: Sat, 8 Feb 2025 01:23:15 +0800 Subject: [PATCH 2/3] fix: add liquidity transaction succ --- apps/ton/src/ton/context/TonContext.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/apps/ton/src/ton/context/TonContext.ts b/apps/ton/src/ton/context/TonContext.ts index 7f51cd8a3b962..98a62c283db89 100644 --- a/apps/ton/src/ton/context/TonContext.ts +++ b/apps/ton/src/ton/context/TonContext.ts @@ -7,15 +7,12 @@ import { Emiter } from 'ton/utils/Emiter' export class TonContext extends Emiter { private tonClient?: TonClient - private endpoint?: string - constructor() { super() const { network } = tonState getHttpEndpoint({ network: network === TonNetworks.Mainnet ? 'mainnet' : 'testnet' }).then((endpoint) => { - this.endpoint = endpoint this.tonClient = new TonClient({ endpoint }) }) } @@ -24,9 +21,5 @@ export class TonContext extends Emiter { return this.tonClient! } - public getEndPoint() { - return this.endpoint - } - public static instance = new TonContext() } From 94518e2aa77ec9f1f429fb1378eb3d6f0b86daa9 Mon Sep 17 00:00:00 2001 From: ChefEric <173023571+chef-eric@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:56:00 +0800 Subject: [PATCH 3/3] fix: integrate addLiquidity and swap to sc --- apps/ton/src/pages/testMint/index.tsx | 209 ------------------ .../ton/logic/liquidity/useAddLiquidity.ts | 62 +----- apps/ton/src/ton/logic/swap/useSwap.ts | 1 - apps/ton/src/ton/wrappers/tact_Router.ts | 8 +- 4 files changed, 6 insertions(+), 274 deletions(-) delete mode 100644 apps/ton/src/pages/testMint/index.tsx diff --git a/apps/ton/src/pages/testMint/index.tsx b/apps/ton/src/pages/testMint/index.tsx deleted file mode 100644 index e8d3c80edf08e..0000000000000 --- a/apps/ton/src/pages/testMint/index.tsx +++ /dev/null @@ -1,209 +0,0 @@ -import { Box, Button, FlexGap, Select, Text } from '@pancakeswap/uikit' -import { beginCell, fromNano, toNano } from '@ton/core' -import { SendTransactionRequest, useTonConnectUI, useTonWallet } from '@tonconnect/ui-react' -import { fetchListAtom } from 'atoms/lists/fetchListAtom' -import { Header } from 'components/Header' -import Container from 'components/Layout/Container' -import { useAtomValue } from 'jotai' -import { useCallback, useState } from 'react' -import { TonContext } from 'ton/context/TonContext' -import { Contracts } from 'ton/def/contracts.def' -import { useAddLiquidity } from 'ton/logic/liquidity/useAddLiquidity' -import { useSwap } from 'ton/logic/swap/useSwap' -import { TonContractNames } from 'ton/ton.enums' -import { parseAddress } from 'ton/utils/address' -import { JettonMasterUSDT } from 'ton/wrappers/tact_JettonMasterUSDT' -import { JettonWalletUSDT } from 'ton/wrappers/tact_JettonWalletUSDT' -import { Router } from 'ton/wrappers/tact_Router' - -export default function TestMint() { - const { data: activeList } = useAtomValue(fetchListAtom) - const tokens = activeList || [] - - // const [amount, setAmount] = useState('') - const [selectedToken, setSelectedToken] = useState(tokens[0] || undefined) - - const [resultMessage, setResultMessage] = useState('') - - const wallet = useTonWallet() - const [tonUI] = useTonConnectUI() - - const { addLiquidity } = useAddLiquidity() - const { swap } = useSwap() - - // useAtomValue( - // poolAddressAtom({ - // token0Address: 'kQArzX0-In2BjRhaq5pB2vmZH80saystVwwbPIpEyGrh723F', // $SYRUP - // token1Address: 'kQABtdKCYuAAIrEAD4LbONdybLTYsYleyYhsy6CfsXkkP0tg', // $P - // }), - // ) - - const handleMint = useCallback(() => { - if (!selectedToken || !wallet?.account.address) throw new Error('Invalid input provided!') - - const body = beginCell() - // .store( - // storeJettonMintMessage({ - // amount: toNano(amount), - // to: parseAddress(wallet?.account?.address), - // forwardPayload: null, - // forwardTonAmount: 0n, - // from: parseAddress(wallet.account.address), - // queryId: 1n, - // responseAddress: parseAddress(wallet.account.address), - // walletForwardValue: 0n, - // }), - // ) - - .storeUint(0, 32) - .storeStringTail('Mint:1') - - .endCell() - - const txRequest: SendTransactionRequest = { - validUntil: Math.floor(Date.now() / 1000) + 60, - messages: [ - { - address: selectedToken?.address, - amount: toNano('1').toString(), - payload: body.toBoc().toString('base64'), - }, - ], - } - - tonUI.sendTransaction(txRequest) - }, [selectedToken, tonUI, wallet?.account.address]) - - const estimateAddLiquidity = useCallback(async () => { - if (!wallet?.account.address) throw new Error('Wallet not connected') - const client = TonContext.instance.getClient() - const contractAddress = parseAddress(Contracts[TonContractNames.PCSRouter].testnet.address) - - const router = client.open(Router.fromAddress(contractAddress)) - - const result = await router.getEstimateAddLiquidity(1000000n) - - setResultMessage(`Estimated Fee: ${fromNano(result)}`) - }, [wallet?.account.address]) - - const readJetton = useCallback(async () => { - if (!wallet?.account.address) throw new Error('Wallet not connected') - - const client = TonContext.instance.getClient() - const contractAddress = parseAddress(Contracts[TonContractNames.USDC].testnet.address) - const jettonMaster = client.open(JettonMasterUSDT.fromAddress(contractAddress)) - const jettonWalletAddress = await jettonMaster.getGetWalletAddress(parseAddress(wallet?.account.address)) - - const jettonData = await jettonMaster.getGetJettonData() - - const jettonWallet = client.open(JettonWalletUSDT.fromAddress(jettonWalletAddress)) - const result = JSON.stringify(jettonWallet.getGetWalletData()) - - console.log('Jetton result:', { jettonData, result }) - setResultMessage(`Jetton result: ${jettonWalletAddress} ${result}`) - }, [wallet?.account.address]) - - const handleAddLiquidity = useCallback(() => { - if (!wallet || !wallet?.account.address) throw new Error('Wallet not connected') - - // addLiquidity({ - // amount0: toNano(100), - // amount1: toNano(100), - // token0Address: 'kQArzX0-In2BjRhaq5pB2vmZH80saystVwwbPIpEyGrh723F', // $SYRUP - // token1Address: 'kQABtdKCYuAAIrEAD4LbONdybLTYsYleyYhsy6CfsXkkP0tg', // $PAN - // }) - }, [wallet]) - - const handleSwap = useCallback(() => { - if (!wallet || !wallet?.account.address) throw new Error('Wallet not connected') - - // swap({ - // amount0: '100', - // minOut: '0', - // token0: 'kQArzX0-In2BjRhaq5pB2vmZH80saystVwwbPIpEyGrh723F', // $SYRUP - // token1: 'kQABtdKCYuAAIrEAD4LbONdybLTYsYleyYhsy6CfsXkkP0tg', // $PAN - // }) - }, [wallet]) - - const getPoolAddress = useCallback(async () => { - const client = TonContext.instance.getClient() - const routerAddress = parseAddress(Contracts[TonContractNames.PCSRouter].testnet.address) - const router = client.open(Router.fromAddress(routerAddress)) - - const token0Address = parseAddress('kQArzX0-In2BjRhaq5pB2vmZH80saystVwwbPIpEyGrh723F') // $SYRUP - const token1Address = parseAddress('kQABtdKCYuAAIrEAD4LbONdybLTYsYleyYhsy6CfsXkkP0tg') // $PAN - - const jetton0 = client.open(JettonMasterUSDT.fromAddress(token0Address)) - const jetton1 = client.open(JettonMasterUSDT.fromAddress(token1Address)) - - const [routerJettonWallet0, routerJettonWallet1] = await Promise.all([ - jetton0.getGetWalletAddress(routerAddress), - jetton1.getGetWalletAddress(routerAddress), - ]) - - const poolAddress = await router.getGetPoolAddress(routerJettonWallet0, routerJettonWallet1) - - setResultMessage(`Pool Address: ${poolAddress.toString()}`) - }, []) - - return ( - <> -
- - - Mint Test Tokens - - To be minted: 100 - - setAmount(e.target.value)} /> */} - - - - - {wallet?.account ? ( - <> - Wallet Connected - {wallet?.account.address} - Chain: {wallet?.account.chain} - - ) : ( - Wallet not connected - )} - - - - - Test Contracts - - - {resultMessage} - - - Router - - - - - - - - - - Jetton - - - - - - ) -} diff --git a/apps/ton/src/ton/logic/liquidity/useAddLiquidity.ts b/apps/ton/src/ton/logic/liquidity/useAddLiquidity.ts index 255064ef60b54..8c30f3e44e2b8 100644 --- a/apps/ton/src/ton/logic/liquidity/useAddLiquidity.ts +++ b/apps/ton/src/ton/logic/liquidity/useAddLiquidity.ts @@ -34,70 +34,12 @@ export const useAddLiquidity = () => { const routerJettonWallet0 = await getJettonWalletAddress(client, routerAddress, token0) const routerJettonWallet1 = await getJettonWalletAddress(client, routerAddress, token1) - // const forwardPayload0 = beginCell() - // .storeUint(TON_OPCODES.ADD_LIQUIDITY, 32) - // .storeAddress(routerJettonWallet1) - // .storeCoins(toNano(900)) // MinLP - // .endCell() - // const payload0 = beginCell() - // .store( - // storeJettonTransferMessage({ - // queryId: 1n, - // amount: amount0, - // destination: routerAddress, - // responseDestination: walletAddress, - // customPayload: null, - // forwardAmount: toNano('0.1'), - // forwardPayload: forwardPayload0, - // }), - // ) - // .endCell() - - // const forwardPayload1 = beginCell() - // .storeUint(TON_OPCODES.ADD_LIQUIDITY, 32) - // .storeAddress(routerJettonWallet0) - // .storeCoins(toNano(900)) // MinLP - // .endCell() - // const payload1 = beginCell() - // .store( - // storeJettonTransferMessage({ - // queryId: 2n, - // amount: amount1, - // destination: routerAddress, - // responseDestination: walletAddress, - // customPayload: null, - // forwardAmount: toNano('0.1'), - // forwardPayload: forwardPayload1, - // }), - // ) - // .endCell() - - // const txRequest: SendTransactionRequest = { - // validUntil: Math.floor(Date.now() / 1000) + 60 * 2, - // messages: [ - // { - // address: userJettonWallet0.toString(), - // amount: toNano('0.2').toString(), - // payload: payload0.toBoc().toString('base64'), - // }, - // { - // address: userJettonWallet1.toString(), - // amount: toNano('0.2').toString(), - // payload: payload1.toBoc().toString('base64'), - // }, - // ], - // } - - // tonUI.sendTransaction(txRequest) - - /// NEW WAY OF SENDING ADD LIQUIDITY TXN - const newForwardPayload0 = beginCell() .store( storeAddLiquidity({ queryId: 1n, $$type: 'AddLiquidity', - minLPOut: 0n, + minLPOut: 1n, tokenWallet: routerJettonWallet1, }), ) @@ -121,7 +63,7 @@ export const useAddLiquidity = () => { storeAddLiquidity({ queryId: 2n, $$type: 'AddLiquidity', - minLPOut: 0n, + minLPOut: 1n, tokenWallet: routerJettonWallet0, }), ) diff --git a/apps/ton/src/ton/logic/swap/useSwap.ts b/apps/ton/src/ton/logic/swap/useSwap.ts index 8ef9254b3de89..8b0f9012c7f34 100644 --- a/apps/ton/src/ton/logic/swap/useSwap.ts +++ b/apps/ton/src/ton/logic/swap/useSwap.ts @@ -86,7 +86,6 @@ export const useSwap = () => { const routerAddress = parseAddress(Contracts[TonContractNames.PCSRouter].testnet.address) const userJettonWallet0 = await getJettonWalletAddress(client, userAddress, token0) - const userJettonWallet1 = await getJettonWalletAddress(client, userAddress, token1) const routerJettonWallet1 = await getJettonWalletAddress(client, routerAddress, token1) const forwardPayload = beginCell() diff --git a/apps/ton/src/ton/wrappers/tact_Router.ts b/apps/ton/src/ton/wrappers/tact_Router.ts index 3011490010214..5adf31b579ec5 100644 --- a/apps/ton/src/ton/wrappers/tact_Router.ts +++ b/apps/ton/src/ton/wrappers/tact_Router.ts @@ -4730,13 +4730,13 @@ function loadGetterTupleAddLiquidity(source: TupleReader) { function storeTupleAddLiquidity(source: AddLiquidity) { let builder = new TupleBuilder() builder.writeNumber(source.queryId) - builder.writeNumber(source.newAmount0) - builder.writeNumber(source.newAmount1) + // builder.writeNumber(source.newAmount0) + // builder.writeNumber(source.newAmount1) builder.writeNumber(source.minLPOut) return builder.build() } -function dictValueParserAddLiquidity(): DictionaryValue { +/* function dictValueParserAddLiquidity(): DictionaryValue { return { serialize: (src, builder) => { builder.storeRef(beginCell().store(storeAddLiquidity(src)).endCell()) @@ -4745,7 +4745,7 @@ function dictValueParserAddLiquidity(): DictionaryValue { return loadAddLiquidity(src.loadRef().beginParse()) }, } -} +} */ export type LPAccountData = { $$type: 'LPAccountData'