Skip to content

Commit

Permalink
fix(tags): update epoch tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jun 24, 2024
1 parent 770d609 commit 767bfa7
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ local ActionMap = {
PrescribedNames = "Epoch-Prescribed-Names",
Observations = "Epoch-Observations",
Distributions = "Epoch-Distributions",
-- hypenated for all of these
-- NAME REGISTRY READ APIS
Record = "Record",
Records = "Records",
Expand Down Expand Up @@ -136,7 +135,7 @@ end)
Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.CreateVault), function(msg)
local function checkAssertions()
assert(
tonumber(msg.Tags["Lock-Length"]) > 0 and utils.isInteger(tonumber(msg.Tags.LockLength)),
tonumber(msg.Tags["Lock-Length"]) > 0 and utils.isInteger(tonumber(msg.Tags["Lock-Length"])),
"Invalid lock length. Must be integer greater than 0"
)
assert(
Expand Down Expand Up @@ -201,7 +200,7 @@ Handlers.add(ActionMap.VaultedTransfer, utils.hasMatchingTag("Action", ActionMap
msg.From,
msg.Tags.Recipient,
tonumber(msg.Tags.Quantity),
tonumber(msg.Tags.LockLength),
tonumber(msg.Tags["Lock-Length"]),
msg.Timestamp,
msg.Id
)
Expand Down Expand Up @@ -622,15 +621,18 @@ Handlers.add(
Handlers.add(ActionMap.DelegateStake, utils.hasMatchingTag("Action", ActionMap.DelegateStake), function(msg)
local checkAssertions = function()
assert(utils.isValidArweaveAddress(msg.Tags.Target), "Invalid target address")
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)

if not inputStatus then
ao.send({
Target = msg.From,
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = "Bad-Input", },
Tags = { Action = "Invalid-Delegate-Stake-Notice", Error = "Bad-Input" },
Data = tostring(inputResult),
})
return
Expand Down Expand Up @@ -659,15 +661,18 @@ Handlers.add(
function(msg)
local checkAssertions = function()
assert(utils.isValidArweaveAddress(msg.Tags.Target), "Invalid target address")
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)

if not inputStatus then
ao.send({
Target = msg.From,
Tags = { Action = "Invalid-Decrease-Delegate-Stake-Notice", Error = "Bad-Input", },
Tags = { Action = "Invalid-Decrease-Delegate-Stake-Notice", Error = "Bad-Input" },
Data = tostring(inputResult),
})
return
Expand Down Expand Up @@ -770,7 +775,12 @@ Handlers.add(ActionMap.SaveObservations, utils.hasMatchingTag("Action", ActionMa
if status then
ao.send({ Target = msg.From, Action = "Save-Observations-Notice", Data = json.encode(result) })
else
ao.send({ Target = msg.From, Action = "Invalid-Save-Observations-Notice", Error = "Invalid-Saved-Observations", Data = json.encode(result) })
ao.send({
Target = msg.From,
Action = "Invalid-Save-Observations-Notice",
Error = "Invalid-Saved-Observations",
Data = json.encode(result),
})
end
end)

Expand Down Expand Up @@ -814,7 +824,12 @@ Handlers.add("tick", utils.hasMatchingTag("Action", "Tick"), function(msg)
NameRegistry = previousState.NameRegistry
Epochs = previousState.Epochs
DemandFactor = previousState.DemandFactor
ao.send({ Target = msg.From, Action = "Invalid-Tick-Notice", Error = "Invalid-Tick", Data = json.encode(result) })
ao.send({
Target = msg.From,
Action = "Invalid-Tick-Notice",
Error = "Invalid-Tick",
Data = json.encode(result),
})
end
end
end)
Expand Down Expand Up @@ -884,7 +899,7 @@ Handlers.add(ActionMap.Balance, Handlers.utils.hasMatchingTag("Action", ActionMa
Data = balance,
Balance = balance,
Ticker = Ticker,
Address = msg.Tags.Target or msg.Tags.Address or msg.From
Address = msg.Tags.Target or msg.Tags.Address or msg.From,
})
end)

Expand All @@ -894,7 +909,12 @@ Handlers.add(ActionMap.DemandFactor, utils.hasMatchingTag("Action", ActionMap.De
if status then
ao.send({ Target = msg.From, Action = "Demand-Factor-Notice", Data = tostring(result) })
else
ao.send({ Target = msg.From, Action = "Invalid-Demand-Factor-Notice", Error = "Invalid-Demand-Factor", Data = json.encode(result) })
ao.send({
Target = msg.From,
Action = "Invalid-Demand-Factor-Notice",
Error = "Invalid-Demand-Factor",
Data = json.encode(result),
})
end
end)

Expand Down Expand Up @@ -945,7 +965,7 @@ end)
Handlers.add(ActionMap.Epoch, utils.hasMatchingTag("Action", ActionMap.Epoch), function(msg)
-- check if the epoch number is provided, if not get the epoch number from the timestamp
local checkAssertions = function()
assert(msg.Tags.EpochIndex or msg.Tags.Timestamp or msg.Timestamp, "Epoch index or timestamp is required")
assert(msg.Tags["Epoch-Index"] or msg.Tags.Timestamp or msg.Timestamp, "Epoch index or timestamp is required")
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -959,7 +979,7 @@ Handlers.add(ActionMap.Epoch, utils.hasMatchingTag("Action", ActionMap.Epoch), f
return
end

local epochIndex = tonumber(msg.Tags.EpochIndex)
local epochIndex = tonumber(msg.Tags["Epoch-Index"])
or epochs.getEpochIndexForTimestamp(tonumber(msg.Tags.Timestamp or msg.Timestamp))
local epoch = epochs.getEpoch(epochIndex)
ao.send({ Target = msg.From, Action = "Epoch-Notice", Data = json.encode(epoch) })
Expand All @@ -973,7 +993,7 @@ end)
Handlers.add(ActionMap.PrescribedObservers, utils.hasMatchingTag("Action", ActionMap.PrescribedObservers), function(msg)
-- check if the epoch number is provided, if not get the epoch number from the timestamp
local checkAssertions = function()
assert(msg.Tags.EpochIndex or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
assert(msg.Tags["Epoch-Index"] or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -987,15 +1007,15 @@ Handlers.add(ActionMap.PrescribedObservers, utils.hasMatchingTag("Action", Actio
return
end

local epochIndex = tonumber(msg.Tags.EpochIndex) or epochs.getEpochIndexFromTimestamp(tonumber(msg.Timestamp))
local epochIndex = tonumber(msg.Tags["Epoch-Index"]) or epochs.getEpochIndexFromTimestamp(tonumber(msg.Timestamp))
local prescribedObservers = epochs.getPrescribedObserversForEpoch(epochIndex)
ao.send({ Target = msg.From, Action = "Prescribed-Observers-Notice", Data = json.encode(prescribedObservers) })
end)

Handlers.add(ActionMap.Observations, utils.hasMatchingTag("Action", ActionMap.Observations), function(msg)
-- check if the epoch number is provided, if not get the epoch number from the timestamp
local checkAssertions = function()
assert(msg.Tags.EpochIndex or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
assert(msg.Tags["Epoch-Index"] or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -1009,16 +1029,21 @@ Handlers.add(ActionMap.Observations, utils.hasMatchingTag("Action", ActionMap.Ob
return
end

local epochIndex = tonumber(msg.Tags.EpochIndex)
local epochIndex = tonumber(msg.Tags["Epoch-Index"])
or epochs.getEpochIndexFromTimestamp(tonumber(msg.Timestamp or msg.Tags.Timestamp))
local observations = epochs.getObservationsForEpoch(epochIndex)
ao.send({ Target = msg.From, Action = "Observations-Notice", EpochIndex = tostring(epochIndex), Data = json.encode(observations) })
ao.send({
Target = msg.From,
Action = "Observations-Notice",
EpochIndex = tostring(epochIndex),
Data = json.encode(observations),
})
end)

Handlers.add(ActionMap.PrescribedNames, utils.hasMatchingTag("Action", ActionMap.PrescribedNames), function(msg)
-- check if the epoch number is provided, if not get the epoch number from the timestamp
local checkAssertions = function()
assert(msg.Tags.EpochIndex or msg.Tags.Timestamp or msg.Timestamp, "Epoch index or timestamp is required")
assert(msg.Tags["Epoch-Index"] or msg.Tags.Timestamp or msg.Timestamp, "Epoch index or timestamp is required")
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -1032,7 +1057,7 @@ Handlers.add(ActionMap.PrescribedNames, utils.hasMatchingTag("Action", ActionMap
return
end

local epochIndex = tonumber(msg.Tags.EpochIndex)
local epochIndex = tonumber(msg.Tags["Epoch-Index"])
or epochs.getEpochIndexForTimestamp(tonumber(msg.Timestamp or msg.Tags.Timestamp))
local prescribedNames = epochs.getPrescribedNamesForEpoch(epochIndex)
ao.send({ Target = msg.From, Action = "Prescribed-Names-Notice", Data = json.encode(prescribedNames) })
Expand All @@ -1041,7 +1066,7 @@ end)
Handlers.add(ActionMap.Distributions, utils.hasMatchingTag("Action", ActionMap.Distributions), function(msg)
-- check if the epoch number is provided, if not get the epoch number from the timestamp
local checkAssertions = function()
assert(msg.Tags.EpochIndex or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
assert(msg.Tags["Epoch-Index"] or msg.Timestamp or msg.Tags.Timestamp, "Epoch index or timestamp is required")
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -1055,7 +1080,7 @@ Handlers.add(ActionMap.Distributions, utils.hasMatchingTag("Action", ActionMap.D
return
end

local epochIndex = tonumber(msg.Tags.EpochIndex)
local epochIndex = tonumber(msg.Tags["Epoch-Index"])
or epochs.getEpochIndexFromTimestamp(tonumber(msg.Timestamp or msg.Tags.Timestamp))
local distributions = epochs.getDistributionsForEpoch(epochIndex)
ao.send({ Target = msg.From, Action = "Distributions-Notice", Data = json.encode(distributions) })
Expand All @@ -1068,7 +1093,12 @@ end)

Handlers.add(ActionMap.ReservedName, utils.hasMatchingTag("Action", ActionMap.ReservedName), function(msg)
local reservedName = arns.getReservedName(msg.Tags.Name)
ao.send({ Target = msg.From, Action = "Reserved-Name-Notice", ReservedName = msg.Tags.Name, Data = json.encode(reservedName) })
ao.send({
Target = msg.From,
Action = "Reserved-Name-Notice",
ReservedName = msg.Tags.Name,
Data = json.encode(reservedName),
})
end)

-- END READ HANDLERS
Expand Down

0 comments on commit 767bfa7

Please sign in to comment.