Examples for python in algov2 - General - Algorand
Examples for python in algov2
post by harish on Jul 13, 2021
I want compiled code examples for hash time lock, indexer, transaction fee, read and write transaction in python programming in algoversion2. Where can I get? Can anyone help me?
post by fabrice on Jul 13, 2021
Most tutorials are using algodv2.
If you find a tutorial that is using algodv1, let us know on this thread so that we can fix it.
To get started, there are many ways:
- Go over “Start Building” and “Explore Features” sections of 1. Workspace Setup - Algorand Developer Portal
- Read Getting Started on Algorand | Algorand Developer Portal
- If you are more hands-on and want a very step-by-step tutorial, do: GitHub - algorandfoundation/buildweb3
Specifically for:
transaction fee/read/write txs: 3. Your First Transaction - Algorand Developer Portal
indexer: Searching the Blockchain (Indexer V2) - Algorand Developer Portal
hash time lock seems still in v1.
post by harish on Jul 13, 2021
I want correctly working code for hash time lock, limit order in python. Where can I get it?
post by fabrice on Jul 13, 2021
While it is V1, Hash Time Lock Contract Template With Python | Algorand Developer Portal should still work nowadays as it does not use indexer v1 which is the main issue.
Let us know if there is an issue.
When turning it into production, you need to switch to v2.
post by harish on Jul 13, 2021
code:
Error:
What i have to change?
post by fabrice on Jul 13, 2021
PureStake does not support v1 anymore. Use algoexplorer api:
from algosdk import algod
algod_token = ""
algod_address = "https://testnet.algoexplorerapi.io"
headers = {"User-Agent": "DoYouLoveMe?"}
client = algod.AlgodClient(algod_token, algod_address, headers)
print(client.suggested_params())
PS: You can write code and output between triple backquotes ``` instead of snapshots. This makes search easier.
post by harish on Jul 13, 2021
what i have to put in algod_token variable and header variable
post by fabrice on Jul 13, 2021
You can directly use the code I wrote. No need for a personal token.
post by harish on Jul 13, 2021
is I put DoYouLoveMe in useragent?
post by fabrice on Jul 13, 2021
Yes The goal is to bypass CloudFlare’s issues: Requests need to merge headers · Issue #169 · algorand/py-algorand-sdk · GitHub
post by harish on Jul 13, 2021
Error:
Traceback (most recent call last):
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\site-packages\algosdk\algod.py", line 75, in algod_request
resp = urlopen(req)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 523, in open
response = meth(req, response)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 632, in http_response
response = self.parent.error(
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 561, in error
return self._call_chain(*args)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 494, in _call_chain
result = func(req, response)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 641, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
post by harish on Jul 13, 2021
This is the hash lock extended program which makes above error. Then first set of hash time lock program is compiled successfully
from algosdk import algod, transaction, template
# Create an algod client
algod_token = ""
algod_address = "https://testnet.algoexplorerapi.io"
headers = {"User-Agent": "DoYouLoveMe?"}
client = algod.AlgodClient(algod_token, algod_address, headers)
# Get suggested parameters from the network
tx_params = client.suggested_params()
# Specify TLHC-related template params.
template_data = {
"owner": "J52X5I4TY5MIWDXGPRVD7QCRYURRRIYFY4QBGFNEM7DKEUP5TWFOQPD3SY",
"receiver": "4XKLRNORGC2W42OQRSEX2DSH3MSFFWPRIVZWJE2R4JN3M4E3IJZUQCIPJM",
"hash_function": "sha256",
"hash_image": "QzYhq9JlYbn2QdOMrhyxVlNtNjeyvyJc/I8d8VAGfGc=",
"expiry_round": 5000000,
"max_fee": 2000
}
## Inject template data into HTLC template
c = template.HTLC(**template_data)
# Get the address for the escrow account associated with the logic
addr = c.get_address()
print("Escrow Address: {}\n".format(addr))
# Retrieve the program bytes
program = c.get_program()
# Get the program and parameters and use them to create an lsig
# For the contract account to be used in a transaction
# In this example 'hero wisdom green split loop element vote belt'
# hashed with sha256 will produce our image hash
# This is the passcode for the HTLC
args = [\
"hero wisdom green split loop element vote belt".encode()\
]
# Add the program bytes and args to a LogicSig object
lsig = transaction.LogicSig(program, args)
# Before submitting this transaction, you must first make sure the escrow
# account is sufficiently funded. Once it is funded, create and submit
# transactions from the escrow account like the transaction below.
# Transaction data
tx_data = {
"sender": addr,
"receiver": "4XKLRNORGC2W42OQRSEX2DSH3MSFFWPRIVZWJE2R4JN3M4E3IJZUQCIPJM",
"amt": 0,
"close_remainder_to": "42NJMHTPFVPXVSDGA6JGKUV6TARV5UZTMPFIREMLXHETRKIVW34QFSDFRE",
"fee": 1000,
"flat_fee": True,
"first": tx_params.get('lastRound'),
"last": tx_params.get('lastRound') + 1000,
"gen": tx_params.get('genesisID'),
"gh": tx_params.get('genesishashb64')
}
# Instantiate a payment transaction type
txn = transaction.PaymentTxn(**tx_data)
# Instantiate a LogicSigTransaction with the payment txn and the logicsig
logicsig_txn = transaction.LogicSigTransaction(txn, lsig)
# Send the transaction to the network.
txid = client.send_transaction(logicsig_txn, headers={'content-type': 'application/x-binary'})
print("Transaction ID: {}".format(txid))
post by fabrice on Jul 13, 2021
To debug this kind of issues, you want to write the transaction to file, by writing:
transaction.write_to_file([logicsig_txn], "htlc.tx")
just before
# Send the transaction to the network.
And then you can debug using the Algorand software in bash:
$ goal clerk dryrun -t htlc.tx
You see that the issue is:
121 txn Receiver => (e5d4b8b5d130b56e69d08c897d0e47db2452d9f14573649351e25bb6709b4273)
123 global ZeroAddress => (0000000000000000000000000000000000000000000000000000000000000000)
125 == => (0 0x0)
which means the smart contract expect the Receiver to be the ZeroAddress.
The issue is that you changed the wrong address.
You should have changed close_remainder_to instead of receiver.
receiver should be AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ, the zero address.
Concretely, you want:
tx_data = {
"sender": addr,
"receiver": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ",
"amt": 0,
"close_remainder_to": "4XKLRNORGC2W42OQRSEX2DSH3MSFFWPRIVZWJE2R4JN3M4E3IJZUQCIPJM",
"fee": 1000,
"flat_fee": True,
"first": tx_params.get('lastRound'),
"last": tx_params.get('lastRound') + 1000,
"gen": tx_params.get('genesisID'),
"gh": tx_params.get('genesishashb64')
}
You’ll get another error because the escrow account is not funded. You need to fund it using the dispenser: https://bank.testnet.algorand.network/
post by harish on Jul 14, 2021
Thank you. It works. Is limit order program which is in the link: LimitOrder Contract with Python | Algorand Developer Portal work properly? Can I use the code link code? Is it work?
post by fabrice on Jul 14, 2021
Should work too.
Essentially v1 examples should work unless they involve indexer v1, that is unless they try to search for old transactions.