Solidity

如何儲存一個 uint堅固結構內的記憶體

  • July 20, 2022

我有以下程式碼

struct Ticket {
       uint256 id;
       uint256[] numbers;
       address payable owner;
}


function enter(uint256[] memory userNumbers) public {
       Ticket memory newTicket = Ticket(
           id,
           userNumbers,
           payable(msg.sender)
       );

       entries[id][msg.sender] = newTicket;
   }

但是,當我檢查保存的 Ticket 對象時,我只得到id, 和address.

我應該如何正確保存userNumbers結構內部?

這對我來說很好。但是,如果這是您的意思,它不會在 Remix 中顯示動態數組?但是,這並不意味著數組不存在。這似乎是一個 UI 錯誤/限制。

我寫了這段程式碼來測試它:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract StackOverflowAnswer {

 struct Ticket {
       uint id;
       uint[] numbers;
       address payable owner;
 }
 mapping(uint => mapping(address => Ticket)) public entries;

 function enter(uint256[] memory userNumbers, uint id) public {

   Ticket memory newTicket = Ticket(
       id,
       userNumbers,
       payable(msg.sender)
   );
   
   entries[id][msg.sender] = newTicket;
 }

 function seeTicket(uint id, address _address) public view returns (uint[] memory) {
   return entries[id][_address].numbers;
 }
}

我寫了一個額外的方法來檢索結構的動態數組部分,並在特別選擇時呈現它,我猜:

在此處輸入圖像描述

引用自:https://ethereum.stackexchange.com/questions/132052