Questions tagged [arrays]
Questions regarding the use of Arrays in solidity.
77
questions with no upvoted or accepted answers
3
votes
3answers
1k views
How to handle dynamic size string array in solidity?
I have a shop owner and a shop owner can have multiple products. I want to keep the products id in an array.
Also, A public method can return all the product id list of a shop owner.
I tried the ...
3
votes
0answers
201 views
Implement constant search function in Solidity or in Javascript?
I am working with Meteor + Web3. Is it recommendable to implement a constant search function in Solidity or to transfer the data into a javascript array each time to then search in it?
Thank you!
2
votes
0answers
16 views
Why `array[array.length] = elem;` fails for appending to a dynamic storage array in Solidity?
Consider the following contract:
pragma solidity ^0.8.0;
// SPDX-License-Identifier: WTFPL
contract Demo {
uint256[] public numbers;
function append(uint256 num) public {
...
2
votes
0answers
55 views
Performing a C-like pointer increment in Solidity
I have a memory or calldata struct with an attribute of type byte[]. I want to have a memory variable with that data, except for the first n Bytes.
In C, you'd just write
typename variablename = ...
2
votes
1answer
92 views
How to return values from dynamic array push function?
How to fetch value from dynamic array push function? Below is the code of pushing function, but how will I fetching that array values?
contract PushStruct {
struct MyStruct {
uint field1;
...
2
votes
0answers
79 views
Why is putting data to storage more expensive during the deployment of contract than storing the same data by a method?
I am working with truffle and solidity (v0.4.23) and found out that when I pass in an argument of constructor some bigger amount of data (16kB) that are saved to storage of a smart contract, it is ...
2
votes
0answers
158 views
Global storage array - setting length on declaration vs inside a function
I have a global storage array of 64 entries, which I declare as follows:
uint256[64] globalArr;
However, its length really depends on a constant variable in my code:
uint8 constant MAX_VAL = 63;
...
2
votes
0answers
459 views
Why is dynamic array length not updated after addition of new element?
I created a struct as follows:
struct Node{ // each node models each device on the network
address nodeAdress; // adress of the node
uint trustValue; // trust value ...
2
votes
0answers
59 views
How does a plot “Gas price to call / memory allocated” looks like?
Pretty simple question: i.e. I have a dummy contract like this one:
contract simple {
uint[] dataArray;
function push_one_more() {
dataArray.push(1);
}
}
How does the cost of ...
1
vote
0answers
6 views
Invalid type for argument in function call for array
I am currently a beginner to solidity programming. However, I am getting an error when I try to send an array for an argument for a function.
constructor() {
addQuestion('Is this a question?', ['...
1
vote
0answers
12 views
How to sort structs by a certain value?
I want to sort next Struct by amount. Any idea how to get it? Thnks
pragma solidity 0.5.1;
contract AllWalletsContract{
struct AllWallets{
address wallet;
uint amount;
}
...
1
vote
0answers
31 views
Saving String Memory Argument to Struct Array
I have the following Solidity code that compiles and tests okay, but the insurancePlan property of the Patient struct is not being saved. I've tried many variations and it is just blank (empty) when I ...
1
vote
0answers
24 views
Solidity. Pushing a struct to an array. But that struct contains an array. Copying of type struct memory[] memory to storage not yet supported
Here is my code. I need to have a "checkpoint" object that stores some information including an array of "delegations" which is another object.
struct delegation{
uint256 ...
1
vote
0answers
28 views
Checking if an item present in an array in Solidity
I want to check whether a specific item is included in an array or not. For example, consider the following array.
uint[] a = [1, 2, 3, 4]
Is there any way that I can get true if I have given 4, and ...
1
vote
0answers
56 views
how to display a struct inside mapping inside struct in solidity on react js frontend
I have a mapping of struct (mapping (address => struct)) inside another struct. I'm having issue in how to display the struct in the front end while using Reactjs.
1
vote
1answer
25 views
arrays and structs for function arguments
A lot of contracts and libraries with functions that use arrays and structs for arguments. Structs can themselves include arrays, structs and mappings. For example:
function doSomething(MyStruct ...
1
vote
0answers
78 views
How to pass array parameter. Drizzle
Is there have a way to pass array parameter from the drizzle to my contract ?
I want to pass an array of uint[]
i have read a lot about getData() function and encodeABI but how to use it in Drizzle ?
1
vote
1answer
30 views
Use Values Returned from One Function in Another
need a little help if you can spare the time.
Making a little card game where people can collect "warrior" cards.
We'll have an initial 12 master "cards" that supply the initial traits for the ...
1
vote
0answers
27 views
Why is an address changing when I add it to an array?
I'm encountering a memory / storage issue (I think) w/ an array of addresses where I want to swap out one address with another.
Here's how I've initialized the array:
address[] public maintainers;
...
1
vote
4answers
998 views
loop to find the highest value in an array
I have made a struct which has an address called playersAddress and a uint called numbers.
I then have a fixed sized array which has in it the playersAddress and the numbers.
When someone calls ...
1
vote
0answers
382 views
passing byte[] to smart contract using nethereum
I made a solidity function that allow a user to send multiple transactions with the same nonce as a singleton Tx signer:
function multipleTransferPreSigned
(
bytes[] _signature,
address[] ...
1
vote
0answers
52 views
Gas cost of adding an item to a class-level (storage) array
I would like to make sure that the gas cost of adding an item to a (storage) array is constant, i.e., not dependent on the length of the array.
To my understanding, the relevant EVM operation is ...
1
vote
0answers
121 views
Understanding Solidity Libraries using Solidity Docs code example
I was going through this code (see code below) about how to use libraries in solidity docs :
https://solidity.readthedocs.io/en/v0.4.24/contracts.html?highlight=library#libraries and there are some ...
1
vote
1answer
29 views
Best ways to get only “active” in arrays - get group members
let's say I have an group contract where users can join and leave groups. When a user joins a group, his address will be added to members[groupId]. When he lefts, the members[groupId] will be set to ...
1
vote
1answer
394 views
solidity sorting array - LIFO / FIFO
I am adding to an bytes32[] array via push().
When I traverse the array with for (uint i = 0; i < array.length; i++), This is First in First Out (FIFO) sequence right?
How can one perform a ...
1
vote
0answers
443 views
Dynamic arrays VS mappings when it comes to gas usage?
I'm puzzled what is more efficient to use, from the point of gas consumption (storage and execution), dynamic array or the mapping and element counter.
To be more clear, I'm not asking the basic data ...
1
vote
0answers
335 views
Mocha timeout, not resolving promise of large array from method.mymethod.call()
I'm trying to test a contract getter method that returns a large array. I am testing using the web3 library in Mocha with ganache-cli provider. My contract deploys alright, but trying to call the ...
1
vote
0answers
98 views
Remix testing timeout with JavaScript VM when calling a large array
I'm trying to test a contract getter method that returns a large array. I am using Remix and deploying using JavaScript VM. My contract deploys alright, but trying to call the getArray() method causes ...
1
vote
0answers
193 views
Modifiers: can I pass an array or specify that not all of them have to pass, but one is enough (OR functionality instead of AND)
Say I want to restrict a function to only authors and editors. And later on, I want to restrict another function to authors and contributors only.
So ideally, I would add a modifier like this:
...
1
vote
3answers
333 views
How do i add to a nested array within a struct
I am unable to add elements to a array within the struct
struct D {
string name;
address key;
}
struct Aone {
string aid;
B[] blist;
D[] dlist;
}
...
1
vote
1answer
104 views
Smart contract returning values as zeros
pragma solidity ^0.4.18;
contract TransManager {
//state variables
// contract owner
address private creator;
uint transCounter;
// File Meta metaData
struct metaData{
uint id;
...
1
vote
0answers
57 views
What's the suggested way to create a 2D array which can be sparse at the beginning and grow dense later?
I can think of two ways:
1) Allocate a 2D array:
uint256 public W = 15;
uint256 public H = 15;
uint256[W][H] public grid;
2) Use mapping:
mapping (uint8) => mapping(uint8 => uint256)) public ...
1
vote
2answers
393 views
Invalid Op Code - passing bytes32 array to a contract method called from another method of another contract
I've been banging my head on this one for some hours, I'd appreciate any help :)
This is a final edit to clearly state the issue: Is there a proper way to pass a bytes32 array to a method on contract ...
1
vote
1answer
104 views
What does State Trie save when pushing into array?
I'm interested/worried about the size of the State Trie over time
Lets suppose I have a contract with two vars:
uint myNumber;
uint[] myArray;
As far as I understand, If I change the value of "...
1
vote
0answers
103 views
Removing Array Element by Copying to Temp Array
I read you can delete an array element like this:
How to delete an element at a certain index in an array?
I was wondering if I can do it this way too:
function playerUnjoins() {
address[] ...
0
votes
0answers
17 views
How to add data to an array inside a struct?
I have made a struct which contains an array . Now when I am adding data to the struct, I'm not able to add data to the array .
struct User {
uint256 limit; // spending limit
uint256 ...
0
votes
0answers
11 views
Solidity: Handling multi-mappings with multiple entries per key
I am wondering how I would implement a smart contract that would allow me to iterate through a list of (possibly matching) results in a mapping in order to "find" the right one to get. In a ...
0
votes
1answer
19 views
Update tuple in array
I am trying to update a tuple in my array in solidity. I currently have an array that looks like this
Array[(a,b),(x,y)].
What i would like to do is to later be able to update it to look like so Array[...
0
votes
0answers
25 views
invalid opcode while getting an element from a string of array
I am having some problems executing the final function of this contract (getmsg)
this is the code:
struct req{
uint yes;
}
struct fri{
uint yes;
...
0
votes
1answer
13 views
Increase values in array
I am increasing the value at a particular index in Solidity but it is giving me bad instruction error.
Can anyone tell me what I am missing here?
Please check the sample contract for reference:
pragma ...
0
votes
1answer
26 views
Returning a dynamic array of structs
I'm trying to return a dynamic array of structs created on the fly from storage data, similar to how we return ViewModel from DB data in MVC. What is the best way of doing it in Solidity?
Tried ...
0
votes
0answers
25 views
how to pass fix size array via interface in solidity 0.4.25
I used the following code
interface test{
function testfunction(uint[20] memory P, uint[20] memory Q) external returns(uint,bool,uint);
}
It gives the error Location has to be calldata for external ...
0
votes
0answers
13 views
Arrays not showing up inside structures
I am creating a record of people and have created a structure
struct asset{
uint assetId;
string hash;
address creatorAddress;
uint cost;
uint soldCount;
}
struct person{
uint ...
0
votes
0answers
24 views
How to retrieve all the values form the mapping?
Yield farming. Unaudited food token. Anon founder. Crazy APR%. Of course, if it is "too good to be true" it probably is...
I would like to "don't trust, verify" and verify who is ...
0
votes
1answer
25 views
Storing multiple values in one key mapping dynamically
I am making a blockchain project using solidity which requires storing various objectIDs corresponding to a personId.
The person can add and remove objects and we cannot hard-code the number of ...
0
votes
0answers
15 views
Acess position in array vs Access key in mapping (uint IDs) - What's the efficient way?
I have a doubt about efficiency in solidity, when searching in lists of integer values, in mapping vs array.
Array
Imagine that I use the array position as element ID, and the content as the value, as ...
0
votes
0answers
23 views
How store memory array in storage
In this code, I want to send an array of bytes32 and store them in the struct but I get the error which is
TypeError: Type string memory[8] memory is not implicitly convertible to expected type ...
0
votes
0answers
13 views
EthersJS returns array double wrapped i.e [ [“0x838383…”, “0x3BC2…”, ] ]
I have a method that is supposed to return an array of deployed contract addresses, it works albeit the value that is returned client side is wrapped with an extra set of brackets and also when ...
0
votes
1answer
11 views
push to a temporary array within a function
I come from nodejs and this seems odd to me..
can't seem to push anything to a temporary array.
function get_unconfirmed() public view returns (address[] memory, uint[] memory, uint[] memory) {
...
0
votes
0answers
52 views
join an array of strings and map, filter, reduce functions in solidity
I want to concatenate an array of strings into a single string using solidity, just like the array join function in javascript and python. Also I want to do map/filter/reduce functions in an array. Is ...