Skip to content

Commit

Permalink
Update 0rbit-Price-Feed.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Astan34 authored May 21, 2024
1 parent b93fa25 commit a08b337
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions price-feed-bot-astan34/0rbit-Price-Feed.lua
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
)

0 comments on commit a08b337

Please sign in to comment.