0

I've used the factory model before, but I've never encountered a circular reference error, and I'm having trouble seeing how this could be a circular reference when I'm creating a brand new AssetTokenizationContract below.

I'm trying to accept an ERC 721 token and deploy a corresponding ERC 20 token contract with the ERC 721 token underlying. You'll see below, once the 721 token is locked, I'm trying to deploy a new AssetTokenizationContract, which is where I get the following error:

TypeError: Circular reference for contract creation (cannot create instance of derived or same contract).
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes calldata _data) external returns(bytes4){
        (address _distributionAddress, 
            address _paymentAddress,
            address _taxAddress, 
            uint256 _erc20Supply,
            string memory _erc20Name, 
            string memory _erc20Symbol, 
            uint _erc20Decimals,
            uint _minimumShares, 
            bytes memory _deploymentData) = abi.decode(_data, (
                address, 
                address,
                address, 
                uint256, 
                string, 
                string, 
                uint, 
                uint,
                bytes));
        require(lock721Token(_operator, _distributionAddress, _paymentAddress, _taxAddress, _tokenId, _erc20Supply, _erc20Name, _erc20Symbol, _erc20Decimals, _minimumShares, _deploymentData) == true);
        return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
    }

    function lock721Token (
        address _tokenToLockAddress, 
        address _distributionAddress, 
        address _paymentAddress,
        address _taxAddress,
        uint256 _tokenToLockId, 
        uint256 _erc20Supply, 
        string memory _erc20Name, 
        string memory _erc20Symbol, 
        uint _erc20Decimals,
        uint _minimumShares,   
        bytes memory _deploymentData) private returns(bool)
    {
        TokenToLock memory _tokenToLock = TokenToLock(_tokenToLockAddress, _tokenToLockId);
        locked721Tokens.push(_tokenToLock); //need to think about if this will cause space issues
        AssetTokenizationContract newAssetTokenizationContract = new AssetTokenizationContract(_tokenToLockAddress, _distributionAddress, _paymentAddress, _taxAddress, _tokenToLockId, _erc20Supply, _erc20Name, _erc20Symbol, _erc20Decimals, _minimumShares, _deploymentData);
        ERC20ToToken[address(newAssetTokenizationContract)] = _tokenToLock;

        bytes32 _tokenToLockHash = abi.encode(keccak256(addressToString(_tokenToLockAddress), _tokenToLockId));
        tokenToERC20[_tokenToLockHash] = address(newAssetTokenizationContract);
        return true;
    }
  • From which contract is AssetTokenizationContract inheriting? In which contracts are your functions above located? – ivicaa Jan 26 at 20:43
  • @ivicaa the functions above are in TokenizeCore (./tokenize-core.sol) and AssetTokenizationContract inherits from it. Here's the full repo if you're quite interested :) github.com/ps2-controller/tokenize-that - i'm still very much a beginner, but really just wanted to hone in on this specific issue which is beating me up lol. I found this article which may be related, but I've yet to figure out how to implement the solution proposed in the article: medium.com/coinmonks/… – a94 Jan 26 at 20:52
1

The circular reference error is coming from AssetTokenizationContract inheriting from TokenizeCore and TokenizeCore trying to instantiate AssetTokenizationContract.

This is not allowed in Solidity, because the code has to be included with the new operator.

You'll have to redesign your code and get new AssetTokenizationContract out of TokenizeCore.

  • 1
    Thank you!! This helped me realize that there's no need for AssetTokenizationContract to inherit from TokenizeCore – a94 Jan 27 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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