Solidity – Error Passing Literal String ” ” to Function with Bytes Calldata Parameter

chainlinkhardhatsolidity

I am trying to implement the chainlink keeper to my contract.
Actually I want to use the function checkUpkeep that is defined in this way:

function checkUpKeep(bytes calldata /* */) public returns (bool /* */, bytes memory /* */)

When I try to call this function with the literal string " " as parameter, solidity compiler (0.8.8) throw me this error:

TypeError: Invalid type for argument in function call. Invalid implicit conversion from literal_string "" to bytes calldata requested.

I call this function in this way:

checkUpKeep("")

My code:

function checkUpkeep(bytes calldata /* checkData */) public override returns (bool upkeepNeeded, bytes memory/* performData*/ ) {
        bool isOpen = (RaffleState.OPEN == s_raffleState);
        bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval);
        bool hasPlayers = (s_players.length > 0);
        bool hasBalance = address(this).balance > 0;
        upkeepNeeded = (isOpen && timePassed && hasPlayers && hasBalance);
    }

    function performUpkeep(bytes calldata /* performData */) external override{
        (bool upkeepNeeded, ) = checkUpkeep(""); // Error here
        ...
    }

Best Answer

There are some of issues. The function checkUpkeep accepts bytes calldata but it is public. Solc generates an error with that combination.

function checkUpkeep(bytes calldata /* checkData */) public override returns ... {

The problem using calldata is that type of data cannot be created by the contract. It just could forward the existing data of that type.

One possible solution is to change the type to memory, then (bool upkeepNeeded, ) = checkUpkeep("") will work.

function checkUpkeep(bytes memory /* checkData */) public override returns ... {

Other function is to declare the function as external and modify the call to (bool upkeepNeeded, ) = this.checkUpkeep(""), because external function are called that way.