Program JSX & CSS
Here's the complete JSX code for App.tsx:
import React, { useEffect, useState } from 'react';
import * as web3 from '@solana/web3.js';
import './SolanaFeeEstimator.css';
const SolanaFeeEstimator: React.FC = () => {
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(() => {
const estimateTransactionFee = async () => {
try {
const customRpcUrl = "https://testnet.dev2.eclipsenetwork.xyz";
const connection = new web3.Connection(customRpcUrl);
setRpcUrl(customRpcUrl);
const transaction = new web3.Transaction();
const senderPublicKey = new web3.PublicKey('8HE3zo1iwCGnzcdVgrbRXikSYoBmv9MAaJLBAAkcmBBd');
const recipientPublicKey = new web3.PublicKey('B3xJNQ8LKSStbjzCD3EMPq3xdcav6mmMWBsHAFE9TAkT');
const amountInLamports = 1_000_000_000; // Example amount for transferring 1 ETH
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);
} catch (err) {
setError(err instanceof Error ? err.message : 'An unknown error occurred');
}
};
estimateTransactionFee();
}, []);
return (
<div>
<h1>Eclipse Testnet ETH Transfer Transaction Fee Estimator</h1>
<p>Connected RPC URL: {rpcUrl}</p>
<p>Sender Address: {senderAddress}</p>
<p>Receiver Address: {receiverAddress}</p>
{fee !== null ? (
<p>Estimated Fee: {fee} wei</p>
) : error ? (
<p>Error: {error}</p>
) : (
<p>Estimating fee...</p>
)}
</div>
);
};
export default SolanaFeeEstimator;Here is the complete CSS code for SolanaFeeEstimator.css:
This component efficiently encapsulates the functionality required to estimate transaction fees on the Eclipse testnet, providing a user-friendly interface for interacting with the blockchain's fee estimation capabilities.
Was this helpful?