-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
developer
committed
Nov 30, 2023
0 parents
commit 470712d
Showing
22 changed files
with
865 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length = 90 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# IDEs | ||
.idea | ||
.vscode | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
install: | ||
pip3 install -r ./requirements.txt | ||
|
||
install_dev: | ||
pip3 install -r ./requirements_dev.txt | ||
|
||
lint: | ||
black --check src/ tests/ | ||
isort --check-only src/ tests/ | ||
flake8 src/ tests/ | ||
|
||
format: | ||
black src/ tests/ | ||
isort src/ tests/ | ||
flake8 src/ tests/ | ||
|
||
test: | ||
pytest -v | ||
|
||
verify: lint test | ||
|
||
e2e-test: | ||
pytest ./tests/e2e/e2e.py -v | ||
|
||
clean: | ||
rm -rf build dist docs/build pip-wheel-metadata .mypy_cache .pytest_cache | ||
find . -regex ".*/__pycache__" -exec rm -rf {} + | ||
find . -regex ".*\.egg-info" -exec rm -rf {} + | ||
|
||
package: clean install | ||
python3 -m pip install build && python3 -m build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
![](docs/logo.png) | ||
|
||
## A python SDK connecting you with the big blockchain data ! | ||
|
||
This python SDK is build to accelerate your development efforts in getting data from the big blockchains, by utilizing Chainbase's powerful data infrastructure. | ||
See the docs [here](https://docs.chainbase.com/docs) | ||
|
||
## How do I install this SDK? | ||
|
||
To install this python Chainbase from the Python Package Index (PyPI) run: | ||
|
||
``` | ||
pip install chainbase_sdk | ||
``` | ||
|
||
## What are the main features of this SDK? | ||
|
||
| Feature | What is this? | API Reference | | ||
| -------------------- |-------------------------------------------------------------------------------------|-------------------------------------------------------------| | ||
| SQL | Run low-latency SQL queries against all our indexed datasets for your custom needs. | [link](https://docs.chainbase.com/reference/data-cloud-sql) | | ||
|
||
## How do I use the Chainbase SDK? | ||
|
||
First you need to setup the `CHAINBASE_API_KEY` environment variable, or pass it as parameter in `Chainbase('[api-key]')`. | ||
|
||
Follow the steps below to obtain your API key: | ||
|
||
1. Go to the [Chainbase console](https://console.chainbase.com/) | ||
2. Under `Dashboard` | ||
3. Select existing project or `New Project` | ||
4. Copy the `API Key`. | ||
|
||
``` | ||
from chainbase_sdk import Chainbase | ||
client = Chainbase() | ||
df = client.sql.query_pandas("select * from ethereum.blocks limit 10") | ||
print(df) | ||
``` | ||
|
||
See more under [examples](./examples) | ||
|
||
## Local development | ||
|
||
1. Clone: `git clone` | ||
2. `cd ./chainbase_python_sdk` | ||
3. `make install_dev` | ||
|
||
## How to install dependencies | ||
|
||
Declare any dependencies in `requirements.txt` for production and `requirements_dev.txt` for development. | ||
|
||
To install them, run: | ||
|
||
For production: | ||
``` | ||
make install | ||
``` | ||
|
||
For development: | ||
``` | ||
make install_dev | ||
``` | ||
|
||
## How to test your project | ||
|
||
All test files are under [examples](./tests). You can run your tests: | ||
|
||
``` | ||
make test | ||
``` | ||
|
||
This will also run coverage. | ||
|
||
## How to check your code formatting | ||
|
||
This project is using [black](https://black.readthedocs.io/en/stable/), [flake8](https://flake8.pycqa.org/en/latest/) and [isort](https://pycqa.github.io/isort/) to keep coding formatting standards. | ||
|
||
To format your code run: | ||
|
||
``` | ||
make format | ||
``` | ||
|
||
To check your code formatting: | ||
|
||
``` | ||
make lint | ||
``` | ||
|
||
## How to verify your code | ||
|
||
Verifying your project requires all the lint formatting to pass and tests to pass. | ||
You can run the command bellow to execute: | ||
|
||
``` | ||
make verify | ||
``` | ||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from chainbase_sdk import Chainbase | ||
|
||
client = Chainbase() | ||
df = client.sql.query_pandas("select * from ethereum.blocks limit 10") | ||
print(df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[tool.isort] | ||
profile = "black" | ||
|
||
[tool.pytest.ini_options] | ||
addopts = """ | ||
--cov-report term-missing \ | ||
--cov src -ra""" | ||
|
||
[tool.coverage.report] | ||
fail_under=90 | ||
show_missing=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
requests==2.31.0 | ||
pandas==2.1.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-r ./requirements.txt | ||
pytest-cov==4.1.0 | ||
requests-mock==1.11.0 | ||
black==23.11.0 | ||
flake8==6.1.0 | ||
isort==5.12.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup( | ||
name="chainbase_python_sdk", | ||
version="0.0.1", | ||
packages=find_packages(exclude=["tests"]), | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
from typing import Optional | ||
|
||
from src.sql import ChainbaseSQL | ||
|
||
MISSING_API_KEY_ERROR = """ | ||
Please provide a Chainbase API key, or set the `CHAINBASE_API_KEY` environment variable. | ||
Follow the steps below to obtain your API key: | ||
1. Go to: https://console.chainbase.com/ | ||
2. Under `Dashboard` | ||
3. Select existing project or `New Project` | ||
4. Copy the `API Key`. | ||
""" | ||
|
||
|
||
class Chainbase: | ||
""" | ||
A client for interacting with the Chainbase API. https://docs.chainbase.com/docs | ||
This client provides an interface to perform operations using the Chainbase API. | ||
It requires an API key for authentication, which can be provided either directly | ||
or through an environment variable. | ||
Attributes: | ||
sql: An instance of ChainbaseSQL for executing SQL queries. | ||
Details here: https://docs.chainbase.com/reference/data-cloud-sql | ||
Raises: | ||
ValueError: If the API key is not provided either as a parameter or | ||
through the environment variable 'CHAINBASE_API_KEY'. | ||
""" | ||
|
||
def __init__(self, api_key: Optional[str] = None): | ||
""" | ||
Initializes the Chainbase client with the provided API key. | ||
Args: | ||
api_key: The API key for authenticating with the Chainbase API. | ||
If not provided, the API key is obtained from the | ||
'CHAINBASE_API_KEY' environment variable. | ||
Raises: | ||
ValueError: If the API key is not provided either directly or through the | ||
environment variable. | ||
""" | ||
api_key = api_key if api_key else os.environ.get("CHAINBASE_API_KEY") | ||
|
||
if not api_key: | ||
raise ValueError(MISSING_API_KEY_ERROR) | ||
|
||
self.sql = ChainbaseSQL(api_key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Dict | ||
|
||
import requests | ||
|
||
DEFAULT_ERROR = "An unexpected error occurred while calling the Chainbase API." | ||
|
||
|
||
class ChainbaseAPI: | ||
""" | ||
A client for the Chainbase API. | ||
This class provides methods to interact with the Chainbase API. It handles the | ||
details of making HTTP requests and processing responses. | ||
Attributes: | ||
url (str): The base URL for the Chainbase API. | ||
headers (dict): HTTP headers to include with requests, including the API key. | ||
""" | ||
|
||
def __init__(self, url: str, api_key: str): | ||
""" | ||
Initializes the ChainbaseAPI client with the specified API URL and key. | ||
Args: | ||
url (str): The base URL for the Chainbase API. | ||
api_key (str): The API key for authenticating with the Chainbase API. | ||
""" | ||
self.url = url | ||
|
||
self.headers = { | ||
"x-api-key": api_key, | ||
"Content-Type": "application/json; charset=utf-8", | ||
} | ||
|
||
def post(self, body: any) -> Dict[str, any]: | ||
""" | ||
Sends a POST request to the Chainbase API with the provided body. | ||
Args: | ||
body (Any): The payload to be sent in the POST request. | ||
Returns: | ||
Dict[str, Any]: The JSON response from the API. | ||
Raises: | ||
Exception: If the API returns an error code or if there's an issue with | ||
the response data structure. | ||
""" | ||
res = requests.post(self.url, json=body, headers=self.headers) | ||
res.raise_for_status() | ||
json_res = res.json() | ||
|
||
if json_res.get("code") != 0: | ||
error = json_res.get("error", DEFAULT_ERROR) or DEFAULT_ERROR | ||
raise Exception(error) | ||
|
||
data = json_res.get("data") | ||
|
||
if data is None: | ||
raise Exception(DEFAULT_ERROR) | ||
|
||
if data and data.get("err_msg"): | ||
raise Exception(data.get("err_msg", DEFAULT_ERROR)) | ||
|
||
return json_res |
Oops, something went wrong.