Existing Job Request
Using an existing Oracle Job makes your smart contract code more succinct. This page explains how to retrieve the gas price from an existing Chainlink job that calls etherscan gas tracker API.
Example
In Single Word Response Example, the example contract code declared which URL to use, where to find the data in the response, and how to convert it so that it can be represented on-chain.
This example uses an existing job that is pre-configured to make requests to get the gas price. Using specialized jobs makes your contracts succinct and more simple.
Etherscan gas oracle returns the current Safe, Proposed and Fast gas prices. To check the response, you can directly paste the following URL in your browser https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=YourApiKeyToken
or run this command in your terminal:
curl -X 'GET' \
'https://api.etherscan.io/api?module=gastracker&action=gasoracle&apikey=YourApiKeyToken' \
-H 'accept: application/json'
The response should be similar to the following:
{
"status": "1",
"result": {
"LastBlock": "14653286",
"SafeGasPrice": "33",
"ProposeGasPrice": "33",
"FastGasPrice": "35",
"suggestBaseFee": "32.570418457",
"gasUsedRatio": "0.366502543599508,0.15439818258491,0.9729006,0.4925609,0.999657066666667"
}
}
For this example, we created a job that leverages the EtherScan External Adapter to fetch the SafeGasPrice , ProposeGasPrice and FastGasPrice. You can learn more about External Adapters here.
To consume an API, your contract must import ChainlinkClient.sol. This contract exposes a struct named Chainlink.Request
, which your contract can use to build the API request. The request must include the following parameters:
- Link token address
- Oracle address
- Job id
- Request fee
- Task parameters
- Callback function signature
Making a GET request will fail unless your deployed contract has enough LINK to pay for it. Learn how to Acquire testnet LINK and Fund your contract.
To use this contract:
-
Open the contract in Remix.
-
Compile and deploy the contract using the Injected Provider environment. The contract includes all the configuration variables for the Sepolia testnet. Make sure your wallet is set to use Sepolia. The constructor sets the following parameters:
- The Chainlink Token address for Sepolia by calling the
setChainlinkToken
function. - The Oracle contract address for Sepolia by calling the
setChainlinkOracle
function. - The
jobId
: A specific job for the oracle node to run. In this case, the job is very specific to the use case as it returns the gas prices. You can find the job spec for the Chainlink node here.
- The Chainlink Token address for Sepolia by calling the
-
Fund your contract with 0.1 LINK. To learn how to send LINK to contracts, read the Fund Your Contracts page.
-
Call the
gasPriceFast
,gasPriceAverage
andgasPriceSafe
functions to confirm that thegasPriceFast
,gasPriceAverage
andgasPriceSafe
state variables are equal to zero. -
Run the
requestGasPrice
function. This builds theChainlink.Request
. Note how succinct the request is. -
After few seconds, call the
gasPriceFast
,gasPriceAverage
andgasPriceSafe
functions. You should get a non-zero responses.