Skip to content

Commit

Permalink
Documentation: Adding examples to be pulled in to docs (#441)
Browse files Browse the repository at this point in the history
* tmp

* adding atomic transfers examples

* use sandbox to fetch accounts

* add examples for atc

* adding asa examples

* adding some account examples

* Adding codec examples

* fmt

* improve kmd examples

* adding tx fee override

* adding more examples

* tmp

* adding app examples

* remove check for creator on noop

* Add recover account

* add clear state

* adding more lsig examples

* fmt and asset config example

* fmt with black==23.1.0

---------

Co-authored-by: Barbara Poon <[email protected]>
Co-authored-by: John Lee <[email protected]>
Co-authored-by: Jack Smith <[email protected]>
Co-authored-by: Lucky Baar <[email protected]>
Co-authored-by: Arthur Kepler <[email protected]>
  • Loading branch information
6 people authored Mar 16, 2023
1 parent e38f581 commit 8ab33dd
Show file tree
Hide file tree
Showing 21 changed files with 1,704 additions and 0 deletions.
96 changes: 96 additions & 0 deletions _examples/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from utils import get_accounts, get_algod_client
from algosdk import account, mnemonic
from algosdk import transaction

# example: ACCOUNT_GENERATE
private_key, address = account.generate_account()
print(f"address: {address}")
print(f"private key: {private_key}")
print(f"mnemonic: {mnemonic.from_private_key(private_key)}")
# example: ACCOUNT_GENERATE

# example: ACCOUNT_RECOVER_MNEMONIC
mn = "cost piano sample enough south bar diet garden nasty mystery mesh sadness convince bacon best patch surround protect drum actress entire vacuum begin abandon hair"
pk = mnemonic.to_private_key(mn)
print(f"Base64 encoded private key: {pk}")
addr = account.address_from_private_key(pk)
print(f"Address: {addr}")
# example: ACCOUNT_RECOVER_MNEMONIC

accts = get_accounts()
account_1 = accts.pop()
account_2 = accts.pop()
account_3 = accts.pop()

# example: MULTISIG_CREATE
version = 1 # multisig version
threshold = 2 # how many signatures are necessary
# create a Multisig given the set of participants and threshold
msig = transaction.Multisig(
version,
threshold,
[account_1.address, account_2.address, account_3.address],
)
print("Multisig Address: ", msig.address())
# example: MULTISIG_CREATE

algod_client = get_algod_client()
sp = algod_client.suggested_params()
ptxn = transaction.PaymentTxn(
account_1.address, sp, msig.address(), int(1e7)
).sign(account_1.private_key)
txid = algod_client.send_transaction(ptxn)
transaction.wait_for_confirmation(algod_client, txid, 4)
# dont check response, assume it worked

# example: MULTISIG_SIGN
msig_pay = transaction.PaymentTxn(
msig.address(), sp, account_1.address, int(1e5)
)
msig_txn = transaction.MultisigTransaction(msig_pay, msig)
msig_txn.sign(account_2.private_key)
msig_txn.sign(account_3.private_key)
txid = algod_client.send_transaction(msig_txn)
result = transaction.wait_for_confirmation(algod_client, txid, 4)
print(
f"Payment made from msig account confirmed in round {result['confirmed-round']}"
)
# example: MULTISIG_SIGN


# example: ACCOUNT_REKEY
# Any kind of transaction can contain a rekey
rekey_txn = transaction.PaymentTxn(
account_1.address, sp, account_1.address, 0, rekey_to=account_2.address
)
signed_rekey = rekey_txn.sign(account_1.private_key)
txid = algod_client.send_transaction(signed_rekey)
result = transaction.wait_for_confirmation(algod_client, txid, 4)
print(f"rekey transaction confirmed in round {result['confirmed-round']}")

# Now we should get an error if we try to submit a transaction
# signed with account_1s private key
expect_err_txn = transaction.PaymentTxn(
account_1.address, sp, account_1.address, 0
)
signed_expect_err_txn = expect_err_txn.sign(account_1.private_key)
try:
txid = algod_client.send_transaction(signed_expect_err_txn)
except Exception as e:
print("Expected error: ", e)

# But its fine if we sign it with the account we rekeyed to
signed_expect_err_txn = expect_err_txn.sign(account_2.private_key)
txid = algod_client.send_transaction(signed_expect_err_txn)
result = transaction.wait_for_confirmation(algod_client, txid, 4)
print(f"transaction confirmed in round {result['confirmed-round']}")

# rekey account1 back to itself so we can actually use it later
rekey_txn = transaction.PaymentTxn(
account_1.address, sp, account_1.address, 0, rekey_to=account_1.address
)
signed_rekey = rekey_txn.sign(account_2.private_key)
txid = algod_client.send_transaction(signed_rekey)
result = transaction.wait_for_confirmation(algod_client, txid, 4)
print(f"rekey transaction confirmed in round {result['confirmed-round']}")
# example: ACCOUNT_REKEY
100 changes: 100 additions & 0 deletions _examples/application/approval.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#pragma version 4
// Handle each possible OnCompletion type. We don't have to worry about
// handling ClearState, because the ClearStateProgram will execute in that
// case, not the ApprovalProgram.
txn ApplicationID
int 0
==
bnz handle_approve

txn OnCompletion
int NoOp
==
bnz handle_noop

txn OnCompletion
int OptIn
==
bnz handle_approve

txn OnCompletion
int CloseOut
==
bnz handle_closeout

txn OnCompletion
int UpdateApplication
==
bnz handle_updateapp

txn OnCompletion
int DeleteApplication
==
bnz handle_deleteapp

// Unexpected OnCompletion value. Should be unreachable.
err

handle_noop:
// Handle NoOp

// read global state
byte "counter"
dup
app_global_get

// increment the value
int 1
+

// store to scratch space
dup
store 0

// update global state
app_global_put

// read local state for sender
int 0
byte "counter"
app_local_get

// increment the value
int 1
+
store 1

// update local state for sender
int 0
byte "counter"
load 1
app_local_put

// load return value as approval
load 0
return


handle_closeout:
// Handle CloseOut
//approval
int 1
return

handle_deleteapp:
// Check for creator
global CreatorAddress
txn Sender
==
return

handle_updateapp:
// Check for creator
global CreatorAddress
txn Sender
==
return

handle_approve:
int 1
return
107 changes: 107 additions & 0 deletions _examples/application/approval_refactored.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#pragma version 4
// Handle each possible OnCompletion type. We don't have to worry about
// handling ClearState, because the ClearStateProgram will execute in that
// case, not the ApprovalProgram.

txn ApplicationID
int 0
==
bnz handle_approve

txn OnCompletion
int NoOp
==
bnz handle_noop

txn OnCompletion
int OptIn
==
bnz handle_approve

txn OnCompletion
int CloseOut
==
bnz handle_closeout

txn OnCompletion
int UpdateApplication
==
bnz handle_updateapp

txn OnCompletion
int DeleteApplication
==
bnz handle_deleteapp

// Unexpected OnCompletion value. Should be unreachable.
err

handle_noop:
// Handle NoOp

// read global state
byte "counter"
dup
app_global_get

// increment the value
int 1
+

// store to scratch space
dup
store 0

// update global state
app_global_put

// read local state for sender
int 0
byte "counter"
app_local_get

// increment the value
int 1
+
store 1

// update local state for sender
// update "counter"
int 0
byte "counter"
load 1
app_local_put

// update "timestamp"
int 0
byte "timestamp"
txn ApplicationArgs 0
app_local_put

// load return value as approval
load 0
return

handle_closeout:
// Handle CloseOut
//approval
int 1
return

handle_deleteapp:
// Check for creator
global CreatorAddress
txn Sender
==
return

handle_updateapp:
// Check for creator
global CreatorAddress
txn Sender
==
return

handle_approve:
int 1
return
3 changes: 3 additions & 0 deletions _examples/application/clear.teal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma version 4
int 1
return
Loading

0 comments on commit 8ab33dd

Please sign in to comment.