import React, { useState, useEffect, useMemo } from 'react'; import './v2.css'; import { coinWithBalance, Transaction } from '@mysten/sui/transactions'; import { getSuiClient } from '../src/config/sui'; import { MIST_PER_SUI } from '@mysten/sui/dist/cjs/utils'; export const getSendSuiCoinTx = async ({ fromAddress, toAddress, amount, coinType, }: { fromAddress: string; toAddress: string; amount: string; coinType?: string; }): Promise<{ transaction: string; bytes: Uint8Array } | null> => { const suiClient = getSuiClient(); const txb = new Transaction(); txb.transferObjects( [coinWithBalance({ balance: Number(amount), type: coinType || undefined })], toAddress ); try { txb.setSender(fromAddress); const transaction = (await txb.toJSON({ client: suiClient })).toString(); const bytes = await txb.build({ client: suiClient }); console.log('bytes', transaction, bytes); return { transaction, bytes }; } catch (e) { console.warn(e); return null; } }; const tg_tomo_sui = window.tomo_sui; const usdtToken = '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN'; export default function Index() { const [connect, setConnect] = useState(false); const [addr, setAddr] = useState(''); const [toAddr1, setToAddr1] = useState(''); const [toValue1, setToValue1] = useState('0.1'); const [signRes, setSignRes] = useState(''); const [signMsg, setSignMsg] = useState('hello tomo'); const [signMsgRes, setSignMsgRes] = useState(''); const [contractAddr, setContractAddr] = useState(usdtToken); const [toAddr2, setToAddr2] = useState(''); const [toValue2, setToValue2] = useState('0.1'); const [signRes2, setSignRes2] = useState(''); const [sendTxHash, setSendTxHash] = useState(''); const [sendTokenTxHash, setSendTokenTxHash] = useState(''); useEffect(() => { walletAddressReq(); }, []); const canSend = useMemo(() => { return !!addr && !!toAddr1 && !!toValue1; }, [addr, toAddr1, toValue1]); const walletAddressReq = async () => { const address = tg_tomo_sui.getAddress(); setAddr(address); setConnect(!!address); }; const connectWallet = async () => { if (connect) { await tg_tomo_sui.disconnect(); setAddr(''); setConnect(false); return; } const res = await tg_tomo_sui.connectWallet(); setAddr(res.address); setConnect(tg_tomo_sui.isConnected); }; const getTX4Step1 = async () => { if (!addr || !toAddr1 || !toValue1) return; const res = await getSendSuiCoinTx({ fromAddress: addr, toAddress: toAddr1, amount: +toValue1 * Number(MIST_PER_SUI) + '', }); const { transaction, bytes } = res || {}; const input = { transactionBlock: transaction, chain: 'sui:mainnet', txBytes: bytes, options: { showEffects: true, }, }; console.log('transaction', transaction, bytes); return input; }; const getTX4Step3 = async () => { const res = await getSendSuiCoinTx({ fromAddress: addr, toAddress: toAddr2, amount: +toValue2 * 10 ** 6 + '', coinType: contractAddr, }); const { transaction, bytes } = res || {}; const input = { transactionBlock: transaction, chain: 'sui:mainnet', txBytes: bytes, options: { showEffects: true, }, }; console.log('transaction token', transaction, bytes); return input; }; const signTx = async () => { if (!canSend) return; const input = await getTX4Step1(); if (!input?.txBytes) return; const response = await tg_tomo_sui.signTransaction(input); setSignRes(response); }; const signAndSendTx = async () => { if (!canSend) return; const input = await getTX4Step1(); if (!input?.txBytes) return; const res = await tg_tomo_sui.signAndExecuteTransaction(input); setSendTxHash(JSON.stringify(res)); }; const signMessage = async () => { if (signMsg && addr) { const response = await tg_tomo_sui.signMessage(signMsg); console.log('Multi Chain Tomo Wallet Sign result:', response.result); setSignMsgRes(response); } }; const canSendStep3 = useMemo(() => { return !!addr && !!toAddr2 && !!toValue2; }, [addr, toAddr2, toValue2]); const signTokenTx = async () => { if (!canSendStep3) return; const input = await getTX4Step3(); if (!input?.txBytes) return; const response = await tg_tomo_sui.signTransaction(input); setSignRes2(JSON.stringify(response)); }; const signAndSendTokenTx = async () => { if (!canSendStep3) return; const input = await getTX4Step3(); if (!input?.txBytes) return; const res = await tg_tomo_sui.signAndExecuteTransaction(input); setSendTokenTxHash(JSON.stringify(res)); console.log('Multi Chain Tomo Wallet sign eth tx success', res?.result); }; return (

SUI Demo For Tomo TG Wallet

{addr ? 'My Tomo Wallet SUI Address' : 'Connect To Tomo Wallet'}

{addr ?? ''}
{addr ? 'Disconnect' : 'Connect'}

1. Sign SUI Transaction

Please input data

To Address:

{ const v = e.target.value; setToAddr1(v); setSignRes(''); }} />

Value:

{ const v = e.target.value; setToValue1(v); setSignRes(''); }} /> <> {signRes ? ( <>

Sign Result:

{signRes ?? ''}
) : null} <> {sendTxHash ? ( <>

send Result:

{sendTxHash ?? ''}
) : null}
Sign Tx
Sign and Send Tx

2. Sign message by private key

Click button below to sign your msg <> {signMsgRes ? ( <>

Sign Msg Result:

{signMsgRes ?? ''}
) : null}
Sign Msg

3. Sign SUI Token Transaction

Please input data

Contract Address:

{ const v = e.target.value; setContractAddr(v); setSignRes2(''); }} />

To Address:

{ const v = e.target.value; setToAddr2(v); setSignRes2(''); }} />

Amount:

{ const v = e.target.value; setToValue2(v); setSignRes2(''); }} /> <> {signRes2 ? ( <>

Sign Result:

{signRes2 ?? ''}
) : null} <> {sendTokenTxHash ? ( <>

sendRes Result:

{sendTokenTxHash ?? ''}
) : null}
Sign Token Tx
Sign and Send Token Tx
); }