Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

0
votes
2answers
15 views

Display Array of struct within array of struct in react

I'm trying to create 2d array of different types, one of the users have suggested to use struct within a struct method -> 2d array of different types? Now I run into problem how to display it in my ...
0
votes
2answers
32 views

initialise array of struct inside a new struct

Following up on the question of 2d array of different types: 2d array of different types? How would one go about initializing a new array of structs (Vote) when creating a new struct (Project) ...
0
votes
1answer
26 views

2d array of different types?

I got quite an interesting question. I have created an array of structs, and in each struct, I have an array of address. Then I got a method that when its called on given struct [index] its adds ...
0
votes
2answers
24 views

Solidity: How to initialize an array within a struct, passed as a parameter?

pragma solidity 0.4.25; contract A{ struct N{ string any; uint256[] numberArray; } N[] public numbers; function init(uint256[] _numbers) public{ for (uint256 ...
0
votes
1answer
14 views

Why is there a difference between a public array and a function which returns it?

On-chain example: pragma solidity 0.4.24; contract MyContract { uint[] public array; constructor() public { array.push(1); array.push(2); array.push(3); } ...
1
vote
0answers
18 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[] ...
0
votes
1answer
40 views

How to loop through a array of addresses?

I have a mapping and an array that contains a list of addresses of students. When I loop over that array, the method .length is not working. How can I loop through the array Student_list shown below?...
1
vote
3answers
68 views

How to map an address with a string array in solidity?

Goal: I wish to store an array of names with an address in a smart contract. It's more like Key-Value pairs where addresses are paired with a list of type strings. Can I use mapping to implement this?...
1
vote
0answers
13 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
1answer
27 views

Changing dynamic array index content

I'm trying to to make a contract which lets the user choose any number he likes to populate an array of integers used in a mapping. Then he can choose any index of this array to increase its value by ...
2
votes
1answer
18 views

Array of items vs mapping+array of ids with web3js?

In most articles they recommend to use mapping. But mappings don't allow to iterate their items directly. And nobody says what to do if you need to get all the stored items. In my case I need store ...
2
votes
1answer
28 views

Can i create a new address this way?

I am busy with a project. So I have an array of a struct within another struct. In this case for example array of CattleHealth in Cattle. In my RecordHealth function, I am taking the address of the ...
1
vote
1answer
19 views

How to add an instance of a struct to an array of another?

Hi I am pretty new to solidity. I am busy with a project that records all kinds of information about cattle. In my code below my cattle struct has an array of CattleHealth and CattleGrowth which are ...
1
vote
1answer
49 views

How to map Array of Structs in Solidity [duplicate]

Need this architecture { address1 => [struct1, struct2], address2 => [struct1, struct2, struct3,], address3 => [struct1, struct2, struct3, struct4, struct5], ......
1
vote
1answer
57 views

Oraclize handling long response from cryptocompare

I have very custom task and I need to make sure what are limits of each technology. I haven't found nothing similair in this forum yet. I am using Oraclize to get response from this url: https://...
0
votes
1answer
49 views

Array of Struct always returns 0x0 at index 0

Here's my sample contract code. contract SimpleStore { uint public BetRound; uint public SelectedUser; struct Struct { uint _round; address _owner; uint _index; bool _active; ...
1
vote
1answer
67 views

Creating a 2D in-memory array in Solidity

I'm trying to work with 2D in-memory arrays in Solidity, in which the size is not known at compile time. I've noticed that if I define the array size as a constant, then I can use this syntax, which ...
3
votes
1answer
73 views

Pass array of structs to contract from truffe using ABIEncoderV2

How do you pass an array of structs from Truffle (javascript) to a smart contract (Solidity)? There are a few similar questions (like this one and this one) whose answers say you cannot pass a struct ...
1
vote
0answers
17 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 ...
2
votes
1answer
30 views

Working on my first smart contract for college…Need help with one error!

pragma solidity ^0.4.0; contract ZaPartnere { address[] partneri = [0x118531D9C6C9114c8B999Ac1197778d1669Fc6c6, 0x029c2D637e193716246A55c62F2302a61FaC81dC, 0x36FFE679f923BdADd17E79Ce159DE80f779b512C,...
2
votes
1answer
33 views

how to create a smart contract that receives funds, displays addresses that sent the funds?

function safeWithdrawal() public afterDeadline { if (!fundingGoalReached) { uint amount = balanceOf[msg.sender]; balanceOf[msg.sender] = 0; if (amount > 0) { ...
1
vote
1answer
22 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
77 views

Storing a dynamic array of structs which contains a dynamic array of structs

I am trying to create the following objects: Bounty[] public bounties; struct Bounty { address issuer; uint256 fulfillmentAmount; uint256 balance; bool bountyOpen; Fulfillment[] ...
1
vote
2answers
46 views

How to delete parts of an array?

How to delete the 0s within the array and reduce array to array.length - (array[i] == 0).length entries that the return is uint256[]: 1,2,3? (array[i] == 0).length not works, it just should show that ...
0
votes
0answers
59 views

Unable to pass an array of addresses to a contract from js

I am trying to write a test for an airdrop contract, which takes in an array of addresses and sends a number of tokens to them. The test code is as follows: it("should airdrop 20 tokens to 3 ...
0
votes
0answers
25 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
2answers
35 views

How to manipulate value stored in array (Solidity smart contract)?

I'm currently fighting with a seemingly very simple issue. I just try to increment the value stored in an array in a Solidity contract. uint256[] public barLengthToBarAmtForSale; I initialise the ...
1
vote
1answer
45 views

How to retrieve length of array stored in mapping on frontend?

I have a contract with the following mapping: mapping (uint16 => uint256[]) public barLengthToBarIndexForSale; What's the best way to retrieve the length of an array for a given key on the ...
1
vote
2answers
33 views

Returning dynamic arrays: out of gas

I'm trying to return a dynamic array of structures from a Solidity function to a client. Since I need to build the result, I'm declaring a storage variable and add elements to it: function ...
0
votes
0answers
31 views

How to use multidimensional memory array in solidity

I'm working with a project where I have requirement of multidimensional memory array. Here is basic structure of types and person(s) Id -Types -0 -Person1 -Person2 -1 -Person1 -...
1
vote
2answers
86 views

What should happen if I push a value into array and the function fail afterwards?

What should happen when I modify an array, afterwards in the same execution the function fails? Should I expect the pushed value to be presented or does it get deleted? for example: contract ...
1
vote
1answer
140 views

Code Explanation of how to return an array of structs

I was reading a tutorial on a medium about how to return an array of structs. This is the code they have for doing it. pragma solidity ^0.4.13; contract Project { struct Person { ...
0
votes
1answer
46 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 ...
0
votes
0answers
97 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 ...
0
votes
1answer
33 views

What is the cheapest and most efficient way to send data to a struct, and why?

I'm struggling to find which way to send data to a struct is cheaper and most efficient. I came up with these 2 different styles: struct Database { string name; string addressPerson; } ...
1
vote
1answer
67 views

Assigning Array Parameters

Attempting to assign array parameters globally bytes32[] params; (params[0], params[1]) = ("p1", "p2"); results in ParserError: Function, variable, struct or modifier declaration expected. When ...
0
votes
1answer
34 views

copying bytes32 values from map to array

I am trying to copy stored values from a map to an array. I have the following code: pragma solidity ^0.4.21; contract test { event LogTest( bytes32[5] a ); bytes32[5] arr; mapping (uint=...
0
votes
1answer
40 views

Passing very large array from solidity to web3

I have a large data array in the BC. The array has 15,000,000 items of Car struct struct Car{ string tag; uint mileage; string make; string model; address address; string country; } I need to ...
1
vote
1answer
52 views

an alternate way to get all products from dynamic array?

I'm newbie with Solidity and trying to make some smart contract for eCommerce :D basically. Here is code of that: pragma solidity ^0.4.16; contract ecommerce{ event OnProductAdd(uint, string, ...
1
vote
0answers
91 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
47 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 ...
3
votes
2answers
79 views

How to pop from (decrease the length of) a memory array in Solidity

I have a function that gets an address array from another contract, conditionally deletes the msg.sender from the array, then saves the new array back to that contract. Because the array backerList ...
1
vote
1answer
47 views

Compare memory pointers in Solidity

Is there a way to compare pointers in Solidity? I have two local variables: uint256[] memory a = new uint256[](256); uint256[] memory b = a; If I try a == b I get this compiler error: TypeError: ...
0
votes
1answer
67 views

Initializing fixed-size storage array given function parameter

I would like to store an array of fixed size in my contract storage, with a user-defined size, initialized to all 0s. Here's an example contract: pragma solidity ^0.4.24; contract Test { ...
0
votes
1answer
103 views

Why do arrays declared within functions reference storage point 0 by default?

According to the Solidity Documentation, for the following code, ". . . the compiler interprets x as a storage pointer and will make it point to the storage slot 0 by default. This has the effect ...
1
vote
2answers
68 views

Deleting index of an array in terms of optimization for looping

I have a dapp where an admin can upload a number of cakes (20 to 30). People can come into the site and decide whether they want to buy a cake uploaded by admin. So for smart contract, I made an ...
0
votes
1answer
171 views

Solidity: How to add data to a dynamic array

I am trying to create a list of bytes data on a dynamic array. Here is a very simple contract for an example of what I want to do. I want to be able to call the array to get a list, but it only ...
0
votes
1answer
37 views

How to pass and store more than 16 arguments in a contract [duplicate]

I'm trying to create a contract which stores simple user profile information like name, email, age, address, etc (all string/bytes type) and I've come across the size restriction issue when passing ...
0
votes
0answers
27 views

Why does a byte array (byte[ ]) waste 31 extra bits per byte?

According to the Solidity documentation, It is possible to use an array of bytes as byte[], but it is wasting a lot of space, 31 bits every element, to be exact when passing in calls. It is better ...
0
votes
1answer
49 views

how to replace an array in a mapping?

I'm learning solidity right now and making a dApp where users get paid to perform actions. Actions are paid based on certain criteria. Since I can't schedule a cron job on ethereum, I can only pay ...