简体   繁体   中英

Unable to deploy smart contract to Polygon, Gas estimation error, Internal JSON-RPC error

Good afternoon,

I am new to Polygon (but have some Ethereum experience) and I am attempting to deploy the smart contract from the chainlink documentation https://docs.chain.link/docs/fulfilling-requests/ on the Polygon MUMBAI tes.net, using remix as my in browser IDE.

I initially attempted to launch the original contract, as published in the docs. I got this error message:

"Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }"

When that failed, I trimmed it down to a smaller, more bare bones contract (in case there is a smart contract size limit on Polygon). Here is the trimmed down code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
import "@chainlink/contracts/src/v0.8/ConfirmedOwner.sol";

contract ATestnetConsumer is ChainlinkClient, ConfirmedOwner {
  using Chainlink for Chainlink.Request;

  uint256 constant private ORACLE_PAYMENT = 1 * LINK_DIVISIBILITY/10;
  uint256 public currentPrice;
  int256 public changeDay;
  bytes32 public lastMarket;

  event RequestEthereumPriceFulfilled(
    bytes32 indexed requestId,
    uint256 indexed price
  );

  constructor() ConfirmedOwner(msg.sender){
    setPublicChainlinkToken();
  }

  function requestEthereumPrice(address _oracle, string memory _jobId)
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(_jobId), address(this), this.fulfillEthereumPrice.selector);
    req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
    req.add("path", "USD");
    req.addInt("times", 100);
    sendChainlinkRequestTo(_oracle, req, ORACLE_PAYMENT);
  }

  function fulfillEthereumPrice(bytes32 _requestId, uint256 _price)
    public
    recordChainlinkFulfillment(_requestId)
  {
    emit RequestEthereumPriceFulfilled(_requestId, _price);
    currentPrice = _price;
  }

  function getChainlinkToken() public view returns (address) {
    return chainlinkTokenAddress();
  }

  function withdrawLink() public onlyOwner {
    LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
    require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
  }

  function cancelRequest(
    bytes32 _requestId,
    uint256 _payment,
    bytes4 _callbackFunctionId,
    uint256 _expiration
  )
    public
    onlyOwner
  {
    cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
  }

  function stringToBytes32(string memory source) private pure returns (bytes32 result) {
    bytes memory tempEmptyStringTest = bytes(source);
    if (tempEmptyStringTest.length == 0) {
      return 0x0;
    }

    assembly { // solhint-disable-line no-inline-assembly
      result := mload(add(source, 32))
    }
  }
}

But I get the same error. My wallet is funded with MATIC and LINK on polygon (mumbai). I am able to deploy the oracle contract to the mumbai tes.net (and can see it on polygon scan https://mumbai.polygonscan.com/address/0x078cF10C20f7A8aac7b49F078B38007A49334b96 ), so it appears its all set up correctly, just for some reason this contract errors out.

I also increased the max gas I was willing to pay, I have attempted to just push the transaction through (it mines but the resulting contract fails to have any data https://mumbai.polygonscan.com/address/0xb9bc5681a15353c9b1b19d3db097323b92137ddd ).

I was also able to deploy a contract that does does not use Oracles but is a working contract (on Rinkeby) to Mumbai, further indicating its specific to this contract, or the Chainlink infrastructure on Polygon in general.

Side note, I am attempting to run and use my own Chainlink node on Polygon, but that should not be effecting this issue, as in this demo contract you send the node info and the job ID as parameters when you make a call to this function, its not in the smart contract itself.

What I have considered is wrong: -Contract size is too big (even after I trimmed it down??) -MATIC is not the only currency for gas on polygon? -There is an unknown error in the Chinlink Documentation -Something unique about Polygon is throwing an error

Thanks!

After going through and commenting line by line, I was able to track down the code that was at fault. I had gone in and changed the LINK address in ChainlinkClient.sol to the mumbai LINK address. As written, the demo code calls setPublicChainlinkToken(); which then assigns the stored value as the link token address. Changing that value to the correct address did not solve my issue. Instead, I used setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB); and that has cleared up my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM