-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.lua
67 lines (61 loc) · 1.47 KB
/
example.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
local w = require("w")
local r = require("r")
local k = require("k")
local jua = require("jua")
os.loadAPI(fs.exists("json.lua") and "json.lua" or "json")
local json = _G.json
_G.json = nil
local await = jua.await
r.init(jua)
w.init(jua)
k.init(jua, json, w, r)
local ws
local function prints(...)
local objs = {...}
for i, obj in ipairs(objs) do
print(json.encode(obj))
end
end
local function printMeta(meta)
if meta.domain then
print((meta.name and meta.name.."@" or "")..meta.domain..(meta.message and ": "..meta.message or ""))
end
end
jua.on("terminate", function()
if ws then ws.close() end
jua.stop()
print("Terminated")
end)
jua.go(function()
local success, address = await(k.address, "k7as0j87id")
if success then
prints(address)
else
print("Failed to request address.")
prints(address)
jua.stop()
end
local success, ws = await(k.connect, "test") --a
if success then
print("Connected to websocket.")
ws.on("hello", function(data)
print("MOTD: "..data.motd)
local success = await(ws.subscribe, "transactions", function(data)
local tx = data.transaction
if tx.metadata then
local meta = k.parseMeta(tx.metadata)
printMeta(meta)
end
prints(tx)
end)
if success then
print("Subscribed successfully.")
else
print("Failed to subscribe.")
end
end)
else
print("Failed to request a websocket url.")
jua.stop()
end
end)