SmartPy Documentation
website·Indexed 39 days ago
https://smartpy.tezos.com/manual/introduction/overview.htmlDocumentation for SmartPy, a Python-based language for writing smart contracts on Tezos. It includes guides on data types (sp.address, sp.nat, sp.record), compilation, local development, and the FA2 library for creating fungible and non-fungible token contracts.
What's inside SmartPy
- The SmartPy FA2 library provides a set of classes and tools to create tokens that adhere to the FA2 standard on Tezos. The FA2 standard is a universal interface that ensures tokens are compatible with Tezos wallets and applications while allowing developers to define custom transfer rules and behaviors. To create a token, a developer creates a contract that inherits from a base class in the library to specify the token type (fungible, single-asset, or non-fungible) and then uses mixins to customize the token's functionality.
Overview of SmartPy and Tezos Smart Contracts
SmartPy is a high-level programming language used to write smart contracts for the Tezos blockchain, abstracting the complexity of the on-chain Michelson language. A Tezos smart contract consists of three primary components: persistent storage (data the contract reads/writes), one or more entrypoints (functions that clients call), and a Tezos account capable of storing tokens.Overview of SmartPy
SmartPy is a solution for developing and testing smart contracts on the Tezos blockchain. It consists of a Python-like domain-specific language (DSL), a compiler that translates SmartPy code into Michelson (the low-level language of Tezos), testing tools for contract scenarios, and utility libraries.Overview of FA2 Library Transfer Policies
The FA2 library provides several standard transfer policies to control who can transfer tokens. By default, only the token owner and their designated operators can transfer tokens. The administrator account has no special transfer permissions unless a specific policy is applied.Understand Tezos Tickets in SmartPy
Tezos tickets are authenticated quantities issued by contracts or users. A ticket of typesp.ticket[t]consists of three immutable elements: a ticketer (the issuing contract address), contents (the payload of typet), and an amount (sp.nat). Tickets cannot be duplicated, but they can be split into multiple tickets or joined into one, provided the ticketer and contents match. The sum of amounts is always preserved during splitting and joining.Use Options to handle null or empty values
Options in SmartPy are a specialized type of variant with two cases:
None(representing a null/empty value) andSome(carrying a value of a defined type). They are commonly used to catch errors or undefined cases, such as the result ofsp.edivwhich returnsNoneif dividing by zero.To define an option type, use
sp.option[T]whereTis the type of the value carried bySome.# An option that can contain an integer value_opt = sp.Some(5) sp.cast(value_opt, sp.option[sp.int])Understand the difference between SmartPy modules and test scenarios
SmartPy code is divided into two distinct environments with different behaviors and limitations:
- Modules: Contain smart contracts intended to be compiled to Michelson and deployed to the Tezos blockchain. They are subject to strict typing and blockchain-specific constraints (e.g., no external API calls, no standard Python
try/except). - Test Scenarios: Pure Python code used to test smart contracts. They run in a simulated Tezos environment and allow full Python capabilities, including importing Python libraries and using standard exception handling.
Note: Test scenarios must be defined in
.pyfiles, whereas SmartPy modules typically use.spyfiles.- Modules: Contain smart contracts intended to be compiled to Michelson and deployed to the Tezos blockchain. They are subject to strict typing and blockchain-specific constraints (e.g., no external API calls, no standard Python
Access global contract variables in SmartPy
SmartPy provides several built-in global variables that can be accessed within a contract to retrieve information about the current transaction, the contract's state, or the blockchain network.# Examples of available global variables: # sp.amount: The amount of tez sent with the transaction (sp.mutez) # sp.balance: The amount of tez in the contract's balance (sp.mutez) # sp.chain_id: The chain ID (sp.chain_id) # sp.level: The current block level (sp.int) # sp.now: The timestamp of the current block (sp.timestamp) # sp.sender: The immediate account that sent the operation (sp.address) # sp.source: The account that initiated the chain of operations (sp.address) # sp.self_address: The address of the current contract (sp.address) # sp.total_voting_power: Total tez staked by bakers (sp.int)Access the SmartPy Online IDE
For a traditional IDE experience with built-in visualization of scenario action results, use the SmartPy online IDE available at https://smartpy.io/ide.Understand Tezos contract metadata (TZIP-16)
Contract metadata provides off-chain details about a smart contract (such as name, description, and version) used by wallets, block explorers, and dApps. It is distinct from token metadata. Contract metadata must follow the TZIP-16 standard and is typically structured as a JSON file containing keys such asname,version,description,interfaces,authors,homepage,source, andpermissions.{ "name": "MyContract", "version": "1.0.0", "description": "This implements FA2 (TZIP-012) using SmartPy.", "interfaces": ["TZIP-012", "TZIP-016"], "authors": ["SmartPy <https://smartpy.io>"], "homepage": "https://smartpy.io/ide?template=fa2_lib.py", "source": { "tools": ["SmartPy"], "location": "https://gitlab.com/SmartPy/smartpy/-/raw/master/python/templates/fa2_lib.py" }, "permissions": { "receiver": "owner-no-hook", "sender": "owner-no-hook", "operator": "owner-or-operator-transfer" } }Understand SmartPy numeric types
SmartPy provides three primary numeric types. Note that floating-point numbers are not supported.
sp.nat: Natural numbers (non-negative integers, 0 and above).sp.int: All integers (positive, negative, and zero).sp.mutez: Non-negative amounts of micro-tez (one-millionth of a tez).
sp.mutezis not compatible withsp.natorsp.int; they cannot be compared or combined using standard mathematical operators without conversion functions from the standard library.Use the SmartPy Jupyter Notebook Web App
A client-side Jupyter Notebook web app is available for writing and running SmartPy code and scenarios.