1import smartpy as sp
2
3# This contract shows how to generate the metadata for a contract.
4
5
6@sp.module
7def main():
8 class MyContract(sp.Contract):
9 """My Contract"""
10
11 def __init__(self):
12 self.data.x = "Test"
13 self.data.metadata = sp.cast(sp.big_map(), sp.big_map[sp.string, sp.bytes])
14
15 @sp.entrypoint
16 def ep(self):
17 pass
18
19 @sp.offchain_view
20 def add_10(self, n):
21 """This adds 10."""
22 sp.cast(n, sp.nat)
23 return n + 10
24
25
26if "main" in __name__:
27 pin_on_ipfs = False
28
29 @sp.add_test()
30 def test():
31 sc = sp.test_scenario("Contract metadata", main)
32 # Instantiate the contract
33 c1 = main.MyContract()
34 # SmartPy source code of the contract
35 source = c1.get_source()
36 if pin_on_ipfs:
37 # ⚠️ Don't do that if you don't want to reveal your SmartPy
38 # source code to the world!
39 source_uri = sp.pin_on_ipfs(source, api_key=None, secret_key=None)
40 else:
41 source_uri = "ipfs://QmaV5gQ6p9ND9pjc1BPD3dc8oyi8CWEDdueSmkmasiaWGA"
42 # Build the metadata
43 metadata = sp.create_tzip16_metadata(
44 name="MyContract",
45 version="1.0.0",
46 license_name="CC0",
47 description="This is a demo contract using SmartPy.",
48 authors=["SmartPy <https://smartpy.io/>"],
49 homepage="https://smartpy.io/ide?template=contract_metadata.py",
50 offchain_views=c1.get_offchain_views(),
51 # Don't add the source_uri if you don't want to reveal your SmartPy
52 # source code to the world.
53 source_uri=source_uri,
54 )
55 if pin_on_ipfs:
56 metadata_uri = sp.pin_on_ipfs(metadata, api_key=None, secret_key=None)
57 else:
58 metadata_uri = "ipfs://QmRpHDSf1P2sCbgYWrxhuWvt3bzTnAmXxPaNeTLCEweCxE"
59 # Update the metadata on the contract
60 c1.data.metadata = sp.scenario_utils.metadata_of_url(metadata_uri)
61 # Originate the contract
62 sc += c1