我正在进行一个项目,在那里我需要实现一个ERC721市场。它不起作用,因为某种原因,并想知道我是否做了正确的一切。
我处理这个问题的方式是:
The seller approves the contract address to act on their behalf.
The token is listed as available for sale in the marketplace.
Then, when the buyer wants to buy the token, the contract transfers the token from the seller to the buyer, and transfers the payment from buyer to seller.我找不到很多资源,但找到了这些参考资料:
https://github.com/status-im/embark-tutorial-erc721/tree/tutorial-series/contracts
下面是我实现的代码:
守则如下:
struct Token {
uint id;
uint price;
uint prevPrice;
bool forSale;
}
function buyMarketplaceToken(uint _tokenId) public payable whenNotPaused() isForSale(_tokenId) {
address ticketOwner = ownerOf(_tokenId);
require(msg.value >= tokens[_tokenId].price, "It costs more!");
ticketOwner.transfer(msg.value);
safeTransferFrom(ticketOwner, msg.sender, _ticketId);
clearApproval(this, _ticketId);
tokens[_tokenId].forSale = false;
}
function addTokenForSale(uint _tokenId, uint newPrice) public onlyOwner(_tokenId) whenNotPaused() {
tokens[_tokenId].forSale = true;
tokens[_tokenId].prevPrice = tokens[_tokenId].price;
tokens[_tokenId].price = newPrice;
approve(address(this), _tokenId);
emit TokenOnSale(_tokenId, newPrice);
}
function removeTokenForSale(uint _tokenId) public onlyOwner(_tokenId) whenNotPaused() {
tokens[_tokenId].forSale = false;
tokens[_tokenId].price = tokens[_tokenId].prevPrice;
clearApproval(address(this), _tokenId);
emit TokenOffSale(_tokenId, tokens[_tokenId].prevPrice);
}因此,我的逻辑是,卖家用tokenId调用tokenId,并给出一个出售价格。此函数批准合同代表所有者传输令牌,并将此令牌的forSale布尔值设置为true。
买方使用buyMarketplaceToken调用tokenId,并支付转移给原始令牌所有者的令牌价格。令牌通过合同转移到msg.sender,并且批准被清除。然后,令牌forSale布尔值被设置为false,意味着它不再出售。
代码在safeTransferFrom中停止工作,因为即使合同获得了传输令牌的批准,合同也不是msg.sender。
如何确保safeTransferFrom调用方是合同而不是msg.sender?
我在这里错过了什么?任何帮助都将不胜感激。
发布于 2019-07-26 17:36:45
ERC721类似于ERC20在“批准/转移”中的内容。它必须是用户批准您的市场合同,市场合同不能请求批准。
例如,要批准市场,用户将执行
token721.approve(marketAddress, id, { from: userAddress })现在市场可以将用户令牌传输给接收方(或者您可以使用safeTransferFrom)。
token721.transferFrom(userAddress, recipient, id)https://ethereum.stackexchange.com/questions/73236
复制相似问题