Questions tagged [struct]
Question about the solidity Structs
479
questions
1
vote
1
answer
12
views
Dynamic update of struct field inside mapping
I have a struct
struct Shots{
uint coverDrive;
uint straightDrive;
uint pull;
uint squareCut;
}
and I used a mapping
mapping(address => Shots) public BattingSkill;
Now I want to ...
0
votes
0
answers
30
views
Can't Access Properties Off of Struct In Foundry / Forge: Possibly Due to Nested Mapping
I am unable to get attributes off a struct in my contract from my Foundry test file. How can I get a property off an instance of a struct in my contract from my Foundry test file.
In DAO.sol contract (...
1
vote
1
answer
29
views
What is the problem with structs having just arrays as its members?
I'm getting this error
TypeError: The struct has all its members omitted, therefore the getter cannot return any values.
--> Test.sol:11:5
for the following smart contract code:
// SPDX-License-...
0
votes
1
answer
11
views
Array of structures
How to store the returned array of structs from a contract to some other contract?
Like for eg. I have a doctor contract that has the function as :
function getRegisteredDoctors() public view returns(...
0
votes
1
answer
20
views
How can i call an already deployed contract with struct as parameters from a new contract?
I want to call some function in a contract B from a contract A. In my contract B I have some function who's signature is function execute(ExecutionInfo[] calldata executionInfos) where ExecutionInfo ...
0
votes
1
answer
11
views
Variable Values do not persist in between function calls
I am new to Solidity, and I am experimenting on smart contracts using Remix-IDE connected to a deployed local Geth instance.
What I am trying to do is to create a struct containing multiple fields for ...
2
votes
0
answers
13
views
What's the benefit of encapsulating storage slots as structs?
Often times you'll see the following pattern for reading and writing to storage slots:
struct Slot {
address value;
}
...
function getSlot(bytes32 slot) returns (Slot storage s) {
assembly { s....
1
vote
2
answers
33
views
Update struct variable based on the other variable from same struct
I'm trying to create a contract with a struct (Category) containing an array of another structs (Item). Item struct has a boolean variable minted with a default value of false. I'd like to change that ...
0
votes
1
answer
32
views
Error When Passing Struct as Input to Function in Solidity
I'm trying to just call a function that has a struct argument...The thing is that Remix somehow doesn't recognize all the inputs from the struct in the function.
I tried making the image very ...
3
votes
1
answer
49
views
How to parse event args: struct of custom internalType
Question: How does one parse the contents of a custom struct emitted as an indexed parameter with a contract event? Or, is it even possible to emit and then parse a custom struct from the event args?
...
1
vote
2
answers
48
views
Convert Custom Struct to Uint in solidty
I'm unable to convert Uint/bytes32 back to my struct Jury
Here is the struct im trying to convert to bytes32.
struct Jury {
address userAddress; //20 bytes
uint16 credibilityScore; //2 bytes
...
0
votes
3
answers
30
views
Unable to push structure in an array dynamically
I have a election contract as follows:
contract election{
struct Candidate{
uint id_db;
string name;
string party;
string election_sign;
...
0
votes
0
answers
29
views
How to compare two values of structs of the same msg.sender from different mappings
I'm having a Smart Contract contest among friends about who loses the most weight. Each competitor is expected to enter its weight at the beginning of the week and at the end of the week. The ...
2
votes
0
answers
49
views
Struct returns indexed numbers and property names
I'm using ethers and hardhat to run my React application. I want to pass an object as a parameter to a Solidity function. The function should map the object (struct) to the id provided as a property ...
0
votes
1
answer
57
views
Retreive a mapping nested inside a struct from Ethers
Hello Ethereum community,
I have couple of structs in my solidity code to organize things. Here is one struct from the code:
struct Staking {
uint total;
uint currentIndex;
mapping(address => ...
1
vote
0
answers
28
views
Best approach for modifying/implementing structures on Upgradable Smart Contracts
I am developing a project that is heavily reliant on the ability to upgrade smart contracts. However, I am not sure of the best way of handling structures in such contracts.
Trivial Example: (Imagine ...
3
votes
1
answer
24
views
Initializing Stuct
I would like to know if it is considered good practice initializing a struct when instantiating it or if it is just equally good to initialize its content right after instantiating it.
In other words, ...
0
votes
1
answer
45
views
How to call another function in solidity that has a struct for a parameter of over 16 variables?
Let's say I have the following code:
// This is in a separate imported interface.
interface MyInterface{
struct MyStruct {
uint256 one;
uint256 two;
// . . .
uint256 sixteen;
}
}
/...
1
vote
1
answer
15
views
What are the advantages/disatvantages to specify a struct param in a function as calldata vs. memory?
If I am not modifying the input parameter in the function body are there any reasons a reference type parameter should be labeled as stored in memory?
More generally what are the reasons to go for ...
2
votes
1
answer
51
views
Dynamic array inside a struct in solidity
contract EventContract {
struct Event {
address organiser;
uint price;
uint totalTickets;
uint ticketsAvailable;
uint startDate;
uint endDate;
...
1
vote
1
answer
36
views
How to return an array of structs from one contract to another contract in solidity
I am creating a Course contract which contains some details about the course hosted...
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Course{
string courseName;
uint256 ...
1
vote
2
answers
62
views
Define a struct in library vs in the contract
I have a struct defined in a library (to call it from different contracts):
library DataTypes {
struct Test{
uint256 a;
uint256 b;
}
when i want to initialize it or update in my ...
2
votes
1
answer
65
views
Unable to reach storage struct via mapping
Gm,
I've got a smart contract for 2D board game purposes. There is a struct Unit
struct Unit
{
UnitType unitType;
uint16 x;
uint16 y;
int8 hp;
int8 dmg;
...
0
votes
2
answers
25
views
Updating a struct property in an array stored as mapping value
This is my data structure for storing a list of questions which will be different for every address
struct Question{
uint16 id;
string question_string;
string answer;
}
and this is stored ...
0
votes
1
answer
46
views
Testing Struct Data
I have a struct and getter function that should return the id to zero when I delete it from my function but I dont know on how to return from my testing script
Here is my struct, mapping & ...
0
votes
2
answers
34
views
Is this the right way to assign values to struct array with mapping?
struct userdata
{
string task;
bool isdone;
}
userdata[] public userinfo;
address public user;
// mapping (uint[]=>string) public tasksno;
mapping (address=>userdata[]) ...
0
votes
2
answers
40
views
Return an array of structs for a specific condition
I would like to know if it is possible to create a function that would return an array of structs for a specific condition. Let's consider this for the following contract:
// SPDX-License-Identifier: ...
0
votes
1
answer
54
views
Mint and Create a Struct give me give me error "Stack too deep try removing local variables"
I have an issue with my code, I want to create a function to be able to Mint a token, and add a struct to represent all attributes of this token.
The issue is, I have lot's of args on my function (8) ...
2
votes
1
answer
126
views
Do the tight variable packing rules apply to arrays in structs?
Tight variable packing is a known coding pattern in Solidity for saving gas by placing variables that are lower than 256 bits closer to one another in structs.
For instance, the following will cost ...
0
votes
1
answer
56
views
Operator == Not compatible with types address[] storage ref and address
I have a mapping which maps uniqueids to an array of structs called Listing:
mapping(uint256 => Market.Listing[]) private _listings;
// the struct in the Market.sol
struct Listing {
address[] ...
0
votes
0
answers
27
views
Mapping or Struct to store data in NFT : gas fee and best practice
What is the best practice and gas less to store data inside the nft, not in metadata.
First exemple with 3 writes and second with 1 write.
But it seems Struct is more expensive. And when it will be ...
0
votes
1
answer
26
views
Copy nested struct from memory to storage
I have a fully filled struct ( in memory ), I want to store it ( in storage ), but I can't convert it.
I've tested many ways and I got two kinds of errors:
copying of type struct of Player memory[] ...
0
votes
1
answer
110
views
How to unpack Tuple to Struct in Solidity?
I saw a similar question here which I tried to use in my example but I still get confused from the code. Can you tell me what is the difference between those two examples?
contract First {
struct ...
1
vote
0
answers
58
views
How to Test Solidity Struct in Args?
I have an event, where I forward a Struct when issuing it.
But I'm not able to test this with chai + hardhat, I get the following error:
Here is my getResult function, my struct, and my emitted event ...
0
votes
0
answers
14
views
Adding structure in array using new keyword similar to adding contract in another contract
Below is the source code which I wrote to deploy one smart contract using another smart contract
//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import "./contractone.sol" ;
...
0
votes
1
answer
30
views
Normal function asking to be made Payable
Below is the programme in solidity I wrote which assigns value to elements of an array.
pragma solidity 0.8.16 ;
contract arr
{
uint i = 0 ;
people[] public person ;
struct people
...
0
votes
1
answer
17
views
how can i use mapping on structure inside another structure and create some elements
i want to create some Pledges for Startup there are these structures:
struct Startup {
address payable founder;
string title;
string startupDescription;
uint goal;
uint ...
1
vote
2
answers
51
views
How to create migration for smart contract with Struct Array in constructor
I have a smart contract that has the following Struct Array in its constructor:
struct Details {
string tokenType;
address tokenAddress;
uint256 tokenId;
uint256 amount;...
0
votes
3
answers
153
views
When storing a struct in mapping, how does the EVM storage layout handle if the slot's capacity (32 bytes) is exceeded?
contract test {
struct StructData {
address a; // 20 bytes
uint128 b; // 16 bytes
uint128 c; // 16 bytes
bool d; // 1 bytes
}
mapping(uint256 => StructData) public ...
0
votes
1
answer
21
views
How to set a require conditions for nested mapping on struct array
I have a nested mapping that has a group id and the item id so that it can cancel the order based on these id's specifically. But how do I set a condition e.g revert the error if the id doesnt exist ...
0
votes
1
answer
34
views
Return argument type struct Details.Prosumer storage ref is not implicitly convertible to expected type (type of first return variable) uint256
Prosumer[] public ProsumeArray;
struct Prosumer{
uint ProsumerID;
uint Pros_power;
uint per_unit_rate;
}
function setPros(uint ProsumerID, uint Pros_power,uint per_unit_rate)public {
...
3
votes
2
answers
795
views
How to call function with struct parameter in ethers?
My solidity:
contract Test {
struct Num {
uint value;
}
function double(Num calldata num) external pure returns(Num memory) {
return Num({ value: num.value * 2 });
}
}...
0
votes
1
answer
39
views
How to append item into an array inside a struct that's inside a (nested) mapping?
Very new to Solidity/Coding in general. Trying to make a Kahoot-like game where there's a Teacher (owner) that can upload questions with an ID to each question, 4 optional answers & 1 correct ...
1
vote
1
answer
34
views
Structs and Mappings
I am going through a tutorial at the moment and have just covered the basics of mappings and structs which I feel like I understand. However, in the next section of the tute we put together mappings ...
0
votes
1
answer
107
views
How to properly format an argument that is a struct with nested structs, with web3.py?
I have the following ABI:
{
"inputs": [{
"components": [{
"internalType": "enum LibNFTOrder.TradeDirection",
...
0
votes
0
answers
15
views
Is storing two copies of a mapping of a struct cheaper than storing one copy and two mappings of integer?
I am working on a smart contract that stores transactions of complicated objects (a struct with 11 properties including strings, addresses, boolean, and uint256). I want each user to have access to ...
2
votes
1
answer
502
views
How to send array of structs using Foundry cast
Smart contract
struct NewPrice{
address asset;
uint256 timestamp;
uint256 price;
}
function updatePrices(NewPrice[] memory _array) public onlyRole(FEEDER_ROLE) {
...
1
vote
0
answers
14
views
Can't run the getbook function in the below given code due to "Payable"error
'''
pragma solidity >=0.1.0 <0.9.0;
contract Day5 {
constructor() payable {
}
struct Book { string title; string author; uint256 book_id; }
Book[] types;
uint count=1;
function setBook(string ...
1
vote
1
answer
63
views
Declaratively creating an array of structs with one statement in Solidity
I want to create a modifier that accepts a dynamic array, like this:
modifier parameterizedTest(Set[] memory sets) {
// ...
}
Where the Set struct is defined like this:
struct Set {
uint256 x;...
1
vote
1
answer
33
views
Push opeartion not working
I am creating a To-Do list with the limitation that one address should only be able to add upto 100 notes.
Below is the snippet of code
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
contract ...