[英]How to get token sent to a ERC721 contract?
I have a ERC721 contract, and my mint method requires 1 ether.我有一个 ERC721 合约,我的铸币方法需要 1 个以太币。 My contract is here:我的合同在这里:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Example is ERC721 {
uint256 private _tokenId = 0;
function mint(uint256 tokenURI, uint256 id) public payable returns(uint256){
require(msg.value == 1 ether);
require(id > 0);
_tokenId += 1;
_mint(msg.sender, _tokenId);
_setTokenURI(_tokenId, tokenURI);
return _tokenId;
}
}
My question is how can I know where that value is stored?我的问题是我怎么知道该值存储在哪里? On the explorer, I can see there's 1 ether on the contract address, how can I get the token out?在浏览器上,我可以看到合约地址上有 1 个以太币,我怎样才能取出代币? Using balanceOf from ERC721 can only get the number of tokens that an address have.使用 ERC721 中的 balanceOf 只能获取地址拥有的代币数量。 I want to see the values on this contract address.我想查看此合约地址上的值。
Your question is not 100% clear but it looks like you are not able to get tokens data because your contract does not store any token.您的问题不是 100% 清楚,但看起来您无法获取代币数据,因为您的合同不存储任何代币。
You are missing many basic things in your smart contract.您在智能合约中遗漏了许多基本的东西。 For example where are the tokens stored?例如,令牌存储在哪里? I cannot see any array for that.我看不到任何数组。 What is the data associated with a token?与令牌关联的数据是什么? I cannot see any struct defined.我看不到任何定义的结构。
Once you define how the tokens are stored then you can retrieve their data.一旦定义了令牌的存储方式,就可以检索它们的数据。
Please note the example below is just to show you a basic implementation and many things have to be defined.请注意,下面的示例只是向您展示了一个基本的实现,很多东西都需要定义。 If you just copy and paste it then it won't work.如果您只是复制并粘贴它,那么它将不起作用。
YourTokenStruct[] public yourTokens;
struct YourTokenStruct {
string name;
uint256 id;
}
function mint(string memory name, uint256 id) public payable returns(uint256){
require(msg.value == 1 ether);
require(id > 0);
uint _tokenId = yourTokens.push(YourTokenStruct(name, id)) - 1;
_mint(msg.sender, _tokenId);
return _tokenId;
}
As you can see the array yourTokens
stores all tokens created, and the struct YourTokenStruct
defines the data structure of a token.如您所见,数组yourTokens
存储所有创建的令牌,结构YourTokenStruct
定义令牌的数据结构。
I suggest you to follow the tutorial https://cryptozombies.io/ it explains all the basics on how to code ERC721 token standard.我建议您遵循教程https://cryptozombies.io/ ,它解释了有关如何编写 ERC721 令牌标准的所有基础知识。
EDIT: The ether is stored on the smart contract address (the address where the contract has been deployed).编辑:以太存储在智能合约地址(部署合约的地址)。 Below an example of a function that transfers the ether from the smart contract to the caller of the function (of course define the modifier ifOwner
yourself):下面是一个 function 的例子,它将以太从智能合约转移到 function 的调用者(当然你自己定义修饰符ifOwner
):
function withdraw() payable external ifOwner {
msg.sender.transfer(address(this).balance);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.