结构数组 init 和 getter
Array of structs init and getter
我对 Solidity 开发还很陌生,目前正在为结构而苦苦挣扎。
我遵循了几个示例,但无法找到将结构添加到我的结构数组的方法。我最后一次尝试是:
pragma solidity ^0.4.18;
contract Iceicebaby {
struct Parcel {
string state;
string flavour;
uint256 weight;
address holder;
}
Parcel[] public parcels;
function newParcel(string _flavour, uint256 _weight) public {
parcels.length++;
parcels[parcels.length-1].state="ORDERED";
parcels[parcels.length-1].flavour=_flavour;
parcels[parcels.length-1].weight=_weight;
parcels[parcels.length-1].holder=msg.sender;
}
function getParcelsCount () view public returns (uint){
return parcels.length;
}
function getParcel(uint256 index) view public returns (string, string, uint256, address) {
return (parcels[index].state, parcels[index].flavour, parcels[index].weight ,parcels[index].holder);
}}
现在我得到:
myInstance.order("Flavour",1) :
{ tx: '0xfad42f92c158557c46496df3fd104d7a09899e641e66748e57b03262f4f5fc62',
receipt:
{ transactionHash: '0xfad42f92c158557c46496df3fd104d7a09899e641e66748e57b03262f4f5fc62',
transactionIndex: 0,
blockHash: '0xc39e94e8e9e9a26fd372ad12d2eba4a72f06251d2f29c4a344cd9e58849d9e49',
blockNumber: 17,
gasUsed: 22168,
cumulativeGasUsed: 22168,
contractAddress: null,
logs: [],
status: 1 },
logs: [] }
myInstance.getParcelsCount()
BigNumber { s: 1, e: 0, c: [ 0 ] }
myInstance.getParcel(0) 或 myInstance.getParcel(1)
[ '', '', BigNumber { s: 1, e: 0, c: [ 0 ] }, '0x' ]
我尝试了其他几种解决方案,像这样映射结构和东西,但无法处理这个应该很容易的东西,不是吗?
另外,我找不到如何正确调试和显示日志的方法,这有什么标准吗?我正在使用 truffle 和本地 ganache 网络。
谢谢!
交易似乎没有足够的气体来执行存储数据的代码。
默认情况下 web3
发送 90000 gas (needs confirmation
),这对于您尝试执行的交易来说是不够的。
使用额外的可选参数更改以下代码行。我在这里为交易提供 150000 gas。通过查看 remix 的交易日志,您可以轻松估算交易需要多少 gas。
myInstance.order("Flavour",1)
myInstance.order("Flavour",1, {from: web3.eth.accounts[0], gas: 150000})
我对 Solidity 开发还很陌生,目前正在为结构而苦苦挣扎。 我遵循了几个示例,但无法找到将结构添加到我的结构数组的方法。我最后一次尝试是:
pragma solidity ^0.4.18;
contract Iceicebaby {
struct Parcel {
string state;
string flavour;
uint256 weight;
address holder;
}
Parcel[] public parcels;
function newParcel(string _flavour, uint256 _weight) public {
parcels.length++;
parcels[parcels.length-1].state="ORDERED";
parcels[parcels.length-1].flavour=_flavour;
parcels[parcels.length-1].weight=_weight;
parcels[parcels.length-1].holder=msg.sender;
}
function getParcelsCount () view public returns (uint){
return parcels.length;
}
function getParcel(uint256 index) view public returns (string, string, uint256, address) {
return (parcels[index].state, parcels[index].flavour, parcels[index].weight ,parcels[index].holder);
}}
现在我得到:
myInstance.order("Flavour",1) :
{ tx: '0xfad42f92c158557c46496df3fd104d7a09899e641e66748e57b03262f4f5fc62', receipt: { transactionHash: '0xfad42f92c158557c46496df3fd104d7a09899e641e66748e57b03262f4f5fc62', transactionIndex: 0, blockHash: '0xc39e94e8e9e9a26fd372ad12d2eba4a72f06251d2f29c4a344cd9e58849d9e49', blockNumber: 17, gasUsed: 22168, cumulativeGasUsed: 22168, contractAddress: null, logs: [], status: 1 }, logs: [] }
myInstance.getParcelsCount()
BigNumber { s: 1, e: 0, c: [ 0 ] }
myInstance.getParcel(0) 或 myInstance.getParcel(1)
[ '', '', BigNumber { s: 1, e: 0, c: [ 0 ] }, '0x' ]
我尝试了其他几种解决方案,像这样映射结构和东西,但无法处理这个应该很容易的东西,不是吗?
另外,我找不到如何正确调试和显示日志的方法,这有什么标准吗?我正在使用 truffle 和本地 ganache 网络。
谢谢!
交易似乎没有足够的气体来执行存储数据的代码。
默认情况下 web3
发送 90000 gas (needs confirmation
),这对于您尝试执行的交易来说是不够的。
使用额外的可选参数更改以下代码行。我在这里为交易提供 150000 gas。通过查看 remix 的交易日志,您可以轻松估算交易需要多少 gas。
myInstance.order("Flavour",1)
myInstance.order("Flavour",1, {from: web3.eth.accounts[0], gas: 150000})