[英]Unexpected msg.sender in onERC721Received
我与构造函数签订了拍卖合同,如下所示:
address payable public beneficiary;
ERC721 nftContract;
bool tokenAdded;
uint256 public tokenId;
constructor() public payable {
beneficiary = msg.sender;
}
我已启用合约以使用safeTransferFrom
接收令牌:
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;
}
以下是我铸造代币的方式:
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;
}
}
这个想法是,部署拍卖合约的人应该是唯一可以将代币转移到拍卖合约中的人。
问题是,即使我使用与部署拍卖合同相同的帐户来铸造新代币,我Only the beneficiary can transfer the token into the auction.
收到错误消息, Only the beneficiary can transfer the token into the auction.
来自onERC721Received
方法,当我尝试使用构造函数来铸造令牌并将其转移到拍卖合约时。
我不知道,如果msg.sender
成为MyMintingContract
因为它是直接一个调用了onERC721Received
方法,但是当我检查混音,这表明被称为帐户mintToken
是from
(这是我用过的同一帐户部署拍卖合约),这意味着它应该与beneficiary
变量一致。
如果我查询beneficiary
变量,我得到0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
这是相同的地址from
上面的图像。 我假设msg.sender
与from
相同是否正确?
在给定的上下文中, msg.sender
指向调用链中的直接父(当前合约的调用者),而tx.origin
指向调用链的根(入口点)。
考虑一个调用链:
user
contract A
contract B
contract C
对于合约 C 的上下文, msg.sender
是合约 B, tx.origin
是用户。
截图中的from
值是tx.origin
,但不一定是msg.sender
(它只是合约 A 的msg.sender
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.