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)?

self.data.ownersMap = sp.cast(None, 
			      sp.map(
				  {key=sp.address, 
				   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: sp.map[<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({}, sp.map[<key_type>, <value_type>]).

For your specific example where the key is an address and the value is a nat:

self.data.ownersMap = sp.cast({}, sp.map[sp.address, sp.nat])

If you wanted the value to be a record of an address and a nat:

self.data.ownersMap = sp.cast(
    {},
    sp.map[sp.address, sp.record(contract=sp.address, balance=sp.nat)])
1 Like