Skip to content

Commit

Permalink
examples: Migrate v1 algod usage to v2 algod (#403)
Browse files Browse the repository at this point in the history
* Update examples to use v2client

* Fix token constants to sandbox defaults

* formatting
  • Loading branch information
algochoi committed Dec 2, 2022
1 parent c3f347b commit af81001
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Next, create a wallet and an account:

Visit the [Algorand dispenser](https://bank.testnet.algorand.network/) and enter the account address to fund your account.

Next, in [tokens.py](https://github.com/algorand/py-algorand-sdk/blob/master/examples/tokens.py), either update the tokens and addresses, or provide a path to the data directory.
Next, in [tokens.py](https://github.com/algorand/py-algorand-sdk/blob/master/examples/tokens.py), either update the tokens and addresses, or provide a path to the data directory. Alternatively, `tokens.py` also defaults to the sandbox harness configurations for algod and kmd, which can be brought up by running `make harness`.

You're now ready to run example.py!

Expand Down
4 changes: 2 additions & 2 deletions examples/custom_header_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# key, instead of a string, as the token.

import tokens
from algosdk import algod
from algosdk.v2client import algod

headers = {
"X-API-Key": "#######",
Expand Down Expand Up @@ -37,7 +37,7 @@ def main():
)

# Retrieve latest block information
last_round = algod_client.status().get("lastRound")
last_round = algod_client.status().get("last-round")
print("####################")
block = algod_client.block_info(last_round)
print(block)
Expand Down
6 changes: 4 additions & 2 deletions examples/log_sig_example.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Example: creating a LogicSig transaction signed by a program that never approves the transfer.

import tokens
from algosdk import algod, account
from algosdk import account
from algosdk.v2client import algod
from algosdk.future import transaction

program = b"\x01\x20\x01\x00\x22" # int 0
lsig = transaction.LogicSigAccount(program)
sender = lsig.address()
receiver = account.generate_account()
_, receiver = account.generate_account()

# create an algod client
acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address)
Expand All @@ -25,4 +26,5 @@
assert lstx.verify()

# send them over network
# Logicsig will reject the transaction
acl.send_transaction(lstx)
6 changes: 4 additions & 2 deletions examples/multisig_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Example: manipulating multisig transactions

import tokens
from algosdk import account, algod, encoding

from algosdk import account, encoding
from algosdk.future import transaction
from algosdk.v2client import algod

# generate three accounts
private_key_1, account_1 = account.generate_account()
Expand All @@ -16,7 +18,7 @@

# get suggested parameters
acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address)
suggested_params = acl.suggested_params_as_object()
suggested_params = acl.suggested_params()

# create a transaction
sender = msig.address()
Expand Down
9 changes: 6 additions & 3 deletions examples/notefield_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
# with an auction bid. Note that you can put any bytes you want in the "note"
# field; you don't have to use the NoteField object.

import base64

import tokens
from algosdk import algod, mnemonic, account, auction, constants, encoding

from algosdk import account, auction, constants, encoding
from algosdk.future import transaction
import base64
from algosdk.v2client import algod

acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address)

# generate an account
private_key, public_key = account.generate_account()

# get suggested parameters
sp = acl.suggested_params_as_object()
sp = acl.suggested_params()

# Set other parameters
amount = 100000
Expand Down
8 changes: 5 additions & 3 deletions examples/rekey_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Example: rekeying

from algosdk import account, algod
from algosdk.future import transaction
import tokens

from algosdk import account
from algosdk.future import transaction
from algosdk.v2client import algod

# this should be the current account
sender_private_key, sender = account.generate_account()
rekey_private_key, rekey_address = account.generate_account()
Expand All @@ -12,7 +14,7 @@

# get suggested parameters
acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address)
suggested_params = acl.suggested_params_as_object()
suggested_params = acl.suggested_params()

# To rekey an account to a new address, add the `rekey_to` argument to creation.
# After sending this rekeying transaction, every transaction needs to be signed by the private key of the new address
Expand Down
18 changes: 10 additions & 8 deletions examples/tokens.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
# examples helper file
# Examples helper file

from os import listdir
from os.path import expanduser

home = expanduser("~")

# change these after starting the node and kmd
# These values are initialized for the SDK sandbox harness.
# You can bring the harness up by running `make harness`.
#
# If you are using your own node installation, change these after starting the node and kmd.
# algod info is in the algod.net and algod.token files in the data directory
# kmd info is in the kmd.net and kmd.token files in the kmd directory in data
kmd_token = "a" * 64
kmd_address = "http://localhost:59999"

kmd_token = ""
kmd_address = ""

algod_token = ""
algod_address = ""
algod_token = "a" * 64
algod_address = "http://localhost:60000"

# you can also get tokens and addresses automatically
get_automatically = True
get_automatically = False

# path to the data directory
data_dir_path = home + "/node/network/Node"
Expand Down
6 changes: 4 additions & 2 deletions examples/transaction_group_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Example: working with transaction groups

import tokens
from algosdk import algod, kmd, account

from algosdk import account, kmd
from algosdk.future import transaction
from algosdk.v2client import algod

# generate accounts
private_key_sender, sender = account.generate_account()
Expand All @@ -13,7 +15,7 @@
kcl = kmd.KMDClient(tokens.kmd_token, tokens.kmd_address)

# get suggested parameters
sp = acl.suggested_params_as_object()
sp = acl.suggested_params()

# create a transaction
amount = 10000
Expand Down

0 comments on commit af81001

Please sign in to comment.