web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.17k stars 550 forks source link

What are the exact parameters in estimateGas method of contracts? #179

Closed mbsaberi closed 2 years ago

mbsaberi commented 4 years ago

Hi. I've used the provided samples and something I have found in other issues to estimate the transfer function of contracts like below, but I receive errors after executing the code: My Code:

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

Error:

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

What is the $params parameter in the estimateGas method exactly?

mbsaberi commented 4 years ago

@sc0Vu could you help me please?

YonesSaeedi commented 3 years ago

+1

conorwatt1 commented 3 years ago

After a whole day of trying to figure this out I found the solution (hopefully this helps someone out)

In the readme it states you use this for gas estimation:

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

What we (atleast I) assume that $params is an array to provide, in which it's not

The correct way to use this function is the following

$contract->at($contract_address)->estimateGas('transfer', $to_addr, the_number_of_tokens_to_send, ['from' => $from_addr], function($err, $resp) use (&$gas){
            if($err){
                Log::info("estimate gas error: ".$err->getMessage());
            }

            $gas = $resp->toString();
        });

Which results in: web3p-gas-estimate

Since I'm using infura as my provider (they don't let you unlock wallets etc), you would just need to use Web3p/ethereum-tx to sign with a private key and send..

Again, I really hope someone doesn't have to spend as much time on this now

Cheers

nithronium commented 3 years ago

Addition to that, for contract calls that doesn't have any input parameters, such as enableTransfers, you can use that;

$contract->at($contractAddress)->estimateGas('enableTransfers',['from' => $fromAddr],function($err,$result) use($contract) {
            if ($err !== null) {
                throw $err;
            }
            if ($result) {
                var_dump($result);
            }
        });

Which results in:

{
  ["value"]=>
  string(8) "0x018d16"
  ["engine"]=>
  string(16) "bcmath (OpenSSL)"
}

Thanks @conorwatt1 for adding the ['from' => $fromAddr] part.

aminesmkhani commented 2 years ago

@mbsaberi

how fix this problem?

sc0Vu commented 2 years ago

Thanks for your reply (@conorwatt1 @nithronium )!

Basically, the function parameter of estimate gas are: functionName, paramsToSmartContract(leave empty)... and transactionObject.

Take erc20 transfer as example:

estimateGas('transfer', $toAccount, $amount, [
    'from' => $fromAccount,
]

You can find example here: https://github.com/web3p/examples