-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pages on creating, testing and deploying smart contract (#152)
* pages on creating, testing and deploying smart contract * fixed file location & language style, recovered package.json and package-lock.json * Eliminate bold formatting in lists * updated creating smart contracts * testing updated * updated creating and testing * updated deploying * New format for dates based on #154 * updated pages * fix indent * Update docs/smart-contracts/creating.md Co-authored-by: Tim McMackin <[email protected]> * Update docs/smart-contracts/deploying.md Co-authored-by: Tim McMackin <[email protected]> * Update docs/smart-contracts/testing.md Co-authored-by: Tim McMackin <[email protected]> * Update docs/smart-contracts/testing.md Co-authored-by: Tim McMackin <[email protected]> * Update docs/smart-contracts/deploying.md Co-authored-by: Tim McMackin <[email protected]> * Interacting with the contract --------- Co-authored-by: Tim McMackin <[email protected]> Co-authored-by: Tim McMackin <[email protected]>
- Loading branch information
1 parent
cfc2d36
commit c747d1b
Showing
5 changed files
with
207 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: Creating smart contracts | ||
author: 'Yuxin Li' | ||
last_update: | ||
date: 6 November 2023 | ||
--- | ||
## Introduction | ||
This documentation provides step-by-step instructions for creating smart contracts on Tezos. After creating the contract, you can find the resources on [testing](testing.md) and [deploying](deploying.md). | ||
|
||
## Choosing your smart contract language | ||
Tezos supports a variety of smart contract [languages](./languages): Michelson, SmartPy, LIGO, Archetype. | ||
|
||
You can select a language based on your familarity with programming paragims, the complexity of the contract you want to deploy, and the specific features you require. Here's a more detailed table for each language: | ||
|
||
| | **Michelson** | **SmartPy** | **LIGO** | **Archetype** | | ||
|:----------------:|:----------------------------------------------------------:|:-----------------------------------------------------:|:-------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------:| | ||
| **Complexity** | High (stack-based, low-level) | Low (Python-like, high-level) | Moderate (various high-level syntaxes) | Moderate (includes formal specification features) | | ||
| **Capabilities** | Full control over contract, optimal for gas efficiency | Easy to write, automatically manages stack operations | Statically-typed, strong error checking | Specialized for formal verification and correctness | | ||
| **Use Cases** | Optimized contracts, developers with blockchain experience | Python developers, rapid prototyping | Developers familiar with static typing, variety of mainstream programming backgrounds | High-security contracts, developers looking for formal proof of contract behavior | | ||
|
||
For beginners, we recommand **SmartPy** or **LIGO** for their higher-level more abstracted approach. | ||
|
||
|
||
## Making a strategic choice | ||
Before writing your code, take some time to consider whether your project is suitable for starting with a pre-existing template or if it would be better to start from scratch. Essentially, this depends on the type of contract you are building. For example: | ||
- FA2 contract: it’s better to use a template to start. | ||
- Others: build it from scratch. | ||
|
||
## Coding your contract | ||
Before coding, you should clearly outline the purpose of your smart contract, define the problem it addresses, detail the functions it will perform, and specify any external interactions or transactions it will manage. | ||
|
||
### Starting with online IDE | ||
The online editor is the quickest and easiest way to get started. | ||
|
||
For example: | ||
- For smartpy user, we recommend to use the [SmartPy online IDE](https://smartpy.io/) | ||
- For Ligo user, we recommend to use the [Ligo online IDE](https://ligolang.org/?lang=jsligo) | ||
|
||
|
||
### Defining contract storage | ||
Contract storage holds the persistent state of your smart contract. It’s important to carefully design your storage since storage is expensive on-chain. You should avoid storing any data that the contract will not use. | ||
|
||
- SmartPy: Use Pythonic classes and types to represent storage. SmartPy provides a straightforward way to map these into Michelson storage requirements. | ||
- LIGO: Choose the most suitable syntax flavor and use the type definitions to lay out the storage structure. | ||
|
||
In Tezos, big maps are a storage optimization feature for large sets of data, , especially when handling large datasets that don't need to be fully loaded into memory at once. Big maps are ideal for ledger applications with numerous accounts, as they load data lazily, fetching only necessary parts on demand. In contrast to regular maps, suitable for smaller collections, and lists, which order data, big maps save costs when the dataset is large. | ||
|
||
In SmartPy, you can define a big map using `sp.big_map`, and in LIGO, you use `big_map` keyword for the type declaration. | ||
|
||
### Defining entrypoints | ||
Entrypoints serve as methods to receive external communication in Tezos. | ||
|
||
- SmartPy: Entrypoints are defined as methods within a Python class that extends `sp.Contract`. They use decorators like `@sp.entry_point` to denote entrypoints | ||
- LIGO: Entrypoints in LIGO are defined as functions that manipulate storage. The `function` keyword is used, and each entrypoint function must be explicitly marked for export in the contract interface | ||
|
||
You should clearly define the parameters and storage interaction for both languages. | ||
|
||
- Each entrypoint's **parameters** must be well-specified, with types that match the expected inputs. For example, if an entrypoint is supposed to accept an integer and a string, the parameter list should reflect this. | ||
- The contract **storage** is usually passed as an argument to the entrypoints. In SmartPy, the storage is accessed through the self.data attribute inside the entrypoint methods. In LIGO, storage is a parameter of the function, and it's often the last parameter by convention. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
title: Deploying smart contracts | ||
author: 'Yuxin Li' | ||
last_update: | ||
date: 6 November 2023 | ||
--- | ||
## Introduction | ||
In Tezos, deploying a smart contract is often referred to as “origination”. This process essentially creates a new account that holds the smart contract's script. Contracts originated in this manner have addresses that start with `KT1` (known as originated accounts), which distinguishes them from the implicit accounts with addresses beginning with `tz1`, `tz2`, or `tz3`. | ||
|
||
## Prerequisites | ||
- Compile your contract and its initial storage | ||
- Set up an wallet account on Tezos with some tez to pay the fees | ||
- Ensure that you have obtained the [Tezos client](../developing/octez-client/installing) | ||
|
||
## Deploying a smart contract | ||
Generally, there are two methods for deploying your smart contracts: either using the command line in your terminal or deploying through an online IDE. | ||
|
||
### Deploying via terminal | ||
The first one is to deploy through your terminal. Here is the syntax for the Tezos command line to deploy a smart contract: | ||
```bash | ||
octez-client originate contract CONTRACT_NAME transferring AMOUNT_TEZ from FROM_USER \ | ||
running MICHELSON_FILE \ | ||
--init 'INITIAL_STORAGE' --burn-cap GAZ_FEE | ||
``` | ||
where: | ||
- `CONTRACT_NAME` is the name given to the contract. | ||
- `MICHELSON_FILE` is the path for the Michelson smart contract code (.tz file). | ||
- `AMOUNT_TEZ` is the quantity of Tez being transferred to the newly deployed contract. If a contract balance reaches 0 then it is deactivated. | ||
- `FROM_USER` account from which the Tez are taken (and transferred to the new contract). | ||
- `INITIAL_STORAGE` is a Michelson expression. The --init parameter is used to specify the initial state of the storage. | ||
- `GAZ_FEE` is a specified maximal fee the user is willing to pay for this operation (using the --burn-cap parameter). | ||
|
||
### Deploying via online IDE | ||
As for deploying through your online IDE, if you are using Ligo or SmartPy programming languages, you can simply deploy your smart contracts through their respective online IDEs. | ||
- [SmartPy online IDE](https://smartpy.io/) | ||
- [Ligo online IDE](https://ligolang.org/?lang=jsligo) | ||
|
||
## Interacting with the contract | ||
Once you have successfully originated the smart contract and it is included in a baked block, there are two ways to interact with it: through command lines or through a block explorer. | ||
|
||
### Interacting through command lines | ||
The first method involves interacting with the contract's entry points using command lines. | ||
|
||
For example, suppose you have a smart contract with an entrypoint called `update_data`, which takes an integer as an argument to update some data in its storage. Here's how you might invoke this entrypoint: | ||
|
||
```bash | ||
octez-client call CONTRACT_NAME from YOUR_ACCOUNT_ADDRESS \ | ||
--arg 'New_Integer_Value' \ | ||
--entrypoint update_data \ | ||
--burn-cap FEE_LIMIT | ||
``` | ||
Where: | ||
|
||
- `CONTRACT_NAME`: Identifier or the address of the contract that you want to interact with. | ||
- `YOUR_ACCOUNT_ADDRESS` Your own account address that will initiate the transaction. | ||
- `--arg`: Argument that you're passing to the entrypoint, in this case, an integer value. You need to format this according to the expected input in the contract's Michelson code. | ||
- `--entrypoint`: Method in the smart contract that you're calling. | ||
- `--burn-cap`: The maximum fee you are willing to spend for this transaction to be included in the blockchain. | ||
|
||
Here's an example with hypothetical values filled in: | ||
|
||
```bash | ||
octez-client call KT1Vsw5kh4P1Vn... from tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb \ | ||
--arg '42' \ | ||
--entrypoint update_data \ | ||
--burn-cap 0.05 | ||
``` | ||
Where: | ||
|
||
- `KT1Vsw5kh4P1Vn...`: Contract address. | ||
- `tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb`: User's account address. | ||
- `'42'`: New integer value you wish to pass to the update_data entrypoint. | ||
- `0.05`: Maximum amount of Tez you're willing to pay in fees. | ||
|
||
:::note | ||
Always ensure that you check the documentation specific to the smart contract you are interacting with, as the expected arguments (`--arg`) and the name of the entrypoint (`--entrypoint`) can vary widely depending on the contract's design and purpose. | ||
::: | ||
|
||
### Interacting via blockchain explorers | ||
|
||
A blockchain explorer is an efficient and user-friendly tool that enables you to interact with deployed contracts. In the Tezos ecosystem, there are two main blockchain explorers: | ||
|
||
- [Better Call Dev](https://better-call.dev/) | ||
- [TzKT](https://tzkt.io/) | ||
|
||
To interact with a contract, simply copy its address into one of these blockchain explorers. Below is the user interface for interacting with a contract through Better Call Dev: | ||
|
||
![UI for Better Call Dev](/img/tutorials/better-call.png) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.