1

Im trying to import a contract and call its primary function via another contract. This seems to work in most cases but in this case i get the following error.

localhost/tokenmarket/SecurityToken.sol:8:25: TypeError: Invalid type for argument in modifier invocation. Invalid implicit conversion from address to contract UpgradeableToken requested. TestMigrationTarget(_oldToken) { ^-------^

  1. Code of the contract im getting this issue with.

    pragma solidity ^0.4.18;
    import "./test/TestMigrationTarget.sol";
    
    contract UpgraderToken is TestMigrationTarget {
     function UpgraderToken(address _oldToken)
        TestMigrationTarget(_oldToken) {
    }
    }
    
  2. Code of Contract TestMigrationTarget which it is importing which deploys fine on its own.

    pragma solidity ^0.4.18;
    
    import "../SafeMathLib.sol";
    import "../UpgradeableToken.sol";
    import "../MintableToken.sol";
    
    
    contract TestMigrationTarget is StandardTokenExt, UpgradeAgent {
    
      using SafeMathLib for uint;
    
      UpgradeableToken public oldToken;
    
      uint public originalSupply;
    
      function TestMigrationTarget(UpgradeableToken _oldToken) {
    
        oldToken = _oldToken;
    
        // Let's not set bad old token
        if(address(oldToken) == 0) {
          throw;
        }
    
        // Let's make sure we have something to migrate
        originalSupply = _oldToken.totalSupply();
        if(originalSupply == 0) {
          throw;
        }
      }
    
      function upgradeFrom(address _from, uint256 _value) public {
        if (msg.sender != address(oldToken)) throw; // only upgrade from oldToken
    
        // Mint new tokens to the migrator
        totalSupply_ = totalSupply_.plus(_value);
        balances[_from] = balances[_from].plus(_value);
        Transfer(0, _from, _value);
      }
    
      function() public payable {
        throw;
      }
    
    }
    
  3. Another case where import and calling functions works just fine.

    pragma solidity ^0.4.18;
    
    import "./BurnableCrowdsaleToken.sol";
    
    contract MyToken is BurnableCrowdsaleToken {
        function MyToken(string _name, string _symbol, uint _initialSupply, uint _decimals, bool _mintable)
        CrowdsaleToken(_name, _symbol, _initialSupply, _decimals, _mintable) {
      }
    }
    

What am i doing wrong in step 1 and how can it be addressed ?

Thanks for reading and your help.

1

Your error is exactly what the error message says. You are trying to implicitly convert between UpgradeableToken and address - your lower level contract passes on the parameter of type address but upper level contract expects type UpgradeableToken.

Change the contract UpgradeableToken to access an address parameter and it should be fine. You can then cast the address into a UpgradeableToken like this: UpgradeableToken tok = UpgradeableToken(_oldToken);.

P.S. What you are referring to as "primary function" is actually called a constructor. You should also use a newer version of Solidity and get used to using the constructor keyword instead of special function name - your way will no longer work starting from Solidity 0.5.

  • Thanks Lauri, i was able to however use this workaround without needing to change the UpgradeableToken contract by doing this instead function UpgraderToken(UpgradeableToken _oldToken) TestMigrationTarget(_oldToken) public { } Thanks for sharing info on solidity 0.5 mandating constructor() instead of function with same name as contract. good to know for future. cheers. – Tanveer Roowala Sep 6 '18 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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