Algorand Coding Challenge - Volume 2: 🐍 Python #03.
Check out the GitHub repository.
🚩 Challenge 3: Algorand smart contract can send transactions!? 🤯
I want to deposit my Super RARE Oranges NFT into my ASA Vault contract but the contract is failing to opt-in to the asset! What is going on?
Inside of smart_contracts/asa_vault/contract.py
file, there is a simple ASA vault smart contract written in Algorand Python.
It is a simple contract that has a deposit_asa
function and a withdraw_asa
function and the depositor’s balance is recorded in the global state asa_balance
. There is also an opt_in_to_asset
method that opts the smart contract account into the asset that is being deposited.
If you don’t know what opt-in
is it is a unique feature on Algorand that prevents someone from sending a token to an account that hasn’t opted in to the token that is being sent. You can learn more about opt-ins here
The smart contract builds successfully but if you try deploying and testing the smart contract by opening Docker Desktop, and then running:
algokit bootstrap all # set up dev environment on your computer
algokit localnet start # launch an Algorand local network for testing
algokit project run build # Compile the smart contract and get low-level TEAL code.
algokit project deploy localnet # Run the `smart_contracts/asa_vault/deploy_config.py` file to deploy and test the smart contract.
it will fail and show this error:
algosdk.error.AlgodHTTPError: TransactionPool.Remember: transaction ESKVFLSCWNUNJJWMF34QBEQHTXGWFQNDTBYS3MBXZQNEIQOYLKXQ: receiver error: must optin, asset 1442 missing from NIADJIGAJ2KYNX5HJ35I7RK66VVN6SE6NBUB46NJXAC4USYXJEYSXDH4JQ
Find out what is wrong and fix the bug.
💬 Meet other hackers working on this challenge and get help in the Algorand Python Discord Channel!
Checkpoint 1: 🧰 Prerequisites
- Install Python 3.12 or higher
- Install AlgoKit.
- Install Docker. It is used to run a local Algorand network for development.
Checkpoint 2: 💻 Set up your development environment
- Fork this repository.
- Clone the repository
cd [DIRECTORY_OF_YOUR_CHOICE]
git clone [FORKED_REPO_URL]
Now you have 2 ways of opening your AlgoKit project.
With VSCode Workspaces
- Open the cloned repository with the code editor of your choosing.
- Open workspace mode by clicking
open workspace
inside ofpython-challenge-3.code-workspace
file at the root level. - Go inside of the
challenge
folder. - To setup your dev environment using AlgoKit, run the below command:
algokit project bootstrap all #algokit bootstrap all is being deprecated. Use this command from now on.
This command will install all dependencies and also generate a .env
file for you.
5. Activate Python virtual environment by running:
poetry shell
venv will automatically be activated the next time you open the project.
Please note, in addition to built-in support for VSCode Workspaces, the cli provides native support for monorepos called
algokit workspaces
. Refer to documentation for detailed guidelines for recommended project structures and ability to leverage custom command orchestration viaalgokit project run
.
Without VSCode Workspaces
All AlgoKit projects initialized with --workspace
option has the following directory structure:
├── projects
│ ├── smart-contract
│ └── frontend # doesn't exist for this project
│ └── ...
├── {several config files...}
├── ...
├── .algokit.toml # workspace-typed algokit project config
├── {project-name}.code-workspace
├── README.md
So to access a single project under the projects
folder, it is recommended to cd
into the project you want to work with and then open your code editor (alternatively refer to VSCode Workspace file at the root). If you are reading this and didn’t open the challenge
folder directly, go do that now!! 😁
- cd into
projects/challenge
then open the code editor - To setup your dev environment using AlgoKit, run the below command:
algokit project bootstrap all #algokit bootstrap all is being deprecated. Use this command from now on.
This command will install all dependencies and also generate a .env
file for you.
3. Activate the Python virtual environment by running below inside of challenge
folder:
poetry shell
venv will automatically be activated the next time you open the project.
Video walkthrough of forking and cloning this repository:
Now you are ready to fix the bug!
Checkpoint 3: 🐞 Fix the bug 🧐
- Open Docker Desktop and launch Algorand localnet by running
algokit localnet start
in your terminal For more info click me!. - Go to
smart_contracts/asa_vault/contract.py
and see the source code of the ASA vault smart contract. - Compile the contract with
algokit project run build
and then try running the test script withalgokit project deploy localnet
. It will fail. - Go to
smart_contracts/asa_vault/deploy_config.py
to understand the deployment/test script. - Read the error, figure out what is wrong, and fix the bug! The bug is inside of the ASA Vault contract. There is nothing wrong with the deploy script inside of
smart_contracts/asa_vault/deploy_config.py
- After fixing the bug, build and run the deploy script again with the below command:
algokit project run build
algokit project deploy localnet
OR if you are on VSCode, hit F5 or go to the Run and Debug
tab and run the debug script.
If you see something like this in the console, you successfully fixed the bug! 😆
😰 Are you struggling?
- Inner Transaction Documentation
- Algorand Python Documentation
- How to opt-in to an asset with assetTransfer txn
Checkpoint 4: 💯 Submit your answer
- After fixing the bug, push your code to your forked Github repo and make a PR to the original repo.
- Inside the PR include:
- What was the problem?
- How did you solve the problem?
- Screenshot of your terminal showing the result of running the deploy script.
Checkpoint 5: 🏆 Claim your certificate of completion NFT! 🎓
The Algorand Developer Relations team will review the submission and “approve” the PR by labeling it Approved
. Once it’s approved, we will share the magic link to claim your certificate of completion NFT in the comment of the PR!
The certificate of completion NFT is a verifiable credential minted on the GoPlausible platform that follows the W3C standard for certificates and OpenBadges standard for badges.
The certificate of completion NFT for Python challenges was designed by Maars, an artist & a dev in web3. Definitely follow his work! It’s awesome. 😆
🎉 Congratulations on completing the challenge Algodev!
Below is the default readme of the Python AlgoKit Template. Read to learn more about this template.
This project has been generated using AlgoKit. See below for default getting started instructions.
Setup
Pre-requisites
- Python 3.12 or later
- Docker (only required for LocalNet)
Initial setup
- Clone this repository locally
- Install pre-requisites:
- Make sure to have Docker installed and running on your machine.
- Install
AlgoKit
- Link: The recommended version is1.7.3
. Ensure you can executealgokit --version
and get1.7.1
or later. - Bootstrap your local environment; run
algokit bootstrap all
within this folder, which will:- Install
Poetry
- Link: The minimum required version is^1.7
. Ensure you can executepoetry -V
and get1.2
+ - Run
poetry install
in the root directory, which will set up a.venv
folder with a Python virtual environment and also install all Python dependencies - Copy
.env.template
to.env
- Install
- Run
algokit localnet start
to start a local Algorand network in Docker. If you are using VS Code launch configurations provided by the template, this will be done automatically for you.
- Open the project and start debugging / developing via:
- VS Code
- Open the repository root in VS Code
- Install recommended extensions
- Hit F5 (or whatever you have debug mapped to) and it should start running with breakpoint debugging.
Note If using Windows: Before running for the first time you will need to select the Python Interpreter.
- Open the command palette (Ctrl/Cmd + Shift + P)
- Search for
Python: Select Interpreter
- Select
./.venv/Scripts/python.exe
- JetBrains IDEs (please note, this setup is primarily optimized for PyCharm Community Edition)
- Open the repository root in the IDE
- It should automatically detect it’s a Poetry project and set up a Python interpreter and virtual environment.
- Hit Shift+F10|Ctrl+R (or whatever you have debug mapped to) and it should start running with breakpoint debugging. Please note, JetBrains IDEs on Windows have a known bug that in some cases may prevent executing shell scripts as pre-launch tasks, for workarounds refer to JetBrains forums.
- Other
- Open the repository root in your text editor of choice
- In a terminal run
poetry shell
- Run
python -m smart_contracts
through your debugger of choice
- VS Code
Subsequently
- If you update to the latest source code and there are new dependencies you will need to run
algokit bootstrap all
again - Follow step 3 above
For guidance on
smart_contracts
folder and adding new contracts to the project please see README on the respective folder.
Tools
This project makes use of Algorand Python to build Algorand smart contracts. The following tools are in use:
- Algorand - Layer 1 Blockchain; Developer portal, Why Algorand?
- AlgoKit - One-stop shop tool for developers building on the Algorand network; docs, intro tutorial
- Algorand Python - A semantically and syntactically compatible, typed Python language that works with standard Python tooling and allows you to express smart contracts (apps) and smart signatures (logic signatures) for deployment on the Algorand Virtual Machine (AVM); docs, examples
- AlgoKit Utils - A set of core Algorand utilities that make it easier to build solutions on Algorand.
- Poetry: Python packaging and dependency management. It has also been configured to have a productive dev experience out of the box in VS Code, see the .vscode folder.
Solution:
- What was the bug?
The error stated that the asset wasn’t opted in.
- How did you fix the bug?
According to the documentation, if a smart contract wishes to transfer an asset it holds or needs to opt into an asset this can be done with an asset transfer inner transaction. So I added the transaction to opt_in_to_asset.