Beispiel #1
0
    def get_raw_transactions(address, start_ts=None, end_ts=None, limit=1000):
        """Gets raw transactions for a particular address

        @param address: A single address string
        @param start_ts: The starting date & time. Should be a unix epoch object. If passed as None, defaults to 60 days before the end_date
        @param end_ts: The ending date & time. Should be a unix epoch object. If passed as None, defaults to the current date & time
        @param limit: the maximum number of transactions to return; defaults to one thousand
        @return: Returns the data, ordered from newest txn to oldest. If any limit is applied, it will cut back from the oldest results
        """
        def get_address_history(address, start_block=None, end_block=None):
            address_dict = {}

            address_dict['balances'] = util.call_jsonrpc_api(
                "get_balances", {'filters': [{'field': 'address', 'op': '==', 'value': address}, ],
                                 }, abort_on_error=True)['result']

            address_dict['debits'] = util.call_jsonrpc_api(
                "get_debits",
                {'filters': [{'field': 'address', 'op': '==', 'value': address},
                             {'field': 'quantity', 'op': '>', 'value': 0}],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['credits'] = util.call_jsonrpc_api(
                "get_credits",
                {'filters': [{'field': 'address', 'op': '==', 'value': address},
                             {'field': 'quantity', 'op': '>', 'value': 0}],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['burns'] = util.call_jsonrpc_api(
                "get_burns",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['sends'] = util.call_jsonrpc_api(
                "get_sends",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, {'field': 'destination', 'op': '==', 'value': address}],
                 'filterop': 'or',
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']
            #^ with filterop == 'or', we get all sends where this address was the source OR destination

            address_dict['orders'] = util.call_jsonrpc_api(
                "get_orders",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['order_matches'] = util.call_jsonrpc_api(
                "get_order_matches",
                {'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address}, ],
                 'filterop': 'or',
                 'order_by': 'tx0_block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['btcpays'] = util.call_jsonrpc_api(
                "get_btcpays",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, {'field': 'destination', 'op': '==', 'value': address}],
                 'filterop': 'or',
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['issuances'] = util.call_jsonrpc_api(
                "get_issuances",
                {'filters': [{'field': 'issuer', 'op': '==', 'value': address}, {'field': 'source', 'op': '==', 'value': address}],
                 'filterop': 'or',
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['broadcasts'] = util.call_jsonrpc_api(
                "get_broadcasts",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['bets'] = util.call_jsonrpc_api(
                "get_bets",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['bet_matches'] = util.call_jsonrpc_api(
                "get_bet_matches",
                {'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address}, ],
                 'filterop': 'or',
                 'order_by': 'tx0_block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['dividends'] = util.call_jsonrpc_api(
                "get_dividends",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['cancels'] = util.call_jsonrpc_api(
                "get_cancels",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['bet_expirations'] = util.call_jsonrpc_api(
                "get_bet_expirations",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['order_expirations'] = util.call_jsonrpc_api(
                "get_order_expirations",
                {'filters': [{'field': 'source', 'op': '==', 'value': address}, ],
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['bet_match_expirations'] = util.call_jsonrpc_api(
                "get_bet_match_expirations",
                {'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address}, ],
                 'filterop': 'or',
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']

            address_dict['order_match_expirations'] = util.call_jsonrpc_api(
                "get_order_match_expirations",
                {'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address}, ],
                 'filterop': 'or',
                 'order_by': 'block_index',
                 'order_dir': 'asc',
                 'start_block': start_block,
                 'end_block': end_block,
                 }, abort_on_error=True)['result']
            return address_dict

        now_ts = calendar.timegm(time.gmtime())
        if not end_ts:  # default to current datetime
            end_ts = now_ts
        if not start_ts:  # default to epoch
            start_ts = 0
        start_block_index, end_block_index = database.get_block_indexes_for_dates(
            start_dt=datetime.datetime.utcfromtimestamp(start_ts),
            end_dt=datetime.datetime.utcfromtimestamp(end_ts) if now_ts != end_ts else None)

        # make API call to counterparty-server to get all of the data for the specified address
        txns = []
        d = get_address_history(address, start_block=start_block_index, end_block=end_block_index)
        # mash it all together
        for category, entries in d.items():
            if category in ['balances', ]:
                continue
            for e in entries:
                e['_category'] = category
                e = messages.decorate_message(e, for_txn_history=True)  # DRY
            txns += entries
        txns = util.multikeysort(txns, ['-_block_time', '-_tx_index'])
        txns = txns[0:limit]  # TODO: we can trunk before sorting. check if we can use the messages table and use sql order and limit
        #^ won't be a perfect sort since we don't have tx_indexes for cancellations, but better than nothing
        # txns.sort(key=operator.itemgetter('block_index'))
        return txns
Beispiel #2
0
    def get_raw_transactions(address, start_ts=None, end_ts=None, limit=500):
        """Gets raw transactions for a particular address
        
        @param address: A single address string
        @param start_ts: The starting date & time. Should be a unix epoch object. If passed as None, defaults to 60 days before the end_date
        @param end_ts: The ending date & time. Should be a unix epoch object. If passed as None, defaults to the current date & time
        @param limit: the maximum number of transactions to return; defaults to ten thousand
        @return: Returns the data, ordered from newest txn to oldest. If any limit is applied, it will cut back from the oldest results
        """
        def get_address_history(address, start_block=None, end_block=None):
            address_dict = {}
            
            address_dict['balances'] = util.call_jsonrpc_api("get_balances",
                { 'filters': [{'field': 'address', 'op': '==', 'value': address},],
                }, abort_on_error=True)['result']
            
            address_dict['debits'] = util.call_jsonrpc_api("get_debits",
                { 'filters': [{'field': 'address', 'op': '==', 'value': address},
                              {'field': 'quantity', 'op': '>', 'value': 0}],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['credits'] = util.call_jsonrpc_api("get_credits",
                { 'filters': [{'field': 'address', 'op': '==', 'value': address},
                              {'field': 'quantity', 'op': '>', 'value': 0}],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['burns'] = util.call_jsonrpc_api("get_burns",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['sends'] = util.call_jsonrpc_api("get_sends",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address}, {'field': 'destination', 'op': '==', 'value': address}],
                  'filterop': 'or',
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            #^ with filterop == 'or', we get all sends where this address was the source OR destination 
            
            address_dict['orders'] = util.call_jsonrpc_api("get_orders",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
    
            address_dict['order_matches'] = util.call_jsonrpc_api("get_order_matches",
                { 'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address},],
                  'filterop': 'or',
                  'order_by': 'tx0_block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['btcpays'] = util.call_jsonrpc_api("get_btcpays",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address}, {'field': 'destination', 'op': '==', 'value': address}],
                  'filterop': 'or',
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['issuances'] = util.call_jsonrpc_api("get_issuances",
                { 'filters': [{'field': 'issuer', 'op': '==', 'value': address}, {'field': 'source', 'op': '==', 'value': address}],
                  'filterop': 'or',
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['broadcasts'] = util.call_jsonrpc_api("get_broadcasts",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
    
            address_dict['bets'] = util.call_jsonrpc_api("get_bets",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['bet_matches'] = util.call_jsonrpc_api("get_bet_matches",
                { 'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address},],
                  'filterop': 'or',
                  'order_by': 'tx0_block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['dividends'] = util.call_jsonrpc_api("get_dividends",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            
            address_dict['cancels'] = util.call_jsonrpc_api("get_cancels",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['bet_expirations'] = util.call_jsonrpc_api("get_bet_expirations",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['order_expirations'] = util.call_jsonrpc_api("get_order_expirations",
                { 'filters': [{'field': 'source', 'op': '==', 'value': address},],
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['bet_match_expirations'] = util.call_jsonrpc_api("get_bet_match_expirations",
                { 'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address},],
                  'filterop': 'or',
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
        
            address_dict['order_match_expirations'] = util.call_jsonrpc_api("get_order_match_expirations",
                { 'filters': [{'field': 'tx0_address', 'op': '==', 'value': address}, {'field': 'tx1_address', 'op': '==', 'value': address},],
                  'filterop': 'or',
                  'order_by': 'block_index',
                  'order_dir': 'asc',
                  'start_block': start_block,
                  'end_block': end_block,
                }, abort_on_error=True)['result']
            return address_dict

        now_ts = time.mktime(datetime.datetime.utcnow().timetuple())
        if not end_ts: #default to current datetime
            end_ts = now_ts
        if not start_ts: #default to 60 days before the end date
            start_ts = end_ts - (60 * 24 * 60 * 60)
        start_block_index, end_block_index = database.get_block_indexes_for_dates(
            start_dt=datetime.datetime.utcfromtimestamp(start_ts),
            end_dt=datetime.datetime.utcfromtimestamp(end_ts) if now_ts != end_ts else None)
        
        #make API call to counterparty-server to get all of the data for the specified address
        txns = []
        d = get_address_history(address, start_block=start_block_index, end_block=end_block_index)
        #mash it all together
        for category, entries in d.iteritems():
            if category in ['balances',]:
                continue
            for e in entries:
                e['_category'] = category
                e = messages.decorate_message(e, for_txn_history=True) #DRY
            txns += entries
        txns = util.multikeysort(txns, ['-_block_time', '-_tx_index'])
        txns = txns[0:limit] #TODO: we can trunk before sorting. check if we can use the messages table and use sql order and limit
        #^ won't be a perfect sort since we don't have tx_indexes for cancellations, but better than nothing
        #txns.sort(key=operator.itemgetter('block_index'))
        return txns