我正在尝试一种与TokenURI略有不同的方法,在ERC721 NFT中使用动态元数据。由于元数据可以更改,因此我根据需要在契约中生成TokenURI,它可以正常工作,但我现在达到了堆栈深度的极限。这是我的代码,但它给了我堆栈深度错误。
contract TokenURIMaker {
constructor() {}
function baseSection(string calldata name, string calldata description, string calldata image, string calldata annimation)internal pure returns(bytes memory) {
bytes memory base = abi.encodePacked('{"name": "',
name,
'", "image": "',
image,
'", "description": "',
description,
'","animation_url": "',
annimation);
return base;
}
function atributesSection(string calldata avatarWL, uint wlSpots, uint winChances, uint softClay )internal pure returns(bytes memory){
bytes memory atri = abi.encodePacked('", "attributes": [{ "trait_type": "Avatar WL", "value": "',
avatarWL,
'"},{ "display_type": "boost_number","trait_type": "City WL Spots", "value": "',
Strings.toString(wlSpots),
'"},{ "display_type": "boost_number","trait_type": "Win Chances", "value": "',
Strings.toString(winChances),
'"},{ "display_type": "boost_number","trait_type": "Soft Clay", "value": "',
Strings.toString(softClay),
'"}]}');
return atri;
}
function maketokenURi(string calldata name, string calldata image, string calldata description, string calldata annimation, string calldata avatarWL, uint wlSpots, uint winChances, uint softClay ) external pure returns(string memory){
bytes memory base = baseSection(name, description, image, annimation);
bytes memory atri = atributesSection(avatarWL, wlSpots, winChances, softClay);
bytes memory dataURI = abi.encodePacked(
base, atri
);
return
string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(dataURI)
)
);
}
}这段代码可以工作,但它没有我想要显示的所有元数据。
function maketokenURi(string calldata name, string calldata image, string calldata description, string calldata annimation, string calldata avatarWL, uint wlSpots, uint winChances, uint softClay ) external pure returns(string memory){
bytes memory dataURI = abi.encodePacked(
'{"name": "',
name,
'", "image": "',
image,
'","animation_url": "',
annimation,
'", "attributes": [{ "trait_type": "Avatar WL", "value": "',
avatarWL,
'"},{ "display_type": "boost_number","trait_type": "City WL Spots", "value": "',
Strings.toString(wlSpots),
'"},{ "display_type": "boost_number","trait_type": "Win Chances", "value": "',
Strings.toString(winChances),
'"},{ "display_type": "boost_number","trait_type": "Soft Clay", "value": "',
Strings.toString(softClay),
'"}]}'
);
return
string(
abi.encodePacked(
"data:application/json;base64,",
Base64.encode(dataURI)
)
);
}对如何使这件事奏效有什么想法吗?
发布于 2022-06-08 10:49:18
函数参数将变量添加到堆栈中。我相信这也是“abi.encode”的意思。你应该可以像这样把它分开。
function maketokenURi(
string calldata name,
string calldata image,
string calldata description,
string calldata annimation,
string calldata avatarWL,
uint256 wlSpots,
uint256 winChances,
uint256 softClay
) external pure returns (string memory) {
bytes memory attributes = abi.encodePacked(
'[{ "trait_type": "Avatar WL", "value": "',
avatarWL,
'"},{ "display_type": "boost_number","trait_type": "City WL Spots", "value": "',
Strings.toString(wlSpots),
'"},{ "display_type": "boost_number","trait_type": "Win Chances", "value": "',
Strings.toString(winChances),
'"},{ "display_type": "boost_number","trait_type": "Soft Clay", "value": "',
Strings.toString(softClay),
'"}]'
);
bytes memory dataURI = abi.encodePacked(
'{"name": "',
name,
'", "image": "',
image,
'","animation_url": "',
annimation,
'","attributes": ',
attributes,
'}'
);
return string(abi.encodePacked("data:application/json;base64,", Base64.encode(dataURI)));
}其他方法是调用另一个返回json部分字符串的中间函数。
PS:如果使用0.8.13 's string memory result = string.concat(a, b)方法,则奖励点数。删除字体和遵守命名约定("makeTokenURI“、”动画“)的额外积分。
编辑:我可能误解了它实际上是你正在挣扎的顶级代码。
在这里,另一种可能的补救方法是为您的数据使用结构:
contract TokenURIMaker {
constructor() {}
struct JsonData {
string name;
string image;
string description;
string annimation;
string avatarWL;
uint256 wlSpots;
uint256 winChances;
uint256 softClay;
}
function baseSection(JsonData calldata json) internal pure returns (bytes memory) {
bytes memory base = abi.encodePacked(
'{"name": "',
json.name,
'", "image": "',
json.image,
'", "description": "',
json.description,
'","animation_url": "',
json.annimation
);
return base;
}
function atributesSection(JsonData calldata json) internal pure returns (bytes memory) {
bytes memory atri = abi.encodePacked(
'", "attributes": [{ "trait_type": "Avatar WL", "value": "',
json.avatarWL,
'"},{ "display_type": "boost_number","trait_type": "City WL Spots", "value": "',
Strings.toString(json.wlSpots),
'"},{ "display_type": "boost_number","trait_type": "Win Chances", "value": "',
Strings.toString(json.winChances),
'"},{ "display_type": "boost_number","trait_type": "Soft Clay", "value": "',
Strings.toString(json.softClay),
'"}]}'
);
return atri;
}
function maketokenURi(JsonData calldata json) external pure returns (string memory) {
bytes memory base = baseSection(json);
bytes memory atri = atributesSection(json);
bytes memory dataURI = abi.encodePacked(base, atri);
return string(abi.encodePacked("data:application/json;base64,", Base64.encode(dataURI)));
}
}https://ethereum.stackexchange.com/questions/129814
复制相似问题