1

I need to make the following struct call in smartPy.

Types.TransferCoin(_strFrom, _toAddress, _assets)

The Types.sol files has the following struct:

struct TransferCoin {
        string from;
        string to;
        Asset[] assets;
    }
struct Asset {
        string coinName;
        uint256 value;
    }

1 Answer 1

1

In SmartPy you don't call a structure, you simply create a record.

Two records with the same field names and associated value types are of the same type. Naming a type doesn't make it different and is often not needed thanks to the induce power of SmartPy.

To create a record:

my_structure = sp.record(from_=_strFrom, to_=_toAddress, assets=_assets)

Notice that with the legacy sp.record(...) returns an expression that when evaluated will create a record, not a variable with the record inside. If you use my_structure multiple times the record will be created multiple type. Surround it by sp.compute() to avoid that.

If you want to enforce the type, you have to write the type as explained here: How do we achieve a variable type of struct in smartPy and enforce it.

With the new syntax:

sp.cast(my_structure, my_record_type)

With the legacy syntax:

my_structure = sp.set_type_expr(my_structure, my_record_type)

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.