0
rpc = RippleNetwork(app.config['RIPPLE_SERVER'])
poll = RippleNetwork(app.config['RIPPLE_SERVER'])
last_ledger = Variables.get('ripple_last_ledger')
poll.subscribe(streams=['ledger'])
while True:
trans = []
try:
rst = rpc.account_tx(account=withdraw_wallet, ledger_index_min=last_ledger, limit=1000)
trans.extend(rst['transactions'])
while rst.get('marker') is not None:
rst = rpc.account_tx(account=withdraw_wallet, ledger_index_min=last_ledger, marker=rst['marker'], limit=1000)
trans.extend(rst['transactions'])
except RippleException as e:
if e.resp['error'] == 'lgrIdxsInvalid':
log.debug('ledger_index_min too new, sleeping for 10s')
time.sleep(10)
continue
raise
for rec in trans:
tx = rec['tx']
log.debug('Got tx %s', tx['hash'])
if tx['TransactionType'] != 'Payment':
continue
if tx['Destination'] != withdraw_wallet:
continue
log.info('Queue tx %s', tx['hash'])
tube.put(str(tx['hash']))
last_ledger = rst['ledger_index_max'] + 1
Variables.set('ripple_last_ledger', last_ledger)
db.session.commit()
# Wait for next(or several) ledger close here.
Code above is my current implementation, but this method does not scale to thousands of accounts.
Is there any 'best practice'?
First, unless you're catching up on past transactions use subscribe instead of repeated account_tx calls. Second, for a large number of accounts it can be more efficient to just subscribe to the the full transaction stream and filter it down to the transactions you care about. – dchapes – 2014-04-06T08:54:47.840
dchapes: why don't you add that as an answer? I agree that if you're monitoring a significant number of accounts, it's best to get the full stream and filter. – Eric S – 2014-04-06T10:28:52.020