Skip to content

add docs for chainlink & switchboard #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apps/nextra/pages/en/build/guides/_meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ export default {
"system-integrators-guide": {
title: "Applications",
},
"---oracles---": {
type: "separator",
title: "Oracles",
},
pyth: {
title: "Pyth",
},
chainlink: {
title: "Chainlink",
},
switchboard: {
title: "Switchboard",
},
};
155 changes: 155 additions & 0 deletions apps/nextra/pages/en/build/guides/chainlink.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
---
Title: Use Chainlink Oracle in Your Aptos Applications
---

# Chainlink Oracle Integration on Aptos

This reference guide explains how to integrate **Chainlink Data Feeds** into your Aptos applications.

Chainlink provides tamper-proof, decentralized oracle data that powers the world's leading DeFi protocols and smart contract applications.

## Overview

[Chainlink](https://chain.link/) is the industry-standard decentralized oracle network that enables smart contracts to securely access off-chain data, APIs, and computations.

## How to Use Chainlink Data Feeds in Aptos Contracts

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.

### Configuring the Move.toml File

Add the Chainlink dependencies to your project in the `Move.toml` file:

```toml
[package]
name = "chainlink-example"
version = "1.0.0"
authors = []

[addresses]
sender = "<YOUR_ACCOUNT_ADDRESS>"
owner = "<YOUR_ACCOUNT_ADDRESS>"
data_feeds = "0xf1099f135ddddad1c065203431be328a408b0ca452ada70374ce26bd2b32fdd3" # Testnet
platform = "0x516e771e1b4a903afe74c27d057c65849ecc1383782f6642d7ff21425f4f9c99"
move_stdlib = "0x1"
aptos_std = "0x1"

[dev-addresses]

[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main" }
MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib", rev = "main" }
ChainlinkDataFeeds = { local = "./ChainlinkDataFeeds" }
```

**Note**: Replace `<YOUR_ACCOUNT_ADDRESS>` with your actual Aptos account address. You can find this in `~/.aptos/config.yaml` or by running:
```bash
aptos account list --query balance
```

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.

### Download Chainlink Dependencies

Download the compiled bytecode for the Chainlink packages:

```bash
aptos move download --account 0x516e771e1b4a903afe74c27d057c65849ecc1383782f6642d7ff21425f4f9c99 --package ChainlinkPlatform && \
aptos move download --account 0xccad6853cabea164842907df3de4f89bb34be5bf249bbf16939f9c90db1bf63b --package ChainlinkDataFeeds
```

Update the ChainlinkDataFeeds package configuration file (`ChainlinkDataFeeds/Move.toml`) to point to your local ChainlinkPlatform dependency:

```toml
ChainlinkPlatform = { local = "../ChainlinkPlatform" }
```

### Write Contract Code

Create a Move module that interacts with Chainlink Data Feeds. This example fetches and stores price data:

```move
module sender::MyOracleContractTest {
use std::vector;
use std::signer;
use data_feeds::router::get_benchmarks;
use data_feeds::registry::{Benchmark, get_benchmark_value, get_benchmark_timestamp};
use move_stdlib::option::{Option, some, none};

struct PriceData has copy, key, store {
/// The price value with 18 decimal places of precision
price: u256,
/// Unix timestamp in seconds
timestamp: u256,
}

// Function to fetch and store the price data for a given feed ID
public entry fun fetch_price(account: &signer, feed_id: vector<u8>) acquires PriceData {
let feed_ids = vector[feed_id]; // Use the passed feed_id
let billing_data = vector[];
let benchmarks: vector<Benchmark> = get_benchmarks(account, feed_ids, billing_data);
let benchmark = vector::pop_back(&mut benchmarks);
let price: u256 = get_benchmark_value(&benchmark);
let timestamp: u256 = get_benchmark_timestamp(&benchmark);

// Check if PriceData exists and update it
if (exists<PriceData>(signer::address_of(account))) {
let data = borrow_global_mut<PriceData>(signer::address_of(account));
data.price = price;
data.timestamp = timestamp;
} else {
// If PriceData does not exist, create a new one
move_to(account, PriceData { price, timestamp });
}
}

// View function to get the stored price data
#[view]
public fun get_price_data(account_address: address): Option<PriceData> acquires PriceData {
if (exists<PriceData>(account_address)) {
let data = borrow_global<PriceData>(account_address);
some(*data)
} else {
none()
}
}
}

```

### Compile and Publish Your Contract

Compile your Move package:

```bash
aptos move compile --named-addresses sender=<YOUR_ACCOUNT_ADDRESS>
```

Publish the contract to Aptos testnet:

```bash
aptos move publish --named-addresses sender=<YOUR_ACCOUNT_ADDRESS>
```

### Available Feed IDs

Chainlink Data Feeds on Aptos use specific feed IDs to identify different price pairs. Here are some common feed IDs for testnet:

| Asset Pair | Feed ID |
|------------|---------|
| BTC/USD | `0x01a0b4d920000332000000000000000000000000000000000000000000000000` |
| ETH/USD | `0x01d585327c000332000000000000000000000000000000000000000000000000` |
| APT/USD | `0x011e22d6bf000332000000000000000000000000000000000000000000000000` |

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).

## Resources

### Documentation
- [Official Chainlink Aptos Documentation](https://docs.chain.link/data-feeds/aptos)
- [Chainlink Data Feeds Overview](https://docs.chain.link/data-feeds)
- [Feed Addresses and IDs](https://docs.chain.link/data-feeds/price-feeds/addresses?page=1&testnetPage=1&network=aptos)

### Community Support
- [Chainlink Discord](https://discord.gg/aSK4zew)
- [Aptos Developer Community](https://discord.gg/aptoslabs)
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
---
Title: Use Oracles in Your Aptos Applications
Title: Use Pyth Oracle in Your Aptos Applications
---

import { Callout } from "nextra/components";

# Oracles
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.

## Pyth Network

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).
Expand Down
111 changes: 111 additions & 0 deletions apps/nextra/pages/en/build/guides/switchboard.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
Title: Use Switchboard Oracle in Your Aptos Applications
---

# Switchboard Oracle Integration on Aptos

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.

## Overview

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.

## How to Use Switchboard On-Demand Feeds in Aptos Contracts

This guide explains how to integrate Switchboard's on-demand oracle feeds into your Aptos Move applications.

### Configuring the Move.toml File

Add the Switchboard contract to your project dependencies in the `Move.toml` file:

```toml
[addresses]
switchboard = "0x890fd4ed8a26198011e7923f53f5f1e5eeb2cc389dd50b938f16cb95164dc81c"

[dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework", rev = "main" }
Switchboard = { git = "https://github.com/switchboard-xyz/aptos.git", subdir = "mainnet", rev = "main" }
```

For testnet development, use the testnet subdir:
```toml
Switchboard = { git = "https://github.com/switchboard-xyz/aptos.git", subdir = "testnet", rev = "main" }
```

### Write Contract Code

The code snippet below provides an example module for fetching price data from Switchboard feeds:

```move
module example::example_basic_read {
use aptos_std::event;
use aptos_framework::object::{Self, Object};
use aptos_framework::aptos_coin::AptosCoin;
use switchboard::aggregator::{Self, Aggregator, CurrentResult};
use switchboard::decimal::Decimal;
use switchboard::update_action;

#[event]
struct AggregatorUpdated has drop, store {
aggregator: address,
value: Decimal,
timestamp: u64,
}

public entry fun update_and_read_feed(
account: &signer,
update_data: vector<vector<u8>>,
) {

// Update the feed with the provided data
update_action::run<AptosCoin>(account, update_data);

// Get the feed object - here it's testnet BTC/USD
let aggregator: address = @0x4bac6bbbecfe7be5298358deaf1bf2da99c697fea16a3cf9b0e340cb557b05a8;
let aggregator: Object<Aggregator> = object::address_to_object<Aggregator>(aggregator);

// Get the latest update info for the feed
let current_result: CurrentResult = aggregator::current_result(aggregator);

// Access various result properties
let result: Decimal = aggregator::result(&current_result); // Update result
let timestamp_seconds = aggregator::timestamp(&current_result); // Timestamp in seconds

// Emit an event with the updated result
event::emit(AggregatorUpdated {
aggregator: object::object_address(&aggregator),
value: result,
timestamp: timestamp_seconds,
});
}
}
```

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.

The code snippet above does the following:
- Calls `update_action::run` to update the Switchboard aggregator with fresh data
- Gets the aggregator object using the feed address
- Calls `aggregator::current_result` to read the latest aggregated data
- Extracts various statistical properties including price, timestamp.


### Core Functions

- `update_action::run<CoinType>(account, update_data)` - Updates aggregator with new data
- `aggregator::current_result(aggregator)` - Gets the latest aggregated result
- `aggregator::result(current_result)` - Extracts the primary price value
- `aggregator::timestamp(current_result)` - Gets the timestamp of the latest update

## Resources

### Documentation
- [Official Switchboard Aptos Documentation](https://docs.switchboard.xyz/product-documentation/data-feeds/aptos)
- [Switchboard TypeScript SDK](https://www.npmjs.com/package/@switchboard-xyz/aptos-sdk)

### Example Applications
- [Basic Price Read Contract](https://github.com/switchboard-xyz/aptos/tree/main/examples/example_basic_read)

### Community Support
- [Switchboard Discord](https://discord.gg/switchboardxyz)
- [Aptos Developer Community](https://discord.gg/aptoslabs)
14 changes: 13 additions & 1 deletion apps/nextra/public/sitemap-en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,19 @@
<priority>0.7</priority>
</url>
<url>
<loc>https://aptos.dev/en/build/guides/oracles</loc>
<loc>https://aptos.dev/en/build/guides/pyth</loc>
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://aptos.dev/en/build/guides/chainlink</loc>
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://aptos.dev/en/build/guides/switchbboard</loc>
<lastmod>2025-04-21T21:41:29.689Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
Expand Down
14 changes: 13 additions & 1 deletion apps/nextra/public/sitemap-ja.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,19 @@
<priority>0.7</priority>
</url>
<url>
<loc>http://localhost:3030/ja/build/guides/oracles</loc>
<loc>http://localhost:3030/ja/build/guides/chainlink</loc>
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>http://localhost:3030/ja/build/guides/pyth</loc>
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>http://localhost:3030/ja/build/guides/switchboard</loc>
<lastmod>2025-03-05T08:51:16.608Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
Expand Down
Loading