[Ethereum] Invalid implicit conversion from address to contract

contract-debuggingsolidity

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.

Best Answer

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.

Related Topic