我怎样才能牢固地返回一个结构体数组? [英] How can I return an array of struct in solidity?

查看:101
本文介绍了我怎样才能牢固地返回一个结构体数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计用于出价的以太坊智能合约的解决方案。用例包括保留名称,例如。 myName并分配给一个地址。然后,人们可以竞标该名称(在本例中为myName)。

 结构出价{
地址bidOwner;
uint bidAmount;
bytes32 nameEntity;
}

mapping(bytes32 => Bid [])maximumBidder;

因此,如您在上面看到的,Bid结构保存一个投标人的数据,同样,密钥(



现在,当我尝试返回诸如maximumBidder [ myName]



显然,坚固性不支持返回结构数组(动态数据)。我要么需要重新设计我的解决方案,要么找到一些解决方法以使其起作用。



如果你们对这个问题有任何疑问,请告诉我,我将尽力



我被困在这里,我们将不胜感激。

解决方案

如前所述,Solidity尚不支持此功能。正在计划改变它的功能,以便您可以进行更改,但是现在,您必须检索元素的数量,然后将已分解的结构作为元组检索。

  function getBidCount(bytes32 name)public constant return(uint){
return maximumBidder [name] .length;
}

函数getBid(bytes32名称,uint索引)公共常量返回(地址,uint,bytes32){
出价存储出价= maximumBidder [name] [index];

回报(bid.bidOwner,bid.bidAmount,bid.nameEntity);
}

编辑以解决有关的评论问题存储内存



本地存储变量是指针声明变量(始终在存储中)。从



getBid( foo,0)使用出价存储出价





在这种情况下,存储更便宜。


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.

解决方案

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.

function getBidCount(bytes32 name) public constant returns (uint) {
    return highestBidder[name].length;
}

function getBid(bytes32 name, uint index) public constant returns (address, uint, bytes32) {
    Bid storage bid = highestBidder[name][index];

    return (bid.bidOwner, bid.bidAmount, bid.nameEntity);
}

Edit to address question in comment regarding storage vs memory in this case

Local storage variables are pointers to state variables (which are always in storage). From the Solidity docs:

The type of the local variable x is uint[] storage, but since storage is not dynamically allocated, it has to be assigned from a state variable before it can be used. So no space in storage will be allocated for x, but instead it functions only as an alias for a pre-existing variable in storage.

This is referring to an example where the varable used is uint[] x. Same applies to my code with Bid bid. In other words, no new storage is being created.

In terms of cost:

getBid("foo", 0) using Bid memory bid:

getBid("foo", 0) using Bid storage bid:

In this case, storage is cheaper.

这篇关于我怎样才能牢固地返回一个结构体数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆