Skip to content

Commit

Permalink
Add support for sync broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunus Emre Yilmaz committed Feb 8, 2021
1 parent c261490 commit 1da0c79
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
39 changes: 39 additions & 0 deletions docs/broadcasting.rst
Expand Up @@ -184,8 +184,47 @@ Example: Using convert function for HBDs
Note: requestid and the owner is unique together.



.. important ::
Since, lighthive doesn't introduce any encapsulation on operations, you are responsible to create operation data yourself. To find out the specs for each operation, you may review the block explorers for raw data or the source code of hiveblocks.
Bonus: Broadcasting Synchronously
---------------------------------------------------------------------------------
This helper function broadcasts the transaction and waits for it to be processed by the
network. You can get transaction id as a result.


.. code-block:: python
from lighthive.client import Client
from lighthive.datastructures import Operation
c = Client(
keys=["<active_key>>",])
op = Operation('transfer', {
'from': '<username>',
'to': '<username>>',
'amount': '0.001 HIVE',
'memo': 'Test'
})
resp = c.broadcast_sync(op)
print(resp)
Result:

.. code-block:: python
{
'id': 'a0a6655bf839f8fc4aa0fbb4c5779835f662045c',
'block_num': 51151610,
'trx_num': 6,
'expired': False
}
10 changes: 7 additions & 3 deletions lighthive/broadcast/transaction_builder.py
Expand Up @@ -112,7 +112,7 @@ def _is_canonical(self, sig):
and not (sig[32] & 0x80)
and not (sig[32] == 0 and not (sig[33] & 0x80)))

def broadcast(self, operations, chain=None, dry_run=False):
def broadcast(self, operations, chain=None, dry_run=False, sync=False):
preferred_api_type = self.client.api_type

try:
Expand Down Expand Up @@ -195,8 +195,12 @@ def broadcast(self, operations, chain=None, dry_run=False):
self.transaction["signatures"] = sigs
if dry_run:
return self.transaction
resp = self.client('condenser_api').broadcast_transaction(
self.transaction)
if sync:
resp = self.client('condenser_api').broadcast_transaction_synchronous(
self.transaction)
else:
resp = self.client('condenser_api').broadcast_transaction(
self.transaction)

return resp
finally:
Expand Down
12 changes: 10 additions & 2 deletions lighthive/client.py
Expand Up @@ -13,8 +13,12 @@

DEFAULT_NODES = [
"https://api.hive.blog",
"https://api.hivekings.com",
"https://anyx.io",
"https://hived.emre.sh",
"https://api.deathwing.me",
"https://rpc.ausbit.dev",
"https://api.pharesim.me",
"https://rpc.ecency.com",
"https://hive-api.arcange.eu",
]


Expand Down Expand Up @@ -168,6 +172,10 @@ def broadcast(self, op, dry_run=False):
return self.transaction_builder.broadcast(
op, chain=self.chain, dry_run=dry_run)

def broadcast_sync(self, op, dry_run=False):
return self.transaction_builder.broadcast(
op, chain=self.chain, dry_run=dry_run, sync=True)

def account(self, username):
return Account(self, username)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='lighthive',
version='0.2.7',
version='0.2.8',
packages=find_packages('.'),
url='http://github.com/emre/lighthive',
license='MIT',
Expand Down

0 comments on commit 1da0c79

Please sign in to comment.