如何在结构中包含结构数组并在函数中编辑它

问题描述 投票:0回答:1

我训练自己的稳定性并编写一些智能合约来填充我的 github 项目。 我当前的项目涉及用户将 ETH 发送到智能合约中以换取代码。然后,同一用户(或其他用户)可以在输入中输入此代码,进行验证,然后他将收到与创建者代码相对应的 ETH。 这就是该项目的背景。


对于商店用户的代码,我创建了一个

struct Code
struct UserCode
:

struct Code {
    uint256 code;
    uint256 value;
}

struct UserCode {
    address userAddress;
    Code[] codes;
}

UserCode[] private userCodes;

这样,一个用户可以有多个代码,我可以在 dApp 中向用户显示他的代码,等等... 但是当我尝试将数据推送到

userCodes
时,我不知道该怎么做。

我试试这个:

function createCode() public payable {
    UserCode memory newDeposit;
    Code[] memory newCode;
    newCode.push([Code(
        getRandom(),     /// A function I create before to return a pseudo random uint256
        msg.value
    )]);
    newDeposit.userAddress = msg.sender;
    newDeposit.codes.push(newCode);

    userCodes.push(newDeposit);

但是不起作用,因为我有一个错误,比如我无法使用

push()
方法,因为
newCode
是内存。 我也尝试不将
newCode
声明为结构数组:

function createCode() public payable {
    UserCode memory newDeposit;
    Code memory newCode;
    newCode.code = getRandom();
    newCode.value = msg.value;
    newDeposit.userAddress = msg.sender;
    newDeposit.codes.push(newCode);

    userCodes.push(newDeposit);

同样的错误,另一个类似于 UserCode struct 中的错误,

codes
是一个
struct Code
数组,在这里,我只推送一个
Code
变量。

之后我改变了策略并使用了mapping,如下所示:

struct Code {
    uint256 code;
    uint256 value;
}

mapping(address => Code[]) private userCodes;

它工作完美,我改变了ma

createCode()
这样的功能:

/**
 * @notice Assigns a code corresponding to the value sent by the user. And stores them in the "userCodes" mapping.
 */
function createCode() public payable {
    /// Push the new code with its value in the mapping
    userCodes[msg.sender].push(
        Code(
            getRandom(), /// To generate a random code (cf. getRandom() function)
            msg.value
        ));
    }

但是当我尝试创建一个函数时

redeemCode(uint256 code)
。我无法浏览映射来查看代码是否存在...


所以我的结论是,我认为我必须在结构中使用结构数组

UserCode
,但我不知道如何添加我的数据。

我希望我说清楚了,非常感谢您的帮助。

v4ss。

struct solidity smartcontracts
1个回答
0
投票

看起来您在 Solidity 项目中取得了一些进展,并且您已改用映射来存储用户代码,这对于您的用例来说是一个很好的方法。要检查映射中是否存在代码并兑换它,您可以创建如下函数:

/**
 * @notice Redeems a code and transfers the corresponding ETH value to the sender.
 * @param code The code to redeem.
 */
function redeemCode(uint256 code) public {
    // Get the array of codes for the sender
    Code[] storage codes = userCodes[msg.sender];

    // Iterate through the user's codes to find a matching one
    for (uint256 i = 0; i < codes.length; i++) {
        if (codes[i].code == code) {
            // Found a matching code, transfer the ETH value to the sender
            payable(msg.sender).transfer(codes[i].value);

            // Remove the redeemed code from the array (optional)
            if (i < codes.length - 1) {
                // Move the last code in the array to the position of the redeemed code
                codes[i] = codes[codes.length - 1];
            }
            // Reduce the length of the array to remove the last element
            codes.pop();
            
            // Exit the loop once the code is redeemed
            break;
        }
    }
}

这个 redeemCode 功能允许用户通过提供他们想要兑换的代码来兑换他们的代码。它检查所提供的代码是否存在于用户的代码数组中,并将相应的 ETH 值传输给发送者。如果找到代码,则会将其从数组中删除(可选)。

© www.soinside.com 2019 - 2024. All rights reserved.