nft-mix icon indicating copy to clipboard operation
nft-mix copied to clipboard

invalid op code in create collectible

Open 0xVinx opened this issue 2 years ago • 5 comments

image in the advanced collectible, it works fine till deploying and funding with link but throws when createCollectible

this is how the solidity looks

function createCollectible() 
        public returns (bytes32){
            bytes32 requestId = requestRandomness(keyhash, fee);
            requestIdToSender[requestId] = msg.sender;
            emit requestedCollectible(requestId, msg.sender);
    }

function fulfillRandomness(bytes32 requestId, uint256 randomNumber) internal override {
    Breed breed = Breed(randomNumber%3);
    uint256 newTokenId = tokenCounter;
    tokenIdToBreed[newTokenId] = breed;
    emit BreedAssigned(newTokenId, breed);
    address owner = requestIdToSender[requestId];
    _safeMint(owner, newTokenId);
    // _setTokenURI(newTokenId, tokenURI);
    tokenCounter = tokenCounter + 1;

}

function setTokenURI(uint256 tokenId, string memory _tokenURI) public {
    require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not owner or approved");
    _setTokenURI(tokenId, _tokenURI);
}

and this is how the python looks

def deploy_and_create():
    account = get_account()
    advanced_collectible = AdvancedCollectible.deploy(
        get_contract("vrf_coordinator").address,
        get_contract("link_token").address,
        config["networks"][network.show_active()]["fee"],
        config["networks"][network.show_active()]["keyhash"],
        {"from": account},
        publish_source=config["networks"][network.show_active()].get("verify", False)
    )
    fund_with_link(advanced_collectible.address)
    tx = advanced_collectible.createCollectible({"from":account})
    tx.wait(1)
    print("created ")

0xVinx avatar Mar 10 '22 08:03 0xVinx

Could you please copy paste and format the error?

PatrickAlphaC avatar Mar 10 '22 15:03 PatrickAlphaC

Could you please copy paste and format the error?

Transaction sent: 0xe599c62bfbe22ae839dee3bc8c5132c1bff0ae3f6afd7bd2aa6ff72f7ce20eaa
  Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 5
  AdvancedCollectible.createCollectible confirmed (invalid opcode)   Block: 6   Gas used: 11812927 (98.44%)

  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\_cli\run.py", line 51, in main
    return_value, frame = run(
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\project\scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File ".\scripts\advanced_collectible\deploy_and_create.py", line 26, in main
    deploy_and_create()
  File ".\scripts\advanced_collectible\deploy_and_create.py", line 17, in deploy_and_create
    tx = advanced_collectible.createCollectible({"from":account})
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\contract.py", line 1710, in __call__
    return self.transact(*args)
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\contract.py", line 1583, in transact
    return tx["from"].transfer(
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\account.py", line 682, in transfer
    receipt._raise_if_reverted(exc)
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\transaction.py", line 446, in _raise_if_reverted
    raise exc._with_attr(
VirtualMachineError: revert: invalid opcode
Trace step 449, program counter 2265:
  File "C:/Users/VINAY/.brownie/packages/smartcontractkit/[email protected]/contracts/src/v0.4/vendor/SafeMathChainlink.sol", line 40, in SafeMathChainlink.sub: 

    function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
      assert(_b <= _a);
      return _a - _b;
Terminating local RPC client...

This is the complete error I get after the link token transfer is complete.

0xVinx avatar Mar 10 '22 17:03 0xVinx

Are you running the fund_collectible script? What network is this on?

PatrickAlphaC avatar Mar 14 '22 19:03 PatrickAlphaC

The network is Rinkeby, you can find the deployed contract at https://rinkeby.etherscan.io/address/0x90921d22059d38844f14d84C45003B8E2D7f8CC7#code

the error comes with the createCollectible function after the link token has been transferred as 0.1 link is there in the contract.

this time when I did it, a different error came (idk how), it was :

File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\_cli\run.py", line 51, in main
    return_value, frame = run(
  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\project\scripts.py", line 103, in run
    return_value = f_locals[method_name](*args, **kwargs)
  File ".\scripts\advanced_collectible\deploy_and_create.py", line 26, in main
    deploy_and_create()
  File ".\scripts\advanced_collectible\deploy_and_create.py", line 17, in deploy_and_create
    tx = advanced_collectible.createCollectible({"from":account})
    return tx["from"].transfer(  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\account.py", line 644, in transfer
    receipt, exc = self._make_transaction(  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\network\account.py", line 727, in _make_transaction
    raise VirtualMachineError(e) from None  File "C:\Users\VINAY\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\brownie\exceptions.py", line 93, in __init__
    raise ValueError(str(exc)) from NoneValueError: Gas estimation failed: 'execution reverted'. This transaction will likely revert. If you wish to broadcast, you must set the gas limit manually.

deploy function in python:

def deploy_and_create():
    account = get_account()
    advanced_collectible = AdvancedCollectible.deploy(
        get_contract("vrf_coordinator").address,
        get_contract("link_token").address,
        config["networks"][network.show_active()]["fee"],
        config["networks"][network.show_active()]["keyhash"],
        {"from": account},
        publish_source=config["networks"][network.show_active()].get("verify", False)
    )
    fund_with_link(advanced_collectible.address)
    tx = advanced_collectible.createCollectible({"from":account})
    tx.wait(1)
    print("created ")

the fund_with_link and everything works fine so I think python is good and the problem is in solidity. I have looked at it many times and still cannot find what I am doing wrong, google isn't helping much either, am stuck here for a long time now :(

0xVinx avatar Mar 18 '22 18:03 0xVinx

0xVinx what values are you using for the keyhash and fee?

Can you send the full smart contract?

ReasonZx avatar Aug 07 '23 11:08 ReasonZx