trying to do a sample based on struct, storing a list of struct as a mapping. but when i add an item and trying to retrieve i cannot find that item in the mapping
below is my contract
contract ItemListContract {
struct item
{
bytes iname;
uint16 itemid;
bytes icode;
uint ivalue;
}
uint itemcount;
mapping(bytes => item) itemlist;
item[] itemarray;
function ItemListContract()
{
log0('hi');
}
function AddItem(bytes name, uint16 iid, bytes code, uint val)
{
var itemnew = item(name, iid ,code, val);
log0(itemnew);
itemlist[code] = itemnew;
itemarray.push(itemnew);
itemcount++;
}
function countitemlist() returns (uint count)
{
return itemcount;
}
function removeitem(bytes code)
{
delete itemlist[code];
itemcount--;
}
function getitem(bytes code) returns (bytes iname, uint val)
{
return (itemlist[code].iname,itemlist[code].ivalue);
}
}
I tried to add item by using the below statements
var listarrayinterface =[{"constant":false,"inputs":[{"name":"name","type":"bytes"},{"name":"iid","type":"uint16"},{"name":"code","type":"bytes"},{"name":"val","type":"uint256"}],"name":"AddItem","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"code","type":"bytes"}],"name":"getitem","outputs":[{"name":"iname","type":"bytes"},{"name":"val","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"countitemlist","outputs":[{"name":"count","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"code","type":"bytes"}],"name":"removeitem","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"countitemarray","outputs":[{"name":"count","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"code","type":"bytes"}],"name":"getitemfromarray","outputs":[{"name":"iname","type":"bytes"},{"name":"val","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}];
var listarratcontract = eth.contract(listarrayinterface).at("0x62fxxx")
listarratcontract.AddItem.call({data:{name:"item2",iid:2,code:"i2",val:2}})
after calling the contract with the above data i got [] as resoponse
Now I tried to query the saved data by
listarratcontract.countitemlist.call()
0
got response as 0.
when i called using
listarratcontract.getitem.call("i2")
["0xo",0]
I cant get the saved data or dont even know if the data is saved or not?
can any one point out what the issue might be?






.callin web3 simulates a transaction. They wanted to use an actual transaction – Tjaden Hess Dec 12 '16 at 14:12Result: "0x" Transaction cost: 63046 gas. Execution cost: 41070 gas.
Result: "0x" Transaction cost: 48046 gas. Execution cost: 26070 gas.
– mahesh gupta Dec 13 '16 at 05:59addItem(...)function called the first and second time. You can use the same type of analysis to find the difference in the gas cost for theremoveItem(...)function called the second-last and the last time. Please ask a new question referencing this Q&A if you have further queries about the gas cost. – BokkyPooBah Dec 13 '16 at 07:53