Metadata builder โ
Check metadata guides here.
Build metadata โ
Generate a JSON metadata document for string name containing an arbitrary constant Python expression
converted into JSON.
python
# A python dictionary that contains metadata entries
metadata = {
"name": "Contract Name",
"description": "A description about the contract",
"version": 1,
"views" : [self.get_x, self.get_storage],
}
self.init_metadata('SOME CONTRACT', metadata)
Defining an off-chain view โ
A decorator which defines an off-chain view.
Off-chain views are defined using the @sp.offchain_view(ย nameย =ย <name>,ย pureย =ย <True|False>,ย docย =ย None)
decorator inside the contract class.
By default, the view
name is equal to the method name, where <name>
is an optional argument and can be used to set a view name explicitly.
It has three optional parameters:
name
: Defines the view name explicitly;pure
(default is False): Defines the purity of view (dependent only on storage and parameters);doc
: Sets the view documentation. If doc isNone
, the documentation is the docstring of the method.
python
@sp.offchain_view(pure = True)
def get_x(self, params):
"""blah blah ' fdsfds"""
sp.result(sp.record(a = self.data.x, b = 12 + params))
Testing an off-chain view โ
Off-chain views can be called from test scenarios the same way as entrypoints.
The example below shows how to do it.
Example โ
python
import smartpy as sp
class MyContract(sp.Contract):
def __init__(self, param):
self.init(param)
@sp.offchain_view()
def state(self, param):
sp.verify(param < 5, "This is false: param > 5")
sp.result(self.data * param)
@sp.add_test(name = "Minimal")
def test():
scenario = sp.test_scenario()
c1 = MyContract(1)
scenario += c1
""" Test views """
# Display the offchain call result
scenario.show(c1.state(1))
# Assert the view result
scenario.verify(c1.state(2) == 2)
# Assert call failures
scenario.verify(sp.is_failing(c1.state(6))); # Expected to fail
scenario.verify(~ sp.is_failing(c1.state(1))); # Not expected to fail
# Assert exception result
# catch_exception returns an option:
# sp.none if the call succeeds
# sp.some(<exception>) if the call fails
e = sp.catch_exception(c1.state(7), t = sp.TString)
scenario.verify(e == sp.some("This is false: param > 5"))
Utils โ
Convert a URL
into a metadata big_map
.
python
sp.utils.metadata_of_url(url)
A simple alias for sp.big_map({ย ""ย : sp.utils.bytes_of_string(url)ย })
Metadata example โ
python
import smartpy as sp
class MyContract(sp.Contract):
def __init__(self, **kargs):
self.init(**kargs)
# A python dictionary that contains metadata entries
metadata = {
"name": "Contract Name",
"description": "A description about the contract",
"version": 1,
"views" : [self.get_x, self.get_storage],
}
# Helper method that builds the metadata and produces the JSON representation as an artifact.
self.init_metadata("example1", metadata)
@sp.offchain_view(pure = True)
def get_x(self, params):
"""blah blah ' some documentation """
sp.result(sp.record(a = self.data.x, b = 12 + params))
@sp.offchain_view(doc = "The storage")
def get_storage(self):
sp.result(self.data.x)
@sp.entrypoint()
def change_metadata(self, metadata):
self.data.metadata = metadata
@sp.add_test(name = "Metadata")
def test():
scenario = sp.test_scenario()
c1 = MyContract(x=1, metadata = sp.utils.metadata_of_url("ipfs://Qme9L4y6ZvPwQtaisNGTUE7VjU7PRtnJFs8NjNyztE3dGT"))
scenario += c1
c1.change_metadata(sp.utils.metadata_of_url("https://cloudflare-ipfs.com/ipfs/Qme9L4y6ZvPwQtaisNGTUE7VjU7PRtnJFs8NjNyztE3dGT"))