How to access a struct element when the struct is inside mapping - solidity

struct adminMember{
address admin;
bool _isDeleted;
}
mapping(address => adminMember) adminMembers;
I want to access _isDeleted value and return it from a function.

you create this function which returns a bool value
function myFunction(address admin) external view returns (bool) {
return adminMembers[address]._isDeleted;
}

Simple answer for access.
adminMembers[adminAddress]._isDeleted
Function.
function isDeleted(address adminAddress) external view returns (bool) {
return adminMembers[adminAddress]._isDeleted;
}

Related

How to run multiples contracts functionalities in a single contract Solidity

I'm new to Solidity but I can't find much information about my problem.
For example, I want to make different contracts for different functionalities (I see them as classes)
For example
Main contract
// SPDX-License-Identifier: None
pragma solidity >=0.8.6;
import "./AuthContract.sol";
contract Contract {
string public message;
constructor() {
message = "test";
}
function getMessage() public view returns(string memory) {
return message;
}
}
and second contract
contract Auth {
struct UserDetail {
address addr;
string name;
string password;
string CNIC;
bool isUserLoggedIn;
}
mapping(address => UserDetail) user;
// user registration function
function register(
address _address,
string memory _name,
string memory _password,
string memory _cnic
) public returns (bool) {
require(user[_address].addr != msg.sender);
user[_address].addr = _address;
user[_address].name = _name;
user[_address].password = _password;
user[_address].CNIC = _cnic;
user[_address].isUserLoggedIn = false;
return true;
}
// user login function
function login(address _address, string memory _password)
public
returns (bool)
{
if (
keccak256(abi.encodePacked(user[_address].password)) ==
keccak256(abi.encodePacked(_password))
) {
user[_address].isUserLoggedIn = true;
return user[_address].isUserLoggedIn;
} else {
return false;
}
}
// check the user logged In or not
function checkIsUserLogged(address _address) public view returns (bool) {
return (user[_address].isUserLoggedIn);
}
// logout the user
function logout(address _address) public {
user[_address].isUserLoggedIn = false;
}
}
How could I use the functionalities from that contract in the main contract?
Is such a thing possible in the blockchain?
I am quite new here also but i can solve your problem, if not solve i can lead you to a good path .
so firstly you said you want to create multiple contract for different functionalities, this is good but keep in my you are going to exhaust a lot of gas.
so the answer to your problem is easy you can just read it and implement it.
if you want to use a contract in the Another contract (main in your case) you can do it in two ways(according to my knowledge there might be other).
using new keyword
using address of your previously deployed contract
we will be using the First case as i suppose you havenot deployed the second contract yet
In Order to do it you can use
Auth myObj=new Auth();
This will create a new instance of the contract Auth in your main contract and now you can use the Auth contract's function in your Main contract.you can create a function copy the above line and you can use the Functions using dot operator.
myObj.register(_address,_name,moreandmore);
I believe this will solve your problem if not you can ask it.
Thank You!

How to test the Solidity-By-Example Mapping smart contract in Remix?

I want to test the following code in Remix. But what is the procedure?
What do I put in the input field for the function labeled set?
Mapping.sol
// https://solidity-by-example.org/mapping
// Mapping
// Maps are created with the syntax mapping(keyType => valueType).
// The keyType can be any built-in value type, bytes, string, or any contract.
// valueType can be any type including another mapping or an array.
// Mappings are not iterable.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}
contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(
address _addr1,
uint _i,
bool _boo
) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}
This code came from Solidity by Example.
When I implement it on Remix, I get the following screen.
At this point, to test it, I think I need to map an address to a uint256, so I enter the following in the field next to the set button:
["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3", 7439]
The value of the address was a randomly generated hash (I suspect the random hash might be a problem?)
I expect to see the set function render the value 7439. But, instead, it throws the following error:
transact to Mapping.set errored: Error encoding arguments: Error: invalid address (argument="address", value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=address/5.5.0) (argument=null, value=["0xcf646ed6e21fd0756ec45a6be5e1057fc24a1b8308175ff0b9f97f565b594eb3",7439], code=INVALID_ARGUMENT, version=abi/5.5.0)
What am I doing wrong?
You have generated a random SHA-256 that is of the format of a valid address, but it doesn't exist on the Remix's browser-based (Javascript VM) chain as any account. If it does, you might be just lucky.
If you want to use valid addresses that do exist on the browser-based VM chain, copy and use the addresses in the account section.

I keep on getting ParseError: Expected type name // when I want to return a struct I have just created

I am new in Solidity and I have been trying to create and get STRUCT without adding it to an Array. I alway see Structs with arrays and the method .push and I wanted to try it without it.
I have created a single Contract with one Struct and one function to create and get it.
If I create a single public function to create, but not return, the struct it doesn´t give me any error... Like the following:
struct Todo {
string name;
uint age;
}
function createTodo(string memory _name, uint _age) public pure{
Todo memory myTodo = Todo(_name, _age);
}
With the above code I would also like to know why it wouldn´t let me set the pointer "Todo" as storage like: Todo storage myTodo = Todo(_name, _age); It gives an TypeError: Todo memory is not implicity convertible to expect type struct storage pointer.
Next, I tried to modify the function to create and RETURN but then is when I start getting the ParseError.
The code is the following:
function getTodo(string memory _name, uint _age) public returns(struct myTodo) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
With the aobve code I also tried in "returns(struct), returns(struct memory)"....
I would really appreciate any type of help here.
Thank you very much
You received this error because you're wrong to set a returns keyword on getTodo() function. In your case, you must change your function in this way:
function getTodo(string memory _name, uint _age) external pure returns(Todo memory) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
If you want to handle storage structs, see this smart contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct Todo {
string name;
uint age;
}
// Declare state variable
Todo[] todoArray;
// Push into array new struct
function createTodo(string memory _name, uint _age) public {
todoArray.push(Todo(_name, _age));
}
// Retrieve ToDo struct from specific index about struct array
function getTodo(uint _index) external view returns(Todo memory) {
return todoArray[_index];
}
}

why this error always on? Identifier not found or not unique

pragma solidity ^0.8.6;
contract mycontract{
address owner;
string name;
bool visible;
uint16 count;
constructor ()public {
owner=msg.sender;
}
function changname (stringmemory _name) public returns (stringmemory){
if(msg.sender==owner){
name=_name;
return "sucsesss";
}else{
return "acsess denid";
}
}
function showname () view public returns(stringmemory){
return name;
}
}
hey guuys i write this codes down but i gut the blow error what should i do now pls help me thank you
this project its belong to my university and if i lost in it i will fail my exam and im bechokh miram toro khoda komak konid thank you
i got some warning too what is this ?
SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> browser/test.sol
DeclarationError: Identifier not found or not unique.
--> browser/test.sol:14:21:
|
14 | function changname (stringmemory _name) public returns (stringmemory){
| ^^^^^^^^^^^^
It has a couple of bugs in its code.
builder visibility is not required
in the parameters of its functions it has a bad type
'stringmemory' the variable would be type 'sting' and it would be read from memory.
In this way your code would look like this:
pragma solidity ^0.8.6;
contract mycontract{
address owner;
string name;
bool visible;
uint16 count;
constructor () {
owner=msg.sender;
}
function changname (string memory _name) public returns (string memory){
if(msg.sender==owner){
name=_name;
return "sucsesss";
}else{
return "acsess denid";
}
}
function showname () view public returns(string memory){
return name;
}
}

How to return the Entered Value from Mapping Corresponds to address[msg.sender]

I am hitting hard to creating a lottery smart contract in solidity. I have tried a couple of ways, first I stored all the players address inside an array, but unfortunately that logic fail, just because of slow execution of array (when entries are more 1000+).
Now I am trying mapping mapping(address => Struct) entries;. Now what I have done here. I created a structure with user first ticket number and user last ticket number i.e uint userFstTcktNumber;
uint userLstTcktNumber;
Here is my structure
struct UserInfo {
uint userFstTcktNumber;
uint userLstTcktNumber;
} UserInfo userinfo;
Then I created a mapping
mapping(address => UserInfo ) public entry;
Then I have Created a function to enter the values inside the mapping and structure.
function letsdo(uint first, uint last) public{
entry[msg.sender];
userinfo.userLstTcktNumber = first;
userinfo.userLstTcktNumber = last;
}
Now what I want to do? I just want to get the values from the mapping and structure corresponding to the address, let's say I have 10 entries from 1 to 10 from address "0x617F2E2fD72FD9D5503197092aC168c91465E7f2" . And I have another 50 entries from 11 to 51 from another address "0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C".
To return this I am using the function
function get(address) public view returns(uint, uint){
return (userinfo.userFstTcktNumber, userinfo.userLstTcktNumber);
}
But it's not returning me the expected result.
It's always returning the userFstTcktNumber is 0, no matter what address I have passed inside the get().
What's wrong with this code?
You are not really using your map, you need to set the values to be able to search inside it.
function letsdo(uint first, uint last) public {
entry[msg.sender] = UserInfo(first, last);
}
You don't need a get function as well, since entry is already public, you can search directly using entry[<address>]
UserInfo userinfo after your struct is also unnecessary, since you want to work with map, you will need more than just an instance, the letsdo function I mentioned above is already doing it
Hey thanks everyone for helping me out, Actually Last night I sorted it out my own. And here is the full logic for the problem.
pragma solidity ^0.4.18;
contract lottery{
uint public lastTicketNumber = 0;
uint youEntredWithAmount;
address [] public players;
uint public entryFee = 0.01 ether;
struct UserInfo {
uint userFstTcktNumber;
uint userLstTcktNumber;
}
mapping(address => UserInfo ) public entry;
function letsdo(uint first, uint last) public{
players.push(msg.sender);
entry[msg.sender].userFstTcktNumber = first;
entry[msg.sender].userLstTcktNumber = last;
}
function currentLevel(address userAddress) public constant returns (uint, uint) {
return (entry[userAddress].userFstTcktNumber, entry[userAddress].userLstTcktNumber);
}
function numberOfParticipents() public view returns(address [] memory){
return players;
}
}