Library for interacting with Chainlink Ethereum contracts.
Project description
Web3.py Chainlink Library
The Web3.py Chainlink Library is a Chainlink Python library designed to simplify the interaction with Chainlink Ethereum contracts. It provides an easy and streamlined way to retrieve real-time prices using the Chainlink price oracle. The library is dependent on Web3.py, so you'll need to provide an active instance during initialization.
Once initialized, you can make use of the get_price
function. This function essentially invokes the latestRoundData
function on the Chainlink price feed contract. As a result, the get_price
function returns a Price
object containing all the data retrieved from the blockchain call.
This library empowers Python developers by eliminating the need to write complex blockchain calls directly. Instead, it enables them to leverage the Chainlink price feed in a more familiar and simple manner.
Table of content
- Prerequisites
- Installation
- Using this library
- Found an issue or have a question or suggestion
- Build & install locally
- Useful links
Prerequisites
Installation
pip3 install chainlink-web3
Using this library
Installing web3.py
pip3 install web3
Usage example
from web3 import Web3, HTTPProvider
from chainlink_web3.utils import ChainlinkUtils, types
w3 = Web3(HTTPProvider('https://rpc.ankr.com/eth', request_kwargs={'timeout': 180}))
chainlink = ChainlinkUtils(w3=w3)
result = chainlink.get_price(types.MAINNET_PRICE_FEEDS['LinkEth'])
print(result)
Plugin Methods
Price Feed Addresses
Included in this library are two dicts that contain the Ethereum contract addresses for specific token pairs: MAINNET_PRICE_FEEDS and SEPOLIA_PRICE_FEEDS. If you cannot find your desired price feed within these dicts, please check here to make sure it's supported, and if it is, please open an issue or a pull request for the missing price feed so that it can be added to the appropriate dict.
get_price
def get_price(
self,
price_feed_address: str,
aggregator_interface_abi:list = None
) -> types.Price:
# class Price:
# roundId: str
# answer: str
# startedAt: str
# updatedAt: str
# answeredInRound: str
aggregator_interface_abi
can be found here.
The get_price
method, accepts evm address for it's first parameter, and an optional second parameter for specifying the Chainlink Aggregator Interface ABI of the Ethereum smart contract you'd like to interact with (the parameter is defaulted to aggregator_interface_abi).
Under the hood, this method is calling the latestRoundData
for the specified price feed, more information about it can be found here.
from web3 import Web3, HTTPProvider
from chainlink_web3.utils import ChainlinkUtils, types
w3 = Web3(HTTPProvider('https://rpc.ankr.com/eth', request_kwargs={'timeout': 180}))
chainlink = ChainlinkUtils(w3=w3)
result = chainlink.get_price(types.MAINNET_PRICE_FEEDS['LinkEth'])
# Price(
# roundId=36893488147419106338,
# answer=164576918062797,
# startedAt=1697700215,
# updatedAt=1697700215,
# answeredInRound=36893488147419106338
# )
Found an issue or have a question or suggestion
- If you found an issue or have a question or suggestion submit an issue or join us on Discord
Build & install locally
- Clone repo
- Install dependencies
pip3 install wheel
pip3 install setuptools
pip3 install twine
- Run the tests
python3 setup.py pytest
- Generate .whl file for external use
python3 setup.py bdist_wheel
This creates a ./dist
folder that contains a .whl
file.
Use the bellow command to install the lib on your local environment:
pip3 install /path/to/wheelfile.whl
Build with docker
-
Clone repo
-
Create docker image
chmod +x ./run-docker.sh
./run-docker.sh install
- Test lib
./run-docker.sh test
- Generate .whl file for external use
./run-docker.sh build
This creates a ./dist
folder that contains a .whl
file.
Use the bellow command to install the lib on your local environment:
pip3 install /path/to/wheelfile.whl
Useful links
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file chainlink_web3-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: chainlink_web3-0.1.4-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f42d261d83a698a6dd0423fefada2e1b91c20264bc73fcb8fb879fac9fe51856 |
|
MD5 | b273cdb5bfabfc61287b8c70321b2fcd |
|
BLAKE2b-256 | 9c280d02f9ecfc6bfaf21ee2c44bb32564012012b021d9301304c1315ec0343b |