我与一个构造函数签订了如下拍卖合同:
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;
}
}这个想法是,无论谁部署拍卖合同,都应该是唯一可以将令牌转移到拍卖合同中的人。
问题是,即使我使用与部署拍卖合约相同的帐户来创建新的令牌,但当我尝试使用构造函数来创建令牌并将令牌传输到拍卖合约时,我从onERC721Received方法中获得了错误Only the beneficiary can transfer the token into the auction.。
我不确定msg.sender是否会变成MyMintingContract,因为它是调用onERC721Received方法的直接方法,但当我检查Remix时,它显示调用mintToken的帐户为from (这与我用来部署拍卖合约的帐户相同),这意味着它应该与beneficiary变量一致。

如果我查询beneficiary变量,我会得到0x5B38Da6a701c568545dCfcB03FcB875f56beddC4,它与上图中from中的地址相同。我假设msg.sender与from相同,对吗?
发布于 2021-07-23 01:06:45
在给定的上下文中,msg.sender指向调用链中的直接父(当前约定的调用者),而tx.origin指向调用链的根(入口点)。
考虑一个调用链:
user
contract A
contract B
contract C对于合同C的上下文,msg.sender是合同B,tx.origin是用户。
屏幕截图中的from值是tx.origin,但不一定是msg.sender (它只是contract A的msg.sender )。
https://stackoverflow.com/questions/68478621
复制相似问题