Tic Tac Toe Smart Contract Example

Creator Algorand Foundation

Source
python
Algorand Python
smart-contract

A comprehensive Python-based smart contract template for the Algorand blockchain. This template demonstrates how to create, test, and deploy smart contracts using Algorand Python, featuring best practices for contract development, testing frameworks, and deployment scripts. Perfect for developers looking to build secure and efficient smart contracts on Algorand.

Features

Algorand Python contract implementation
Unit testing setup
Deployment scripts
Contract interaction examples
Tic Tac Toe Smart Contract Example

Python Smart Contract Example

This project has been generated using AlgoKit. See below for default getting started instructions.

Setup

Pre-requisites

For interactive tour over the codebase, download vsls-contrib.codetour extension for VS Code, then open the .codetour.json file in code tour extension.

Initial Setup

1. Clone the Repository

Start by cloning this repository to your local machine.

2. Install Pre-requisites

Ensure the following pre-requisites are installed and properly configured:

3. Bootstrap Your Local Environment

Run the following commands within the project folder:

Development Workflow

Terminal

Directly manage and interact with your project using AlgoKit commands:

  1. Build Contracts: algokit project run build compiles all smart contracts. You can also specify a specific contract by passing the name of the contract folder as an extra argument. For example: algokit project run build -- hello_world will only build the hello_world contract.
  2. Deploy: Use algokit project deploy localnet to deploy contracts to the local network. You can also specify a specific contract by passing the name of the contract folder as an extra argument. For example: algokit project deploy localnet -- hello_world will only deploy the hello_world contract.

VS Code

For a seamless experience with breakpoint debugging and other features:

  1. Open Project: In VS Code, open the repository root.
  2. Install Extensions: Follow prompts to install recommended extensions.
  3. Debugging:
    • Use F5 to start debugging.
    • Windows Users: Select the Python interpreter at ./.venv/Scripts/python.exe via Ctrl/Cmd + Shift + P > Python: Select Interpreter before the first run.

JetBrains IDEs

While primarily optimized for VS Code, JetBrains IDEs are supported:

  1. Open Project: In your JetBrains IDE, open the repository root.
  2. Automatic Setup: The IDE should configure the Python interpreter and virtual environment.
  3. Debugging: Use Shift+F10 or Ctrl+R to start debugging. Note: Windows users may encounter issues with pre-launch tasks due to a known bug. See JetBrains forums for workarounds.

AlgoKit Workspaces and Project Management

This project supports both standalone and monorepo setups through AlgoKit workspaces. Leverage algokit project run commands for efficient monorepo project orchestration and management across multiple projects within a workspace.

AlgoKit Generators

This template provides a set of algokit generators that allow you to further modify the project instantiated from the template to fit your needs, as well as giving you a base to build your own extensions to invoke via the algokit generate command.

Generate Smart Contract

By default the template creates a single HelloWorld contract under hello_world folder in the smart_contracts directory. To add a new contract:

  1. From the root of the project (../) execute algokit generate smart-contract. This will create a new starter smart contract and deployment configuration file under {your_contract_name} subfolder in the smart_contracts directory.
  2. Each contract potentially has different creation parameters and deployment steps. Hence, you need to define your deployment logic in deploy_config.pyfile.
  3. config.py file will automatically build all contracts in the smart_contracts directory. If you want to build specific contracts manually, modify the default code provided by the template in config.py file.

Please note, above is just a suggested convention tailored for the base configuration and structure of this template. The default code supplied by the template in config.py and index.ts (if using ts clients) files are tailored for the suggested convention. You are free to modify the structure and naming conventions as you see fit.

Generate ‘.env’ files

By default the template instance does not contain any env files. Using algokit project deploy against localnet | testnet | mainnet will use default values for algod and indexer unless overwritten via .env or .env.{target_network}.

To generate a new .env or .env.{target_network} file, run algokit generate env-file

Debugging Smart Contracts

This project is optimized to work with AlgoKit AVM Debugger extension. To activate it: Refer to the commented header in the __main__.py file in the smart_contracts folder.

If you have opted in to include VSCode launch configurations in your project, you can also use the Debug TEAL via AlgoKit AVM Debugger launch configuration to interactively select an available trace file and launch the debug session for your smart contract.

For information on using and setting up the AlgoKit AVM Debugger VSCode extension refer here. To install the extension from the VSCode Marketplace, use the following link: AlgoKit AVM Debugger extension.

Tools

This project makes use of Algorand Python to build Algorand smart contracts. The following tools are in use: