-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.lua
More file actions
88 lines (70 loc) · 2.01 KB
/
queue.lua
File metadata and controls
88 lines (70 loc) · 2.01 KB
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
local assertions = require ".utils.assertions"
local queue = require ".utils.queue"
local utils = require ".utils"
local json = require "json"
local mod = {}
-- A wrapper for patterns to validate if the message is coming
-- from an oToken or not
function mod.fromoToken(pattern)
return function (msg)
local match = utils.matchesSpec(msg, pattern)
if not match or match == 0 or match == "skip" then
return match
end
return utils.find(
function (t) return t.oToken == msg.From end,
Tokens
) ~= nil
end
end
-- Action: "Add-To-Queue"
---@type HandlerFunction
function mod.add(msg)
local user = msg.Tags.User
-- validate address
if not assertions.isAddress(user) then
return msg.reply({ Error = "Invalid user address" })
end
-- check if the user has already been added
if queue.isQueued(user) or UpdateInProgress then
return msg.reply({ Error = "User already queued" })
end
-- add to queue
queue.add(user, msg.From)
msg.reply({ ["Queued-User"] = user })
end
-- Action: "Remove-From-Queue"
---@type HandlerFunction
function mod.remove(msg)
local user = msg.Tags.User
-- validate address
if not assertions.isAddress(user) then
return msg.reply({ Error = "Invalid user address" })
end
-- try to remove the user from the queue
local res = queue.remove(user, msg.From)
if res ~= "removed" then
return msg.reply({
Error = res == "not_queued" and
"The user is not queued" or
"The user was queued from another origin"
})
end
-- reply with confirmation
msg.reply({ ["Unqueued-User"] = user })
end
-- Action: "Check-Queue-For"
---@type HandlerFunction
function mod.check(msg)
local user = msg.Tags.User
-- validate address
if not assertions.isAddress(user) then
return msg.reply({ ["In-Queue"] = "false" })
end
-- the user is queued if they're either in the collateral
-- or the liquidation queues
return msg.reply({
["In-Queue"] = json.encode(queue.isQueued(user) or UpdateInProgress)
})
end
return mod