hey everyone, Idk if this channel is the right one for asking Solidity questions (according to the name, I assume it is), but I have these following structs:
struct Item {
string name;
bytes32 hash;
uint256 id;
uint256 price;
uint256 registeredAt;
string location;
string createdAt;
address retailerAddress;
address consumerAddress;
ItemState[] states;
}

struct ItemState {
uint256 createdAt;
bool isVerified;
}

Problem arises when I try to compile the code when using the following (trackId is basically an ID counter):
bytes32 hash = keccak256(abi.encodePacked(_name, trackId));
items[trackId] = Item({
name: _name,
hash: hash,
registeredAt: block.timestamp,
id: trackId,
price: _price,
createdAt: _creation_date,
location: _location,
retailerAddress: _retailerAd,
consumerAddress: _consumerAd,
states: []
});

items[trackId].states.push({
createdAt: _creation_date,
isVerified: false,
stage: State.ResearchAndDevelopment,
factory: ProductionLocation.ResearchDepartment,
extInputs: InputsToProduct.None
});
Remix compiler says:
TypeError: Unable to deduce common type for array elements.
--> contracts/SupplyChain.sol:146:21:
|
146 | states: []
| ^^****

any one has ideas how I can fix it?

Nov 20, 2022, 8:38 PM
Yeah that's not gonna work. You have to specify array type while creating a new dynamic array
states: new ItemState[](0)
Try that
Nov 20, 2022, 10:01 PM
tnx, I actually changed it for now, but now i'm getting this one...
Nov 20, 2022, 10:01 PM
You are going to store it as a state variable right? Change L137 to storage
Nov 20, 2022, 10:02 PM
sorry for the ping pong btw
Nov 20, 2022, 10:05 PM
That value is the length of array, not struct params
Nov 20, 2022, 10:06 PM
i'm actually trying both solutions at the same time
ah good catch!!
great! that actually solved it! i'm going for the compile now. tnx for the help 🙏
however, Remix is now complaining for UnimplementedFeatureError: Copying of type struct SupplyChain.ItemState memory[] memory to storage not yet supported.
may I send you a dm to not bother the others? @grimreaper619
Nov 20, 2022, 10:10 PM
Can you paste full code somewhere. Seems bits and pieces are missing
Yeh sure
Nov 20, 2022, 10:10 PM
Well, we're learning too 🥲
Nov 20, 2022, 11:20 PM
no worries, grim helped me to fixed it. it was at the end a limitation of solidity compiler.

so having the same structs Item and ItemState, and knowing that I had a mapping(uint256 => Item) items the following worked out tnx to @grimreaper619 to create a new Item and add a state to it:

trackId = idCounter;

bytes32 hash = keccak256(abi.encodePacked(_name, trackId));

Item storage item = items[trackId];

item.name = _name;
item.hash = hash;
item.registeredAt = block.timestamp;
item.id = trackId;
item.price = _price;
item.createdAt = _creation_date;
item.location = _location;
item.retailerAddress = _retailerAd;
item.consumerAddress = _consumerAd;

item.states.push(
ItemState({
createdAt: block.timestamp,
isVerified: false,
stage: State.ResearchAndDevelopment,
factory: ProductionLocation.ResearchDepartment,
extInputs: InputsToProduct.None
})
);
Nov 20, 2022, 11:24 PM
I see
Thanks for sharing 🙂
Nov 20, 2022, 11:26 PM

© 2024 Draquery.com All rights reserved.