谁能帮帮我?我如何创建一个函数来从以太坊区块链获取在我的dapp上注册的整个用户列表?
签约SuperMail {
struct Mail{
int serialNumber;
string title;
string content;
string time;
address _sender;
address reciever;
}
struct Inbox{
int numberOfRecievedMails;
int numberOfSentMails;
mapping(int => Mail) receivedMails;
mapping(int => Mail) sentMails;
}
mapping(address => Inbox) users;
mapping(address => bool) registerd;
function checkUser(address _address) public{
if(!registerd[_address]){
registerd[_address] = true;
//users[_address] = inbox;
}
}
function getTotalReceivedMails(address _address) view public returns(int){
return users[_address].numberOfRecievedMails;
}
function getTotalSentMails(address _address) view public returns(int){
return users[_address].numberOfSentMails;
}
//function to send Mail
function sendMail(address recieverAddress, address senderAddress, string memory Title, string memory Content, string memory Time) public{
checkUser(recieverAddress);
checkUser(senderAddress);
users[senderAddress].numberOfSentMails++;
users[recieverAddress].numberOfRecievedMails++;
Mail memory mail;
mail.title = Title;
mail.reciever = recieverAddress;
mail._sender = senderAddress;
mail.time = Time;
mail.content = Content;
mail.serialNumber = getTotalReceivedMails(recieverAddress);
Inbox storage inboxR = users[recieverAddress];
Inbox storage inboxS = users[senderAddress];
inboxR.receivedMails[getTotalReceivedMails(recieverAddress)] = mail;
inboxS.sentMails[getTotalSentMails(senderAddress)] = mail;
}
function getRecievedMail(address _address, int serialNumber) public view returns(string memory ,string memory, string memory, address){
string memory content = users[_address].receivedMails[serialNumber].content;
string memory Title = users[_address].receivedMails[serialNumber].title;
string memory time = users[_address].receivedMails[serialNumber].time;
address _add = users[_address].receivedMails[serialNumber]._sender;
return (time, Title, content, _add);
}
function getSentMail(address _address, int serialNumber) public view returns(string memory ,string memory, string memory,address){
string memory content = users[_address].sentMails[serialNumber].content;
string memory Title = users[_address].sentMails[serialNumber].title;
string memory time = users[_address].sentMails[serialNumber].time;
address _add = users[_address].sentMails[serialNumber].reciever;
return (time, Title, content, _add);
}
}
有人能帮帮忙吗?我如何创建一个函数来从以太坊区块链获取在我的dapp上注册的整个用户列表?我知道如何通过创建getter函数来获取用户细节通过添加地址,但这是针对单个用户的。我想要一个完整的用户列表,请注册一些机构帮助使功能。
将注册用户存储在registerd
映射中。然而,映射是不可迭代的,所以如果您需要获得所有注册用户的列表,您需要将它们存储在一个数组中。
根据您的用例,您可以在映射旁边添加数组,或者用数组替换映射。
mapping(address => bool) registerd;
address[] public registeredArray; // added the array
function checkUser(address _address) public {
if(!registerd[_address]) {
registerd[_address] = true;
registeredArray.push(_address); // push to the array
}
}
由于现有的逻辑,检查registerd[_address]
值是否为true
,您将不会在registeredArray
中有重复。因此,如果用户执行该函数两次,他们的地址将只在registeredArray
中存储一次。
由于registeredArray
是public
,链下应用程序和其他合约可以使用自动生成的getter函数registeredArray()
读取(不能写入)它。