Skip to content
On this page

FAQ

Discovering SmartPy

What is SmartPy?

SmartPy is a comprehensive solution for developing, testing, and deploying smart contracts on the Tezos blockchain.

How do I get started?

Begin with our tutorial. For practical examples, navigate to the web IDE and explore the variety of templates provided.

What language is used to write smart contracts?

SmartPy provides a domain-specific language, which bears resemblance to Python but has additional types and structures specific to the Tezos blockchain.

Modules in SmartPy are designated with the @sp.module decorator, while other code remains standard Python. Being a Python library, SmartPy can be conveniently installed using pip.

What is a smart contract?

A smart contract is essentially a program executed within the blockchain environment. Once deployed, contracts are immutable and public. This ensures transparency, audibility, and reproducibility.

What is a blockchain?

A blockchain is a decentralized ledger system. You can delve deeper into its intricacies on opentezos.com/blockchain-basics.

What is Tezos?

Tezos is a dynamically evolving blockchain, distinguished by its unique on-chain governance. From inception, it has employed proof-of-stake and continues to provide a multitude of security-enhancing features.

Learn more at tezos.com or opentezos.com/tezos-basics.

Can I use VSCode, Pycharm, emacs, etc.?

Absolutely! SmartPy provides an offline command line interface compatible with your favourite IDE.

Do you recommend using the SmartPy web IDE instead?

While the SmartPy web-based IDE is ideal for beginners due to its simplicity, we suggest using your preferred IDE for more intense development. It's recommended to manage your files on your own system and not depend solely on your browser's local storage.

Can I write smart contracts for other blockchains using SmartPy?

Currently, SmartPy concentrates exclusively on developing smart contracts for the Tezos blockchain.

What is Michelson?

Michelson is a specialized programming language created specifically for Tezos. It offers an elevated level of security and avoids certain issues prevalent in the Ethereum Virtual Machine (EVM). You can learn more about it here.

Can I deploy a smart contract on Tezos using SmartPy?

Yes, the SmartPy website offers an origination tool. You can also employ any other deployment tools available within the Tezos ecosystem.

What are the resources or community forums where I can learn more and get help with SmartPy?

Diving into the language

How to write enums in SmartPy?

You can write enums using the variant system and the unit type.

For example:

smartpy
@sp.module
def main():
    status: type = sp.variant(Active=sp.unit, Inactive=sp.unit)

    class C(sp.Contract):
        def __init__(self):
            self.data.status = sp.cast(sp.variant.Active(), status)

What is the equivalent to struct in SmartPy?

In SmartPy, the equivalent to a struct a record.

How do I accept tez in an entrypoint?

There's no specific step required. In Tezos, received tez are automatically added to the balance of the contract's balance.

If you wish to control the received amount or enforce a payment, the assert instruction can be used in conjunction with the sp.amount instruction, which reflects the amount received. For instance, assert sp.amount > 10 ensures that an amount greater than ten tez is received, otherwise, the entrypoint will fail.

Can I use Python libraries in my smart contracts?

Although other Python libraries cannot be directly used in the smart contract code itself, they can be used in the code that tests your contracts.

Can I use Python f-strings?

While Formatted string literals (known as "f-strings") are not supported within the contract's code due to the absence of this feature in Michelson, they can be used for testing contracts.

Why don't my old contracts work in the simulator anymore?

With version 0.18 SmartPy has migrated to a new syntax. If your contracts are written in the old syntax, you can still access them using the legacy version available at legacy.smartpy.io.

I'm getting the error module 'smartpy' has no attribute 'Contract'.

The code of a smart contract should reside inside a module (sp.module). If you need assistance with this, please refer to the tutorial. If your contract is outdated, refer to the solution for the previous question.

Can I run test scenarios with contracts from the old syntax together with contracts in the new syntax?

Unfortunately this isn't possible. However, migrating contracts to the new syntax is generally a relatively straightforward process.

Is there a way to generate random numbers?

Tezos doesn't offer an in-built instruction to generate random numbers on-chain. If this is a requirement for your project, you might find this resource helpful: opentezos.com/smart-contracts/avoiding-flaws/#7-using-unreliable-sources-of-randomness.

What is the difference between on-chain and off-chain views?

On-chain views are part of the contract's deployed code and can be invoked both on-chain and off-chain and have the ability to call other on-chain views. If a contract calls them, the gas cost is part of the transaction. Off-chain views, on the other hand, are only callable off-chain and are exempt from gas costs. Their code is stored off-chain but referenced in the contract.

How can I optimize my code?

In SmartPy, as in most of computing, optimization largely stems from your application's overall design. Common ways to reduce costs include avoiding lengthy error messages, conducting most of the computations off-chain, and minimizing the complexity of your tasks.

Common pitfalls

Should I use sp.source or sp.sender?

sp.source yields the contract (always an implicit acocunt) that initiated the current transaction, whereas sp.sender yields the contract that called the current entrypoint. They may be identical. sp.sender is typically more appropriate, as using sp.source could unintentionally allow proxy contracts to impersonate the actual sender.

What are some common pitfalls to avoid when programming with SmartPy?

Common errors to avoid include:

  • Executing multiple transfers within a single transaction. If one transfer fails, others won't receive their tokens. To prevent this, implement a system where recipients claim their tokens.

  • Introducing computations that may lead to gas overflows.

  • Performing intensive computations or storing large volumes of data on-chain. Keep private data off-chain. Use on-chain computations solely for tasks that require high trust and public verification. If the data exceeds a few bytes, consider storing references (like hashes) instead of the actual data.

A helpful resource for common pitfalls is opentezos.com/smart-contracts/avoiding-flaws/.

How do I test my SmartPy contracts?

SmartPy provides a robust testing system that allows you to swiftly test code without deploying anything to the blockchain.

Visit the manual to learn more.

How to return a value from an entrypoint?

This question has been answered on our forum.

Standards

In there an ERC-20 equivalent?

Indeed, it is called FA2. Read the guide and FAQ specific to tokens here: tokens FAQ and FA2 library

How to react to a FA2 transfer?

There is no way to react to a FA2 transfer. The solution is to invert the logic: the receiver now claims its tokens. This way they know the transfer has been done. More information on the forum.

How to create NFTs with multiple owners?

Technically, a token that multiple people can independently hold and transfer isn't an NFT, even if an image is associated with it. You might be referring to a fungible or "semi-fungible" token. More information can be found here: FA2 library - base classes

Is there an allowance system in the token standard?

FA1.2 standard

There is an allowance system. See TZIP-7 and fa1_2.py.

FA2 standard

There is no allowance system by default but an operator system. However you can implement your own allowance system after using the (fa2 library)[fa2 library overview]. Here is an example.

Here is the desired choreography: 1) The user calls update_operators and then update_allowance. 2) The user calls the contract. 3) The contract uses the operator privilege limited by the allowance system to move the tokens.

Changing allowance value from a non-zero value to a non-zero value is forbidden to prevent the corresponding attack vector, however this is not enough on its own to guarantee a safe allowance change. See How to safely change the allowance.