import React, { useState, useEffect, useMemo, } from 'react'; import './v2.css'; import SolanaProvider from '../src/v2/provider/SolanaProvider/SolanaProvider'; import { ComputeBudgetProgram, PublicKey, Transaction, SystemProgram, } from '@solana/web3.js'; import { getConnection } from '../src/config/sol'; import { BASE_URL_DEV } from './baseUrlDev'; import WalletTgSdk from '../dist/tomoWalletTgSdkV2.esm'; new WalletTgSdk({ injected: true, ...BASE_URL_DEV }); const tg_tomo_sol = window.tomo_sol as SolanaProvider; const CHAINS = [ { chainId: 501, name: 'SOL', }, ]; const usdtToken = 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'; export default function Index() { const [connect, setConnect] = useState(false); const [addr, setAddr] = useState(''); const [toAddr1, setToAddr1] = useState(''); const [toValue1, setToValue1] = useState('0.1'); const [signMsgRes2, setSignMsgRes2] = useState(''); const [chainId, setChainId] = useState(1); const [signRes, setSignRes] = useState(''); const [signMsg, setSignMsg] = useState('hello tomo'); const [signMsgRes, setSignMsgRes] = useState(''); const [addFlag, setFlag] = useState(0); const [contractAddr, setContractAddr] = useState(usdtToken); const [toAddr2, setToAddr2] = useState(''); const [toValue2, setToValue2] = useState('0.1'); const [signRes2, setSignRes2] = useState(''); const [balance, setBalance] = useState(''); const [balanceAddr, setBalanceAddr] = useState(''); const [sendTxHash, setSendTxHash] = useState(''); const [signTxToAddr, setSignTxToAddr] = useState(''); const [signTxAmount, setSignTxAmount] = useState(0); const [signTxSignRes, setSignTxSignRes] = useState(''); const [signTxHash, setSignTxHash] = useState(''); useEffect(() => { walletAddressReq(); }, []); const switchChain = async chainId => { setChainId(chainId); }; const chainName = chainId => { const temp = {}; CHAINS.forEach(i => { temp[i.chainId] = i.name; }); return temp[chainId]; }; const getChainName = useMemo(() => { return chainName(chainId); }, [chainId]); const walletAddressReq = async () => { const address = tg_tomo_sol.getAddress(); setAddr(address); setBalanceAddr(address); setConnect(!!address); }; const connectWallet = async () => { if (connect) { await tg_tomo_sol.disconnect(); setAddr(''); setConnect(false); return; } const res = await tg_tomo_sol.connectWallet(); setAddr(res.address); setConnect(tg_tomo_sol.isConnected); }; const signTx = async () => { if (!addr || !toAddr1 || !toValue1) return; const val = toValue1; const response = await tg_tomo_sol.signTransaction({ from: addr, to: toAddr1, value: val, }); console.log( 'Multi Chain Tomo Wallet sign solana tx success', response.result ); setSignRes(response); // await tg_tomo_sol.sendTransaction(response) }; const constructTx = async () => { const fromPubkey = new PublicKey(addr); const toPubkey = new PublicKey(signTxToAddr); const tx = new Transaction(); const connection = getConnection(); tx.feePayer = fromPubkey; tx.recentBlockhash = (await connection.getLatestBlockhash('max')).blockhash; tx.add( SystemProgram.transfer({ fromPubkey, toPubkey, lamports: BigInt(+signTxAmount * 10 ** 9), }) ); const simulationResponse = await connection.simulateTransaction(tx); const fees = await tx.getEstimatedFee(connection); const unitsConsumed = simulationResponse.value.unitsConsumed; if (!unitsConsumed) { throw new Error( 'Failed to estimate gas. Please ensure you have enough funds in your wallet.' ); } const transactionSize = tx.serialize({ requireAllSignatures: false, verifySignatures: false, }).byteLength; const priorityFeeRecommendation = (unitsConsumed / transactionSize).toFixed( 0 ); console.log(`Estimated SOL transfer cost: ${fees} lamports`); const computeUnitIx = ComputeBudgetProgram.setComputeUnitLimit({ units: unitsConsumed * 4, }); const computeBudgetIx = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: unitsConsumed * 2, }); console.log({ unitsConsumed, priorityFeeRecommendation }); tx.add(computeUnitIx); tx.add(computeBudgetIx); console.log({ key: 'wrapped-tx', tx, }); const response = await tg_tomo_sol.signTransaction(tx); setSignTxSignRes(response); }; const signDataTx = async () => { if (!addr || !toAddr2 || !toValue2) return; const val = Number(toValue2); // const hexData = getTxData(toAddr2, val.toString()); const response = await tg_tomo_sol.signTransaction({ from: addr, to: toAddr2, value: val.toString(), // token: contractAddr, contract: contractAddr, }); console.log('Multi Chain Tomo Wallet sign eth tx success', response.result); setSignRes2(response); }; const addSeed = async () => {}; const signMessage = async () => { const response = await tg_tomo_sol.signMessage(signMsg); console.log('Multi Chain Tomo Wallet Sign result:', response.result); setSignMsgRes(response); }; const sendSignedTxExample = async () => {}; const getBalance = async () => { const res = await tg_tomo_sol.getBalance(balanceAddr); console.log('getBalance res>>:', res); setBalance(res.formatted); }; const sendTx = async () => { const res = await tg_tomo_sol.sendTransaction(signRes); setSendTxHash(res); }; return (

Demo For Tomo TG Wallet

{addr ? ( <>

My Tomo Wallet Address

{addr ?? ''}
{addr ? 'Disconnect' : 'Connect'}
) : ( <>

Connect To Tomo Wallet

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

Get Balance By Address

Click button below to get your balance

Address:

{ const v = e.target.value; setBalanceAddr(v); }} />
<> {balance ? ( <>

Result: {balance} SOL

) : null}
Get Balance

1. Sign Transaction

Please input data

To Address:

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

Value:

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

To Chain:

<> {signTxSignRes ? ( <>

Sign Result:

{signTxSignRes ?? ''}
{ const res = await tg_tomo_sol.sendTransaction( signTxSignRes ); console.log({ res }); setSignTxHash(res); }} > Send Tx
) : null} <> {signTxHash ? ( <>

Send Result:

{signTxHash ?? ''}
) : null}
construct Tx

1. Transfer

Please input data

To Address:

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

Value:

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

To Chain:

<> {signRes ? ( <>

Sign Result:

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

Send Result:

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

2. Add Your Wallet Seed

Click{' '} Add {' '} button to insert a seed {addFlag !== 0 ? ( {addFlag === 1 ? ( Tip: add seed success ) : ( Tip add seed fail )} ) : null}

3. Sign message by private key

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

Sign Msg Result:

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

4. Sign {getChainName} 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(''); }} />

To Chain:

<> {signRes2 ? ( <>

Sign Result:

{signRes2 ?? ''}
) : null}
Sign Token Tx
); }