0

I am using web3.php project in my Ethereum based project. A part of this project is sending ERC20 tokens, and in this regard, I need to estimate gas limit of transaction. For this purpose, I found the example below in the official documents of this project as below:

$contract->at($contractAddress)->estimateGas($functionName, $params, $callback);

and I changed it to my own code like this:

$gasLimit = 0;
$contract = new Contract($web3->provider, $contract_abi);
$contract->at($contract_address)->estimateGas('transfer', [
     'to'=>'0xa96Ce4C161271C797d979017821A278e1481eAfA',
     'value'=>1
   ], function ($err, $gas) use (&$gasLimit) {
         if ($err !== null) {
             echo $err->getMessage();
             return false;
         } else {
             $gasLimit = $gas;
         }
    }
);

After running this code, I receive this error:

PHP Fatal error:  Uncaught InvalidArgumentException: Please make sure you have put all function params and callback...

And I am not sure what are the required params in $params as the sample which is mentioned in this project. I tried to put something using documents provided for estimateGas() here.

May you help me please to solve this issue?

7
  • Function transfer takes parameters to and amount only. Transaction parameters from, gas, gasPrice and value are passed separately (i.e., as part of the transaction, not as part of the function call). Jul 12, 2020 at 5:45
  • You're missing the transaction call object, as described here. You should encode the function-call to transfer (along with your input to that function), and pass it in the data field of that object. Jul 12, 2020 at 6:31
  • @goodvibration, I removed from in the code and received the same result after that in regard to your first comment. In regard to your 2nd comment, I have used the $contract_abi which is given based on the token user want to use. Do you mean anything else? Sorry, I'm new in Ethereum contracts and its related issues... Jul 12, 2020 at 8:29
  • Read the documentation that I linked in the second comment. Jul 12, 2020 at 8:32
  • 1
    @MohammadSaberi You are missing the methods parameters. estimateGas('transfer', '0xa96Ce4C161271C797d979017821A278e1481eAfA', 1, [ 'from' => '0x1234143..', 'gas' => '0x200b20' ], function (...) { ... }) .
    – Ismael
    Jul 12, 2020 at 22:53

1 Answer 1

1

Try this example:

 $contract->at($contract_addr2)->estimateGas('transfer',
            $to_addr,
            1
        , function ($err, $gas) use (&$gasLimit) {
            if ($err !== null) {
                echo $err->getMessage();
                return false;
            } else {
                $gasLimit = $gas;
            }
        });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.