如何可靠地返回结构数组? [英] How can I return an array of struct in solidity?

查看:28
本文介绍了如何可靠地返回结构数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为执行出价的以太坊智能合约设计解决方案.用例包括保留一个名称,例如.myName"并分配给一个地址.然后,人们可以竞标该名称(在本例中为 myName).可能会针对多个名称进行多次此类出价.

struct Bid {地址bidOwner;单位投标金额;bytes32 nameEntity;}映射(bytes32 => Bid[])highestBidder;

因此,正如您在上面看到的,Bid 结构保存一个投标人的数据,类似地,映射highestBidder 中的键(例如myName)指向一组此类投标人.

现在,当我尝试返回诸如highestBidder[myName]之类的东西时,我遇到了问题.

显然,solidity 不支持返回结构体数组(动态数据).我要么需要重新构建我的解决方案,要么找到一些解决方法来使其发挥作用.

如果你们对这个问题有任何疑问,请告诉我,我会尽量说清楚.

我被困在这里,任何帮助将不胜感激.

解决方案

正如您提到的,Solidity 尚不支持此功能.权力正在计划更改它以便您可以,但现在,您必须检索元素的数量,然后将分解的结构作为元组检索.

function getBidCount(bytes32 name) 公共常量返回 (uint) {返回highestBidder[name].length;}函数 getBid(bytes32 name, uint index) 公共常量返回 (address, uint, bytes32) {投标存储投标=highestBidder[name][index];返回(bid.bidOwner,bid.bidAmount,bid.nameEntity);}

编辑以解决评论中关于 storagememory 在这种情况下的问题

局部存储变量是指向状态变量的指针(它们总是在storage中).来自

getBid("foo", 0) 使用Bid storage bid:

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

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天全站免登陆