Hot answers tagged

4

This seems to be working. But you can test it out on your own with your specific use-case in remix.


4

For those who actually find themselves in the same situation as I did, take a look at this answer, it answers my dilemma. Credits to @goodvibration


4

Trust me. Everytime (everytime!) you are thinking to loop trough a dataset whose dimensions you cannot predict at the “start of the time“ in a blockchain based system, you are using blockchain for the wrong thing and/or your algorithm must be changed by rethinking it. Your model should be something where if a new “user” is added, the simply operation to add ...


3

Since each object is obviously cloned (i.e., one object in the array and another object in the mapping), you can add the object to the array, and then add its index in the array to the mapping. In other words, instead of doing this: mapping (uint => NPC[]) public cityNPCs; ... NPCs.push(newNpc); cityNPCs[_cityId].push(NPCs[newId]); You can do this: ...


3

It's a bit of a dilemma. You need mappings for random access and arrays for iteration. You should aim to avoid loops at all costs, for O(1) complexity so the contract scales. This library helps with CRUD ops on sets, including delete. There is a quick explainer-style walk-though over here, with links back to the detailed tutorial that explains how it works ...


3

The pattern you are using is valid for certain table-like sets, but you should be aware of the limitations. It is ill-advised to do anything with a contract that loops - Getting Loopy with Solidity. You can, however, make it possible for a client to loop. It should be understood that "client" is an off-chain entity, in this context, because if it wasn't ...


3

It's not clear if you want to retrieve all the values in a mapping or use the mapping to store structures that contain interesting, multipart things. No to the first interpretation. Yes to the second. Lay out an instance (for one key) in a struct. Map the structs to the keys. struct MyStruct { uint part1; bool part2; ... } mapping(uint => ...


3

Change this: mapping(string => address) public users; To this: mapping(string => address) private users; Or to this: mapping(string => address) internal users; And add this if you must: function getUser(string str) public view returns (address) { return users[str]; }


3

The below code compiles at vyper.online. I had to drop the private and add self. to your accesses of the state variable teddies: struct Teddy: id: uint256 year: uint256 component: map(uint256, uint256) teddies: map(uint256, Teddy) @public def __init__(): self.teddies[0].id = 0 self.teddies[0].year = 2019


2

The error message gives you both the reason for error (incorrect checksum) and the way to fix the error (the letters in your address must be uppercase). Just copy paste the address provided by the error message in your code. See this answer.


2

Solidity doesn't currently support returning a mapping or a variable-sized list, so you would need to implement a getter function as you described that takes an index. The approach I think you're describing is keeping a second list next to the mapping, and using it to return the mapping in chunks like the source code below. contract SomeContract { ...


2

pragma solidity ^0.5.0; pragma experimental ABIEncoderV2; contract A { B private b; constructor (B _b) public { b = _b; } function bar () public { B.S [] memory structs = b.foo (); } } contract B { struct S { uint x; uint y; } S [] private structs; function foo () public view returns (S [] memory) { return structs; ...


2

Ethereum / solidity does not allow the clearing of mappings because mappings do not inherently keep track of the keys. A user would be required to provide the key for each used spot in the mapping to be removed. Additionally, when you 'delete' or clear each key's value, you are really just setting this key value back to it's default value (zero in many cases)...


2

You can avoid the modifier and the hashing. It is simpler to do: function set(uint _index, string calldata _data_to_store) external { require(bytes(storedData[_index]).length == 0); storedData[_index] = _data_to_store; } Checking the length of the bytes was inspired by this answer.


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; ...


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

It's not easy to conjure up a trivial example. Based on the comments, I think the main concerns are approximately: Clean up expired information responsibly and capture the gas refund offered for releasing unneeded storage. Avoid iteration. https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad Access information in O(1), by user, by epoch, globally ...


1

Because that's not how multiple return is working in Solidity. In almost every programming language whenever a method reaches first return line then everything below is ignored. Just change your function to this and it should be ok: function get() public view returns (string memory, string memory, string memory, string memory, string memory, string memory) { ...


1

The problem is that mappings can only live in storage. When you define PoolStruct memory pool;, the mapping member cannot be created in memory, and therefore the memory struct should be treated as if the mapping member never existed (for solidity < 0.7.0). Starting from solidity 0.7.0, the line PoolStruct memory pool will produce an error saying that ...


1

Might sound silly, but are you sure you are not redeploying the contract on the restart? Make sure you are working on the same contract with the same state (data). If yes then you can try replacing your current validation with this one: require(bytes(hash).length > 0, "Empty hash!"); require(bytes(hashAlreadyRegistered[hash]).length == 0, &...


1

Can I simply change _tokenURIs mapping from private to internal and still be ERC721 compliant? Yes, the ERC721 standard (as any other standard) defines the contract's API, not its internal implementation. Gas efficiency is a key requirement here. Inheritting your contract from the ERC721 contract will allow you to read that variable (after declaring it ...


1

From the official documentation: delete a assigns the initial value for the type to a, i.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements set to their initial value. delete a[x] deletes the item at index x of the array and ...


1

This field's type is editor[], and its name is editors. So change rentalStructs[_bagId].editor to rentalStructs[_bagId].editors. Also, for good practice, I recommend that you move struct editor { ... } before (above) struct BuildingRental { ... }, since the latter depends on the former and not vice-versa.


1

If you need any of the following to run in O(1) operations: Get an item by unique ID which is not a sequential number (e.g., an address) Remove an item Then an array is inappropriate, and you must use a mapping.


1

consider adding the following function to your smart contract: function getSpentLimit(address a, address b) public view returns (uint256) { return spentLimit[a][b]; } then you can call this function in your smart contract using web3.js using a and b as parameters


1

This looks like two or more questions, to me. A high-level question is the overall data layout. I'm not convinced this layout is ideal. It is usually best to think about readability and simplicity first and optimize later. With that in mind, I would probably tackle it with this: https://github.com/rob-Hitchens/UnorderedKeySet/blob/master/contracts/...


1

Just to add to goodvibration's answer, it has to do with Solidity's inability to automatically construct the inspection function requested with public - hence the suggestion to drop it. It also has to do with the compiler version. Here you can see it works fine with 0.5.11. pragma solidity 0.5.11; contract StringMap { mapping(string => address) ...


1

Assuming order doesn't matter (implied by "set"), the combination of a mapping and an array can handle set operations in constant time. The trick is to keep track of where in the array each element is and swap elements to the end for deletion. I wrote a blog post about this pattern: https://programtheblockchain.com/posts/2018/06/03/storage-patterns-set/. ...


1

selectedPair is a mapping of strings to Person structs. That is to say, selectedPair[A_STRING_HERE] will always return a Person struct, regardless of the value of A_STRING_HERE (which has to be a string). You have defined the Person struct as having one uint property called age, and one string property called gender. So to get the gender property of the ...


1

The answer is simple: you can NOT from any other smart contract. BUT you can without problem using forensic analysis, i.e. “downloading” all or “the relevant” portion of the blockchain and inspecting it with proper tools. I.e. “private” does not mean “secret” at all, but means “accessible with specific effort only”. Moreover the transaction you used to set ...


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