7

I am learning the Solidity language and I have been experimenting on the Ropsten Testnet, I have tried several things with a contract I created, yet everytime I try to create it, remix solidity IDE gives me the error transaction cost exceeds gas limit.

I got many recommendations which tell me to split the contract up into smaller ones, Im new to the Ethereum token world and dont know what people mean by this? If I split my contract up into several parts and create each one equal? How will I link theese together?

1
  • 1
    hire a developer
    – rstormsf
    Commented Aug 17, 2017 at 22:43

1 Answer 1

7

You have to do something like this:

  • Token.sol

    // This should be an abstract interface without implementation
    contract Token {
      function mint(address _to, uint _amount) public returns (bool);
    
      // Rest of the code
    }
    
  • CrowdsaleToken.sol

    import "Token.sol";
    
    // This contract contains the implementation of ERC20 methods
    contract CrowdsaleToken is Token {
    
      function mint(address _to, uint _amount) public returns (bool) {
          balances[_to] = balances[_to] + _amount;
          return true;
      }
    
      // Rest of the code
    }
    
  • Crowdsale.sol

    import "Token.sol";
    
    // The Crowdsale  interact with CrowdsaleToke through the
    // abstract interface Token
    contact Crowdsale {
        Token crowdsaleToken; // Address of the CrowdsaleToken
    
        function Crowdsale(address _crowdsaleToken) {
            // We receive the CrowdsaleToken addres as constructor param
            crowdsaleToken = Token(_crowdsaleToken);
    
            // We can use the CrowdsaleToken contract
            crowdsaleToken.mint(msg.sender, 0x31415926535);
        }
    
        // Other crowdsale methods
    }
    

The deployment is done in two steps

  1. Deploy CrowdsaleToken, await for the contract address
  2. Deploy Crowdsale and pass the CrowdsaleToken address as parameter

That is the basic idea.

Some frameworks like Truffle and Populus can be used to automate some of this tasks.

2
  • How CrowdsaleToken.sol can compile if balances is not defined? Where to define balances?
    – Thykof
    Commented Aug 7, 2020 at 8:56
  • @Thykof This is very old example and it is incomplete. I'd suggest to start using OpenZeppelin's github.com/OpenZeppelin/openzeppelin-contracts, they are updated frequently.
    – Ismael
    Commented Aug 7, 2020 at 21:35

Your Answer

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