estimate gas instead of hardcoded value

This commit is contained in:
Sameh Abouelsaad 2025-05-10 01:53:13 +03:00
parent 7298645368
commit feb0a619f7
3 changed files with 18 additions and 2 deletions

View File

@ -2,7 +2,7 @@
// This script demonstrates how to create a wallet from a private key and send tokens // This script demonstrates how to create a wallet from a private key and send tokens
// Define the private key and recipient address // Define the private key and recipient address
let private_key = "0x9ecfd58eca522b0e7c109bf945966ee208cd6d593b1dc3378aedfdc60b64f512"; let private_key = "51c194d20bcd25360a3aa94426b3b60f738007e42f22e1bc97821c65c353e6d2";
let recipient_address = "0xf400f9c3F7317e19523a5DB698Ce67e7a7E083e2"; let recipient_address = "0xf400f9c3F7317e19523a5DB698Ce67e7a7E083e2";
print("=== Agung Wallet Transaction Demo ==="); print("=== Agung Wallet Transaction Demo ===");

View File

@ -114,11 +114,15 @@ pub async fn call_write_function(
let call_data = function.encode_input(&args) let call_data = function.encode_input(&args)
.map_err(|e| CryptoError::ContractError(format!("Failed to encode function call: {}", e)))?; .map_err(|e| CryptoError::ContractError(format!("Failed to encode function call: {}", e)))?;
// Estimate gas
let gas = estimate_gas(contract, wallet, provider, function_name, args.clone()).await?;
log::info!("Estimated gas: {:?}", gas);
// Create the transaction request with gas limit // Create the transaction request with gas limit
let tx = TransactionRequest::new() let tx = TransactionRequest::new()
.to(contract.address) .to(contract.address)
.data(call_data) .data(call_data)
.gas(U256::from(300000)); // Set a reasonable gas limit .gas(gas); // Set a reasonable gas limit
// Send the transaction using the client directly // Send the transaction using the client directly
log::info!("Sending transaction to contract at {}", contract.address); log::info!("Sending transaction to contract at {}", contract.address);

View File

@ -1,6 +1,7 @@
//! Ethereum transaction functionality. //! Ethereum transaction functionality.
use ethers::prelude::*; use ethers::prelude::*;
use ethers::types::transaction::eip2718::TypedTransaction;
use crate::hero_vault::error::CryptoError; use crate::hero_vault::error::CryptoError;
use super::wallet::EthereumWallet; use super::wallet::EthereumWallet;
@ -37,7 +38,18 @@ pub async fn send_eth(
provider.clone(), provider.clone(),
wallet.wallet.clone(), wallet.wallet.clone(),
); );
// Estimate gas
let tx = TransactionRequest::new()
.to(to)
.value(amount);
// Convert TransactionRequest to TypedTransaction explicitly
let typed_tx: TypedTransaction = tx.into();
let gas = client.estimate_gas(&typed_tx, None)
.await
.map_err(|e| CryptoError::SerializationError(format!("Failed to estimate gas: {}", e)))?;
log::info!("Estimated gas: {:?}", gas);
// Create the transaction // Create the transaction
let tx = TransactionRequest::new() let tx = TransactionRequest::new()
.to(to) .to(to)