0

I am using one command tangle. The tangle is running on Ubuntu machine. From the list of APIs given for IRI node, I'm trying to broadcast a transaction. For this, the first step is to get getTransactionsToApprove which returns trunkTransaction and branchTransaction and I have to assign that value to attachToTangle API. I am getting the correct output from getTransactionsToApprove API:

{'trunkTransaction': 'NTCYZTAJTVJIAQW9JXUJCACYPHNMESWGWTANQQZSLGHQTKWGDOYHAHBETUXKNAMMWYDTILDWLPQTOX999', 'branchTransaction': 'NTCYZTAJTVJIAQW9JXUJCACYPHNMESWGWTANQQZSLGHQTKWGDOYHAHBETUXKNAMMWYDTILDWLPQTOX999', 'duration': 2}

The problem is when I am assigning those values to attachToTangle API, I'm getting this error:

HTTP Error 500: Internal Server Error

My code is:

import urllib.request
import urllib.parse
import json
import requests


trytes = "hello world!"
url="http://mydnsname:14265"

headers = {
    'Content-Type': 'application/json',
    "Accept": "application/json",
    'X-IOTA-API-Version': '1'
}

#get transactions
command = {
   "command": "getTransactionsToApprove",
   "depth": 4
}

data= json.dumps(command).encode("utf-8")
request = urllib.request.Request(url, data, headers)
try:
    returnData = urllib.request.urlopen(request).read()
    jsonData = json.loads(returnData)
    print (jsonData)
except urllib.error.URLError as e: 
    ResponseData = e.reason
    print (e)

#attach to tangle
command = {
   "command": "attachToTangle", 
"trunkTransaction": jsonData['trunkTransaction'],
"branchTransaction": jsonData['branchTransaction'],
"minWeightMagnitude": 9,
"trytes": trytes
}

data= json.dumps(command).encode("utf-8") 
request = urllib.request.Request(url, data, headers)
try:
    returnData = urllib.request.urlopen(request).read()
    jsonData = json.loads(returnData)
    print (jsonData)
except urllib.error.URLError as e: 
    ResponseData = e.reason
    print (e)

Any help is much appreciated.

7
  • 1
    Your trytes variable has to be a whole transaction encoded as trytes (so not only the text payload, but also address, amount, bundle hash etc...). Just passing random ASCII characters as trytes is not going to work.
    – mihi
    Apr 25, 2020 at 13:24
  • 1
    Removed pyota tag, as this code is not using the PyOTA library (github.com/iotaledger/iota.py) — although I recommend you give it a try; it might make things easier 😺 Apr 25, 2020 at 20:05
  • @mihi that means the entire bundle from "getTransactions" has to be converted into trytes?
    – degr8sid
    Apr 26, 2020 at 11:34
  • 1
    I'd suggest using some client library (like PyOTA) for building your transactions and converting them to trytes. You cannot get your transactions from the IRI API since iri does not know what kind of transactions you want to publish.
    – mihi
    Apr 26, 2020 at 13:18
  • 1
    Wow. Thank you for the suggestion. I tried with PyOTA and it worked!
    – degr8sid
    Apr 26, 2020 at 15:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.