This forum is a read-only archive. Posting, replying and signing in are disabled.

Problem creating a note field

liz

Can you share more of the code that led to this error? Also are you using your own node or are you connected through an API service? Also which version of the SDK are you using? Run pip3 freeze | grep py-algorand-sdk.

maryevenunye

import json
import time
import base64
import json
from algosdk import algod, transaction
from algosdk import mnemonic

utility for waiting on a transaction confirmation

def wait_for_confirmation( algod_client, txid ):
last_round = algod_client.status().get(‘lastRound’)
while True:
txinfo = algod_client.pending_transaction_info(txid)
if txinfo.get(‘round’) and txinfo.get(‘round’) > 0:
print(“Transaction {} confirmed in round {}.”.format(txid, txinfo.get(‘round’)))
break
else:
print(“Waiting for confirmation…”)
last_round += 1
algod_client.status_after_block(last_round)

def send_note():
algod_address = “https://testnet-algorand.api.purestake.io/ps1”
algod_token = “”
headers = {
“X-API_Key” :“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,
}
algod_client = algod.AlgodClient(algod_token, algod_address, headers)

passphrase = "marriage problem knife entry loyal age bacon square mother easily push arena spirit century company defy afford relief cannon click pig uphold image abstract submit"

private_key = mnemonic.to_private_key(passphrase)
my_address = mnemonic.to_public_key(passphrase)
print("My address: {}".format(my_address))

params = algod_client.suggested_params()
note = ‘{“firstName”:“John”, “LastName”:“Doe”}’.encode()
receiver = “GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A”

data = {
    "sender": my_address,
    "receiver": receiver,
    "fee": params.get('minFee'),
    "flat_fee": True,
    "amt": 0,
    "first": params.get('lastRound'),
    "last": params.get('lastRound') + 1000,
    "note": note,
    "gen": params.get('genesisID'),
    "gh": params.get('genesishashb64')
}

txn = transaction.PaymentTxn(**data)
signed_txn = txn.sign(private_key)
txid = signed_txn.transaction.get_txid()
print("Signed transaction with txID: {}".format(txid))

algod_client.send_transaction(signed_txn)

# wait for confirmation
wait_for_confirmation( algod_client, txid) 

# Read the transction
try:
    confirmed_txn = algod_client.transaction_info(my_address, txid)
except Exception as err:
    print(err)
print("Transaction information: {}".format(json.dumps(confirmed_txn, indent=4)))
print("Decoded note: {}".format(base64.b64decode(confirmed_txn.get('noteb64')).decode()))
person_dict = json.loads(base64.b64decode(confirmed_txn.get('noteb64')).decode())
print(person_dict['firstName'])

I am connected through an API service

pip3 freeze | grep py-algorand-sdk
py-algorand-sdk==1.2.1

liz

Because it is the API service, try adding the content-type header to the send code:

...
algod_client.send_transaction(signed_txn, headers={'content-type': 'application/x-binary'})
...

This is documented in the info box here but it is easy to miss. This will be updated in a future release so you don’t need to specify this header for API services. Here is the related PR to follow.

See if that works.

maryevenunye
import json
import time
import base64
import json
from algosdk import algod, transaction
from algosdk import mnemonic


# utility for waiting on a transaction confirmation
def wait_for_confirmation( algod_client, txid ):
    last_round = algod_client.status().get('lastRound')
    while True:
        txinfo = algod_client.pending_transaction_info(txid)
        if txinfo.get('round') and txinfo.get('round') > 0:
           print("Transaction {} confirmed in round {}.".format(txid, txinfo.get('round')))
           break
        else:
           print("Waiting for confirmation...")
           last_round += 1
           algod_client.status_after_block(last_round)

def send_note():
    algod_address = "https://testnet-algorand.api.purestake.io/ps1"
    algod_token = ""
    headers = {
            "X-API_Key" :"4v78BEOf109w7jJAQgpCL3nNM8jJ4DIQ41LrHdta",
            }
    algod_client = algod.AlgodClient(algod_token, algod_address, headers)


    passphrase ="marriage problem knife entry loyal age bacon square mother easily push arena spirit century company defy afford relief cannon click pig uphold image abstract submit"

    private_key = mnemonic.to_private_key(passphrase)
    my_address = mnemonic.to_public_key(passphrase)
    print("My address: {}".format(my_address))
params = algod_client.suggested_params()
    note = '{"firstName":"John", "LastName":"Doe"}'.encode()
    receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A"

    data = {
        "sender": my_address,
        "receiver": receiver,
        "fee": params.get('minFee'),
        "flat_fee": True,
        "amt": 0,
        "first": params.get('lastRound'),
        "last": params.get('lastRound') + 1000,
        "note": note,
        "gen": params.get('genesisID'),
        "gh": params.get('genesishashb64')
    }

    txn = transaction.PaymentTxn(**data)
    signed_txn = txn.sign(private_key)
    txid = signed_txn.transaction.get_txid()
    print("Signed transaction with txID: {}".format(txid))

    #algod_client.send_transaction(signed_txn)
    algod_client.send_transaction(signed_txn, headers={'content-type': 'application/x-binary'})


    # wait for confirmation
    wait_for_confirmation( algod_client, txid)

    # Read the transction
    try:
        confirmed_txn = algod_client.transaction_info(my_address, txid)
    except Exception as err:
        print(err)
    print("Transaction information: {}".format(json.dumps(confirmed_txn, indent=4)))
 print("Decoded note: {}".format(base64.b64decode(confirmed_txn.get('noteb64')).decode()))
    person_dict = json.loads(base64.b64decode(confirmed_txn.get('noteb64')).decode())
    print(person_dict['firstName']) 
send_note()
Tim

In here headers = { "X-API_Key": "4v78BEOf109w7jJAQgpCL3nNM8jJ4DIQ41LrHdta", }

You are using an underscore instead of a dash, it should be “X-API-Key”

ddev

Hi, I faced this kind of error. It is a connection error. Firstly, you want to check you connection since you are using a third party service. Ensure that the API secret key is correct, the algod_address argument is supplied correctly and the header as well present.

ddev
↩ in reply to Tim

Hi Time, I am getting this error again. I am trying to run a multisig transaction and i had used the
from algodsdk.future import transaction
it never work.

I tried
from algodsdk import transaction
which I used for sending a single transaction.
it also didn’t work.

This error I got

Traceback (most recent call last):
  File "c:/Users/bobman/Documents/pythonTutorial/multiSig.py", line 51, in <module>
    trxn = txnfield(savingAlcAddr, spendAccount, 401000)
  File "c:/Users/bobman/Documents/pythonTutorial/multiSig.py", line 28, in txnfield
    return transaction.PaymentTxn(**txn)
TypeError: __init__() got an unexpected keyword argument 'fee'
PS C:\Users\bobman\Documents\pythonTutorial>```
I specified the transaction field as 

```txn = {

        "sender": my_address,

        "receiver": receiver,

        "fee": params.get('minFee'),

        "flat_fee": True,

        "amt": amount,

        "first": params.get('lastRound'),

        "last": params.get('lastRound') + 1000,

        "note": note,

        "gen": params.get('genesisID'),

        "gh": params.get('genesishashb64')

    }

parsed it here

return transaction.PaymentTxn(**txn)

prepares the transactio
multsig = transaction.Multisig(vers, req, [hisAddress, wifeAddress])

multsig = transaction.Multisig(vers, req, [hisAddress, wifeAddress])

multisigtxn.sign(wifePrivateKey)
multisigtxn.sign(hisPrivateKey)
getConnected.send_transaction(multisigtxn, headers={'content-type': 'application/x-binary'})```
ddev

I really don’t know how to do that. But I’ll try figure it out. Thanks a bunch