diff --git a/apps/nextra/pages/en/build/guides/_meta.tsx b/apps/nextra/pages/en/build/guides/_meta.tsx index 1c7d9f259..07a32a8b8 100644 --- a/apps/nextra/pages/en/build/guides/_meta.tsx +++ b/apps/nextra/pages/en/build/guides/_meta.tsx @@ -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", + }, }; diff --git a/apps/nextra/pages/en/build/guides/chainlink.mdx b/apps/nextra/pages/en/build/guides/chainlink.mdx new file mode 100644 index 000000000..c7642b375 --- /dev/null +++ b/apps/nextra/pages/en/build/guides/chainlink.mdx @@ -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 = "" +owner = "" +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 `` 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) acquires PriceData { + let feed_ids = vector[feed_id]; // Use the passed feed_id + let billing_data = vector[]; + let benchmarks: vector = 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(signer::address_of(account))) { + let data = borrow_global_mut(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 acquires PriceData { + if (exists(account_address)) { + let data = borrow_global(account_address); + some(*data) + } else { + none() + } + } +} + +``` + +### Compile and Publish Your Contract + +Compile your Move package: + +```bash +aptos move compile --named-addresses sender= +``` + +Publish the contract to Aptos testnet: + +```bash +aptos move publish --named-addresses sender= +``` + +### 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) \ No newline at end of file diff --git a/apps/nextra/pages/en/build/guides/oracles.mdx b/apps/nextra/pages/en/build/guides/pyth.mdx similarity index 96% rename from apps/nextra/pages/en/build/guides/oracles.mdx rename to apps/nextra/pages/en/build/guides/pyth.mdx index 49ccedab3..0530bf9a3 100644 --- a/apps/nextra/pages/en/build/guides/oracles.mdx +++ b/apps/nextra/pages/en/build/guides/pyth.mdx @@ -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). diff --git a/apps/nextra/pages/en/build/guides/switchboard.mdx b/apps/nextra/pages/en/build/guides/switchboard.mdx new file mode 100644 index 000000000..9a82d61dd --- /dev/null +++ b/apps/nextra/pages/en/build/guides/switchboard.mdx @@ -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>, + ) { + + // Update the feed with the provided data + update_action::run(account, update_data); + + // Get the feed object - here it's testnet BTC/USD + let aggregator: address = @0x4bac6bbbecfe7be5298358deaf1bf2da99c697fea16a3cf9b0e340cb557b05a8; + let aggregator: Object = object::address_to_object(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(¤t_result); // Update result + let timestamp_seconds = aggregator::timestamp(¤t_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(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) diff --git a/apps/nextra/public/sitemap-en.xml b/apps/nextra/public/sitemap-en.xml index 68410eaf9..5dbc6822f 100644 --- a/apps/nextra/public/sitemap-en.xml +++ b/apps/nextra/public/sitemap-en.xml @@ -361,7 +361,19 @@ 0.7 - https://aptos.dev/en/build/guides/oracles + https://aptos.dev/en/build/guides/pyth + 2025-04-21T21:41:29.689Z + daily + 0.7 + + + https://aptos.dev/en/build/guides/chainlink + 2025-04-21T21:41:29.689Z + daily + 0.7 + + + https://aptos.dev/en/build/guides/switchbboard 2025-04-21T21:41:29.689Z daily 0.7 diff --git a/apps/nextra/public/sitemap-ja.xml b/apps/nextra/public/sitemap-ja.xml index f2a110217..a3524f98c 100644 --- a/apps/nextra/public/sitemap-ja.xml +++ b/apps/nextra/public/sitemap-ja.xml @@ -367,7 +367,19 @@ 0.7 - http://localhost:3030/ja/build/guides/oracles + http://localhost:3030/ja/build/guides/chainlink + 2025-03-05T08:51:16.608Z + daily + 0.7 + + + http://localhost:3030/ja/build/guides/pyth + 2025-03-05T08:51:16.608Z + daily + 0.7 + + + http://localhost:3030/ja/build/guides/switchboard 2025-03-05T08:51:16.608Z daily 0.7