I am designing a solution for an ethereum smart contract that does bidding. The use-case includes reserving a name eg. "myName" and assigning to an address. And then, people can bid for that name (in this case myName). There can be multiple such biddings happening for multiple names.
struct Bid {
address bidOwner;
uint bidAmount;
bytes32 nameEntity;
}
mapping(bytes32 => Bid[]) highestBidder;
So, as you can see above, Bid struct holds data for one bidder, similarly, the key (eg. myName) in the mapping highestBidder points to an array of such bidders.
Now, I am facing a problem when I try to return something like highestBidder[myName].
Apparently, solidity does not support returning an array of structs (dynamic data). I either need to rearchitect my solution or find some workaround to make it work.
If you guys have any concerns regarding the question, please let me know, I will try to make it clear.
I am stuck here any help would be appreciated.
About "returning an array of structs"... just a small workaround in order to return an array of structs extracted from medium
The uint[] index parameters should contain the indexes that you want to access.
Best
Return an array of struct in solidity?
In below function getBid returns array of bid structure.
As you mentioned, this is not yet supported in Solidity. The powers that be are planning on changing it so you can, but for now, you have to retrieve the number of elements and then retrieve the decomposed struct as a tuple.
Edit to address question in comment regarding
storage
vsmemory
in this caseLocal storage variables are pointers to state variables (which are always in
storage
). From the Solidity docs:This is referring to an example where the varable used is
uint[] x
. Same applies to my code withBid bid
. In other words, no new storage is being created.In terms of cost:
getBid("foo", 0)
usingBid memory bid
:getBid("foo", 0)
usingBid storage bid
:In this case,
storage
is cheaper.