Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

54 questions with no upvoted or accepted answers
Filter by
Sorted by
Tagged with
3
votes
3answers
703 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
198 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
33 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
49 views

How to divide and allocate the amount to token holders without loops?

I have a smart contract that keeps record of the people holding the tokens in form of array of addresses. An owner of the smart contract, whenever, sends some ethers to the contract these ethers shall ...
2
votes
1answer
56 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
66 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
156 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
435 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
57 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
1answer
16 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
62 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
19 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
131 views

Standard work-around for using a Solidity constant array (which is not supported) in pure function?

I have an array in my contract like this and a pure method which uses it like this (simplified exmaple because my real exmaple is many more lines): uint[16] constant x = [10, 2, 6, 42, 19, 5, 4, 36, ...
1
vote
0answers
26 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
617 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
265 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
47 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
82 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
25 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
249 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
323 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
304 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
90 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
161 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
247 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
85 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
343 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
96 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
92 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
1answer
15 views

How to delete an element of array in efficient manner?

pragma solidity ^0.4.24; contract test{ uint[] public array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; function removewithgap(uint index) returns(uint[]) { if (index >= array.length) ...
0
votes
1answer
17 views

Ganache Storage

I am trying to push data to an address array within a struct array. There are no errors, however, when I check the storage of the array in my contract in Ganache, it remains empty. Is there something ...
0
votes
2answers
25 views

Arrays copy memory in Solidity

I have this simple code: function restart() public returns (uint256[] ) { uint256[] memory result = new uint256[](5); result[0] = 10; uint256[] memory result1 = new uint256[](5); ...
0
votes
0answers
10 views

Arrays copy memory in Solidity

I have this simple code: uint256[] memory result = new uint256[](5); result[0] = 10; uint256[] memory result1 = new uint256[](5); result1 = result; result1[1] = 20; return result; ...
0
votes
0answers
30 views

Computing matrix determinant in Solidity

I am trying to write a solidity contract to compute Matrix determinant. I am following the C++ implementation from here: https://www.tutorialspoint.com/cplusplus-program-to-compute-determinant-of-a-...
0
votes
1answer
14 views

Mapping multiple structs to a struct and call them

I have a question about how I should design my contract to achieve the following: I want to create products identifiers (can be struct or string) something like: "product#123abc". Then I want to map a ...
0
votes
1answer
9 views

Can you have an array of mappings? The mappings have an address key and a struct value

Can you have an array of mappings? I want to have mappings with addresses as keys and structs as the value. I want to store these addresses in an array and iterate through them. so far it looks like ...
0
votes
0answers
106 views

Web3- Array returned from function is always empty

I have contract in solidity and I like to return array or array by index. I'm using experimental feature on, and in remix in solidity I don't have issue with returing the array, but when I use web3 ...
0
votes
0answers
14 views

loop into an array and extract infos for specific address

I'm new in solidity . I'm trying to make a smartContract as bellow pragma solidity ^0.4.18; pragma experimental ABIEncoderV2; contract AnalyseContract { //struct => new type of data for this ...
0
votes
0answers
20 views

What is an efficient way for converting 'bytes4' into 'bytes' or 'byte[4]'?

I am currently converting bytes4 into bytes like this: function convert(bytes4 x) public { bytes memory y = abi.encodeWithSelector(x); ... } Is there a more efficient way? Also, is there an ...
0
votes
0answers
16 views

How can I distribute funds to present stakers efficiently?

I am building a smart contract which keep the token holder's eths for a while and give them rewards when they withdraw. The reward is given from the ethers collected by early withdraw of people. If ...
0
votes
2answers
322 views

Solidity & truffle: get elements from strings array

i'm glad to follow this forum, it is very helpful. I'm using truffle to develop contracts and i just found that dynamic arrays are not yet suported, for that reason i was using string arrays with a ...
0
votes
0answers
90 views

How to pass the whole array from one contract to another when working with different solidity files?

For example, I am taking one function function transferAddresses() in my contract contract one{} from where I am taking number of addresses in array. Now I want to transfer the whole data of the array ...
0
votes
2answers
141 views

Remove array specific value

In a array that the values are never repeated. Is there a function/library or something similar that you give the value to remove. And the function the empty space in the array?? function remove(uint ...
0
votes
1answer
255 views

Solidity. issue with fixed-sized arrays and dynamic arrays

I am passing a fixed array to a function where a dynamic array is expected. addType([1,5, 2,5, 3,2, 4,2, 5,1], [6,1], [7,5]); addType([1,5], [6,1, 2,5, 3,3], [7,5]); function addType ( uint8[] ...
0
votes
1answer
341 views

How to Serialize/Deserialize array of bytes?

I'm writing a smart contract and inevitably need to have arrays of bytes as input and output of functions. In remix arrays work just fine but with web3j and web3js there are several issues with arrays ...
0
votes
0answers
52 views

hash an array of structs

So what I am trying to do is create a keccak256 of an array of structs. pragma solidity ^0.4.24; pragma experimental ABIEncoderV2; contract Test { struct test1 { address test; ...
0
votes
0answers
219 views

Why are elements in a byte array padded to 32 bytes?

On readthedocs.io, it says that byte[] or bytes1[] elements are padded to 32 bytes. I thought at first it was for even distribution, but distribution would still be even if each element had exactly ...
0
votes
1answer
117 views

Error when trying to view return values

I've written the following contract contract Catalog{ address[] internal array; function pushItem(address _el) external{ array.push(_el); } function getArray() external ...
0
votes
0answers
110 views

Dynamically-sized byte array is value type?

In the doc, it says bytes: Dynamically-sized byte array, see Arrays. Not a value-type! string: Dynamically-sized UTF-8-encoded string, see Arrays. Not a value-type! Not a value-type! If ...