|
| 1 | +--- |
| 2 | +Title: Use Chainlink Oracle in Your Aptos Applications |
| 3 | +--- |
| 4 | + |
| 5 | +# Chainlink Oracle Integration on Aptos |
| 6 | + |
| 7 | +This reference guide explains how to integrate **Chainlink Data Feeds** into your Aptos applications. |
| 8 | + |
| 9 | +Chainlink provides tamper-proof, decentralized oracle data that powers the world's leading DeFi protocols and smart contract applications. |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +[Chainlink](https://chain.link/) is the industry-standard decentralized oracle network that enables smart contracts to securely access off-chain data, APIs, and computations. |
| 14 | + |
| 15 | +## How to Use Chainlink Data Feeds in Aptos Contracts |
| 16 | + |
| 17 | +This guide explains how to integrate Chainlink's reliable data feeds into your Aptos Move applications using the Benchmark structure provided by the data feeds contract. |
| 18 | + |
| 19 | +### Configuring the Move.toml File |
| 20 | + |
| 21 | +Add the Chainlink dependencies to your project in the `Move.toml` file: |
| 22 | + |
| 23 | +```toml |
| 24 | +[package] |
| 25 | +name = "chainlink-example" |
| 26 | +version = "1.0.0" |
| 27 | +authors = [] |
| 28 | + |
| 29 | +[addresses] |
| 30 | +sender = "<YOUR_ACCOUNT_ADDRESS>" |
| 31 | +owner = "<YOUR_ACCOUNT_ADDRESS>" |
| 32 | +data_feeds = "0xf1099f135ddddad1c065203431be328a408b0ca452ada70374ce26bd2b32fdd3" # Testnet |
| 33 | +platform = "0x516e771e1b4a903afe74c27d057c65849ecc1383782f6642d7ff21425f4f9c99" |
| 34 | +move_stdlib = "0x1" |
| 35 | +aptos_std = "0x1" |
| 36 | + |
| 37 | +[dev-addresses] |
| 38 | + |
| 39 | +[dependencies] |
| 40 | +AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main" } |
| 41 | +MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib", rev = "main" } |
| 42 | +ChainlinkDataFeeds = { local = "./ChainlinkDataFeeds" } |
| 43 | +``` |
| 44 | + |
| 45 | +**Note**: Replace `<YOUR_ACCOUNT_ADDRESS>` with your actual Aptos account address. You can find this in `~/.aptos/config.yaml` or by running: |
| 46 | +```bash |
| 47 | +aptos account list --query balance |
| 48 | +``` |
| 49 | + |
| 50 | +The `data_feeds` address is the Chainlink Data Feeds contract address on Aptos testnet. For mainnet, consult the [official Chainlink documentation](https://docs.chain.link/data-feeds/price-feeds/addresses?network=aptos&page=1&testnetPage=1) for the current contract addresses. |
| 51 | + |
| 52 | +### Download Chainlink Dependencies |
| 53 | + |
| 54 | +Download the compiled bytecode for the Chainlink packages: |
| 55 | + |
| 56 | +```bash |
| 57 | +aptos move download --account 0x516e771e1b4a903afe74c27d057c65849ecc1383782f6642d7ff21425f4f9c99 --package ChainlinkPlatform && \ |
| 58 | +aptos move download --account 0xccad6853cabea164842907df3de4f89bb34be5bf249bbf16939f9c90db1bf63b --package ChainlinkDataFeeds |
| 59 | +``` |
| 60 | + |
| 61 | +Update the ChainlinkDataFeeds package configuration file (`ChainlinkDataFeeds/Move.toml`) to point to your local ChainlinkPlatform dependency: |
| 62 | + |
| 63 | +```toml |
| 64 | +ChainlinkPlatform = { local = "../ChainlinkPlatform" } |
| 65 | +``` |
| 66 | + |
| 67 | +### Write Contract Code |
| 68 | + |
| 69 | +Create a Move module that interacts with Chainlink Data Feeds. This example fetches and stores price data: |
| 70 | + |
| 71 | +```move |
| 72 | +module sender::MyOracleContractTest { |
| 73 | + use std::vector; |
| 74 | + use std::signer; |
| 75 | + use data_feeds::router::get_benchmarks; |
| 76 | + use data_feeds::registry::{Benchmark, get_benchmark_value, get_benchmark_timestamp}; |
| 77 | + use move_stdlib::option::{Option, some, none}; |
| 78 | +
|
| 79 | + struct PriceData has copy, key, store { |
| 80 | + /// The price value with 18 decimal places of precision |
| 81 | + price: u256, |
| 82 | + /// Unix timestamp in seconds |
| 83 | + timestamp: u256, |
| 84 | + } |
| 85 | +
|
| 86 | + // Function to fetch and store the price data for a given feed ID |
| 87 | + public entry fun fetch_price(account: &signer, feed_id: vector<u8>) acquires PriceData { |
| 88 | + let feed_ids = vector[feed_id]; // Use the passed feed_id |
| 89 | + let billing_data = vector[]; |
| 90 | + let benchmarks: vector<Benchmark> = get_benchmarks(account, feed_ids, billing_data); |
| 91 | + let benchmark = vector::pop_back(&mut benchmarks); |
| 92 | + let price: u256 = get_benchmark_value(&benchmark); |
| 93 | + let timestamp: u256 = get_benchmark_timestamp(&benchmark); |
| 94 | +
|
| 95 | + // Check if PriceData exists and update it |
| 96 | + if (exists<PriceData>(signer::address_of(account))) { |
| 97 | + let data = borrow_global_mut<PriceData>(signer::address_of(account)); |
| 98 | + data.price = price; |
| 99 | + data.timestamp = timestamp; |
| 100 | + } else { |
| 101 | + // If PriceData does not exist, create a new one |
| 102 | + move_to(account, PriceData { price, timestamp }); |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | + // View function to get the stored price data |
| 107 | + #[view] |
| 108 | + public fun get_price_data(account_address: address): Option<PriceData> acquires PriceData { |
| 109 | + if (exists<PriceData>(account_address)) { |
| 110 | + let data = borrow_global<PriceData>(account_address); |
| 111 | + some(*data) |
| 112 | + } else { |
| 113 | + none() |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +
|
| 118 | +``` |
| 119 | + |
| 120 | +### Compile and Publish Your Contract |
| 121 | + |
| 122 | +Compile your Move package: |
| 123 | + |
| 124 | +```bash |
| 125 | +aptos move compile --named-addresses sender=<YOUR_ACCOUNT_ADDRESS> |
| 126 | +``` |
| 127 | + |
| 128 | +Publish the contract to Aptos testnet: |
| 129 | + |
| 130 | +```bash |
| 131 | +aptos move publish --named-addresses sender=<YOUR_ACCOUNT_ADDRESS> |
| 132 | +``` |
| 133 | + |
| 134 | +### Available Feed IDs |
| 135 | + |
| 136 | +Chainlink Data Feeds on Aptos use specific feed IDs to identify different price pairs. Here are some common feed IDs for testnet: |
| 137 | + |
| 138 | +| Asset Pair | Feed ID | |
| 139 | +|------------|---------| |
| 140 | +| BTC/USD | `0x01a0b4d920000332000000000000000000000000000000000000000000000000` | |
| 141 | +| ETH/USD | `0x01d585327c000332000000000000000000000000000000000000000000000000` | |
| 142 | +| APT/USD | `0x011e22d6bf000332000000000000000000000000000000000000000000000000` | |
| 143 | + |
| 144 | +For the complete list of available feeds and their IDs, visit the [Chainlink Feed Addresses page](https://docs.chain.link/data-feeds/aptos#feed-addresses). |
| 145 | + |
| 146 | +## Resources |
| 147 | + |
| 148 | +### Documentation |
| 149 | +- [Official Chainlink Aptos Documentation](https://docs.chain.link/data-feeds/aptos) |
| 150 | +- [Chainlink Data Feeds Overview](https://docs.chain.link/data-feeds) |
| 151 | +- [Feed Addresses and IDs](https://docs.chain.link/data-feeds/price-feeds/addresses?page=1&testnetPage=1&network=aptos) |
| 152 | + |
| 153 | +### Community Support |
| 154 | +- [Chainlink Discord](https://discord.gg/aSK4zew) |
| 155 | +- [Aptos Developer Community](https://discord.gg/aptoslabs) |
0 commit comments