In SmartPy it corresponds to records.
With the new syntax:
import smartpy as sp
@sp.module
def main():
Asset: type = sp.record(
coinName=sp.string,
value=sp.nat,
)
AssetTransferDetail: type = sp.record(
coinName=sp.string,
value=sp.nat,
fee=sp.mutez,
)
class MyContract(sp.Contract):
def __init__(self, coinNames, values, fees):
self.data.assets = sp.cast(sp.big_map(), sp.big_map[sp.nat, Asset])
self.data.assetDetails = sp.cast(sp.big_map(), sp.big_map[sp.nat, AssetTransferDetail])
for i in coinNames.keys():
self.data.assets[i] = sp.record(
coinName=coinNames[i],
value=values[i],
)
self.data.assetDetails[i] = sp.record(
coinName=coinNames[i],
value=values[i],
fee=fees[i]
)
@sp.entrypoint
def ep(self):
pass
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
sc = sp.test_scenario(main)
c1 = main.MyContract(
{0: "MyCoin"}, {0: 0}, {0: sp.mutez(5)}
)
sc += c1
c1.ep()
With the legacy syntax:
import smartpy as sp
Asset = sp.TRecord(
coinName=sp.TString,
value=sp.TNat,
)
AssetTransferDetail = sp.TRecord(
coinName=sp.TString,
value=sp.TNat,
fee=sp.TMutez,
)
class MyContract(sp.Contract):
def __init__(self, coinNames, values, fees):
assets = {}
assetDetails = {}
for i in range(len(coinNames)):
assets[i] = sp.record(
coinName=coinNames[i],
value=values[i],
)
assetDetails[i] = sp.record(
coinName=coinNames[i],
value=values[i],
fee=fees[i]
)
self.init(
assets=sp.big_map(assets, tkey=sp.TNat, tvalue=Asset),
assetDetails=sp.big_map(assetDetails, tkey=sp.TNat, tvalue=AssetTransferDetail),
)
@sp.entry_point
def ep(self):
pass
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
sc = sp.test_scenario()
c1 = MyContract(
["MyCoin"], [0], [sp.mutez(5)]
)
sc += c1
c1.ep()
Something to notice: on Tezos the constructor is never pushed onchain. With the legacy syntax what you can and cannot do inside the init is different from what you cna do in an entrypoint.
If you want the same logic inside an entrypoint you have to make to changes: instead of giving three lists you may prefer give one, you'll have to use sp.if
instead of if
.
Mind you, this translation is an example. It's not necessarily the best way to create this contract or something that I would approve for production.