/chainlink-web3

A web3.py library for interacting with Chainlink Ethereum contracts.

Primary LanguagePythonMIT LicenseMIT

Web3.py Chainlink Library

Note

This project is provided to demonstrate how to interact with Chainlink’s systems, products, and services to integrate them into your own. This template is provided “AS IS” and “AS AVAILABLE” without warranties of any kind, it has not been audited, and it may be missing key checks or error handling to make the usage of the system, product or service more clear. Do not use the code in this example in a production environment without completing your own audits and application of best practices. Kalmia is not responsible for unintended outputs that are generated due to errors in code.

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

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

Build & install locally

  1. Clone repo
  2. Install dependencies
pip3 install wheel
pip3 install setuptools
pip3 install twine
  1. Run the tests
python3 setup.py pytest
  1. 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

  1. Clone repo

  2. Create docker image

chmod +x ./run-docker.sh
./run-docker.sh install
  1. Test lib
./run-docker.sh test
  1. 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