Questions tagged [arrays]

Questions regarding the use of Arrays in solidity.

Filter by
Sorted by
Tagged with
0
votes
1answer
10 views

Passing array of struct as parameter: Only libraries are allowed to use the mapping type in public or external functions

I am trying to push multiple items to the array of structs in the contract. Couldn't find a clear answer if it is possible to pass an array of struct as a parameter. Apparently I can't but I get a ...
0
votes
1answer
12 views

Display matrix in React

I am trying to return a matrix in React that I get from a contract function. This is the function that resonate the matrix: int[][] public publicData; function handleAddData(int[] memory ...
0
votes
0answers
67 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 ...
2
votes
0answers
23 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 = ...
0
votes
1answer
15 views

Trying to get array size but it reverts

I wanted to get array length by getter function, but it keeps reverts. But I have no idea why it's reverting. Here is my code. struct ItemAddress { uint8 sendNatCd; uint8 rcvNatCd; uint16 ...
1
vote
2answers
99 views

Array Costs in Solidity

Why is storing in an array the first time in solidity costs higher than the other times? The other times it has a constant cost which is less than the first time.
1
vote
1answer
34 views

Add Number to Item in all Struct Members

I have a struct where I want to increment an item across all struct members. For example, say there is a struct 'Contract', and new customers get their own bytes32 ID, which points to their own struct:...
0
votes
1answer
17 views

Query each struct of a Mapping vs looping getter function for front-end?

I have a mapping of structs and a counter variable to keep track of how many structs are stored in the mapping. For front-end development would you recommend to query each struct using the auto-...
0
votes
0answers
13 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
16 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
1answer
17 views

Check if Struct already exists in array of structs

I am new to solidity. I have a struct and an array of structs, like this: struct Adopter{ uint age; uint gender; // 0 woman, 1 man // maybe add an id field } Adopter[] adopters; So, a ...
0
votes
1answer
18 views

return array of strings

how do I return an array of strings? This is my code: contract PlanetEarthContract { string[7] continents; constructor() public { continents = ["North America", "South America", "...
1
vote
1answer
30 views

Weighted Random Number Array generation in solidity

I have an array like this in the array, uint256[] public numberArr= [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Each number in an array have their own weight, uint256[] public weightNumberArr = [1, 3, 9, 7, 15,...
2
votes
1answer
39 views

Shuffle Array of integers in solidity

I have an array of integers like this, uint256[] public numberArr = [0,0,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,5,5,5] What can be the best way to shuffle this array in solidity
0
votes
1answer
89 views

How to loop an array and store values in another after if conditions

I am trying to do something that I believe it should be simple enough. I want to make a getter function iterating a struct array find the min value and the values that is equal to this minvalue of a ...
2
votes
1answer
39 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 ...
0
votes
1answer
49 views

How to save data using multiple structures and arrays in solidity?

How can i use multiple arrays please? e.g: I have this contract: pragma solidity >=0.4.21 <0.6.0; contract SessionManagement { struct ChargeProfileSegment{ uint startDateTimeSeg;...
0
votes
1answer
24 views

What is the meaning of “memory is erased between (external) function calls”?

From the official documentation on the memory keyword: The second is “memory”, this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use. Can ...
0
votes
0answers
14 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 ...
3
votes
1answer
195 views

TypeError: Indexed expression has to be a type, mapping or array (is function (address) view external returns (string memory,string memory…)

I have 2 contracts first one is sub second one is main . First i deploy main and create a new subtoken. Then I want to access my maincontract in subToken contract but I cant :( When I compile this ...
0
votes
3answers
44 views

Is it possible to find out dynamic array size looking at a smart contract?

This question already answers that in order to get the size of an array programmaticaly, a getter should be defined. However, assuming that there isn't a getter, is there a way to find out size of a ...
0
votes
1answer
33 views

Getting information from contract address?

pragma solidity ^0.5.0; import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol'; contract SubToken{ using SafeMath for uint; struct token{ ...
0
votes
1answer
44 views

Return an array of structs using events?

I wan’t my function to spit out an array of structs. I know this is not straightforwardly possible. What is the best way for me to proceed? Should I loop through my array in my function and trigger ...
0
votes
1answer
27 views

What is the best way to store a hexadecimal value in an array in Solidity?

I am attempting to store a hash (web3.sha3) into an array in a smart contract. What is the best way to do that?
0
votes
1answer
41 views

Expected ')' but got identifier if(string name != users[name]){ ^--^

pragma solidity >=0.4.0 <0.7.0; contract NewHello{ mapping(address => string) public users; // contains a name for every address function updateUser(string memory name) public { ...
0
votes
1answer
238 views

Type string memory is not implicitly convertible to expected type struct InsanFactory.User storage ref

pragma solidity >=0.4.0 <0.7.0; contract InsanFactory{ struct User{ string name; address myaddress; } mapping(address => User) public userStructs; ...
2
votes
2answers
94 views

Where is the length of an array located in terms of memory layout?

For example, given the following cases: uint[3] memory arr; uint[] memory arr = new uint[](3); Suppose I try to access arr from within an assembly block (using mload, for example). Where (...
1
vote
1answer
91 views

Drizzle - accessing array items from a getter

In my contract I have a getter function that returns a full array of addresses: function getDeployedCampaigns() public view returns(address[] memory) { return deployedCampaigns; } Using drizzle ...
1
vote
0answers
44 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 ?
0
votes
1answer
43 views

Initialize an array of struct within a struct

I am working on a use case of "Purchase Order" wherein there will be multiple Products listed/ordered under one Purchase Order(PO) by a buyer. 1 PO : N Products struct Product{ bytes32 ...
0
votes
1answer
27 views

Storing geo location in contract

I am beginner in solidity programming, I am trying to write my first contract. I am trying to store a piece of land (which is array of coordinates) against a person. I am having trouble storing the ...
0
votes
1answer
83 views

Setting array.length to 0 triggers an error on solidity 0.5.0

Why setting array.length to 0 triggers an error ? I have a function that deletes an element from a dynamic array of structs within a mapping as follows: mapping(address => MyStruct[]) public ...
0
votes
2answers
43 views

Should I use the 'external' modifier to return a large array?

If I need to access a large array from my contract... say for my front end. Should I be using external to make it cheaper for myself? A comment by William Entriken on this question is what got me ...
0
votes
1answer
145 views

How can I send array of structs as arguments?

I want to send to my contract an array of currency and its price so it will store it: pragma solidity ^0.4.24; contract ExchangeContract { enum CurrencyType { USD, TWOKEY, BTC, ETH, DAI, USDT, ...
0
votes
1answer
44 views

Solidity array overflow

I'm trying to understand this array overflow attack on Solidity. (https://www.youtube.com/watch?v=gUqHgFuSsqg) In that video, the code is basically like this pragma solidity ^0.4.17; contract ...
0
votes
1answer
76 views

How can I push data to array when using mapping (address => address[]) mappingName?

What is the exact syntax to push data using the above given syntax?
0
votes
0answers
10 views

What is the maximum storage size to store number of addresses in array? [duplicate]

I want to map multiple addresses using one address by doing this: mapping (address => arrayOfAddresses[]) mappingName; What is the maximum size of addresses I can store in that array?
0
votes
1answer
35 views

map multikey how-to?

I'm trying to create a map whose key is composed of two bytes32 keys. I've tried with tuples-keys and struct-keys but both are rejected by the compiler. I'm also thinking about "hashing" both keys ...
1
vote
2answers
233 views

Calling an address array in web3 returns an error that the method needs arguments

I'm declaring a public address array in my contract, but when I call it, I get an error saying it's expecting an argument. Can someone shine a light on what is happening here? Contract.sol uint ...
1
vote
1answer
36 views

Create Contract Solidity 0.5

My code works with compiler 0.4.25 but not with the latest version. I want to keep track of every "Contract 2" that "Contract 1" create. Contract 1 uint public countContract; address[] public ...
2
votes
1answer
43 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; ...
0
votes
0answers
50 views

Best way to support restoring of all the keys of a mapping

I have a contract, in which I maintain the keys of a mapping in two different ways: Storing each key in an array Emitting an event for each key I do this in order to be able to restore the keys (and ...
0
votes
2answers
210 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 ...
1
vote
3answers
377 views

how do I loop through a mapping of address [duplicate]

I have a mapping of list of struct (mapping (address => struct[])) I'm having a trouble in how to display all them together in the front end (i'm using React js). Thnx.
0
votes
1answer
47 views

Solidity: call from function to modifier with array [closed]

I have the following problem, I'm trying to call a function from Solidity function to Solidity modifier: contract B is A function checkIfIsValid() external pure onlyValidAddresses([manually put ...
1
vote
1answer
17 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 ...
0
votes
1answer
63 views

Pushing a stuct into an array

What i am attempting to do is elementary, adding a struct to a storage array. But for some reason I am getting the following error TypeError: Indexed expression has to be a type, mapping or array (...
0
votes
1answer
82 views

Transaction pending

If I call this function the transaction get pending and never finish. Any idea? pragma solidity ^0.4.24; contract test { uint[] values = [1,2,3,4,5,6]; function removeValueFromArray(uint _num) ...
0
votes
0answers
82 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
1answer
24 views

How to set token holder's address without duplication?

In following code I've implemented two functions to set and get Token holders. function setTokenHolders(address _to)internal returns(bool){ for(uint i = 0; i<= tokenHolders.length; i++){ ...