0

I have a contract which acts as an auction house. Each Auction is a struct. Therefore, each time a user wants to sell an item, a new Auction struct is created in storage.

This wouldn't be a problem if I stored all the Auctions in one storage array. However, due to different query reasons, I need to have the same Auction reference, stored within multiple arrays.

I come from a Java/C# background, so in that sense, what makes sense to me is:

  1. Create an object on heap
  2. Get the reference to that object
  3. Add the reference to N arrays
  4. Changing the object from any of these arrays changes the data on the heap, thus effectively changing it for every array which holds a reference to it

The problem is, I have no idea how to pull this off in Solidity. Here is what my code does:

  1. mapping(uint256 => Auction) allAuctions keeps track of all auctions
  2. mapping(address => Auction[]) userAuctions keeps track of all auctions made by a wallet

What I tried to do is the following:

allAuctions[n] = Auction(); // I thought this would make an object "on the heap" and add a reference to the map

userAuctions[wallet].push(allAuctions[n]); // Push the reference from the map into the array

However, when I execute this, it doesn't work. It seems to me that 2 separate structs are being made in the storage, because changing 1 doesn't change the other.

Is there a way to make this work or is my paradigm completely incorrect? Thanks in advance.

0

Solidity doesn't have references. As everything in software engineering you can workaround that limitation using an indirection. Store all auction in a mapping or array and use auctionId to refer it from anywhere else.

 maping (uint256 => Auction) auctions;
 mapping(address => uint256[]) userAuctions;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.