generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
begin setup for weather data request through chainlink function
- Loading branch information
1 parent
44d3127
commit 1574a22
Showing
6 changed files
with
252 additions
and
90 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
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
72 changes: 72 additions & 0 deletions
72
packages/hardhat/functions-source-scripts/fetch-weather-data.js
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,72 @@ | ||
// This function fetches the latest temperature for a particular area from openweathermap API | ||
// Args include the zipcode of your location, ISO 3166 country code | ||
// units- unit in which we want the temperature (standard, metric, imperial) | ||
|
||
if (!secrets.apiKey) { | ||
throw Error("Weather API Key is not available!"); | ||
} | ||
|
||
const zipCode = `${args[0]},${args[1]}`; | ||
|
||
const geoCodingURL = "http://api.openweathermap.org/geo/1.0/zip?"; | ||
|
||
console.log(`Sending HTTP request to ${geoCodingURL}zip=${zipCode}`); | ||
|
||
const geoCodingRequest = Functions.makeHttpRequest({ | ||
url: geoCodingURL, | ||
method: "GET", | ||
params: { | ||
zip: zipCode, | ||
appid: secrets.apiKey, | ||
}, | ||
}); | ||
|
||
const geoCodingResponse = await geoCodingRequest; | ||
|
||
if (geoCodingResponse.error) { | ||
console.error(geoCodingResponse.error); | ||
throw Error("Request failed, try checking the params provided"); | ||
} | ||
|
||
console.log(geoCodingResponse); | ||
|
||
const latitude = geoCodingResponse.data.lat; | ||
const longitude = geoCodingResponse.data.lon; | ||
const unit = args[2]; | ||
|
||
const url = `https://api.openweathermap.org/data/2.5/weather?`; | ||
|
||
console.log(`Sending HTTP request to ${url}lat=${latitude}&lon=${longitude}&units=${unit}`); | ||
|
||
const weatherRequest = Functions.makeHttpRequest({ | ||
url: url, | ||
method: "GET", | ||
params: { | ||
lat: latitude, | ||
lon: longitude, | ||
appid: secrets.apiKey, | ||
units: unit, | ||
}, | ||
}); | ||
|
||
// Execute the API request (Promise) | ||
const weatherResponse = await weatherRequest; | ||
if (weatherResponse.error) { | ||
console.error(weatherResponse.error); | ||
throw Error("Request failed, try checking the params provided"); | ||
} | ||
|
||
// gets the current temperature | ||
const temperature = weatherResponse.data.main.temp; | ||
|
||
// Gives the whole response from the request | ||
console.log("Weather response", weatherResponse); | ||
|
||
// result is in JSON object, containing only temperature | ||
const result = { | ||
temp: temperature, | ||
}; | ||
|
||
// Use JSON.stringify() to convert from JSON object to JSON string | ||
// Finally, use the helper Functions.encodeString() to encode from string to bytes | ||
return Functions.encodeString(JSON.stringify(result)); |
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,68 @@ | ||
// TODO: convert this to typescript | ||
|
||
// const { SecretsManager } = require("@chainlink/functions-toolkit") | ||
// const { networks } = require("../../networks") | ||
// const process = require("process") | ||
// const path = require("path") | ||
|
||
// task("functions-upload-secrets-don", "Encrypts secrets and uploads them to the DON") | ||
// .addParam( | ||
// "slotid", | ||
// "Storage slot number 0 or higher - if the slotid is already in use, the existing secrets for that slotid will be overwritten" | ||
// ) | ||
// .addOptionalParam( | ||
// "ttl", | ||
// "Time to live - minutes until the secrets hosted on the DON expire (defaults to 10, and must be at least 5)", | ||
// 10, | ||
// types.int | ||
// ) | ||
// .addOptionalParam( | ||
// "configpath", | ||
// "Path to Functions request config file", | ||
// `${__dirname}/../../Functions-request-config.js`, | ||
// types.string | ||
// ) | ||
// .setAction(async (taskArgs) => { | ||
// const signer = await ethers.getSigner() | ||
// const functionsRouterAddress = networks[network.name]["functionsRouter"] | ||
// const donId = networks[network.name]["donId"] | ||
|
||
// const gatewayUrls = networks[network.name]["gatewayUrls"] | ||
|
||
// const slotId = parseInt(taskArgs.slotid) | ||
// const minutesUntilExpiration = taskArgs.ttl | ||
|
||
// const secretsManager = new SecretsManager({ | ||
// signer, | ||
// functionsRouterAddress, | ||
// donId, | ||
// }) | ||
// await secretsManager.initialize() | ||
|
||
// // Get the secrets object from Functions-request-config.js or other specific request config. | ||
// const requestConfig = require(path.isAbsolute(taskArgs.configpath) | ||
// ? taskArgs.configpath | ||
// : path.join(process.cwd(), taskArgs.configpath)) | ||
|
||
// if (!requestConfig.secrets || requestConfig.secrets.length === 0) { | ||
// console.log("No secrets found in the request config.") | ||
// return | ||
// } | ||
|
||
// console.log("Encrypting secrets and uploading to DON...") | ||
// const encryptedSecretsObj = await secretsManager.encryptSecrets(requestConfig.secrets) | ||
|
||
// const { | ||
// version, // Secrets version number (corresponds to timestamp when encrypted secrets were uploaded to DON) | ||
// success, // Boolean value indicating if encrypted secrets were successfully uploaded to all nodes connected to the gateway | ||
// } = await secretsManager.uploadEncryptedSecretsToDON({ | ||
// encryptedSecretsHexstring: encryptedSecretsObj.encryptedSecrets, | ||
// gatewayUrls, | ||
// slotId, | ||
// minutesUntilExpiration, | ||
// }) | ||
|
||
// console.log( | ||
// `\nYou can now use slotId ${slotId} and version ${version} to reference the encrypted secrets hosted on the DON.` | ||
// ) | ||
// }) |
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
Oops, something went wrong.