-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// WeatherOracle.js | ||
const axios = require("axios"); | ||
const { ethers } = require("ethers"); | ||
const DecentralizedOracleABI = require("./artifacts/DecentralizedOracle.json"); // Adjust the path as necessary | ||
|
||
const provider = new ethers.providers.JsonRpcProvider("YOUR_INFURA_OR_ALCHEMY_URL"); | ||
const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); | ||
const oracleContractAddress = "YOUR_ORACLE_CONTRACT_ADDRESS"; | ||
|
||
async function fetchWeatherData(location) { | ||
// Simulate fetching weather data | ||
// In a real implementation, you would use an API like OpenWeatherMap | ||
const simulatedWeatherData = { | ||
temperature: Math.floor(Math.random() * 100), // Random temperature | ||
humidity: Math.floor(Math.random() * 100), // Random humidity | ||
}; | ||
return simulatedWeatherData; | ||
} | ||
|
||
async function updateWeatherData(location) { | ||
const oracleContract = new ethers.Contract(oracleContractAddress, DecentralizedOracleABI.abi, wallet); | ||
const feedId = ethers.utils.keccak256(ethers.utils.toUtf8Bytes(location)); | ||
|
||
const weatherData = await fetchWeatherData(location); | ||
const temperature = weatherData.temperature; | ||
|
||
try { | ||
const tx = await oracleContract.updateDataFeed(feedId, temperature); | ||
console.log(`Updating weather data... Transaction Hash: ${tx.hash}`); | ||
await tx.wait(); | ||
console.log("Weather data updated successfully!"); | ||
} catch (error) { | ||
console.error("Error updating weather data:", error); | ||
} | ||
} | ||
|
||
// Example usage | ||
const location = "New York"; // Replace with the desired location | ||
updateWeatherData(location); |