坚固性:如何键入铸造字符串存储器以地址和UINT类型?

发布于 2025-01-24 13:46:08 字数 1562 浏览 6 评论 0原文

尝试键入铸造字符串内存以地址和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2025-01-31 13:46:08

而不是使用语句使用一组验证输入并根据函数逻辑对每个值进行打字,而是可以在准备好的结构中的预期类型中传递输入数据:

struct inputData {
    address admin;
    uint256 price;
    uint256 balance;
    address rewardToken;
}

function setAllowedTokensData(address _token, inputData[] memory _data) public {
    for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
        allowedTokensData[_token] = allowedTokenDetails(
            _data[dataIndex].admin,
            _data[dataIndex].price,
            _data[dataIndex].balance,
            _data[dataIndex].rewardToken,
            0, // `timestampAdded` not set in your snippet
            block.timestamp
        );
    }
}

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:

struct inputData {
    address admin;
    uint256 price;
    uint256 balance;
    address rewardToken;
}

function setAllowedTokensData(address _token, inputData[] memory _data) public {
    for (uint256 dataIndex = 0; dataIndex < _data.length; dataIndex++) {
        allowedTokensData[_token] = allowedTokenDetails(
            _data[dataIndex].admin,
            _data[dataIndex].price,
            _data[dataIndex].balance,
            _data[dataIndex].rewardToken,
            0, // `timestampAdded` not set in your snippet
            block.timestamp
        );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文