0

Remix has told me that the function state can be restricted to pure, however, I believe it can't. As you see, I am accessing external variables in return, can someone explain it to me?

function _getCreationFee(string memory merchantName) internal view returns (uint256) {
            uint8 nameLength = uint8(bytes(merchantName).length);
            if (nameLength == 3) {
                return baseMerchantFee * veryShortNameMultiplier;
            } else if (nameLength == 4) {
                return baseMerchantFee * shortNameMultiplier;
            } else {
                return baseMerchantFee;
            }
        }
1
  • What are baseMerchantFee, veryShortNameMultiplier and shortNameMultiplier? Are they constant?
    – Ismael
    Oct 15, 2022 at 20:50

2 Answers 2

1

Yes, you are accessing an external variable but not changing the value. **You should use Pure function state mutability.**You are only returning your input multiplied by the external variable. Your new code should appear as below;

function _getCreationFee(string memory merchantName) internal pure returns (uint256) {
            uint8 nameLength = uint8(bytes(merchantName).length);
            if (nameLength == 3) {
                return baseMerchantFee * veryShortNameMultiplier;
            } else if (nameLength == 4) {
                return baseMerchantFee * shortNameMultiplier;
            } else {
                return baseMerchantFee;
            }
        }
0

You are right, It can't be pure. As you stated you are accessing external variables. I went ahead and copy pasted this function to a contract in remix. It didn't give any warnings. And, gave an error and didn't compile when I declare it as Pure.

The problem might be about your compiler, try to reload and try with a newer compiler version.

Edit after comment: Constants can be reached within Pure functions. Because, to read constants you don't have to read from blockchain, it's written in the declaration and cannot be changed.

2
  • 1
    try adding this to the contract: pastebin.com/WGq4V3VZ (obviously not in the function haha) let me know what happens
    – iMal
    Oct 14, 2022 at 10:10
  • I assumed that your state variables can be changed by other functions but in your case they are constants. Constants can be reached within pure functions. You can make it pure.
    – Yetik
    Oct 14, 2022 at 10:17

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.