Integrating off-chain data into your workflow is essential for creating robust and dynamic decentralized applications. This section provides a detailed guide on how to access eOracle's API off-chain.
import { ethers } from 'ethers';
// ABI of the IEOFeedManager interface
const IEOFeedManagerAbi = [
"function getLatestPriceFeed(uint16 symbol) external view returns (tuple(uint256 value, uint256 timestamp))",
"function getLatestPriceFeeds(uint16[] calldata symbols) external view returns (tuple(uint256 value, uint256 timestamp)[])"
];
// Address of the deployed IEOFeedManager contract on Holesky network
const IEOFeedManagerAddress = "0x723BD409703EF60d6fB9F8d986eb90099A170fd0";
async function main() {
// Connect to the Ethereum network (Holesky in this case)
const provider = new ethers.providers.JsonRpcProvider('https://some.holesky.rpc');
// Create a contract instance
const feedManagerContract = new ethers.Contract(IEOFeedManagerAddress, IEOFeedManagerAbi, provider);
// Example to get the latest price feed for a single symbol (e.g., BTC:USD with symbol ID 1)
async function getPrice(symbol: number) {
try {
const priceFeed = await feedManagerContract.getLatestPriceFeed(symbol);
console.log(`Price: ${priceFeed.value.toString()}, Timestamp: ${priceFeed.timestamp.toString()}`);
return priceFeed;
} catch (error) {
console.error('Error fetching price feed:', error);
}
}
// Example to get the latest price feeds for multiple symbols
async function getPrices(symbols: number[]) {
try {
const priceFeeds = await feedManagerContract.getLatestPriceFeeds(symbols);
priceFeeds.forEach((feed: { value: ethers.BigNumber, timestamp: ethers.BigNumber }, index: number) => {
console.log(`Symbol: ${symbols[index]}, Price: ${feed.value.toString()}, Timestamp: ${feed.timestamp.toString()}`);
});
return priceFeeds;
} catch (error) {
console.error('Error fetching price feeds:', error);
}
}
// Call the functions with example symbol(s)
await getPrice(1); // For BTC:USD
await getPrices([1, 2]); // Example for multiple symbols (e.g., BTC:USD and ETH:USD)
}
main().catch(console.error);
from web3 import Web3
# Connect to holesky
rpc_url = "https://some.holesky.rpc" # Replace with your rpc
web3 = Web3(Web3.HTTPProvider(rpc_url))
# Check if connected to the node
if not web3.isConnected():
print("Failed to connect to the Ethereum node.")
exit()
# Contract address and ABI
contract_address = '0x723BD409703EF60d6fB9F8d986eb90099A170fd0'
contract_abi = [
{
"inputs": [{"internalType": "uint16","name": "symbol","type": "uint16"}],
"name": "getLatestPriceFeed",
"outputs": [{"components": [{"internalType": "uint256","name": "value","type": "uint256"},{"internalType": "uint256","name": "timestamp","type": "uint256"}],"internalType": "struct IEOFeedManager.PriceFeed","name": "","type": "tuple"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{"internalType": "uint16[]","name": "symbols","type": "uint16[]"}],
"name": "getLatestPriceFeeds",
"outputs": [{"components": [{"internalType": "uint256","name": "value","type": "uint256"},{"internalType": "uint256","name": "timestamp","type": "uint256"}],"internalType": "struct IEOFeedManager.PriceFeed[]","name": "","type": "tuple[]"}],
"stateMutability": "view",
"type": "function"
}
]
# Initialize contract
contract = web3.eth.contract(address=contract_address, abi=contract_abi)
# Function to get the latest price feed for a single symbol
def get_latest_price_feed(symbol):
price_feed = contract.functions.getLatestPriceFeed(symbol).call()
return price_feed
# Function to get the latest price feeds for multiple symbols
def get_latest_price_feeds(symbols):
price_feeds = contract.functions.getLatestPriceFeeds(symbols).call()
return price_feeds
# Example usage
if __name__ == '__main__':
# Get the latest price feed for symbol 1 (e.g., BTC:USD)
symbol = 1
price_feed = get_latest_price_feed(symbol)
print(f'Price Feed for symbol {symbol}: Value = {price_feed[0]}, Timestamp = {price_feed[1]}')
# Get the latest price feeds for multiple symbols
symbols = [1, 2] # Example symbols
price_feeds = get_latest_price_feeds(symbols)
for i, feed in enumerate(price_feeds):
print(f'Price Feed for symbol {symbols[i]}: Value = {feed[0]}, Timestamp = {feed[1]}')
import { ethers } from "ethers";
// Define the ABI of the IEOFeedAdapter contract
const IEOFeedAdapterABI = [
"function latestRoundData() view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)"
];
// Define the address of the IEOFeedAdapter contract
const IEOFeedAdapterAddress = "0xDD8387185C9e0a173702fc4a3285FA576141A9cd";
async function getLatestRoundData() {
// Connect to the Ethereum provider (in this case holesky)
const provider = new ethers.JsonRpcProvider("https://some.holesky.rpc");
// Create a contract instance
const feedAdapterContract = new ethers.Contract(IEOFeedAdapterAddress, IEOFeedAdapterABI, provider);
// Call the latestRoundData function
const latestRoundData = await feedAdapterContract.latestRoundData();
// Destructure the returned data
const [roundId, answer, startedAt, updatedAt, answeredInRound] = latestRoundData;
// Log the data
console.log("Round ID:", roundId.toString());
console.log("Answer:", answer.toString());
console.log("Started At:", new Date(startedAt * 1000).toLocaleString());
console.log("Updated At:", new Date(updatedAt * 1000).toLocaleString());
console.log("Answered In Round:", answeredInRound.toString());
}
// Execute the function
getLatestRoundData().catch(console.error)
from web3 import Web3
# Connect to holesky
rpc_url = "https://some.holesky.rpc" # Replace with your rpc
web3 = Web3(Web3.HTTPProvider(rpc_url))
# Check if connected to the node
if not web3.isConnected():
print("Failed to connect to the Ethereum node.")
exit()
# Contract address and ABI
contract_address = web3.toChecksumAddress("0xDD8387185C9e0a173702fc4a3285FA576141A9cd")
contract_abi = [
{
"constant": True,
"inputs": [],
"name": "latestRoundData",
"outputs": [
{"name": "roundId", "type": "uint80"},
{"name": "answer", "type": "int256"},
{"name": "startedAt", "type": "uint256"},
{"name": "updatedAt", "type": "uint256"},
{"name": "answeredInRound", "type": "uint80"}
],
"payable": False,
"stateMutability": "view",
"type": "function"
}
]
# Initialize the contract
contract = web3.eth.contract(address=contract_address, abi=contract_abi)
# Call the latestRoundData function
try:
round_data = contract.functions.latestRoundData().call()
round_id, answer, started_at, updated_at, answered_in_round = round_data
print(f"Round ID: {round_id}")
print(f"Answer: {answer}")
print(f"Started At: {started_at}")
print(f"Updated At: {updated_at}")
print(f"Answered In Round: {answered_in_round}")
except Exception as e:
print(f"An error occurred: {e}")