Compiling Launching and Interacting with Your First Contract
In this chapter, you'll learn how to compile, launch, and interact with a basic smart contract on the Algorand blockchain. You'll set up essential tools, generate and fund accounts, deploy your contract, and make your first interaction using Python.
YouTube Tutorial
Compiling
Make sure you have Python installed, and then install algokit and algorand-python via Python's pip installer:
pip install algokit
pip install algorand-python
Use algokit compile py helloWorldContract.py
in the terminal line (assuming your contract is named helloWorldContract.py
) to generate your approval and clear TEAL files, as well as the ARC32 JSON for the contract. We will use these to launch and interact with the contract later.
helloWorldContract.py
from algopy import ARC4Contract, String
from algopy.arc4 import abimethod
class HelloWorldContract(ARC4Contract):
@abimethod
def hello(self, name: String) -> String:
return "Hello, " + name
Once you have your approval and clear TEAL files, as well as your ARC32 JSON, it's time to launch your contract!
Generating and Funding an Account
Generate and fund an account using the code below. Use the testnet dispenser at https://bank.testnet.algorand.network/.
from algosdk.account import generate_account
private_key, address = generate_account()
print(private_key, address)
Then, place your private key and the public testnet API and URL provided by nodely.io (as indicated in the tutorial) into your .env
file.
Note: Feel free to reuse this template when launching future contracts, but remember to adjust approval_teal_file_name
and clear_teal_file_name
variables, as well as the global
and local
state num_uints
and num_bytes
values for future contracts.
Setting Environment Variables (.env)
Set up environment variables in a .env
file. This includes the Algod token and server details, your private key, and the application ID of your contract:
# Algod Token is blank when using Nodely API
algod_token=
algod_server=https://testnet-api.4160.nodely.dev
# Private key for generated account
private_key=ENTER PRIVATE KEY FOR ACCOUNT HERE
# Application ID from contract generated by LaunchSmartContract.py
app_id=ENTER GENERATED APP ID HERE
Launching the Contract
import os
from algosdk.account import address_from_private_key
from algosdk.v2client.algod import AlgodClient
from base64 import b64decode
from algosdk.transaction import ApplicationCreateTxn, StateSchema, OnComplete, wait_for_confirmation, PaymentTxn
from algosdk.logic import get_application_address
from dotenv import load_dotenv
load_dotenv()
algod_token = os.getenv('algod_token')
algod_server = os.getenv('algod_server')
private_key = os.getenv('private_key')
address = address_from_private_key(private_key)
algod_client = AlgodClient(algod_token, algod_server)
path = Path(__file__).parent / './HelloWorldContract.arc32.json'
app_id = int(os.getenv('app_id'))
my_signer = AccountTransactionSigner(private_key)
params = algod_client.suggested_params()
application_client = ApplicationClient(
algod_client=algod_client,
app_spec=path,
app_id=app_id,
signer=my_signer,
sender=address,
suggested_params=params,
)
atc = AtomicTransactionComposer()
application_client.compose_call(atc, call_abi_method='hello', name='AlgoLearn Community')
result = atc.execute(algod_client, 2)
print(result.abi_results[0].tx_id)
print(result.abi_results[0].return_value)
Once your contract is launched, place your new app_id
into your .env
file, and try interacting with it using the application client methods above!