TokenofownerByIndex给出了“内部JSON-RPC错误”错误。
我试图成功地汇编和部署了由钱包地址保存的NFT令牌ID,但是在调用WalletoFowner函数时,我会给我以下错误,以下
呼叫testnft.walletofowner。错误:内部JSON-RPC错误。
{
“代码”:-32000,
“消息”:“恢复执行”
}
我在此错误中挣扎,有人可以帮忙。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "./Base64.sol";
contract TestNFT is ERC721URIStorage, Ownable {
//using Strings for uint256;
event Minted(uint256 tokenId);
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
string v;
uint256 a;
uint256 b;
uint256 c;
uint256 d;
uint256 e;
uint256 f;
uint256 g;
constructor() ERC721("PTestNFT", "PTN") {}
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
function svgToImageURI(string memory svg)
public
pure
returns (string memory)
{
//string memory baseURL = "data:image/svg+xml;base64,";
string memory baseURL = "ipfs://";
//string memory svgBase64Encoded = Base64.encode(bytes(svg));
string memory svgBase64Encoded = svg;
/*
abi.encodePacked is a function provided by Solidity which
is used to concatenate two strings, similar to a `concat()`
function in JavaScript.
*/
return string(abi.encodePacked(baseURL, svgBase64Encoded));
}
function simplifiedFormatTokenURI(string memory imageURI, string memory _v, uint256 _a, uint256 _b, uint256 _c, uint256 _d, uint256 _e, uint256 _f, uint256 _g)
public
pure
returns (string memory)
{
string memory baseURL = "data:application/json;base64,";
string memory json = string(
abi.encodePacked(
'{"name": "',
_v,'", "description": "A simple SVG based on-chain NFT", "image":"',
imageURI,
'", "attributes": [ { "trait_type": "A", "value": "', uint2str(_a),'" }, { "trait_type": "B", "value": "', uint2str(_b),'" }, { "trait_type": "C", "value": "', uint2str(_c),'" }, { "trait_type": "D", "value": "', uint2str(_d),'" }, { "trait_type": "E", "value": "', uint2str(_e),'" }, { "trait_type": "F", "value": "', uint2str(_f),'" }, { "trait_type": "G", "value": "', uint2str(_g),'" } ]}'
)
);
string memory jsonBase64Encoded = Base64.encode(bytes(json));
return string(abi.encodePacked(baseURL, jsonBase64Encoded));
}
function mint(string memory svg, string memory _v, uint256 _a, uint256 _b, uint256 _c, uint256 _d, uint256 _e, uint256 _f, uint256 _g) public {
/* Encode the SVG to a Base64 string and then generate the tokenURI */
string memory imageURI = svgToImageURI(svg);
string memory tokenURI = simplifiedFormatTokenURI(imageURI, _v, _a, _b, _c, _d, _e, _f, _g);
/* Increment the token id everytime we call the mint function */
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
/* Mint the token id and set the token URI */
_safeMint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
/* Emit an event that returns the newly minted token id */
emit Minted(newItemId);
}
function walletOfOwner(address contractAddress, address owner_) public view returns (uint256[] memory){
uint256 _tokenCount = IERC721Enumerable(contractAddress).balanceOf(owner_);
uint256[] memory _tokens = new uint256[](_tokenCount);
for(uint256 i=0; i < _tokenCount; i++){
_tokens[i] = (IERC721Enumerable(contractAddress).tokenOfOwnerByIndex(owner_, i));
}
return _tokens;
}
}
I am trying to get the NFT token id holding by the wallet address and code is compiled and deployed successfully but while calling the walletOfOwner function gives me the error below
call to TestNFT.walletOfOwner errored: Internal JSON-RPC error.
{
"code": -32000,
"message": "execution reverted"
}
I am struggling with this error can one anyone please help.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "./Base64.sol";
contract TestNFT is ERC721URIStorage, Ownable {
//using Strings for uint256;
event Minted(uint256 tokenId);
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
string v;
uint256 a;
uint256 b;
uint256 c;
uint256 d;
uint256 e;
uint256 f;
uint256 g;
constructor() ERC721("PTestNFT", "PTN") {}
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
function svgToImageURI(string memory svg)
public
pure
returns (string memory)
{
//string memory baseURL = "data:image/svg+xml;base64,";
string memory baseURL = "ipfs://";
//string memory svgBase64Encoded = Base64.encode(bytes(svg));
string memory svgBase64Encoded = svg;
/*
abi.encodePacked is a function provided by Solidity which
is used to concatenate two strings, similar to a `concat()`
function in JavaScript.
*/
return string(abi.encodePacked(baseURL, svgBase64Encoded));
}
function simplifiedFormatTokenURI(string memory imageURI, string memory _v, uint256 _a, uint256 _b, uint256 _c, uint256 _d, uint256 _e, uint256 _f, uint256 _g)
public
pure
returns (string memory)
{
string memory baseURL = "data:application/json;base64,";
string memory json = string(
abi.encodePacked(
'{"name": "',
_v,'", "description": "A simple SVG based on-chain NFT", "image":"',
imageURI,
'", "attributes": [ { "trait_type": "A", "value": "', uint2str(_a),'" }, { "trait_type": "B", "value": "', uint2str(_b),'" }, { "trait_type": "C", "value": "', uint2str(_c),'" }, { "trait_type": "D", "value": "', uint2str(_d),'" }, { "trait_type": "E", "value": "', uint2str(_e),'" }, { "trait_type": "F", "value": "', uint2str(_f),'" }, { "trait_type": "G", "value": "', uint2str(_g),'" } ]}'
)
);
string memory jsonBase64Encoded = Base64.encode(bytes(json));
return string(abi.encodePacked(baseURL, jsonBase64Encoded));
}
function mint(string memory svg, string memory _v, uint256 _a, uint256 _b, uint256 _c, uint256 _d, uint256 _e, uint256 _f, uint256 _g) public {
/* Encode the SVG to a Base64 string and then generate the tokenURI */
string memory imageURI = svgToImageURI(svg);
string memory tokenURI = simplifiedFormatTokenURI(imageURI, _v, _a, _b, _c, _d, _e, _f, _g);
/* Increment the token id everytime we call the mint function */
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
/* Mint the token id and set the token URI */
_safeMint(msg.sender, newItemId);
_setTokenURI(newItemId, tokenURI);
/* Emit an event that returns the newly minted token id */
emit Minted(newItemId);
}
function walletOfOwner(address contractAddress, address owner_) public view returns (uint256[] memory){
uint256 _tokenCount = IERC721Enumerable(contractAddress).balanceOf(owner_);
uint256[] memory _tokens = new uint256[](_tokenCount);
for(uint256 i=0; i < _tokenCount; i++){
_tokens[i] = (IERC721Enumerable(contractAddress).tokenOfOwnerByIndex(owner_, i));
}
return _tokens;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论