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.

2
  • Does this answer your question? payable() function In solidity Commented Feb 24, 2022 at 21:21
  • I don't see how. It's just a clarification of "payable" as a modifier, and usage of call inside a payable function.
    – licorice
    Commented Feb 24, 2022 at 21:24

1 Answer 1

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.

1
  • 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
    Commented Feb 25, 2022 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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