SmartPy Documentation

website·Indexed 39 days ago

https://smartpy.tezos.com/manual/introduction/overview.html

Documentation 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.

Tokens
44.3K
Snippets
238
Records
330
Agent score
59%

What's inside SmartPy

  1. Overview of the SmartPy FA2 library for token creation

    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.
  2. 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.
  3. 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.
  4. 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.
  5. Understand Tezos Tickets in SmartPy

    Tezos tickets are authenticated quantities issued by contracts or users. A ticket of type sp.ticket[t] consists of three immutable elements: a ticketer (the issuing contract address), contents (the payload of type t), 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.
  6. 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) and Some (carrying a value of a defined type). They are commonly used to catch errors or undefined cases, such as the result of sp.ediv which returns None if dividing by zero.

    To define an option type, use sp.option[T] where T is the type of the value carried by Some.

    # An option that can contain an integer
    value_opt = sp.Some(5)
    sp.cast(value_opt, sp.option[sp.int])
  7. Understand the difference between SmartPy modules and test scenarios

    SmartPy code is divided into two distinct environments with different behaviors and limitations:

    1. 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).
    2. 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 .py files, whereas SmartPy modules typically use .spy files.

  8. 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)
  9. 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 as name, version, description, interfaces, authors, homepage, source, and permissions.
    {
      "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"
      }
    }
  10. 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.mutez is not compatible with sp.nat or sp.int; they cannot be compared or combined using standard mathematical operators without conversion functions from the standard library.