Search code examples
ethereumsoliditysmartcontracts

Explicit type conversion not allowed from "string storage ref" to "contract vault"


When I tried to put a string instance, the error message as shown in the title appears, in the remix environment. I am using version 0.8.0 of solidity. How do I place the string variable inside the 'vault' contract instance from my main contract without getting the error (as shown in the title)

Below is the vault contract:

contract vault{
    string public vaultKey = "vault";
    string public inputKey = "";
    bool public valid = false;

       constructor(string memory _inputKey) {
        inputKey = _inputKey;
        valid =  keccak256(abi.encodePacked((inputKey))) == keccak256(abi.encodePacked((vaultKey)));
    }
    function getValidity()public returns(bool){
        return valid;
    }
}

below is the main contract that will be used:

contract manager{
// most of the irrelevant lines of code have been removed
    string public keyVal; //for vault
    vault public securityVault;

    constructor(string memory _vaultKey ){     
        keyVal = _vaultKey;
        securityVault = vault(keyVal);
    }
    
    
}


Solution

  • securityVault = vault(keyVal);
    

    This line is trying to load the vault contract at the address keyVal. Which fails with the error, because you're passing a string - not an address.


    If you want to deploy the vault contract to a new address, passing the string to the constructor, you need to use the new keyword.

    securityVault = new vault(keyVal);