Contract Invocation – How to Call a Function from Another Upgradable Proxy Smart Contract

contract-invocation

I have 2 diffrent NFT collection. They are both transparentUpgradableProxy smart contracts. I used to call another function like this before I upgraded them to transparentUpgradableProxy.

function mint(uint256 tokenId) public payable {
   IERC721 token = IERC721(mainContractAddress);
   uint256 ownedAmount = token.balanceOf(msg.sender);
   require((ownedAmount / requiredNftAmountFirstStage) - claimedAmountFirstStage[msg.sender] > 0, "You are not eligible for free NFT!");
   require(ownerOf(tokenId) == address(0), "NFT already claimed!");

   _safeMint(msg.sender, tokenId);
   claimedAmountFirstStage[msg.sender] += 1;
}

After I changed it to transparentUpgradableProxy, I tried to do it like this:

IERC721Upgradeable token = IERC721Upgradeable(mainContractAddress);
uint256 ownedAmount = token.balanceOf(msg.sender);

Im guessing this is not how it works. How can I access to another upgradable smart contracts function?

Thanks!

edit1: I cant use delegatecall because it says its unsafe. My both smart contract is upgradableProxy. I tried to do it will call, which returns bool and bytes.

    IERC721Upgradeable token = IERC721Upgradeable(mainContractAddress);
    (bool success, bytes memory result) = address(token).call(abi.encodeWithSignature("balanceOf(address)", msg.sender));
    require(success);
    uint256 ownedAmount = toUint256(result);

Still not working 🙁
My other question is, I need to call that function from proxy smart contract right?

Best Answer

You would use delegatecall() with encoding with a function signature:

uint256 ownedAmount = token.delegatecall(abi.encodeWithSignature("balanceOf(address)", msg.sender)

Related Topic