Solidity
更多白名單的智能合約
如果我有正確的結構,你能解釋一下嗎?當它對我不起作用時,我可能沒有它,但我不知道我做錯了什麼。
我需要創建一個智能合約,我將在其中保留幾個項目的列表,並且每個項目都有自己的使用者列表。
我試過用不同的方式來做,但我總是有一個我無法處理的錯誤消息。所以我想先知道我是笨蛋還是白痴。
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Test { struct Project { uint id; string name; //I want to have a separate list of users for each project BUT Whitelist[] addressList; //return: UnimplementedFeatureError: Copying of type struct Test.Whitelist memory[] memory to storage not yet supported. //Whitelist addressList; //return different error msg, but is not array :-( } // I want more projects Project[] public projects; struct Whitelist { uint signupDate; address userAddress; } Whitelist[] public whitelists; function createProject(uint projectId, string memory projectName) public { // This is only here because I don't know how to create and assign an empty array. Whitelist memory newWhitelist = Whitelist ({ signupDate: block.timestamp, userAddress: msg.sender }); whitelists.push(newWhitelist); Project memory newProject = Project ({ id: projectId, name: projectName, // Array still doesn't want to assign me. I don't know how to edit it addressList: whitelists }); projects.push(newProject); } function getProjects() public view returns (Project[] memory) { // I don't even know what different experiment worked with me, but this will return the list of projects to me with the right list of users? return projects; } function getProjectUsers(uint projectId) public view returns (Whitelist[] memory) { return projects[projectId].addressList; } }
我的問題:結構設計是否正確?如何使用空使用者列表創建項目?這樣我就不必在那裡保存管理員帳戶。如何將使用者添加到特定列表?我有一種感覺,這在任何嘗試中都沒有奏效:
projects[projectId].addressList.push(newUser);
你的整體程式碼太複雜了,我做了一些修改:
- 我刪除了 struct Project,它現在
string[]
是index``projectId
- 我將 id 映射到一個數組
Whitelist
- 我添加了一些事件,以便您可以跟踪所有新的白名單,並從您的前端輕鬆檢索它們,例如
- 添加了一個
whitelistUsers
將使用者列入白名單的函式projectId
,它需要一個數組,address
並且signupDate
是呼叫此函式的時刻- 將函式的可見性從 更改為
public
,external
因為無需公開它們,因為您沒有在程式碼中的任何地方呼叫它們(這將節省氣體)// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract Test { event ProjectCreated(uint id, string name); event UserWhitelisted(uint indexed id, address user, uint signupDate); struct Whitelist { uint signupDate; address userAddress; } //array of string where the index is the id, e.g projects[0] is "ABC" string[] public projects; //We use the indexes of the projects to set the whitelist, each id(index or project) has a whitelist mapping(uint => Whitelist[]) projectsWhitelist; // Push the name to the array of string and emit an event function createProject(string calldata projectName) external { projects.push(projectName); emit ProjectCreated(projects.length - 1, projectName); } function whitelistUsers(uint projectId, address[] calldata users) external { for (uint i; i < users.length; i++) { projectsWhitelist[projectId].push( Whitelist({ userAddress: users[i], signupDate: block.timestamp }) ); emit UserWhitelisted(projectId, users[i], block.timestamp); } } function getProjects() external view returns (string[] memory) { return projects; } function getProjectUsers(uint projectId) external view returns (Whitelist[] memory) { return projectsWhitelist[projectId]; } }