Initializing (empty) map with record as value in a contract

How would I define a storage type for a map of records in the init?

for example - a map where key would be sp.address and value would be a sp.record(contract=sp.address, balance=sp.nat)? = sp.cast(None, 
				   value=sp.record(contract=sp.address, balance=sp.nat)}))

How would you specify types and initialize an empty map where key is address and value is nat?

sry, my syntax is obviously wrong, but hope you get what im trying to get.

To define and initialize a map in SmartPy:

  • The empty map is represented as: {}.
  • To define the type of a map, use:[<key_type>, <value_type>].
  • To enforce a type on a value, you use: sp.cast(<value>, <type>).

Combining these, an empty map with a defined type becomes:
sp.cast({},[<key_type>, <value_type>]).

For your specific example where the key is an address and the value is a nat: = sp.cast({},[sp.address, sp.nat])

If you wanted the value to be a record of an address and a nat: = sp.cast(
    {},[sp.address, sp.record(contract=sp.address, balance=sp.nat)])
1 Like