From 3e261de383de76b2067df60799051bbea2b146f3 Mon Sep 17 00:00:00 2001 From: Lucas Tortora Date: Thu, 25 Jan 2024 10:22:28 -0300 Subject: [PATCH 1/4] add intro to oracles + current oracles on shimmer article --- docs/build/getting-started/oracles.md | 124 +++++++++++++++++++++++++ docs/build/getting-started/sidebars.ts | 5 + 2 files changed, 129 insertions(+) create mode 100644 docs/build/getting-started/oracles.md diff --git a/docs/build/getting-started/oracles.md b/docs/build/getting-started/oracles.md new file mode 100644 index 00000000000..4de40c80f29 --- /dev/null +++ b/docs/build/getting-started/oracles.md @@ -0,0 +1,124 @@ +--- +description: 'An introductory guide to oracles in blockchain, explaining their role in providing external data to smart + contracts and their implementation in Shimmer EVM.' +image: /img/logo/WASP_logo_dark.png +keywords: +- Blockchain Oracles +- Smart Contracts +- Shimmer EVM +- Pyth Network +- Supra Oracles +--- + +# Introduction to Oracles in Blockchain + +Smart contracts are powerful tools in the blockchain ecosystem, enabling automated, self-executing agreements based on +predefined conditions. However, one of their fundamental limitations is that they operate in a sandboxed environment. +This isolation ensures that smart contracts produce the same results every time they run, maintaining the deterministic +nature essential for blockchain's integrity and trust. But this also means smart contracts cannot directly access +or interact with external data or systems. + +## Bridging the Gap with Oracles + +Oracles serve as a critical bridge in this scenario, connecting the isolated world of smart contracts with external data +sources. They play a pivotal role in expanding the functionality of smart contracts by providing them +with real-world information. Oracles fetch data from various external sources, like internet APIs, real-world sensors, +and other data feeds, and then relay this information to the smart contracts on the blockchain. + +### Ensuring Idempotent Results + +The key challenge for oracles in the blockchain context is to provide data with enough specificity and reliability to +guarantee idempotent results - the property that a smart contract will produce the same outcome every time it's executed +under the same conditions. This consistency is crucial for maintaining trust and predictability in the blockchain +environment. + +Oracles achieve this by sourcing data from reliable and verified external sources and using consensus mechanisms or +multiple data points to validate the information's accuracy. This process ensures that the data provided to the smart +contracts is relevant and timely, maintaining the integrity and deterministic nature of the blockchain +operations. + +## Oracles on Shimmer EVM + +### Supra + +Supra Oracles validate and report prices derived from up to 21 data sources with Byzantine Fault Tolerant algorithms +for a wide array of assets and commodities using its own Distributed Oracle Agreement (DORA) to provide data for crypto +prices, forex, equities, and even Web3 data from other blockchains. Its feed gets updated from a minimum of 3 to 8 sources; if a data pair fails to meet this criterion, it gets deprecated to ensure data reliability. + +:::tip + +You can learn more about Supra Oracles in their [official documentation](https://supraoracles.com/docs/overview). + +::: + +### Pyth + +The Pyth Network delivers real-time financial market data across multiple blockchains and can be used in off-chain applications. +Pyth gets contributions from over 90 publishers, including several exchanges. It offers comprehensive and accurate price +feeds for asset classes such as US equities, commodities, and cryptocurrencies. Its price feeds get updates +multiple times per second, ensuring they are current and reliable. + +:::tip + +You can learn more about Pyth Oracles in their [official documentation](https://docs.pyth.network/documentation). + +::: + +#### Example Code + +You can use the following example code to get data from Pyth Oracles in Shimmer EVM: + +```typescript +import ethers from "ethers"; +import fetch from "node-fetch"; + +// Provider +const providerURL = "https://json-rpc.evm.shimmer.network" +const provider = new ethers.providers.JsonRpcProvider(providerURL); + +// Pyth Oracle contract on ShimmerEVM Mainnet +const contractAddress = '0xA2aa501b19aff244D90cc15a4Cf739D2725B5729'; + +// Add Price Feed Ids obtained from https://pyth.network/developers/price-feed-ids#pyth-evm-stable +const priceFeedIds = [ + //SMR/USD + '0xaf5b9ac426ae79591fde6816bc3f043b5e06d5e442f52112f76249320df22449', + //BTC/USD + '0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43' +] + +async function getPrice() { + // Replace 'IPythJSONURL' with the actual URL where the IPyth ABI JSON is hosted + const pythAbiUrl = 'https://raw.githubusercontent.com/pyth-network/pyth-sdk-solidity/main/abis/IPyth.json'; + const pythAbi = await fetch(pythAbiUrl).then((response) => response.json()); + + const pythContract = new ethers.Contract(contractAddress, pythAbi, provider); + + try { + for (const priceFeedId of priceFeedIds) { + const priceData = await pythContract.getPrice(priceFeedId); + const price = Number(priceData.price) * 10 ** (Number(priceData.expo)); + + console.log(`Price for ${priceFeedId}:`); + console.log('Price:', price); + console.log('Confidence Interval:', priceData.conf.toString()); + // Convert publishTime to a formatted date + const publishTimeDate = new Date(priceData.publishTime.toNumber() * 1000); + console.log('Publish Time:', publishTimeDate.toUTCString()); + console.log('\n'); + } + } catch (error) { + console.error('Error:', error.message); + } +} + +async function main() { + try { + await getPrice(); + } catch (error) { + console.error('Main Error:', error.message); + } +} + +main(); +``` \ No newline at end of file diff --git a/docs/build/getting-started/sidebars.ts b/docs/build/getting-started/sidebars.ts index c43731c91c2..a1c2e06e120 100644 --- a/docs/build/getting-started/sidebars.ts +++ b/docs/build/getting-started/sidebars.ts @@ -50,6 +50,11 @@ module.exports = { type: 'link', href: 'https://evm-toolkit.evm.testnet.shimmer.network/', }, + { + label: 'Oracles for Shimmer EVM', + type: 'doc', + id: 'oracles', + }, { label: 'Testnet Faucet', type: 'link', From 64c7a650cedbb236d5bcbe648d2e1321944de3fb Mon Sep 17 00:00:00 2001 From: Lucas Tortora Date: Thu, 25 Jan 2024 10:25:45 -0300 Subject: [PATCH 2/4] update desc --- docs/build/getting-started/oracles.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/build/getting-started/oracles.md b/docs/build/getting-started/oracles.md index 4de40c80f29..78846f8beae 100644 --- a/docs/build/getting-started/oracles.md +++ b/docs/build/getting-started/oracles.md @@ -1,6 +1,5 @@ --- -description: 'An introductory guide to oracles in blockchain, explaining their role in providing external data to smart - contracts and their implementation in Shimmer EVM.' +description: 'Learn about the role of in oracles in blockchain,their role in providing external data to smart contracts and the available oracles in Shimmer EVM.' image: /img/logo/WASP_logo_dark.png keywords: - Blockchain Oracles From 9fd90c51bce383a28053d9264f539a24099a2923 Mon Sep 17 00:00:00 2001 From: Lucas Tortora Date: Mon, 29 Jan 2024 11:23:44 -0300 Subject: [PATCH 3/4] remove wasp tip as it is enabled by default --- docs/build/getting-started/oracles.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/build/getting-started/oracles.md b/docs/build/getting-started/oracles.md index 78846f8beae..1bf937bc905 100644 --- a/docs/build/getting-started/oracles.md +++ b/docs/build/getting-started/oracles.md @@ -67,8 +67,14 @@ You can learn more about Pyth Oracles in their [official documentation](https:// You can use the following example code to get data from Pyth Oracles in Shimmer EVM: +:::note + +The following example uses [`ether.js` v4](https://www.npmjs.com/package/ethers/v/4.0.43). + +::: + ```typescript -import ethers from "ethers"; +import {ethers} from "ethers"; import fetch from "node-fetch"; // Provider From 29308eab1ac511d1c7a4e49df1506e6753396335 Mon Sep 17 00:00:00 2001 From: Dr-Electron Date: Mon, 29 Jan 2024 16:07:28 +0100 Subject: [PATCH 4/4] Apply suggestions from code review --- docs/build/getting-started/oracles.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/docs/build/getting-started/oracles.md b/docs/build/getting-started/oracles.md index 1bf937bc905..11482858ad1 100644 --- a/docs/build/getting-started/oracles.md +++ b/docs/build/getting-started/oracles.md @@ -67,11 +67,6 @@ You can learn more about Pyth Oracles in their [official documentation](https:// You can use the following example code to get data from Pyth Oracles in Shimmer EVM: -:::note - -The following example uses [`ether.js` v4](https://www.npmjs.com/package/ethers/v/4.0.43). - -::: ```typescript import {ethers} from "ethers"; @@ -79,7 +74,7 @@ import fetch from "node-fetch"; // Provider const providerURL = "https://json-rpc.evm.shimmer.network" -const provider = new ethers.providers.JsonRpcProvider(providerURL); +const provider = new ethers.JsonRpcProvider(providerURL); // Pyth Oracle contract on ShimmerEVM Mainnet const contractAddress = '0xA2aa501b19aff244D90cc15a4Cf739D2725B5729'; @@ -108,7 +103,7 @@ async function getPrice() { console.log('Price:', price); console.log('Confidence Interval:', priceData.conf.toString()); // Convert publishTime to a formatted date - const publishTimeDate = new Date(priceData.publishTime.toNumber() * 1000); + const publishTimeDate = new Date(Number(priceData.publishTime) * 1000); console.log('Publish Time:', publishTimeDate.toUTCString()); console.log('\n'); } @@ -125,5 +120,4 @@ async function main() { } } -main(); -``` \ No newline at end of file +main(); \ No newline at end of file