Program Breakdown

Here's a breakdown of the program's functionality:

  1. State Initialization:

    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>('');

    Here, React's useState hook initializes five pieces of state: fee for storing the estimated fee, error for any errors that occur during the estimation process, senderAddress and receiverAddress for the respective blockchain addresses involved in the transaction, and rpcUrl for the URL of the connected RPC.

  2. Effect Hook for Fee Estimation:

    useEffect(() => {
      // Async function to estimate transaction fee
      const estimateTransactionFee = async () => {
        // ... Code to estimate fee
      };
      estimateTransactionFee();
    }, []);

    This useEffect hook runs once when the component mounts, thanks to the empty dependency array ([]). It defines and invokes the estimateTransactionFee async function to estimate the transaction fee.

  3. Setting Up Connection and Transaction Details:

    const customRpcUrl = "https://testnet.dev2.eclipsenetwork.xyz";
    const connection = new web3.Connection(customRpcUrl);
    setRpcUrl(customRpcUrl);

    The program establishes a connection to the Eclipse testnet using a custom RPC URL and updates the customRpcUrl const with this URL.

    const senderPublicKey = new web3.PublicKey('...');
    const recipientPublicKey = new web3.PublicKey('...');
    const amountInLamports = 1_000_000_000;
    setSenderAddress(senderPublicKey.toString());
    setReceiverAddress(recipientPublicKey.toString());

    It sets up the sender and receiver public keys and the amount to transfer (in lamports/wei), updating the corresponding states with these details.

  4. Transaction Preparation and Fee Estimation:

    const transferInstruction = web3.SystemProgram.transfer({
      fromPubkey: senderPublicKey,
      toPubkey: recipientPublicKey,
      lamports: amountInLamports
    });
    transaction.add(transferInstruction);

    A transfer instruction is created and added to the transaction. This instruction specifies the sender, receiver, and amount to transfer.

    const { blockhash } = await connection.getLatestBlockhash();
    transaction.recentBlockhash = blockhash;
    transaction.feePayer = senderPublicKey;

    The transaction is prepared with the latest blockhash and the fee payer's public key.

    const message = transaction.compileMessage();
    const estimatedFee = await connection.getFeeForMessage(message, 'recent');

    The transaction's message is compiled, and the fee is estimated by querying the blockchain with this message.

  5. Updating State with the Estimated Fee or Error:

    if (estimatedFee.value === null) {
      throw new Error('Failed to estimate fee');
    }
    setFee(estimatedFee.value);

    If the fee estimation is successful, the fee state is updated with the estimated value. If the estimation fails or an error occurs, the error state is updated accordingly.

  6. Rendering UI Components: The UI displays the connected RPC URL, sender and receiver addresses, and either the estimated fee, an error message, or a loading indicator, based on the current state:

    <p>Connected RPC URL: {rpcUrl}</p>
    <p>Sender Address: {senderAddress}</p>
    <p>Receiver Address: {receiverAddress}</p>

    This straightforward approach allows users to see the fee estimation results or the error encountered during the process, providing transparency and insight into the transaction fee estimation on the Eclipse blockchain.

Last updated