1

Dear Ethereum developers.

I started to learn Solidity this week and am trying to deploy a contract via Remix, but this doesn't really work.

Running environment is set as Web3 Provider.

(Because the book I am currently reading for studying Solidity asks me to do so, so I can learn about web3.js as well)

Here is my code:

pragma solidity ^0.4.24;
contract MyToken {
    string public name;
    string public symbol;
    uint8 public decimals;

    mapping (address => uint256) public balanceOf;
    event Transfer(address _from, address _to, uint _value);

    constructor(string tokenName, string tokenSymbol, uint8 decimalUnits, uint256 initialSupply) public {
        name = tokenName;
        symbol = tokenSymbol;
        decimals = decimalUnits;
        balanceOf[msg.sender] = initialSupply;
        }

    function transfer(address _to, uint256 _value) public {
        require(_value <= balanceOf[msg.sender]);
        require(balanceOf[_to] + _value >= balanceOf[_to]);
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
    }
}

This is the whole code of my contract, and when I deploy this onto Remix then it keeps returning an error saying "it exceeds block gas limit".

I have adjusted the value of gas limit hundreds times and retried but it still doesn't work.

I also searched similar questions from this site and on the other sites too but nothing really helped.

One of my friends found that this contract can be deployed on Metamask with gas limit 404941 with value 1 gwei, so I have tried the same on Remix but it doesn't work.

Any help I can use?

Thanks in advance!

3
  • To what kind of network is your MetaMask connected? Are you running a Geth node? Ganache? Truffle? Infura maybe?
    – Henk
    Aug 20, 2018 at 13:20
  • Geth I bet. I have never used any others but geth.
    – user44919
    Aug 21, 2018 at 2:40
  • geth --datadir "private-data" --networkid 15 --rpc --rpcapi "db,eth,net,web3,perosnal,miner" --rpccorsdomain "*" console This is exactly what I typed on cmd before I set the running environment as Web3 Provider.
    – user44919
    Aug 21, 2018 at 3:06

3 Answers 3

0

This is because gas used exceed your chain's block gas limit; to avoid it you can see what is the current block gas limit and increase it higher than gas used to deploy the smart contract

17
  • Thanks for replying, but can you please tell me how I can check the gas limit and adjust it? I think it is the thing that I can see under "Run" tab of Remix, but as I wrote in my question it didn't really work.
    – user44919
    Aug 20, 2018 at 13:13
  • you can run this cmd on the console eth.getBlock("latest").gasLimit;
    – Tony Dang
    Aug 20, 2018 at 13:14
  • And because run choose connect to node by using Running environment is set as Web3 Provider. then as Henk mentioned above, what is your node that you connecting to?
    – Tony Dang
    Aug 20, 2018 at 14:02
  • geth --datadir "private-data" --networkid 15 --rpc --rpcapi "db,eth,net,web3,personal,miner" --rpccorsdomain "*" console Maybe you mean this? network id 15. That is exactly what I typed on cmd
    – user44919
    Aug 21, 2018 at 3:00
  • so what is the output by running eth.getBlock("latest").gasLimit; on console of your network?
    – Tony Dang
    Aug 21, 2018 at 3:04
0

enter image description here I am able to deploy the same contract on the remix. Check your Gas limit while deploying contract if it is more or equal to 3000000 then you should be able to deploy the contract.

0

I realize you're new to this. Some of your phrasing suggests possible confusion that could lead to this error.

trying to deploy a contract via Remix

Deploy to what?

deployed on Metamask

to where?

I will break that down a little.

We have to deal with code, compilers, chains and tools. Remix, as you know, is an IDE, has the JavaScript VM which is great for superficial testing, and you can use it to push compiled code to the "real" Ethereum blockchain. We might say "deployed with Remix" and there is still the question of deployed to what? - Rinkeby, Ropsten, Kovan, etc. testnets, or mainnet?

Similarly, MetaMask is basically a signing device and you can use it to push to a testnet or the mainnet.

The EVM, where this is going, has gone through iterations of the protocol known as forks. Those forks create non-trivial differences that must be accounted for in the compiler or the bytecode will not successfully deploy. The EVM is currently on the Istanbul version. This is a fairly recent upgrade, so you need a fairly recent compiler.

This line:

pragma solidity ^0.4.24;

is just ambiguous. We can't be too sure what compiler you are using but IIRC, older compilers in the low range will produce bytecode that doesn't deploy on Istanbul. You can change the compiler to 0.5.x (I'm not sure where the floor is for Istanbul, so aim on the high side) and you will be on the right track. There are breaking changes, so you have to change the constructor slightly - another reason why ^ is misleading.

pragma solidity 0.5.16;

contract MyToken {
    string public name;
    string public symbol;
    uint8 public decimals;

    mapping (address => uint256) public balanceOf;
    event Transfer(address _from, address _to, uint _value);

    constructor(string memory tokenName, string memory tokenSymbol, uint8 decimalUnits, uint256 initialSupply) public {
        name = tokenName;
        symbol = tokenSymbol;
        decimals = decimalUnits;
        balanceOf[msg.sender] = initialSupply;
        }

    function transfer(address _to, uint256 _value) public {
        require(_value <= balanceOf[msg.sender]);
        require(balanceOf[_to] + _value >= balanceOf[_to]);
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
    }
}

When you do that, you will have to select the right compiler in Remix or it will complain. After it compilers, you should be able to deploy to mainnet or a testnet without difficulty.

Hope it helps.

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.