Actions

Interacting With Chainlink Contracts

From Chainlink Community Wiki

Revision as of 14:46, 24 May 2022 by Devtrace (talk | contribs)

How to Query Chainlink Contracts

This page will provide you with information regarding how to query a Chainlink Aggregator

Included are some examples using bash.

All examples are based on the LINK / USD price feed on Ethereum Mainnet


Overview

Each Chainlink Aggregator has a wide range of data that you can query.

From historic price data to the timestamp of the most recent completed round, a lot of information is stored on-chain and available for you to query.

This page will be broken out into each major contract type, and then each individual query that can be made, with examples.


EACAggregatorProxy

LINK / USD EACAggregatorProxy contract address: 0x2c1d072e956affc0d435cb7ac38ef18d24d9127c

The Access Controlled Aggregator Proxy is a proxy contract that enables the owner to point it to other contracts as needed/desired.

This prevents the users from having to update anything if they wanted to move to a new contract version/release

Read Calls

The Read calls are queries you can make to each EACAggregatorProxy to get a wide range of data from the proxy's associated AccessControlledOffchainAggregator contract. One such example is to get the latest round data from a price feed.

Write Calls

The Write calls that are available to each EACAggregatorProxy are used to make changes to the contract. An example of a write call would be to change the associated AccessControlledOffchainAggregator


AccessControlledOffchainAggregator

LINK / USD AccessControlledOffchainAggregator contract address: 0xDfd03BfC3465107Ce570a0397b247F546a42D0fA

The Access Controlled Offchain Aggregator is the aggregator contract that the Chainlink Nodes report to.

This is the contract that contains the on-chain information that is read by the EACAggregatorProxy

Read Calls

blah

Write Calls

blah


Convenient Tools

When we make the requests to the contracts, we have to convert various values into different formats. For example converting the desired round ID to a uint80.

To assist in accomplishing this, there are a couple online tools that we can utilize.

Converting Request Types to Hex

We will need to convert each of the request types to their 4 bit (eight character) hex value

To do this, simply navitgate to emn178.github.io/online-tools/keccak_256.html and copy the desired request into the top box and copy the first 8 characters in the bottom box.

We will want to append a leading 0x in front of the request data.

Example below:

Let's say we want to query getAnswer for round number 92233720368547771539.

GetAnswerExample.png

We'd need to convert getAnswer(uint256) to the first 4 bytes of its hex value, which is b5ab58dc, and then with a leading 0x, it would be 0xb5ab58dc.

ConvertRequestToHex.png

For some requests, we'll also need to include specific data in our read/write request to the contract.

In that case, we would need to convert that data to the expected format before sending.

Convert Request Data To Different Types

Maintaining the above example, we will need to include the desired round number to get the on-chain response from the Chainlink Nodes.

We can make use of the tooling available at adibas03.github.io/online-ethereum-abi-encoder-decoder/#/encode to convert our data to any of the expected types.

In this example, we will need to convert our desired round number, 92233720368547771514, to a uint256.

the uint256 equivalant of 92233720368547771514 is 000000000000000000000000000000000000000000000005000000000000347a

ConvertRequestDataToHex.png

Formatting the Request Data

Now that we have the necessary data to include in our request, we need to combine it into a single hex value.

To accomplish this, we start with the desired call name, in this case 0xb5ab58dc .

Then we append the round ID we converted, 000000000000000000000000000000000000000000000005000000000000347a, to the end of it.

Resulting in the request data below:

0xb5ab58dc000000000000000000000000000000000000000000000005000000000000347a