forked from 0rbit-co/quest
-
Notifications
You must be signed in to change notification settings - Fork 0
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
57 additions
and
2 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 |
---|---|---|
@@ -1,2 +1,57 @@ | ||
0rbit-Price-Feed.lua | ||
0rbit-Price-Feed.png | ||
-- PID NYdNjYNXMpE6_58xflLI_OzqZCek4GgR696b64_0Vzg | ||
|
||
local json = require("json") | ||
|
||
_ORBIT = "WSXUI2JjYUldJ7CKq9wE1MGwXs-ldzlUlHOQszwQe0s" | ||
|
||
function handleError(msg, errorMessage) | ||
ao.send({ | ||
Target = msg.From, | ||
Tags = { | ||
Action = "Error", | ||
["Message-Id"] = msg.Id, | ||
Error = errorMessage | ||
} | ||
}) | ||
end | ||
|
||
Handlers.add("Astan34", | ||
Handlers.utils.hasMatchingTag("Action", "Sponsored-Get-Request"), | ||
function(msg) | ||
local token = msg.Tags.Token | ||
if not token then | ||
handleError(msg, "Token not provided") | ||
return | ||
end | ||
|
||
local url = "https://api.coingecko.com/api/v3/simple/price?ids=" .. token .. "&vs_currencies=usd" | ||
ao.send({ | ||
Target = _ORBIT, | ||
Action = "Get-Real-Data", | ||
Url = url | ||
}) | ||
print("Pricefetch request sent for " .. token) | ||
end | ||
) | ||
|
||
Handlers.add("ReceiveData", | ||
Handlers.utils.hasMatchingTag("Action", "Receive-Response"), | ||
function(msg) | ||
print("Received data: " .. msg.Data) | ||
local res = json.decode(msg.Data) | ||
local token = msg.Tags.Token | ||
if res[token] and res[token].usd then | ||
ao.send({ | ||
Target = msg.From, | ||
Tags = { | ||
Action = "Price-Response", | ||
["Message-Id"] = msg.Id, | ||
Price = res[token].usd | ||
} | ||
}) | ||
print("Price of " .. token .. " is " .. res[token].usd) | ||
else | ||
handleError(msg, "Failed to fetch price") | ||
end | ||
end | ||
) |