-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-weather.ts
48 lines (41 loc) · 1.54 KB
/
update-weather.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { LitOracleKit } from "@lit-protocol/lit-oracle-kit";
import { ethers } from "ethers";
import dotenv from "dotenv";
dotenv.config();
async function main() {
const contractAddress = process.env.WEATHER_ORACLE_ADDRESS;
if (!contractAddress) {
throw new Error("Please set WEATHER_ORACLE_ADDRESS in your .env file");
}
if (!process.env.LIT_ORACLE_KIT_PRIVATE_KEY) {
throw new Error("Please set LIT_ORACLE_KIT_PRIVATE_KEY in your .env file");
}
const sdk = new LitOracleKit(
"datil-dev",
process.env.LIT_ORACLE_KIT_PRIVATE_KEY!
);
await sdk.connect();
const result = await sdk.writeToChain({
dataSource: `
const url = "https://api.weather.gov/gridpoints/LWX/97,71/forecast";
const response = await fetch(url).then((res) => res.json());
const nearestForecast = response.properties.periods[0];
const temp = nearestForecast.temperature;
const probabilityOfPrecipitation = nearestForecast.probabilityOfPrecipitation.value || 0;
return [ temp, probabilityOfPrecipitation ];
`,
functionAbi:
"function updateWeather(int256 temperature, uint8 precipitationProbability) external",
toAddress: contractAddress,
chain: "yellowstone",
});
console.log("Weather update transaction:", result);
const response = JSON.parse(result.response);
const { txnHash } = response;
const explorerUrl = `https://yellowstone-explorer.litprotocol.com/tx/${txnHash}`;
console.log("Explorer URL:", explorerUrl);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});