Skip to content

Commit

Permalink
implement new block mode
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKSI committed Nov 2, 2023
1 parent 1abbd0d commit 08dc067
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 26 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,10 @@ async def main():
)

# Sign and broadcast a transaction
tx = await c.send_tx_sync_mode(wallet.sign_and_build(txn))
print(tx.to_json(indent=4))
tx_block = await c.send_tx_block_mode(wallet.sign_and_build(txn))

# Wait for transaction is included
time.sleep(4)
tx_response = await c.get_tx_response(tx.txhash)
print(tx_response.to_json(indent=4))
# Converting to JSON for readability
print(tx_block.to_json(indent=4))


if __name__ == "__main__":
Expand Down
9 changes: 3 additions & 6 deletions examples/ledger_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ async def main():
)

# Sign and broadcast a transaction
tx = await c.send_tx_sync_mode(wallet.sign_and_build(txn))
print(tx.to_json(indent=4))
tx_block = await c.send_tx_block_mode(wallet.sign_and_build(txn))

# Wait for transaction is included
time.sleep(4)
tx_response = await c.get_tx_response(tx.txhash)
print(tx_response.to_json(indent=4))
# Converting to JSON for readability
print(tx_block.to_json(indent=4))


if __name__ == "__main__":
Expand Down
9 changes: 3 additions & 6 deletions examples/request_data_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ async def main():
)

# Sign and broadcast a transaction
tx = await c.send_tx_sync_mode(wallet.sign_and_build(txn))
print(tx.to_json(indent=4))
tx_block = await c.send_tx_block_mode(wallet.sign_and_build(txn))

# Wait for transaction is included
time.sleep(4)
tx_response = await c.get_tx_response(tx.txhash)
print(tx_response.to_json(indent=4))
# Converting to JSON for readability
print(tx_block.to_json(indent=4))


if __name__ == "__main__":
Expand Down
9 changes: 3 additions & 6 deletions examples/send_band_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ async def main():
)

# Sign and broadcast a transaction
tx = await c.send_tx_sync_mode(wallet.sign_and_build(txn))
print(tx.to_json(indent=4))
tx_block = await c.send_tx_block_mode(wallet.sign_and_build(txn))

# Wait for transaction is included
time.sleep(4)
tx_response = await c.get_tx_response(tx.txhash)
print(tx_response.to_json(indent=4))
# Converting to JSON for readability
print(tx_block.to_json(indent=4))


if __name__ == "__main__":
Expand Down
30 changes: 29 additions & 1 deletion pyband/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def __del__(self) -> None:

def close(self) -> None:
"""Closes the connection."""

self.__channel.close()

def get_channel(self) -> Channel:
Expand Down Expand Up @@ -207,6 +206,35 @@ async def send_tx_async_mode(self, tx_bytes: bytes) -> TxResponse:
)
return resp.tx_response

async def send_tx_block_mode(self, tx_bytes: bytes, timeout: int = 5, pull_interval: int = 1) -> TxResponse:
"""Sends a transaction in block mode.
Sends a transaction and waits until the transaction has been committed to a block before returning the response.
Args:
tx_bytes: A signed transaction in raw bytes.
Returns:
The transaction response.
"""

resp = await self.stub_tx.broadcast_tx(
BroadcastTxRequest(tx_bytes=tx_bytes, mode=BroadcastMode.BROADCAST_MODE_SYNC)
)
if resp.tx_response.code != 0:
return resp.tx_response

tx_hash = resp.tx_response.txhash
while True:
timeout -= pull_interval
time.sleep(pull_interval)

try:
tx_response = await self.get_tx_response(tx_hash)
break
except Exception as e:
if timeout < 0:
raise e

return tx_response

async def get_chain_id(self) -> str:
"""Gets the chain ID.
Expand Down
Loading

0 comments on commit 08dc067

Please sign in to comment.