简体   繁体   中英

How to communicate with any given chain from Smart Contract?

I am trying to build a function that takes the following input

  1. User wallet address
  2. NFT address
  3. Chain ID

and verifies if the NFT is in fact owned by the user on the given Chain.

If it was all within one chain, it could be done easily. But I want this function to be able to verify ownership across any given chain. I've read about Chainlink (Oracles) and they don't seem to provide such a function as far as I looked. To the best of my understanding, all interactions underneath happen via a JSON-RPC call. But I don't really know how to do that in solidity or any other language.

If anyone has any clue on how to approach this, please leave an answer.

Onchain contracts (written in Solidity or other EVM-compatible language) can't communicate directly with other chains, nor can perform JSON-RPC calls.

You can use the oracle pattern to request an information from an off-chain app, that fulfills the request (by querying the other chain), and sends the result back to your contract.

pragma solidity ^0.8;

contract MyContract {
    // TODO implement a way to keep track of the requests
    // so that you can pair the incoming result to its according request data

    address oracle = address(0x123);

    function requestNFTOwnerCheck(address owner, address collection, uint256 tokenID, uint16 chainID) external {
        (bool success, ) = oracle.call(abi.encode(owner, collection, tokenID, chainID));
        require(success);
    }

    function callback(bool result) external {
        require(msg.sender == oracle, "This function can be invoked only by the oracle");
    }
}

The oracle (offchain app) listens to transactions coming to the 0x123 address, decodes the params, performs the query on the other chain, and then sends a transaction containing the result back to your contract (function callback() ).

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