You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tools/oracles/chainlink.md
+6-184Lines changed: 6 additions & 184 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,25 +8,25 @@
8
8
9
9
One of Chainlink's most powerful features is already decentralized, aggregated, and ready to be digested on-chain data on most of the popular cryptocurrencies. These are known as [**Chainlink Data Feeds**](https://docs.chain.link/docs/using-chainlink-reference-contracts).
10
10
11
-
Here is a working example of a contract that pulls the latest price of MATIC in USD on the Mumbai Testnet.
11
+
Here is a working example of a contract that pulls the latest price of POL in USD on the Amoy Testnet.
12
12
13
13
All you need to do is swap out the address [with any address of a data feed](https://docs.chain.link/docs/matic-addresses#config) that you wish, and you can start digesting price information.
Chainlink's Request and Receive cycle enables your smart contracts to make a request to any external API and consume the response. To implement it, your contract needs to define two functions:
51
-
52
-
1. One to **request the data**, and
53
-
2. Another to **receive the response**.
54
-
55
-
To request data, your contract builds a `request` object which it provides to an oracle. Once the oracle has reached out to the API and parsed the response, it will attempt to send the data back to your contract using the callback function defined in your smart contract.
56
-
57
-
## Uses
58
-
59
-
1.**Chainlink Data Feeds**
60
-
61
-
These are decentralized data reference points already aggregated on-chain, and the quickest, easiest, and cheapest way to get data from the real world. Currently supports some of the most popular cryptocurrency and fiat pairs.
62
-
63
-
For working with Data Feeds, use the [**Polygon Data Feeds**](https://docs.chain.link/data-feeds/price-feeds/addresses/?network=polygon) from the Chainlink documentation.
64
-
65
-
2.**Chainlink Verifiable Randomness Function**
66
-
67
-
Get provably random numbers, where the random number is cryptographically guaranteed to be random.
68
-
69
-
For working with Chainlink VRF, use the [**Polygon VRF**](https://docs.chain.link/vrf/v2/subscription/supported-networks) addresses from the [Chainlink documentation](https://docs.chain.link/vrf/v2/subscription/examples/get-a-random-number).
70
-
71
-
3.**Chainlink API Calls**
72
-
73
-
How to configure your smart contract to work with traditional APIs, and customize to get any data, send any requests over the internet, and more.
74
-
75
-
## Code example
76
-
77
-
To interact with external APIs, your smart contract should inherit from [`ChainlinkClient.sol`](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.6/ChainlinkClient.sol), which is a contract designed to make processing requests easy. It exposes a struct called `Chainlink.Request`, which your contract should use to build the API request.
78
-
79
-
The request should define the oracle address, job id, fee, adapter parameters, and the callback function signature. In this example, the request is built in the `requestEthereumPrice` function.
function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId)
152
-
{
153
-
price = _price;
154
-
}
155
-
}
156
-
```
157
-
158
-
## Mainnet Polygon LINK token
159
-
160
-
To get mainnet Polygon LINK token from the Ethereum Mainnet, you must follow a 2-step process.
161
-
162
-
1. Bridge your LINK using the [PoS bridge](https://wallet.polygon.technology/bridge).
163
-
2. Swap the LINK for the ERC677 version via the [Pegswap, deployed by the Chainlink](https://pegswap.chain.link/).
164
-
165
-
The Polygon bridge brings over an ERC20 version of LINK, and LINK is an ERC677, so we just have to update it with this swap.
166
-
167
-
## Addresses
168
-
169
-
There are currently only a few operational Chainlink oracles on the Polygon Mumbai Testnet. You can always run one yourself too, and list it on the Chainlink Marketplace.
To obtain LINK on Mumbai Testnet, head to the [Polygon faucet here](https://faucet.polygon.technology/).
175
-
176
-
## Supported APIs
177
-
178
-
Chainlink's Request and Receive cycle is flexible enough to call any public API, so long as the request parameters are correct and the response format is known. For example, if the response object from a URL we want to fetch from is formatted like this: `{"USD":243.33}`, the path is simple: `"USD"`.
179
-
180
-
If an API responds with a complex JSON object, the **path** parameter would need to specify where to retrieve the desired data, using a dot delimited string for nested objects. For example, consider the following response:
181
-
182
-
```json
183
-
{
184
-
"Prices":{
185
-
"USD":243.33
186
-
}
187
-
}
188
-
```
189
-
190
-
This would require the following path: `"Prices.USD"`. If there are spaces in the strings, or the strings are quite long, we can use the syntax shown in the example above, where we pass them all as a string array.
191
-
192
-
```json
193
-
string[] memory path = new string[](2);
194
-
path[0] = "Prices";
195
-
path[1] = "USD";
196
-
request.addStringArray("path", path);
197
-
```
198
-
199
-
## What are job IDs for?
200
-
201
-
You may have noticed that our [example](#code-example) uses a `jobId` parameter when building the request. Jobs are comprised of a sequence of instructions that an oracle is configured to run. In the [code example](#code-example) above, the contract makes a request to the oracle with the job ID: `da20aae0e4c843f6949e5cb3f7cfe8c4`. This particular job is configured to do the following:
202
-
203
-
* Make a GET request
204
-
* Parse the JSON response
205
-
* Multiply the value by *x*
206
-
* Convert the value to `uint`
207
-
* Submit to the chain
208
-
209
-
This is why our contract adds in the URL, the path of where to find the desired data in the JSON response, and the times amount to the request; using the `request.add` statements. These instructions are facilitated by what's known as Adapters, in the oracle.
210
-
211
-
**Every request to an oracle must include a specific job ID.**
212
-
213
-
Here is the list of jobs that the Polygon oracle is configured to run.
214
-
215
-
| Name | Return Type | ID | Adapters |
216
-
|-----|--------|------|-------|
217
-
| HTTP GET |`uint256`|`da20aae0e4c843f6949e5cb3f7cfe8c4`|`httpget`<br/>`jsonparse`<br/>`multiply`<br/>`ethuint256`<br/>`ethtx`|
218
-
| HTTP GET |`int256`|`e0c76e45462f4e429ba32c114bfbf5ac`|`httpget`<br/>`jsonparse`<br/>`multiply`<br/>`ethint256`<br/>`ethtx`|
219
-
| HTTP GET |`bool`|`999539ec63414233bdc989d8a8ff10aa`|`httpget`<br/>`jsonparse`<br/>`ethbool`<br/>`ethtx`|
220
-
| HTTP GET |`bytes32`|`a82495a8fd5b4cb492b17dc0cc31a4fe`|`httpget`<br/>`jsonparse`<br/>`ethbytes32`<br/>`ethtx`|
221
-
| HTTP GET |`string`|`7d80a6386ef543a3abb52817f6707e3b`|`httpget`<br/>`jsonparse`<br/>`ethstring`<br/>`ethtx`|
222
-
| HTTP POST |`bytes32`|`a82495a8fd5b4cb492b17dc0cc31a4fe`|`httppost`<br/>`jsonparse`<br/>`ethbytes32`<br/>`ethtx`|
223
-
224
-
The complete Chainlink API reference can be found [here](https://docs.chain.link/any-api/api-reference).
0 commit comments