Starting with SmartPy Part 7: Reference Guide
A Compilation of SmartPy Functions and Features
Published in
2 min readAug 16, 2019
This is a reference guide of the SmartPy grammar that was used throughout this tutorial series, as well as some additional helpful information. Please do not treat it as the official SmartPy documentation, which is currently undergoing extensive development and is subject to change. This documentation was accurate as of October 2019.
There is a concept reference and syntax reference maintained by SmartPy as well.
SmartPy Contracts
Annotations
@sp.entryPoint
: Annotation above an entry point definition. If you’re coming from a Python background, this is called a “decorator”.
Function Definitions
def __init__(self, <param1>, <param2>, <...>)
: Function definition for the storage initializer with parametersparam1, param2, ...
.def <entryPoint>(self, params)
: Function definition for an entry point namedentryPoint
with parameterparams
containing multiple implicit fields that are defined during entry point invocation.
Functions
self.init(<field1> = <val1>, <field2> = <val2>, <...>)
: Function that initializes storage with fieldsfield1, field2, ...
that contain the valuesval1, val2, ...
respectively.sp.setType(<var>, <type>)
: Function that setsvar
to be of typetype
.
Access Expressions
params.<field>
: Expression for accessing afield
of entry pointparams
.self.data
: Expression for accessing the contract storage.self.data.<field>
: Expression for accessing afield
of contract storage.sp.sender
: Expression for accessing the sender address.
Assignment Statements
<map>[<key>] = <value>
: Assignement statement that associateskey
inmap
withvalue
.
Control Statements
sp.verify(<booleanExpression>)
: Control statement that checks ifbooleanExpression
is true. Otherwise, the entry point will fail and all executed operations will be reversed.sp.if(<booleanExpression>)
: Control statement that checks ifbooleanExpression
is true. Otherwise, the contract will not execute the code block contained withinsp.if()
.
Type Constructors
sp.address(<address>)
: Constructor for anaddress
.sp.Map()
: Constructor for aMap
.sp.BigMap()
: Constructor for aBigMap
.sp.Record(<field1> = <val1>, <field2> = <val2>, <...>)
: Constructor for aRecord
with fieldsfield1, field2, ...
that contain the valuesval1, val2, ...
respectively.
Others
sp.Contract
: Class that represents a smart contract. Should be inherited when defining a new SmartPy contract class.self.<function>(<params1>, <params2>, <...>)
: Function call for internalfunction
with parametersparams1, params2, etc...
.
SmartPy Tests
@sp.addTest()
: Annotation above a test definition.def test()
: Function definition for a SmartPy test.<contract>.fullHTML()
: Expression that transforms acontract
into an HTMLstring
that contains balance, data, and entry points.<contract>.<entryPoint>(<paramsField1> = <val1>, <paramsField2> = <val2>, <...>)
: Expression that invokes theentryPoint
ofcontract
withparams
encapsulating the fieldsparamsField1, paramsField2, …
that contain the valuesval1, val2, …
respectively. Returns asmartpy.ExecMessage
.<message>.run(sender = <address>)
: Expression that modifiesmessage
of typesmartpy.ExecMessage
to run withaddress
as thesp.sender
. Returns asmartpy.ExecutedMessage
.<object>.html()
: Expression that transformsobject
of typesmartpy object
into an HTMLstring
.setOutput(<val>)
: Expression that displaysval
on the output panel of the SmartPy editor.alert(<val>)
: Expression that creates an alert box containingval
.assert(<booleanExpression>)
: Expression that checks ifbooleanExpression
is true. Otherwise, it will cause anAssertionError
in the SmartPy simulation suite.