Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pages on creating, testing and deploying smart contract #152

Merged
merged 18 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: Creating Smart Contracts
author: 'Yuxin Li'
lastUpdated: 2nd November 2023
---

## Introduction
This documentation outlines the fundamental steps involved in creating a smart contract on the Tezos blockchain. Subsequent chapters will guide you through testing and deploying your smart contract.

## Creating smart contract

### Choosing a Smart Contract Language
Tezos supports a variety of smart contract languages:

- [Michelson](../languages/michelson.mdx): Tezos' native language, which is stack-based and can be challenging for beginners.
- [SmartPy](../languages/smartpy.mdx): A Python-inspired language that compiles into Michelson, suitable for those familiar with Python.
- [LIGO](../languages/ligo.md): Offers a statically-typed paradigm, compiling into Michelson, with syntax variations akin to JavaScript.
- [Archetype]((../languages/archetype.md)): A domain-specific language with formal specification features for safer smart contracts.

For beginners, SmartPy or LIGO are often recommended due to their higher-level more abstracted approach.

### Install the language/IDE that you use
To compile and test your contract, the corresponding command-line interface (CLI) must be installed. Once installed, begin writing your contract by initializing a new file in your preferred editor.

### Writing the Contract
Before you dive into the syntax and intricacies of your chosen language, you should outline what your smart contract is intended to do. Define the problem it solves, the functions it will carry out, and any external interactions or transactions it must handle.

#### Initialize a new file
Initiate a new file with the suitable extension. For instance, a .py extension is required for SmartPy.

#### Define contract storage.
Contract storage is where the persistent state of your smart contract is kept. It’s important to carefully design your storage to efficiently manage the data your contract will maintain.
- For SmartPy: Use Pythonic classes and types to represent storage. SmartPy provides a straightforward way to map these into Michelson storage requirements.
- For LIGO: Choose the most suitable syntax flavor and use the type definitions to lay out the storage structure.

#### Define entrypoints
Entrypoints act as the method for receiving external communication in Tezos contracts. They typically accept two arguments:
- Parameters for the method.
- Contract storage.

Entrypoints also have a predefined signature for the:
- Response
- List of operations
- Updated storage

In the following section, we will explore how to test the smart contract.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: Deploying smart contracts
author: 'Yuxin Li'
lastUpdated: 2nd November 2023
---
## Introduction
The last part will introduce how to deploy smart contracts. 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 initiated 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`.

## Deploying a Smart Contract
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).

:::note
Note that you must replace `<CONTRACT_NAME>` with the address of the deployed contract before running the command.
Obtaining the Tezos Client:
:::

### Obtaining the Tezos Client:
- **GNU/Linux**: the simplest way to get octez-client is through opam using opam install tezos. alternatives are available here
- **MacOsX**: the software is distributed through a brew formula with brew install tezos.

## Interacting with the Devloped Contract

### Invocation
After the smart contract has successfully been originated and included in a baked block, interaction with the contract's entrypoints is possible via 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.
:::




Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Testing Smart Contracts
author: 'Yuxin Li'
lastUpdated: 2nd November 2023
---

## Introduction
Since smart contracts on the Tezos blockchain are immutable post-deployment, rigorous testing is paramount to ensure functionality, prevent errors, and avoid potential financial losses. Importantly, contract testing doesn't require any tokens or a wallet account to execute.

## Testing smart contracts
### Setting up a Testing Environment
SmartPy includes an integrated testing framework that emulates the Tezos blockchain environment, allowing for comprehensive testing without real token expenditure.

For LIGO developers, `ligo-mockup` is recommended for simulating contract execution and testing the contract's logic.

### Writing Automated Tests
Automated testing is crucial for verifying the correctness and security of your smart contract. Your test suite should include:
- **Functionality Tests**: Verifying that all contract functions perform as intended under normal conditions.
- **Edge Cases**: Testing how the contract deals with unexpected or extreme inputs to ensure robustness.
- **Security Tests**: Checking for common security issues, such as reentrancy attacks, integer overflows, and underflows to ensure the contract is not vulnerable to exploits.
- **Gas Optimization**: Ensuring that the contract operations are gas-efficient, conserving resources and minimizing transaction fees.

### Runing Tests
To run the tests:

1. Set up your preferred testing framework according to the documentation.
1. Write test scripts that systematically work through each function and potential interaction with the contract.
1. Execute these tests in your local environment or using a testnet to avoid incurring real-world transaction costs.

### Compiling the smart contract to Michelson
Before deploying the smart contract, you need to compile it into a form that the Tezos blockchain can execute.
Steps for compilation are as follows:

1. **Syntax Validation**: The first step of the compilation is to check the syntax. Your chosen IDE or text editor might highlight syntax errors as you write the code.
1. **Static Analysis**: Tools are available for some languages, like LIGO, to perform static analysis. This analysis can catch common mistakes before compilation.
1. **Compilation Command**: Use the compiler or CLI tool associated with your smart contract language to compile the code. For example, for SmartPy, you would use the [SmartPy online IDE](https://smartpy.io/ide) to compile your .py file into Michelson.

Upon successful compilation, your smart contract is ready for the deployment! In the next section, we will explore how to deploy a smart contract.
Loading