Triggering a function when a MultiversX's transaction has status "success"

81 views Asked by At

I don't find a way to trigger a function automatically when a MultiversX's transaction has status "success".

I wait some amount of time until the transaction ends (or I query the contract until the status is "success"), but I want to run some code in the precise moment it ends.

1

There are 1 answers

0
Sergi-O On

I solved it querying the Elrond's API until the transaction has "success" state (not very efficient).

def waiting_for_results(proxy, start_time, hashes):
  i = 0
  found = []
  found_data = []
  while len(found) < NUM_TRANS:
    try:
      data = proxy.get_transaction(hashes[str(i)], with_results=True)
      dictio = data.to_dictionary()
      print(i, end = "\r")
      if dictio['status'] == 'success' and not found.__contains__(hashes[str(i)]):
        ti, st, ep, ro, so, de = itemgetter('timestamp', 'status', 'epoch', 'round', 'sourceShard', 'destinationShard')(dictio)
        obj = {
          'hash': hashes[str(i)],
          'start_time': start_time,
          'end_time': current_milli_time(),
          'timestamp': ti,
          'status': st,
          'epoch': ep,
          'round': ro,
          'sourceShard': so,
          'destinationShard': de
        }
        found_data.append(obj)
        found.append(hashes[str(i)]) 
    except:
      pass
    finally:
      i = (i+1)%NUM_TRANS
  return found_data