Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 9c42198

Browse files
committed
add docs for chainlink & switchboard
1 parent 90de147 commit 9c42198

File tree

6 files changed

+306
-6
lines changed

6 files changed

+306
-6
lines changed

apps/nextra/pages/en/build/guides/_meta.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,17 @@ export default {
5353
"system-integrators-guide": {
5454
title: "Applications",
5555
},
56+
"---oracles---": {
57+
type: "separator",
58+
title: "Oracles",
59+
},
60+
pyth: {
61+
title: "Pyth",
62+
},
63+
chainlink: {
64+
title: "Chainlink",
65+
},
66+
switchboard: {
67+
title: "Switchboard",
68+
},
5669
};
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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)

apps/nextra/pages/en/build/guides/oracles.mdx renamed to apps/nextra/pages/en/build/guides/pyth.mdx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
---
2-
Title: Use Oracles in Your Aptos Applications
2+
Title: Use Pyth Oracle in Your Aptos Applications
33
---
44

55
import { Callout } from "nextra/components";
66

7-
# Oracles
8-
This reference guide presents various Oracles that you can utilize while building on Aptos. Oracles supply offchain data to the blockchain, enabling smart contracts to access a diverse range of information.
9-
107
## Pyth Network
118

129
The [Pyth Network](https://pyth.network/) is one of the largest first-party Oracle network, delivering real-time data across [a vast number of chains](https://docs.pyth.network/price-feeds/contract-addresses).
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Title: Use Switchboard Oracle in Your Aptos Applications
3+
---
4+
5+
# Switchboard Oracle Integration on Aptos
6+
7+
This reference guide explains how to integrate **Switchboard** oracles into your Aptos applications. Switchboard provides on-demand, pull-based oracle feeds that give developers granular control over data updates and enable custom data source integration.
8+
9+
## Overview
10+
11+
Switchboard is a decentralized oracle network that enables smart contracts to access real-world data through customizable, on-demand feeds. Unlike traditional push-based oracles, Switchboard uses a pull-based mechanism where developers control when and how frequently data is updated.
12+
13+
## How to Use Switchboard On-Demand Feeds in Aptos Contracts
14+
15+
This guide explains how to integrate Switchboard's on-demand oracle feeds into your Aptos Move applications.
16+
17+
### Configuring the Move.toml File
18+
19+
Add the Switchboard contract to your project dependencies in the `Move.toml` file:
20+
21+
```toml
22+
[addresses]
23+
switchboard = "0x890fd4ed8a26198011e7923f53f5f1e5eeb2cc389dd50b938f16cb95164dc81c"
24+
25+
[dependencies]
26+
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main" }
27+
Switchboard = { git = "https://github.com/switchboard-xyz/aptos.git", subdir = "mainnet", rev = "main" }
28+
```
29+
30+
For testnet development, use the testnet subdir:
31+
```toml
32+
Switchboard = { git = "https://github.com/switchboard-xyz/aptos.git", subdir = "testnet", rev = "main" }
33+
```
34+
35+
### Write Contract Code
36+
37+
The code snippet below provides an example module for fetching price data from Switchboard feeds:
38+
39+
```move
40+
module example::example_basic_read {
41+
use aptos_std::event;
42+
use aptos_framework::object::{Self, Object};
43+
use aptos_framework::aptos_coin::AptosCoin;
44+
use switchboard::aggregator::{Self, Aggregator, CurrentResult};
45+
use switchboard::decimal::Decimal;
46+
use switchboard::update_action;
47+
48+
#[event]
49+
struct AggregatorUpdated has drop, store {
50+
aggregator: address,
51+
value: Decimal,
52+
timestamp: u64,
53+
}
54+
55+
public entry fun update_and_read_feed(
56+
account: &signer,
57+
update_data: vector<vector<u8>>,
58+
) {
59+
60+
// Update the feed with the provided data
61+
update_action::run<AptosCoin>(account, update_data);
62+
63+
// Get the feed object - here it's testnet BTC/USD
64+
let aggregator: address = @0x4bac6bbbecfe7be5298358deaf1bf2da99c697fea16a3cf9b0e340cb557b05a8;
65+
let aggregator: Object<Aggregator> = object::address_to_object<Aggregator>(aggregator);
66+
67+
// Get the latest update info for the feed
68+
let current_result: CurrentResult = aggregator::current_result(aggregator);
69+
70+
// Access various result properties
71+
let result: Decimal = aggregator::result(&current_result); // Update result
72+
let timestamp_seconds = aggregator::timestamp(&current_result); // Timestamp in seconds
73+
74+
// Emit an event with the updated result
75+
event::emit(AggregatorUpdated {
76+
aggregator: object::object_address(&aggregator),
77+
value: result,
78+
timestamp: timestamp_seconds,
79+
});
80+
}
81+
}
82+
```
83+
84+
The `update_data` argument contains the latest oracle responses from Switchboard. Calling `update_action::run` with this value updates the on-chain aggregator and ensures your application has recent price data. The `update_data` can be fetched using the Switchboard TypeScript SDK.
85+
86+
The code snippet above does the following:
87+
- Calls `update_action::run` to update the Switchboard aggregator with fresh data
88+
- Gets the aggregator object using the feed address
89+
- Calls `aggregator::current_result` to read the latest aggregated data
90+
- Extracts various statistical properties including price, timestamp.
91+
92+
93+
### Core Functions
94+
95+
- `update_action::run<CoinType>(account, update_data)` - Updates aggregator with new data
96+
- `aggregator::current_result(aggregator)` - Gets the latest aggregated result
97+
- `aggregator::result(current_result)` - Extracts the primary price value
98+
- `aggregator::timestamp(current_result)` - Gets the timestamp of the latest update
99+
100+
## Resources
101+
102+
### Documentation
103+
- [Official Switchboard Aptos Documentation](https://docs.switchboard.xyz/product-documentation/data-feeds/aptos)
104+
- [Switchboard TypeScript SDK](https://www.npmjs.com/package/@switchboard-xyz/aptos-sdk)
105+
106+
### Example Applications
107+
- [Basic Price Read Contract](https://github.com/switchboard-xyz/aptos/tree/main/examples/example_basic_read)
108+
109+
### Community Support
110+
- [Switchboard Discord](https://discord.gg/switchboardxyz)
111+
- [Aptos Developer Community](https://discord.gg/aptoslabs)

apps/nextra/public/sitemap-en.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,19 @@
361361
<priority>0.7</priority>
362362
</url>
363363
<url>
364-
<loc>https://aptos.dev/en/build/guides/oracles</loc>
364+
<loc>https://aptos.dev/en/build/guides/pyth</loc>
365+
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
366+
<changefreq>daily</changefreq>
367+
<priority>0.7</priority>
368+
</url>
369+
<url>
370+
<loc>https://aptos.dev/en/build/guides/chainlink</loc>
371+
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
372+
<changefreq>daily</changefreq>
373+
<priority>0.7</priority>
374+
</url>
375+
<url>
376+
<loc>https://aptos.dev/en/build/guides/switchbboard</loc>
365377
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
366378
<changefreq>daily</changefreq>
367379
<priority>0.7</priority>

apps/nextra/public/sitemap-ja.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,19 @@
367367
<priority>0.7</priority>
368368
</url>
369369
<url>
370-
<loc>http://localhost:3030/ja/build/guides/oracles</loc>
370+
<loc>http://localhost:3030/ja/build/guides/chainlink</loc>
371+
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
372+
<changefreq>daily</changefreq>
373+
<priority>0.7</priority>
374+
</url>
375+
<url>
376+
<loc>http://localhost:3030/ja/build/guides/pyth</loc>
377+
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
378+
<changefreq>daily</changefreq>
379+
<priority>0.7</priority>
380+
</url>
381+
<url>
382+
<loc>http://localhost:3030/ja/build/guides/switchboard</loc>
371383
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
372384
<changefreq>daily</changefreq>
373385
<priority>0.7</priority>

0 commit comments

Comments
 (0)