0

I have the following function and it has been working perfectly in truffle tests as I was developing the smart contracts and now that I am implementing it in the front end I have been getting a error only with this function.

    function deployPlanet(uint256 tokenId) public onlyTokenHolder(tokenId) {
        uint256 yield = GS.getTokenIdToYield(tokenId);
        address _address = pm.createPlanet(
            tokenId,
            abi.encodeWithSignature(
                'initialize(address,address,address,address,uint256,uint256)',
                address(pm),
                address(solar),
                address(fhr),
                address(sats),
                tokenId,
                yield
            )
        );
        GS.setTokenIdToProxyAddress(tokenId, _address);
        GS.setProxyAddressToTokenId(tokenId, _address);
    }

The function itself is calling create planet in PlanetManager, this in turn is creating a proxy Planet contract which you can view bellow.

pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import {Planet} from './Planet.sol';
import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';
import {ITreasury} from './interfaces/ITreasury.sol';

contract PlanetManager is Ownable {
    address public planetImplementation;
    ITreasury ts;
    mapping(address => bool) operators;
    mapping(address => uint256) addressToTokenId;
    event ProxyCreated(address proxy);

    constructor(
        address _planetImplementation,
        address _gameOperations,
        address _treasury
    ) public {
        planetImplementation = _planetImplementation;
        ts = ITreasury(_treasury);
        addOperator(_gameOperations);
    }

    function addOperator(address _operator) public onlyOwner {
        operators[_operator] = true;
    }

    modifier onlyOperator() {
        // Make sure the access is permitted to only contracts in our Dapp
        require(operators[msg.sender], 'Only Operator');
        _;
    }

    modifier onlyPlanet() {
        require(addressToTokenId[msg.sender] != 0, 'Only planets');
        _;
    }

    function createPlanet(uint256 tokenId, bytes memory _data)
        public
        onlyOperator
        returns (address)
    {
        emit ProxyCreated(planetImplementation);
        address proxy = deployMinimal(planetImplementation, _data);
        addressToTokenId[proxy] = tokenId;
        return proxy;
    }

    function rewardPlanet(address _to, uint256 _amount) public onlyPlanet {
        ts.mintSolar(_to, _amount);
    }

    function deployMinimal(address _logic, bytes memory _data) internal returns (address proxy) {
        bytes20 targetBytes = bytes20(_logic);
        assembly {
            let clone := mload(0x40)
            mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
            mstore(add(clone, 0x14), targetBytes)
            mstore(
                add(clone, 0x28),
                0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
            )
            proxy := create(0, clone, 0x37)
        }

        emit ProxyCreated(address(proxy));
        if (_data.length > 0) {
            (bool success, ) = proxy.call(_data);
            require(success);
        }
    }
}

Here you can see the function passing in my tests and costing 250000~ gas.

enter image description here

Now when I have implemented the function on my frontend I get the following error from metamask. As you can see the gas cost is waaaaaay higher than calling that function in truffle. Any help is a +1 from me!

enter image description here

enter image description here

Your Answer

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

Browse other questions tagged or ask your own question.