<jupyter_start><jupyter_text>Tokens on the Algorand Blockchain 04.1 Winter School on Smart Contracts <NAME> (<EMAIL>)2021-11-28  SetupStarting with chapter 3.5, the lines below will always automatically load ...* The functions in `algo_util.py`* The accounts MyAlgo, Alice and Bob* The Purestake credentials<jupyter_code># Loading shared code and credentials
import sys, os
codepath = '..'+os.path.sep+'..'+os.path.sep+'sharedCode'
sys.path.append(codepath)
from algo_util import *
cred = load_credentials()

# Shortcuts to directly access the main accounts
MyAlgo  = cred['MyAlgo']
Alice   = cred['Alice']
Bob     = cred['Bob']
Charlie = cred['Charlie']
Dina    = cred['Dina']
from algosdk import account, mnemonic
from algosdk.v2client import algod
from algosdk.future.transaction import PaymentTxn
from algosdk.future.transaction import AssetConfigTxn, AssetTransferTxn, AssetFreezeTxn
import algosdk.error
import json
# Initialize the algod client (Testnet or Mainnet)
algod_client = algod.AlgodClient(algod_token='', algod_address=cred['algod_test'], headers=cred['purestake_token'])<jupyter_output><empty_output><jupyter_text>ASA = Algorand Standard Assets* All tokens (except the native ALGOs) on the Algorand blockchain are ASAs* Every ASA has a unique index number. Example: USDC* USDC is the second largest USD stablecoin* See https://coinmarketcap.com/view/stablecoin/* Unlike the largest one (Tether), it is US-regulated and audited* USD on the Testnet: https://testnet.algoexplorer.io/asset/10458941* USDC on the Mainnet: https://algoexplorer.io/asset/31566704  Step 0: Try to obtain free (test) USDCTry got some free (test) USDC for your main account `MyAlgo`<jupyter_code>print('https://dispenser.testnet.aws.algodev.network/?account='+MyAlgo['public'])<jupyter_output><empty_output><jupyter_text>**>>This does not work** at the moment, because MyAlgo did not *opt-in*  Step 1: Opt-in Why do we need an opt-in?* To avoid Spam on the blockchain, every account needs to *opt into* each ASA it wants to hold.* Same valid for Smart Contracts. The opt-in transaction* Send 0 units of the ASA to yourself* Need to specify index of the ASA – for (test) USDc this is 10458941**New** transaction type `AssetTransferTxn`<jupyter_code># Step 1a: MyAlgo opts into USDC. 
#          He sends 0 USD to himself using an AssetTransferTxn

sp = algod_client.suggested_params()             # Suggested params
usdc_index = 10458941                            # <-- get this from the issuer of the coin
amt = int(0)                                     # <-- Send 0 coins

txn = AssetTransferTxn(
    sender = MyAlgo['public'],                   # <-- MyAlgo sends ...
    sp=sp,
    receiver=MyAlgo['public'],                   # <-- ... to himself
    amt=amt,                                     # <-- Zero coins
    index=usdc_index                             # <-- Specify which ASA ... here it is USDC
    )                               
# Step 1b+c: sign and send
stxn = txn.sign(MyAlgo['private'])             # Sign
txid = algod_client.send_transaction(stxn)     # Send
print(txid)
# Step 1d: Wait for confirmation
txinfo = wait_for_confirmation(algod_client,txid)<jupyter_output><empty_output><jupyter_text>Step 2: Get some USDcNow that MyAlgo has opted in, he can ask for some USDC<jupyter_code>print('https://dispenser.testnet.aws.algodev.network/?account='+MyAlgo['public'])<jupyter_output><empty_output><jupyter_text>Step 3: Check Balance on the WebClick into the triangle next to *Balances*<jupyter_code>print('https://testnet.algoexplorer.io/address/'+MyAlgo['public'])<jupyter_output><empty_output><jupyter_text>Step 3b: Get the balance in Python<jupyter_code>algod_client.account_info(MyAlgo['public'])
# Just the assets
algod_client.account_info(MyAlgo['public'])['assets']<jupyter_output><empty_output><jupyter_text>ASA bookkeeping* Have a close look at MyAlgo's holdings ...* ASA are divisible* The creator can decide the number of decimals* Example: USDC has 6 decimals, see https://testnet.algoexplorer.io/asset/10458941 All amounts are specified in the *small unit** Holdings* Transactions* Smart contracts >> Different ASA can have different numbers of decimals  Denoting 1 USDC* USDC has 6 Decimals* Hence 1 USDC must be denoted as   $1 \cdot 10^6$   micro-USDC<jupyter_code># Three ways to write "1 Million" in Python
amt_1 = 1000000
amt_2 = 1_000_000       # Python ignores the underscore in numbers
amt_3 = int(1E6)        # Need int(), because 1E6 produces a float number<jupyter_output><empty_output><jupyter_text>Transfer coinsIn this section we will transfer coins ...* To Alice using the Wallet app* Using Python to Charlie  Manual transfer to Alice* Open the Algorand Wallet app* Alice has to opt-in  * Tap `+ Add new asset`, then `All` (not "Only verified")  * Search for name or asset ID* Now you can send 10 USDC via copy-paste or via QR code to your neighbour  Exercise: Python transfer to Bob * Transfer 10 USDC from `MyAlgo` to `Bob`* We already have all the elemens to do this* Just do not forget to ...<jupyter_code># Your Python code goes here ...



# More Python code may go here ...<jupyter_output><empty_output><jupyter_text>Then check Bob's balance on the Web<jupyter_code>print('https://testnet.algoexplorer.io/address/'+Bob['public'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09998117115420825, 'fraction_numerical': 0.018828845791752966, 'mean_word_length': 4.122468659594985, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 43, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '624338', 'license': 'MIT', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1657, 'n_words': 554, 'path': 'ClassMaterial/04 - Tokens/04 code/04.1_WSC_Tokens.ipynb', 'repo_name': 'gruberpeter/smartcontractscourse', 'size': '5329'}