Starting with SmartPy Part 7: Reference Guide

A Compilation of SmartPy Functions and Features

Teckhua Chiang
The Cryptonomic Aperiodical
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 parameters param1, param2, ....
  • def <entryPoint>(self, params) : Function definition for an entry point named entryPoint with parameter params containing multiple implicit fields that are defined during entry point invocation.

Functions

  • self.init(<field1> = <val1>, <field2> = <val2>, <...>) : Function that initializes storage with fields field1, field2, ... that contain the values val1, val2, ... respectively.
  • sp.setType(<var>, <type>) : Function that sets var to be of type type.

Access Expressions

  • params.<field> : Expression for accessing a field of entry point params.
  • self.data : Expression for accessing the contract storage.
  • self.data.<field> : Expression for accessing a field of contract storage.
  • sp.sender : Expression for accessing the sender address.

Assignment Statements

  • <map>[<key>] = <value> : Assignement statement that associates key in map with value.

Control Statements

  • sp.verify(<booleanExpression>) : Control statement that checks if booleanExpression is true. Otherwise, the entry point will fail and all executed operations will be reversed.
  • sp.if(<booleanExpression>) : Control statement that checks if booleanExpression is true. Otherwise, the contract will not execute the code block contained within sp.if().

Type Constructors

  • sp.address(<address>) : Constructor for an address.
  • sp.Map() : Constructor for a Map.
  • sp.BigMap() : Constructor for a BigMap.
  • sp.Record(<field1> = <val1>, <field2> = <val2>, <...>) : Constructor for a Record with fields field1, field2, ... that contain the values val1, 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 internal function with parameters params1, params2, etc....

SmartPy Tests

  • @sp.addTest() : Annotation above a test definition.
  • def test() : Function definition for a SmartPy test.
  • <contract>.fullHTML() : Expression that transforms a contract into an HTML string that contains balance, data, and entry points.
  • <contract>.<entryPoint>(<paramsField1> = <val1>, <paramsField2> = <val2>, <...>) : Expression that invokes the entryPoint of contract with params encapsulating the fields paramsField1, paramsField2, … that contain the values val1, val2, … respectively. Returns a smartpy.ExecMessage.
  • <message>.run(sender = <address>) : Expression that modifies message of type smartpy.ExecMessage to run with address as the sp.sender. Returns a smartpy.ExecutedMessage.
  • <object>.html() : Expression that transforms object of type smartpy object into an HTML string.
  • setOutput(<val>) : Expression that displays val on the output panel of the SmartPy editor.
  • alert(<val>) : Expression that creates an alert box containing val.
  • assert(<booleanExpression>) : Expression that checks if booleanExpression is true. Otherwise, it will cause an AssertionError in the SmartPy simulation suite.

--

--