首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onERC721Received中的意外msg.sender

onERC721Received中的意外msg.sender
EN

Stack Overflow用户
提问于 2021-07-22 11:01:32
回答 1查看 235关注 0票数 0

我与一个构造函数签订了如下拍卖合同:

代码语言:javascript
复制
address payable public beneficiary;
ERC721 nftContract;
bool tokenAdded;
uint256 public tokenId;

constructor() public payable {
    beneficiary = msg.sender;
}

我已经启用了协定来使用safeTransferFrom接收令牌

代码语言:javascript
复制
function onERC721Received(address, address _from, uint256 _tokenId, bytes memory) public virtual override returns (bytes4) {
    require(beneficiary == _from, "Only the beneficiary can transfer the token into the auction.");
    require(tokenAdded == false, "The auction already has a token.");
    
    nftContract = ERC721(msg.sender);
    tokenId = _tokenId;
    tokenAdded = true;
    return this.onERC721Received.selector;
}

以下是我是如何创建令牌的:

代码语言:javascript
复制
pragma solidity ^0.8.0;

import "./ERC721.sol";
import "./Counters.sol";

contract MyMintingContract is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("MyToken", "MTK") {}

    function mintToken(address receiver) external returns (uint256) {
        _tokenIds.increment();

        uint256 newTokenId = _tokenIds.current();
        _safeMint(receiver, newTokenId);

        return newTokenId;
    }
}

这个想法是,无论谁部署拍卖合同,都应该是唯一可以将令牌转移到拍卖合同中的人。

问题是,即使我使用与部署拍卖合约相同的帐户来创建新的令牌,但当我尝试使用构造函数来创建令牌并将令牌传输到拍卖合约时,我从onERC721Received方法中获得了错误Only the beneficiary can transfer the token into the auction.

我不确定msg.sender是否会变成MyMintingContract,因为它是调用onERC721Received方法的直接方法,但当我检查Remix时,它显示调用mintToken的帐户为from (这与我用来部署拍卖合约的帐户相同),这意味着它应该与beneficiary变量一致。

如果我查询beneficiary变量,我会得到0x5B38Da6a701c568545dCfcB03FcB875f56beddC4,它与上图中from中的地址相同。我假设msg.senderfrom相同,对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-23 01:06:45

在给定的上下文中,msg.sender指向调用链中的直接父(当前约定的调用者),而tx.origin指向调用链的根(入口点)。

考虑一个调用链:

代码语言:javascript
复制
user
  contract A
    contract B
      contract C

对于合同C的上下文,msg.sender是合同B,tx.origin是用户。

屏幕截图中的from值是tx.origin,但不一定是msg.sender (它只是contract A的msg.sender )。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68478621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档