Generating Funding and Interacting with Your First Contract
Video Walkthrough
Watch the tutorial video to understand the steps in deploying your first Algorand smart contract:
Generating an Account
from algosdk.account import generate_account
private_key, address = generate_account()
print(private_key, address)
Funding the Account
To fund the account, visit the Algorand testnet dispenser: https://bank.testnet.algorand.network/
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
Writing the Hello World Contract
from algopy import ARC4Contract, String
from algopy.arc4 import abimethod
class HelloWorldContract(ARC4Contract):
@abimethod
def hello(self, name: String) -> String:
return "Hello, " + name
Launching the Contract
Note: Make sure the contract name matches the approval and clear TEAL file names.
If your class is defined as:
class HelloWorldContract(ARC4Contract):
Your approval and clear TEAL file names in LaunchSmartContract.py
should be:
approval_teal_file_name = 'HelloWorldContract.approval.teal'
clear_teal_file_name = 'HelloWorldContract.clear.teal'
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 algosdk.util import microalgos_to_algos, algos_to_microalgos
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)
approval_teal_file_name = 'HelloWorldContract.approval.teal'
clear_teal_file_name = 'HelloWorldContract.clear.teal'
with open(f'./{approval_teal_file_name}', 'r') as f:
approval_teal_source = f.read()
with open(f'./{clear_teal_file_name}', 'r') as f:
clear_teal_source = f.read()
approval_result = algod_client.compile(approval_teal_source)
approval_program = b64decode(approval_result['result'])
clear_result = algod_client.compile(clear_teal_source)
clear_program = b64decode(clear_result['result'])
global_schema = StateSchema(num_uints=0, num_byte_slices=0)
local_schema = StateSchema(num_uints=0, num_byte_slices=0)
params = algod_client.suggested_params()
txn = ApplicationCreateTxn(
sender=address,
sp=params,
on_complete=OnComplete.NoOpOC,
approval_program=approval_program,
clear_program=clear_program,
global_schema=global_schema,
local_schema=local_schema,
)
signed_txn = txn.sign(private_key)
tx_id = algod_client.send_transaction(signed_txn)
print(f'Contract Deployed Tx ID: {tx_id}')
wait_for_confirmation(algod_client, tx_id)
tx_info = algod_client.pending_transaction_info(tx_id)
app_id = tx_info['application-index']
print(f'App ID: {app_id}')
app_address = get_application_address(app_id)
print(f'App Address: {app_address}')
amount_to_send = algos_to_microalgos(0.1)
payment_txn = PaymentTxn(
sender=address,
sp=params,
receiver=app_address,
amt=amount_to_send
)
signed_payment_tx = payment_txn.sign(private_key)
tx_id = algod_client.send_transaction(signed_payment_tx)
print(f'Application funded with 0.1 Algo for MBR: {tx_id}')
Interacting with the Hello World Contract
import os
from algosdk.account import address_from_private_key
from algosdk.v2client.algod import AlgodClient
from algosdk.atomic_transaction_composer import AtomicTransactionComposer, AccountTransactionSigner
from algokit_utils import ApplicationClient
from pathlib import Path
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)