0

The following contract calls another contract using an interface method (code to change):

pragma solidity 0.8.7;

interface MyStorage { function setStorageValue(uint256) external; }

contract StorageFactory { uint256 storageValue;

constructor(uint256 _storageValue) { storage = _storageValue; }

function initStorage(MyStorage store) public payable { store.setStorageValue(storageValue); address payable storeAddress = payable(address(store)); storeAddress.call{value: msg.value}(""); } }

Following is the StorageContract (code cannot be changed):

pragma solidity 0.8.7;

contract Storage {

int _storageValue;

function setStorageValue(int storageValue) public { _storageValue = storageValue; }

receive() external payable { require(_storageValue == -1 || address(this).balance <= uint(_storageValue), "Invalid storage value"); }

fallback() external { _storageValue = -1; } }

I use a test to call initStorage of the first contract by passing a Storage object, where the test is meant to fail because the value is set to a large amount. But somehow, the fallback() function seems to get called, setting the value to -1. I can't figure out why. Any help is appreciated.

1 Answers1

1

The problem is that StorageFactory.initStorage calls setStorageValue(uint256) and in your contract you declare it as setStorageValue(int256) (the input type is different).

For solidity they are completely different functions, so the Storage contract calls the fallback function because setStorageValue(uint256) isn't present.

Ismael
  • 30,570
  • 21
  • 53
  • 96
  • 1
    Thank you so much! This was indeed the case, which I noticed before, but the problem was that I wasn't validating the returned success message, and so the test wasn't failing. Made me notice that this time :) – licorice Feb 25 '22 at 07:13