简体   繁体   English

如何将 ERC-721 智能合约与另一个智能合约连接起来

[英]How to connect a ERC-721 smart contract with another smart contract

I was wondering if it is possibile to connect the ERC-721 contract with another smart contract.我想知道是否可以将 ERC-721 合约与另一个智能合约连接起来。 Based on the data included into the smart contract, I'd like the ERC-721 contract to automatically mint and deliver the nft.根据包含在智能合约中的数据,我希望 ERC-721 合约能够自动铸造和交付 nft。 For this reason, I was wondering if it is possibile for the ERC-721 contract to connect and retrieve the specific data from the smart contract, like some sort of oracle.出于这个原因,我想知道 ERC-721 合约是否可以连接并从智能合约中检索特定数据,例如某种 oracle。

I am new to programming, so thanks in advance.我是编程新手,所以提前谢谢。

Giulia朱利亚

Last I've checked, in order for your contract to receive an ERC721 token your contract needs to include an onERC721Received function.最后我检查过,为了让你的合约接收 ERC721 代币,你的合约需要包含一个onERC721Received function。

import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";

function onERC721Received(
        address,
        address,
        uint256,
        bytes memory
    ) external view override returns (bytes4) {
    //additional logic (optional)
        return IERC721Receiver.onERC721Received.selector;
    }

https://docs.openzeppelin.com/contracts/4.x/api/token/erc721#IERC721Receiver https://docs.openzeppelin.com/contracts/4.x/api/token/erc721#IERC721Receiver


I don't really think it is necessary to use an oracle to get data that's already stored on chain.我真的认为没有必要使用 oracle 来获取已经存储在链上的数据。

As far as accessing data from that smart contract, there are multiple way of going about it.就从该智能合约访问数据而言,有多种方法可以实现。

You could import the contract, initialize it, and access its functions via an interface, such as:您可以通过接口导入合约、初始化合约并访问其功能,例如:

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

constructor(address _nft) {
        nft = IERC721(_nft);
}

function thenYouCould() public {
        nft.functionName(parameters);

or you could use the external call method with the abi.encodedSignature, such as:或者您可以使用带有 abi.encodedSignature 的外部调用方法,例如:

(bool success, bytes memory data) = contractAddress.call{/*optional values*/ value: msg.value, gas: 5000}(
            abi.encodeWithSignature("functionName(string,uint256)", "call foo", 123)

https://solidity-by-example.org/call/ https://solidity-by-example.org/call/

The action always needs to originate from a transaction - eg to the other contract.动作总是需要来自一个交易——例如另一个合约。 So the NFT contract can't just react to any situation.所以 NFT 合约不能只对任何情况做出反应。 But apart from that, it's possible.但除此之外,这是可能的。

// deployed on address 0x123
contract OtherContract {
    function mintNFT() external {
        // invoke the NFT's function `mint()`
        NFT(0x456).mint();
    }
}
// deployed on address 0x456
contract NFT {
    function mint() external {
        // only executable from the `OtherContract` address 
        require(msg.sender == address(0x123));
    }
}

When the user executes the OtherContract .当用户执行OtherContract时。 mintNFT() function, it effectively invokes the NFT . mintNFT() function,它有效地调用了NFT mint() as well. mint()也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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