-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrice-Feed.lua
112 lines (99 loc) · 2.56 KB
/
Price-Feed.lua
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
local json = require("json")
-- processId of the 0rbit process.
_0RBIT = "BaMK1dfayo75s3q1ow6AO64UDpD9SEFbeE8xYrY2fyQ"
_0RBT_TOKEN = "BUhZLMwQ6yZHguLtJYA5lLUa9LQzLXMXRfaq9FVcPJc"
-- Base URL for coingecko API
BASE_URL = "https://api.coingecko.com/api/v3/simple/price"
FEE_AMOUNT = "1000000000000" -- 1 $0RBT
--[[
Mapping to store the Token Details
]]
TOKEN_PRICES = TOKEN_PRICES or {
BTC = {
coingecko_id = "bitcoin",
price = 0,
last_update_timestamp = 0
},
ETH = {
coingecko_id = "ethereum",
price = 0,
last_update_timestamp = 0
},
AR = {
coingecko_id = "arweave",
price = 0,
last_update_timestamp = 0
}
}
ID_TOKEN = ID_TOKEN or {
bitcoin = "BTC",
ethereum = "ETH",
arweave = "AR"
}
--[[
Function to get the price of a token
]]
function getTokenPrice(msg)
local token = msg.Tags.Token
local price = TOKEN_PRICES[token].price
if price == 0 then
Handlers.utils.reply("Price not available!!!")(msg)
else
Handlers.utils.reply(tostring(price))(msg)
end
end
--[[
Function to create a GET request on 0rbit to fetch the token prices
]]
function fetchPrice()
local url;
local token_ids = "";
for _, v in pairs(TOKEN_PRICES) do
token_ids = token_ids .. v.coingecko_id .. ","
end
url = BASE_URL .. "?ids=" .. token_ids .. "&vs_currencies=usd"
Send({
Target = _0RBT_TOKEN,
Action = "Transfer",
Recipient = _0RBIT,
Quantity = FEE_AMOUNT,
["X-Url"] = url,
["X-Action"] = "Get-Real-Data"
})
print(Colors.green .. "GET Request sent to the 0rbit process.")
end
--[[
Function to update the token prices in the TOKEN_PRICES table
]]
function receiveData(msg)
local res = json.decode(msg.Data)
for k, v in pairs(res) do
TOKEN_PRICES[ID_TOKEN[k]].price = tonumber(v.usd)
TOKEN_PRICES[ID_TOKEN[k]].last_update_timestamp = msg.Timestamp
end
print(Colors.green .. "Token prices updated.")
end
--[[
Handler to get the token price from the TOKEN_PRICES table
]]
Handlers.add(
"GetTokenPrice",
Handlers.utils.hasMatchingTag("Action", "Get-Token-Price"),
getTokenPrice
)
--[[
Handler to fetch the token prices by sending request to 0rbit
]]
Handlers.add(
"FetchPrice",
Handlers.utils.hasMatchingTag("Action", "Fetch-Price"),
fetchPrice
)
--[[
Handler to receive the data from the 0rbit process
]]
Handlers.add(
"ReceivingData",
Handlers.utils.hasMatchingTag("Action", "Receive-Response"),
receiveData
)