Hot answers tagged

4

Operation push has changed behavior since since solidity 0.6. It no longer returns the length but a reference to the added element. You can solve this by splitting the assignment in two operations: sketchs.push(_sketch); uint _id = sketchs.length - 1;


2

What you are searching for is a mapping of mapping. I created the following smart contract that I think achieve what you described. // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.8.0; contract myContract { mapping (address => mapping (uint => uint)) public payments; mapping (address => uint) public lastPayment; ...


2

You must use array.length-- in order to accurately persist the length of the array. The confusion comes from the fact that delete in Solidity does not actually delete an element from an array, but rather it just sets the element to it's default value (0 for a uint256, for example). Observe the following code as an example: uint256[] public testArray = [1,2,3,...


2

You're trying to push a non-payable address to the array: manager is not payable. You'll need to change the type of manager to address payable: address payable public manager; function enter() public payable { require(value > 0.01 ether); players.push(manager); } Simply adding msg.sender (e.g. players.push(msg.sender)) to the array will work fine, ...


2

Solidity creates getter functions for public state variables automatically. For example, the following will work: contract A { uint256 public foo = 1; } contract B { A a; function getFoo() public view returns (uint256) { return a.foo(); } } In the case of arrays however, you can only get a single element from the array, rather than the entire ...


2

You need to initialize the dynamic array to the required length: doctor[] memory _doctors = new doctor[](_docIds.length); More generally, there are 4 types of arrays: Inside functions, a static array in memory , e.g., type[3] memory arr; Inside functions, a dynamic array in memory , e.g., type[] memory arr = new type[](3); Outside functions, a static ...


2

You can return multiple values, as per Destructuring Assignments and Returning Multiple Values. But your current code is going to return on the first iteration of the loop. You could either: Create and return two arrays Define a struct that contains one member of each array, then return an array of the structs.


2

Data storage in the blockchain always costs gas. Data reading also costs gas if it is performed in a transaction (typically when the same transaction stores and reads data). You always use a node to access the blockchain and the node has all of the blockchain data. Therefore you can read the data directly from the node without sending any transaction (called,...


2

Nothing like that exists because the obvious solution would involve considerable, variable gas cost to reorganize data at rest. There are patterns that combine mappings and arrays to create iteratable sets with random access. If it is to be sorted or organized in any particular order then one needs to code for that as well. Have a look over here for some ...


2

It seems that is an pending feature https://github.com/ethereum/solidity/issues/1772 However, if the desired range is from 0-255, there is a trick for you. bytes public constant TEST_MAP = hex"000804"; function test(uint8 key) external pure returns (byte) { return TEST_MAP[uint256(key)]; }


2

The first push consumes 41129 gas That'll be 21,000 gas for the transaction base fee, and 20,000 gas for using the storage associated with the array for the first time. (Plus 129 gas - unsure about this, but probably unimportant.) while the second one only consumes 26129 That'll be 21,000 gas for the transaction base fee, and 5,000 gas for updating the ...


1

It always depends on your needs. The biggest difference is that you can't iterate over a mapping. So if you store two entries (key => value) in a mapping, there is no way to get the values without knowing the keys. In your example, it also depends on your needs. If you only want to access the answers based on known key (so for example a function which ...


1

Like this? // SPDX-License-Identifier: UNLICENSED pragma solidity 0.7.6; contract Mapping { struct Struct { bool foo; } mapping(string => Struct) map; } As a sidenote, I would be inclined to map from bytes32 instead of from strings, where the key is a hash that is expected to be unique in all cases. I'm not certain, but ...


1

It is caused by this : User storage user=users[0]; You try to assign to user the first element of the users array but you can't access an empty array. If you want to push your object into the array you can use array.push().


1

The comment is right, removing the [] fixes it. stateStorageForNumbers_[tokenIDs[i]] = _numbers[i]; ^ This works.


1

You can delete an array to achieve your goal. See the following code as an example. pragma solidity 0.6.12; contract Test { uint256[] public bidderItemID; function setBidderItemID() public { bidderItemID = [1,2,3]; } function deleteBidderItemID() public { delete bidderItemID; } }


1

You haven't declared allowed as a 2D mapping, but you're trying to use it as one. You need: mapping (address => mapping (address => uint256)) private allowed;


1

You have corrected it. The problem was you cannot access a non-existent element of an array. So, so given c = 0, and car.length = 0 you cannot access the first non-existent row of the array which is numbered 0. If row 0 existed, then the array length would be 1. When you try car[0] on an empty array, you get an error because you are out of bounds. So, your ...


1

If you pass as a parameter or receive from a function a value of non-canonical type, such as a string or an array, then the offset of its description is passed in the position of the corresponding parameter or return value. Accordingly, for two bytse32 arrays it will be (array1[2], array2[3]): 000 ...


1

You have reference the mapping name at the index you want, which is a struct and the struct member for the math operations. pragma solidity 0.6.0; contract LoopingThroughArray { struct User { uint256 exampleValue; address id; } User[] public users; receive() external payable { for (uint i=0; i&...


1

You can only push one item at a time. In your case, simply split up your function: path.push(address1); path.push(address2);


1

As a general rule, you should try to structure things so iteration isn't necessary, at all. Here are some general thoughts on the topic: https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad Unless I miss my guess the primary concern is awarding winnings to correct betters. A secondary concern is minimizing gas cost, and you can consider garbage ...


1

Interval tree is what you need to efficiently handle intervals. A simple implementation of interval tree in Solidity is provided below. Please note, it's not a balanced implementation, so in worst case it can degrade to a linked list (when you add intervals to it in ascending or descending order). It supports overlapping intervals, and search() can easily be ...


1

You are obviously trying to access the array at an illegal index: proposalID = proposals.length+1; Proposal storage p = proposals[proposalID]; Valid indexes in any array are between 0 and array.length - 1. Most people get it wrong trying to access the array at array.length. You got it wrong trying to access the array at array.length + 1! Note that trying ...


1

'in' is a Reserved Keyword. This keyword are reserved in Solidity. They might become part of the syntax in the future.Check this link But you can loop through an array/list in different way. Compared to python. address[] public arraylist; // Creating a list of '**address**' type arraylist.push(newAddress); //Adding elements to array. ... // Length of ...


1

Reading your code I think I understood what you want to achieve, but I suggest to completely change the approach. Please note that using a smart contract costs money, so as a developer you should aim to create business logic that is both clean - so it cannot be misinterpreted or misleading - and light - so every transaction costs as few as possible. To ...


1

In order to avoid For loops, You can add another mapping to check if a user exists or not. You can add mapping(string => bool) userExists; so your whole code will look like this: pragma experimental ABIEncoderV2; contract structWithMapping{ struct Data{ string[] user; string[] catagory; string[] data; } mapping(...


1

This is Solidity, not Python. Change this: if (a/4 == b/4 == c/4 == d/4 == e/4) To this: if (a/4 == b/4 && b/4 == c/4 && c/4 == d/4 && d/4 == e/4) Of course, in order to reduce gas-cost, you might want to store those intermediate results (at least b/4, c/4 and d/4) in temporary variables before using them.


1

Short answer - It isn't possible through Etherscan's Write Contract at the moment, since Etherscan's write contract functionality uses a customised encoder (which strips empty brackets denoting an empty array, and replaces it with an empty string), then passes the information wrongly to Web3. This is being reported to them and they'll be looking into it. ...


1

Vyper is a statically typed language, which means that the type of each variable must be specified when it is declared. An array is cast by applying a subscript ([]) to a base type. In your example, because some values are less than zero, the base type is int128. There are 16 values, so the final type is int128[16]. Outcomes: int128[16] = [5, 500, 5000, ...


Only top voted, non community-wiki answers of a minimum length are eligible