2023-12-07 09:02:21 +00:00
|
|
|
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, Tuple
|
|
|
|
from airbyte_cdk.sources.streams.http import HttpStream
|
|
|
|
import logging
|
|
|
|
import requests
|
|
|
|
import json
|
2024-01-22 17:56:18 +00:00
|
|
|
import time
|
2024-01-25 17:22:34 +00:00
|
|
|
from .utils import extract_token
|
2023-12-07 09:02:21 +00:00
|
|
|
logger = logging.getLogger("airbyte")
|
|
|
|
|
|
|
|
|
|
|
|
class BlockchainStream(HttpStream):
|
|
|
|
|
|
|
|
primary_key = None
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, wallets: List['str'], **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.wallets = wallets
|
|
|
|
|
|
|
|
def stream_slices(self, **kwargs) -> Iterable[Optional[Mapping[str, Any]]]:
|
|
|
|
for wallet in self.wallets:
|
|
|
|
yield {
|
|
|
|
"address": wallet['address'],
|
2024-01-09 13:46:29 +00:00
|
|
|
"name": wallet['name'],
|
|
|
|
"tags": wallet['tags']
|
2023-12-07 09:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
|
|
|
|
return None
|
|
|
|
|
2024-01-22 17:56:18 +00:00
|
|
|
def backoff_time(self, response: requests.Response) -> Optional[float]:
|
|
|
|
"""This method is called if we run into the rate limit.
|
|
|
|
Slack puts the retry time in the `Retry-After` response header so we
|
|
|
|
we return that value. If the response is anything other than a 429 (e.g: 5XX)
|
|
|
|
fall back on default retry behavior.
|
|
|
|
"""
|
|
|
|
if "Retry-After" in response.headers:
|
|
|
|
return int(response.headers["Retry-After"])
|
|
|
|
else:
|
|
|
|
self.logger.info("Retry-after header not found. Using default backoff value")
|
|
|
|
return 5
|
2023-12-07 09:02:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BitcoinToken(BlockchainStream):
|
|
|
|
|
|
|
|
url_base = "https://blockchain.info/rawaddr/"
|
|
|
|
|
|
|
|
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
|
|
|
|
return f"{stream_slice['address']}"
|
|
|
|
|
|
|
|
def parse_response(
|
|
|
|
self,
|
|
|
|
response: requests.Response,
|
|
|
|
stream_slice: Mapping[str, Any] = None,
|
|
|
|
**kwargs
|
|
|
|
) -> Iterable[Mapping]:
|
|
|
|
logger.info("Getting Bitcoin Balance information")
|
2024-01-10 14:48:26 +00:00
|
|
|
logger.info("Response %s", response.json())
|
2023-12-07 09:02:21 +00:00
|
|
|
bitcoin_data = response.json()
|
|
|
|
yield {
|
|
|
|
"wallet_name": stream_slice['name'],
|
|
|
|
"name":"BTC",
|
|
|
|
"symbol":"BTC",
|
|
|
|
"description": "Bitcoin",
|
|
|
|
"chain": "bitcoin",
|
|
|
|
"balance": bitcoin_data['final_balance'],
|
2024-01-09 13:46:29 +00:00
|
|
|
"decimal":8,
|
|
|
|
"tags": stream_slice['tags']
|
2023-12-07 09:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class EthereumToken(BlockchainStream):
|
|
|
|
|
|
|
|
url_base = "https://api.ethplorer.io/getAddressInfo/"
|
|
|
|
|
|
|
|
def path(self, stream_slice: Mapping[str, Any] = None, **kwargs) -> str:
|
|
|
|
return f"{stream_slice['address']}?apiKey=freekey"
|
|
|
|
|
|
|
|
def parse_response(self,
|
|
|
|
response: requests.Response,
|
|
|
|
stream_slice: Mapping[str, Any] = None,
|
|
|
|
**kwargs
|
|
|
|
) -> Iterable[Mapping]:
|
2024-01-22 17:56:18 +00:00
|
|
|
logger.info("Getting ETH balance information %s", stream_slice['name'])
|
2023-12-07 09:02:21 +00:00
|
|
|
eth_data=response.json()['ETH']
|
|
|
|
yield {
|
|
|
|
"wallet_name": stream_slice['name'],
|
|
|
|
"name":"ETH",
|
|
|
|
"symbol":"ETH",
|
|
|
|
"description": "Native Ethereum token",
|
|
|
|
"chain": "Ethereum",
|
|
|
|
"balance":eth_data['rawBalance'],
|
2024-01-09 13:46:29 +00:00
|
|
|
"decimal":18,
|
2024-02-23 17:20:25 +00:00
|
|
|
"tags": stream_slice['tags'],
|
2024-02-21 10:11:09 +00:00
|
|
|
"address": "eth"
|
2023-12-07 09:02:21 +00:00
|
|
|
}
|
2024-01-22 17:56:18 +00:00
|
|
|
if 'tokens' in response.json():
|
|
|
|
tokens_data=response.json()['tokens']
|
|
|
|
for t in tokens_data:
|
|
|
|
try:
|
|
|
|
yield extract_token(stream_slice['name'], t)
|
|
|
|
except Exception as e:
|
2024-01-25 17:22:34 +00:00
|
|
|
name = t
|
|
|
|
if 'name' in t:
|
|
|
|
name= t['name']
|
|
|
|
logger.warning('Dropping token %s not valid %s',name, e )
|
2024-01-22 17:56:18 +00:00
|
|
|
# Delaying calls - Not great but that works
|
|
|
|
time.sleep(2)
|