0

I am trying to execute unit tests for a solidity smart contract:

I have:

mapping (bytes16=>Device) public Device_Table

struct Device
{
    bytes16 UUID;
    bytes16 hardware_UUID;
    string phone_Number;
    uint256 created_at;
}

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

3
  • So return (bytes16, bytes16, string, uint256) instead. Commented Jul 2, 2019 at 13:35
  • In fact I want to have the total of an object since I have an other servcie that consumes the output of the smart contract and I can't change the all scripts
    – MS B
    Commented Jul 2, 2019 at 13:40
  • Then do (bytes16 UUID, bytes16 hardware_UUID, string phone_Number, uint256 created_at) = yourFunc(...) in this other service. Commented Jul 2, 2019 at 13:48

1 Answer 1

0

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.

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.