You should wrap the snippet in a pragma ... contract
envelope so it compiles as presented. Include the wording of the error message you want to be tracked down, to reduce guesswork.
This works, with a warning:
pragma solidity 0.5.1;
contract StructError {
mapping (bytes16=>Device) public Device_Table;
struct Device {
bytes16 UUID;
bytes16 hardware_UUID;
string phone_Number;
uint256 created_at;
}
}
Here it is in Remix so you can see it recall a mapped struct with bytes16
index input.
The warning about high/infinite gas is due to the string phone_number
. The string could be any length. Gas cost scales with the length, so it is high/infinite.
You can see the error disappears if that line is commented out.
pragma solidity 0.5.1;
contract StructError {
mapping (bytes16=>Device) public Device_Table;
struct Device {
bytes16 UUID;
bytes16 hardware_UUID;
// string phone_Number;
uint256 created_at;
}
}
You could consider using a bytes32
or a fixed-sized bytes
array to hold the phone number so its size is not unlimited. It is worth mentioning that a fixed size bytes32
is probably cheaper than a variable size string because string
uses a uint256
to hold the length as well as bytes to hold the data. The cost difference will be most obvious in write operations.
I wrote a function that returns a Device struct but I get an error?
I am also facing a problem when a struct to a function
You cannot/do not send or receive structs
in function signatures without using the experimental ABI, which you should not do for production. You may pass them around internally (as storage pointers) but not in public
/external
functions. Instead, break down the elements into simpler types. For example:
function setDevice(bytes16 deviceId, bytes16 hardwareUUID, bytes32 phoneNumber) public ... {}
Hope it helps.
return (bytes16, bytes16, string, uint256)
instead.(bytes16 UUID, bytes16 hardware_UUID, string phone_Number, uint256 created_at) = yourFunc(...)
in this other service.