坚固性:如何键入铸造字符串存储器以地址和UINT类型?
尝试键入铸造字符串内存以地址和UINT类型时会遇到以下错误。
typeerror:不允许从“字符串内存”到“地址”的显式类型转换。
typeerror:explicit类型转换不允许从“字符串内存”到“ uint256”。
下面是坚固的代码。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct allowedTokenDetails {
address admin;
uint256 price;
uint256 balance;
address rewardToken;
uint256 timestampAdded;
uint256 timestampLastUpdated;
}
mapping(address => allowedTokenDetails) public allowedTokensData;
function setAllowedTokensData(address _token, string[][] memory _data) public {
for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
string memory dataKey = _data[dataIndex][0];
string memory dataValue = _data[dataIndex][1];
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("admin"))) allowedTokensData[_token].admin = address(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("price"))) allowedTokensData[_token].price = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("balance"))) allowedTokensData[_token].balance = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("rewardToken"))) allowedTokensData[_token].rewardToken = address(dataValue);
allowedTokensData[_token].timestampLastUpdated = block.timestamp;
}
}
}
有解决方案吗?
I get the following errors when trying to type cast string memory to address and uint type.
TypeError: Explicit type conversion not allowed from "string memory" to "address".
TypeError: Explicit type conversion not allowed from "string memory" to "uint256".
Below is the solidity code.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct allowedTokenDetails {
address admin;
uint256 price;
uint256 balance;
address rewardToken;
uint256 timestampAdded;
uint256 timestampLastUpdated;
}
mapping(address => allowedTokenDetails) public allowedTokensData;
function setAllowedTokensData(address _token, string[][] memory _data) public {
for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
string memory dataKey = _data[dataIndex][0];
string memory dataValue = _data[dataIndex][1];
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("admin"))) allowedTokensData[_token].admin = address(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("price"))) allowedTokensData[_token].price = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("balance"))) allowedTokensData[_token].balance = uint256(dataValue);
if (keccak256(abi.encodePacked(dataKey)) == keccak256(abi.encodePacked("rewardToken"))) allowedTokensData[_token].rewardToken = address(dataValue);
allowedTokensData[_token].timestampLastUpdated = block.timestamp;
}
}
}
Is there a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用语句使用一组
验证输入并根据函数逻辑对每个值进行打字,而是可以在准备好的结构中的预期类型中传递输入数据:
Instead of validating the input with a set of
if
statements and typecasting each value based on the function logic, you can already pass the input data in expected types in a prepared struct: