Program Breakdown
const [fee, setFee] = useState<number | null>(null); const [error, setError] = useState<string>(''); const [senderAddress, setSenderAddress] = useState<string>(''); const [receiverAddress, setReceiverAddress] = useState<string>(''); const [rpcUrl, setRpcUrl] = useState<string>('');useEffect(() => { // Async function to estimate transaction fee const estimateTransactionFee = async () => { // ... Code to estimate fee }; estimateTransactionFee(); }, []);const customRpcUrl = "https://testnet.dev2.eclipsenetwork.xyz"; const connection = new web3.Connection(customRpcUrl); setRpcUrl(customRpcUrl);const senderPublicKey = new web3.PublicKey('...'); const recipientPublicKey = new web3.PublicKey('...'); const amountInLamports = 1_000_000_000; setSenderAddress(senderPublicKey.toString()); setReceiverAddress(recipientPublicKey.toString());const transferInstruction = web3.SystemProgram.transfer({ fromPubkey: senderPublicKey, toPubkey: recipientPublicKey, lamports: amountInLamports }); transaction.add(transferInstruction);const { blockhash } = await connection.getLatestBlockhash(); transaction.recentBlockhash = blockhash; transaction.feePayer = senderPublicKey;const message = transaction.compileMessage(); const estimatedFee = await connection.getFeeForMessage(message, 'recent');if (estimatedFee.value === null) { throw new Error('Failed to estimate fee'); } setFee(estimatedFee.value);<p>Connected RPC URL: {rpcUrl}</p> <p>Sender Address: {senderAddress}</p> <p>Receiver Address: {receiverAddress}</p>
Was this helpful?