Skip to content

Instantly share code, notes, and snippets.

@icook
Last active January 24, 2019 19:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icook/8d47f423b700efb42745 to your computer and use it in GitHub Desktop.
Save icook/8d47f423b700efb42745 to your computer and use it in GitHub Desktop.
SoChain python example
"""
A simple demo for using https://chain.so/api#realtime-balance-updates with Python.
Install my fork of pusherclient with:
pip install git+https://github.com/icook/PythonPusherClient.git
"""
import pusherclient
import time
import json
import pprint
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler(sys.stdout)
root.addHandler(ch)
global pusher
def callback(data):
data = json.loads(data)
pprint.pprint(data)
def connect_handler(data):
channel = pusher.subscribe('address_ltctest_muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh')
channel.bind('balance_update', callback)
channel = pusher.subscribe('blockchain_update_ltctest')
channel.bind('block_update', callback)
channel.bind('tx_update', callback)
pusherclient.Pusher.host = "slanger1.chain.so"
pusher = pusherclient.Pusher('e9f5cc20074501ca7395', secure=True, log_level="DEBUG")
pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()
while True:
time.sleep(1)
(scm)╭─isaac2@isaaclt ~/programming/PythonPusherClient ‹master*›
╰─$ python tmp.py 1 ↵
2014-11-11 17:05:56,921 [pusherclient.connection] INFO: Connection: Connection opened
2014-11-11 17:05:56,921 [pusherclient.connection] INFO: Connection: ping to pusher
2014-11-11 17:05:56,925 [pusherclient.connection] INFO: Connection: Message - {"event":"pusher:connection_established","data":{"socket_id":"4772cc44-c235-4442-8ad7-03da8e90e920","activity_timeout":120}}
2014-11-11 17:05:56,926 [pusherclient.connection] INFO: Connection: Sending event - {'data': {'channel': 'address_ltctest_muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh'}, 'event': 'pusher:subscribe'}
2014-11-11 17:05:56,927 [pusherclient.connection] INFO: Connection: Sending event - {'data': {'channel': 'blockchain_update_ltctest'}, 'event': 'pusher:subscribe'}
2014-11-11 17:05:56,981 [pusherclient.connection] INFO: Connection: Message - {"event":"pusher:pong","data":{}}
2014-11-11 17:05:56,998 [pusherclient.connection] INFO: Connection: Message - {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"address_ltctest_muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh"}
2014-11-11 17:05:57,000 [pusherclient.connection] INFO: Connection: Message - {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"blockchain_update_ltctest"}
2014-11-11 17:06:42,923 [pusherclient.connection] INFO: Connection: Message - {"event":"tx_update","data":"{\"type\": \"tx\", \"value\": {\"unconfirmed_txs\": 36, \"total_inputs\": \"1\", \"time\": 1415747202, \"tx_hex\": \"010000000198402bcb721eb1e6c32c9248845f95c07ecfa140e79ced1069518c0b0df63a1f010000006a47304402203cb981bdb2104b841a491994da0a30d345a07091d87cd22b2e09326a9880410a0220599828426230a31c641fce9217d1506137d26647f78e0b733e4ae001d69a8541012103c680d7a830e8205705c6927c7a4cd8efd1608bb58dfa9a0f38581e1edc956869ffffffff03e02eaa37000000001976a9141770029a9e047829d781ca0c626d12873cca26e088ac80c3c901000000001976a91475383493200dbbd67df46915c44a98397903998b88ac00512502000000001976a914354b75978c9d0c4ddcb849ba9ba8c508b30bd05188ac00000000\", \"sent_value\": 10.0, \"total_outputs\": \"3\", \"txpm\": 2.91, \"hashrate\": 88631, \"txid\": \"6b3eeff82ad193ed8947724a312f26be8ab147298afb29c13044609aaa36c05b\"}}","channel":"blockchain_update_ltctest"}
{u'type': u'tx',
u'value': {u'hashrate': 88631,
u'sent_value': 10.0,
u'time': 1415747202,
u'total_inputs': u'1',
u'total_outputs': u'3',
u'tx_hex': u'010000000198402bcb721eb1e6c32c9248845f95c07ecfa140e79ced1069518c0b0df63a1f010000006a47304402203cb981bdb2104b841a491994da0a30d345a07091d87cd22b2e09326a9880410a0220599828426230a31c641fce9217d1506137d26647f78e0b733e4ae001d69a8541012103c680d7a830e8205705c6927c7a4cd8efd1608bb58dfa9a0f38581e1edc956869ffffffff03e02eaa37000000001976a9141770029a9e047829d781ca0c626d12873cca26e088ac80c3c901000000001976a91475383493200dbbd67df46915c44a98397903998b88ac00512502000000001976a914354b75978c9d0c4ddcb849ba9ba8c508b30bd05188ac00000000',
u'txid': u'6b3eeff82ad193ed8947724a312f26be8ab147298afb29c13044609aaa36c05b',
u'txpm': 2.91,
u'unconfirmed_txs': 36}}
2014-11-11 17:06:59,196 [pusherclient.connection] INFO: Connection: Message - {"event":"tx_update","data":"{\"type\": \"tx\", \"value\": {\"unconfirmed_txs\": 37, \"total_inputs\": \"1\", \"time\": 1415747219, \"tx_hex\": \"01000000012e69aec6161e8eeb8c670c323b5075c8814615837fa7481a0ef43ce3701ecf93010000006a473044022063c5b327224f7fa1041ef94cd898102f4b5b84d862a51fa9593ac48c287d689c022032d9444f259a7646074275a4f5134211281ce09c8eb7f8a1235b1cca0d0e7248012102becfe2b506a959df050387b054c742cc0272c82b48185ea6973f2303d78c81c4ffffffff0276533e1d000000001976a91403a2ef5448bead283f8f4b770310a182367e966788ac00e1f505000000001976a9149dee7f398b52984ddef3c241d1f914951134fcfd88ac00000000\", \"sent_value\": 5.91, \"total_outputs\": \"2\", \"txpm\": 2.9, \"hashrate\": 88631, \"txid\": \"e1ef61c6ae0cd9051d4642f636e1a5316f47e84c1f633bc4f0311e80f637679d\"}}","channel":"blockchain_update_ltctest"}
{u'type': u'tx',
u'value': {u'hashrate': 88631,
u'sent_value': 5.91,
u'time': 1415747219,
u'total_inputs': u'1',
u'total_outputs': u'2',
u'tx_hex': u'01000000012e69aec6161e8eeb8c670c323b5075c8814615837fa7481a0ef43ce3701ecf93010000006a473044022063c5b327224f7fa1041ef94cd898102f4b5b84d862a51fa9593ac48c287d689c022032d9444f259a7646074275a4f5134211281ce09c8eb7f8a1235b1cca0d0e7248012102becfe2b506a959df050387b054c742cc0272c82b48185ea6973f2303d78c81c4ffffffff0276533e1d000000001976a91403a2ef5448bead283f8f4b770310a182367e966788ac00e1f505000000001976a9149dee7f398b52984ddef3c241d1f914951134fcfd88ac00000000',
u'txid': u'e1ef61c6ae0cd9051d4642f636e1a5316f47e84c1f633bc4f0311e80f637679d',
u'txpm': 2.9,
u'unconfirmed_txs': 37}}
2014-11-11 17:06:59,202 [pusherclient.connection] INFO: Connection: Message - {"event":"balance_update","data":"{\"type\": \"address\", \"value\": {\"value_sent\": \"0.00000000\", \"value_received\": \"1.00000000\", \"balance_change\": \"1.00000000\", \"tx\": {\"inputs\": [{\"script\": \"3044022063c5b327224f7fa1041ef94cd898102f4b5b84d862a51fa9593ac48c287d689c022032d9444f259a7646074275a4f5134211281ce09c8eb7f8a1235b1cca0d0e724801 02becfe2b506a959df050387b054c742cc0272c82b48185ea6973f2303d78c81c4\", \"input_no\": 0, \"value\": \"5.90723862\", \"address\": \"mmpWoVeyuMR1PAF8mnTUuMSyo4qYdGQLJK\", \"type\": \"pubkeyhash\", \"from_output\": {\"output_no\": 1, \"txid\": \"93cf1e70e33cf40e1a48a77f83154681c875503b320c678ceb8e1e16c6ae692e\"}}], \"outputs\": [{\"script\": \"OP_DUP OP_HASH160 03a2ef5448bead283f8f4b770310a182367e9667 OP_EQUALVERIFY OP_CHECKSIG\", \"type\": \"pubkeyhash\", \"value\": \"4.90623862\", \"output_no\": 0, \"address\": \"mfrBWYUMeqVzStWKFCN8EZ1arQvt2bF8LF\"}, {\"script\": \"OP_DUP OP_HASH160 9dee7f398b52984ddef3c241d1f914951134fcfd OP_EQUALVERIFY OP_CHECKSIG\", \"type\": \"pubkeyhash\", \"value\": \"1.00000000\", \"output_no\": 1, \"address\": \"muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh\"}], \"txid\": \"e1ef61c6ae0cd9051d4642f636e1a5316f47e84c1f633bc4f0311e80f637679d\"}, \"address\": \"muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh\"}}","channel":"address_ltctest_muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh"}
{u'type': u'address',
u'value': {u'address': u'muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh',
u'balance_change': u'1.00000000',
u'tx': {u'inputs': [{u'address': u'mmpWoVeyuMR1PAF8mnTUuMSyo4qYdGQLJK',
u'from_output': {u'output_no': 1,
u'txid': u'93cf1e70e33cf40e1a48a77f83154681c875503b320c678ceb8e1e16c6ae692e'},
u'input_no': 0,
u'script': u'3044022063c5b327224f7fa1041ef94cd898102f4b5b84d862a51fa9593ac48c287d689c022032d9444f259a7646074275a4f5134211281ce09c8eb7f8a1235b1cca0d0e724801 02becfe2b506a959df050387b054c742cc0272c82b48185ea6973f2303d78c81c4',
u'type': u'pubkeyhash',
u'value': u'5.90723862'}],
u'outputs': [{u'address': u'mfrBWYUMeqVzStWKFCN8EZ1arQvt2bF8LF',
u'output_no': 0,
u'script': u'OP_DUP OP_HASH160 03a2ef5448bead283f8f4b770310a182367e9667 OP_EQUALVERIFY OP_CHECKSIG',
u'type': u'pubkeyhash',
u'value': u'4.90623862'},
{u'address': u'muv26EP3gCTQAHq1vwCkVdMGRwXxqpE1mh',
u'output_no': 1,
u'script': u'OP_DUP OP_HASH160 9dee7f398b52984ddef3c241d1f914951134fcfd OP_EQUALVERIFY OP_CHECKSIG',
u'type': u'pubkeyhash',
u'value': u'1.00000000'}],
u'txid': u'e1ef61c6ae0cd9051d4642f636e1a5316f47e84c1f633bc4f0311e80f637679d'},
u'value_received': u'1.00000000',
u'value_sent': u'0.00000000'}}
2014-11-11 17:07:12,312 [pusherclient.connection] INFO: Connection: Message - {"event":"block_update","data":"{\"type\": \"block\", \"value\": {\"size\": 229, \"total_txs\": 1, \"previous_blockhash\": \"e99527ae5b26840a40796655f428cdfebfba85067b7926be74592665e6ecb277\", \"blockhash\": \"94c08c3921f22b97f44a12b7c89c5c20fcda299532ab9f9dda38ee05453ea2f6\", \"time\": 1415747232, \"merkleroot\": \"9f277357c3d47b3be2119bee3fc72ca27c05e8edc9a075414d002aa9682d16b2\", \"block_no\": 436892, \"mined_by\": \"n2ArE8Js...\", \"miner_url\": null, \"mining_difficulty\": \"0.00048821\"}}","channel":"blockchain_update_ltctest"}
{u'type': u'block',
u'value': {u'block_no': 436892,
u'blockhash': u'94c08c3921f22b97f44a12b7c89c5c20fcda299532ab9f9dda38ee05453ea2f6',
u'merkleroot': u'9f277357c3d47b3be2119bee3fc72ca27c05e8edc9a075414d002aa9682d16b2',
u'mined_by': u'n2ArE8Js...',
u'miner_url': None,
u'mining_difficulty': u'0.00048821',
u'previous_blockhash': u'e99527ae5b26840a40796655f428cdfebfba85067b7926be74592665e6ecb277',
u'size': 229,
u'time': 1415747232,
u'total_txs': 1}}
2014-11-11 17:07:12,326 [pusherclient.connection] INFO: Connection: Message - {"event":"tx_update","data":"{\"type\": \"tx\", \"value\": {\"unconfirmed_txs\": 37, \"total_inputs\": 1, \"time\": 1415747232, \"tx_hex\": \"02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3f039caa0632fabe7366617264736d6d450ca629d22d7804e9c5471d86cc99460c000fec0705f69f175923201a08b24c01000000000000004c00000012000000000000000100f2052a010000001976a914e28d41d4d7736573e0dd7af642e87dcaf3efb4ba88ac00000000\", \"sent_value\": 50.0, \"total_outputs\": 1, \"txpm\": 2.9, \"hashrate\": 86113, \"txid\": \"9f277357c3d47b3be2119bee3fc72ca27c05e8edc9a075414d002aa9682d16b2\"}}","channel":"blockchain_update_ltctest"}
{u'type': u'tx',
u'value': {u'hashrate': 86113,
u'sent_value': 50.0,
u'time': 1415747232,
u'total_inputs': 1,
u'total_outputs': 1,
u'tx_hex': u'02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3f039caa0632fabe7366617264736d6d450ca629d22d7804e9c5471d86cc99460c000fec0705f69f175923201a08b24c01000000000000004c00000012000000000000000100f2052a010000001976a914e28d41d4d7736573e0dd7af642e87dcaf3efb4ba88ac00000000',
u'txid': u'9f277357c3d47b3be2119bee3fc72ca27c05e8edc9a075414d002aa9682d16b2',
u'txpm': 2.9,
u'unconfirmed_txs': 37}}
^CTraceback (most recent call last):
File "tmp.py", line 40, in <module>
time.sleep(1)
KeyboardInterrupt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment