Jump to content

Recommended Posts

Hi all

I have the following code.

require 'vendor/autoload.php';
use Web3\Web3;
use Web3p\EthereumTx\EIP1559Transaction;
 
$web3 = new Web3('https://eth-rinkeby.alchemyapi.io/v2/--api--');
 
$fromAddress = '0xB5A2FD09ed2Eb7AA3bBf55C7f8e8D8576B68b0F2';
$toAddress = '0x2928C5Fb489BE1182B3569a3863F1C61Cafde240';
$prvKey   = '--private-key--';
$transactionAmount = 145;
 
 
$gasLimit = 0;
$baseFeePerGas = 0;
$gasUsed = 0;
$maxFeePerGas = 0;
$estimatedGas = 0;
 
 
$web3->eth->getBlockByNumber('pending',false,function($err,$success){
    if ($err !== null) {
      echo 'Error: ' . $err->getMessage();
      exit;
    }
    global $gasLimit;
    global $baseFeePerGas;
    global $gasUsed;
    
    $gasLimit = $success->gasLimit;
    $baseFeePerGas = $success->baseFeePerGas;
    $gasUsed = $success->gasUsed;
   
});
 
$estimateGasRequest = [
    'from' => $fromAddress,
    'to' => $toAddress,
    'value' => $transactionAmount
];

$web3->eth->estimateGas($estimateGasRequest,function($err,$success){
    if ($err !== null) {
      echo 'Error: ' . $err->getMessage();
      exit;
    }   
    global $estimatedGas;
    $estimatedGas = $success->value;
});
 
echo $gasLimit; // Outputs 0x1c8debe
echo '<br/>';
echo $baseFeePerGas; // Outputs 0x8
echo '<br/>';
echo $gasUsed; // Outputs 0x1c8bb1d
echo '<br/>';
echo $estimatedGas; // Outputs 21000
echo '<br/>';

 

I am struggling to understand how I can use the retrieved values in order to fill out the required values for a transaction.

Can someone help me to understand how I can calculate the values marked ?????? in the source code shown?

$gweiFactor = 1 / 1000000000;
$nonce = '113';

// generate transaction instance with transaction parameters
$transactionInfo = [
    'gas' => ??????,
    'maxPriorityFeePerGas' => ??????,
    'maxFeePerGas' => ??????,
    'nonce' => '0x' . dechex($nonce),
    'from' => $fromAddress,
    'to' => $toAddress,
    'value' => '0x' . dechex($transactionAmount),
    'chainId' => 4, // required
    'accessList' => [],
    'data' => ''
]);

 

I tried with some hard-coded values that eth-rinkeby.alchemyapi.io seemed to accept (and give me a transaction hash) but the transaction never appears on the test net or actually transfers any eth.

 

Can anyone help?

Link to comment
https://forums.phpfreaks.com/topic/314554-i-really-need-help-sending-eth-via-php/
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.