ethereum / go-ethereum

Go implementation of the Ethereum protocol
https://geth.ethereum.org
GNU Lesser General Public License v3.0
47.29k stars 20.01k forks source link

"gas uint64 overflow" why i get this error #26888

Closed whonion closed 1 year ago

whonion commented 1 year ago

Hello, guys! Can someone explain what the error is in this code:

// Get the bytecode and ABI from the compiled contract
bytecodeFile := filepath.Join(binDir, binFilename)
bytecodeBytes, err := ioutil.ReadFile(bytecodeFile)
  if err != nil {
  log.Fatal(err)
  }
abiFile := filepath.Join(binDir, abiFilename)
abiBytes, err := ioutil.ReadFile(abiFile)
  if err != nil {
  log.Fatal(err)
  }
// Set the gas price and gas limit
gasPrice, err := client.SuggestGasPrice(context.Background())
  if err != nil {
  log.Fatal(err)
}
// Calculate the gas required for deploying the contract
estimateGas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
  From: crypto.PubkeyToAddress(privateKey.PublicKey),
  To:   nil,
  Data: bytecodeBytes,
  })
  if err != nil {
  fmt.Printf("Estimate gas overflow uint64\n")
  log.Fatal(err)
  }
fmt.Printf("Estimated gas: %v\n", estimateGas)
gasLimit := estimateGas
// Create a new instance of a transaction signer
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(chainId))
  if err != nil {
  log.Fatal(err)
  }
auth.GasPrice = gasPrice
auth.GasLimit = gasLimit //estimateGas +10000

// Load the contract's ABI
contractABI, err := abi.JSON(bytes.NewReader(abiBytes))
  if err != nil {
  log.Fatal(err)
  }

// Deploy the contract
address, tx, _, err := bind.DeployContract(auth, contractABI, bytecodeBytes, client)
  if err != nil {
  log.Fatal(err)
  }

I compile a simple contract here is his code:

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
 */
contract Storage {

    uint256 number;

    /**
     * @dev Store value in variable
     * @param num value to store
     */
    function store(uint256 num) public {
        number = num;
    }

    /**
     * @dev Return value 
     * @return value of 'number'
     */
    function retrieve() public view returns (uint256){
        return number;
    }
}
608060405234801561001057600080fd5b50600080546001600160a01b0319163317905561019c806100326000396000f3fe6080604052600436106100385760003560e01c806312065fe0146100445780632e1a7d4d146100645780638da5cb5b1461008657600080fd5b3661003f57005b600080fd5b34801561005057600080fd5b506040514781526020015b60405180910390f35b34801561007057600080fd5b5061008461007f36600461014d565b6100be565b005b34801561009257600080fd5b506000546100a6906001600160a01b031681565b6040516001600160a01b03909116815260200161005b565b6000546001600160a01b0316331461011c5760405162461bcd60e51b815260206004820152601f60248201527f4f6e6c7920746865204f776e65722063616c6c2074686973206d6574686f6400604482015260640160405180910390fd5b604051339082156108fc029083906000818181858888f19350505050158015610149573d6000803e3d6000fd5b5050565b60006020828403121561015f57600080fd5b503591905056fea2646970667358221220bebbc2c0db38c5a0e3dd0caa1c76332be9cec831645500fad14a9c09498f3a6464736f6c63430008130033

Using ABI:

[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]

I try to deploy bytecode compiled with solcjs or Remix IDE but get the error gas uint64 overflow when I try to calculate the cost of gas, and the transaction fails when I manually set the gas limit: Warning! Error encountered during contract execution [gas uint64 overflow] Maybe I'm not passing the data to the function correctly, tell me please, what's my error ?

holiman commented 1 year ago

Are you sure that the bytecode there is the constructor-code and not the runtime bytecode?

whonion commented 1 year ago

Are you sure that the bytecode there is the constructor-code and not the runtime bytecode?

Not quite sure what you mean? I have tried different contracts, compiled in different ways. I'm just starting to learn this library, maybe I misunderstood something. Could you please tell me sir where I misunderstood or made a mistake ?

whonion commented 1 year ago

Are you sure that the bytecode there is the constructor-code and not the runtime bytecode?

I didn't immediately understand what you're talking about. Yes, this is the runtime code, now I need to figure out how to convert it to constructor bytecode. If everything else I'm passing is correct of course.

holiman commented 1 year ago

The compiler should spit out both the constructor code (for use in tx deployent) and the runtime bytecode. In this step:

estimateGas, err := client.EstimateGas(context.Background(), ethereum.CallMsg{
  From: crypto.PubkeyToAddress(privateKey.PublicKey),
  To:   nil,
  Data: bytecodeBytes,
  })

you need to provide the constructor-code, not the runtime code.

whonion commented 1 year ago

Thank you, sir! Thanks to you, I solved the problem and realized what my mistake was

constructorBytes, err := hex.DecodeString(bytecodeStr[:len(bytecodeStr)-68])