Skip to content

Commit

Permalink
chore: add vault handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jul 15, 2024
1 parent 965d2a9 commit 203e897
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ local ActionMap = {
Gateways = "Gateways",
GatewayRegistrySettings = "Gateway-Registry-Settings",
-- writes
Vault = "Vault",
Vaults = "Vaults",
CreateVault = "Create-Vault",
VaultedTransfer = "Vaulted-Transfer",
ExtendVault = "Extend-Vault",
Expand Down Expand Up @@ -1230,6 +1232,37 @@ Handlers.add(ActionMap.ReservedName, utils.hasMatchingTag("Action", ActionMap.Re
})
end)

Handlers.add(ActionMap.Vaults, utils.hasMatchingTag("Action", ActionMap.Vaults), function(msg)
local vaults = vaults.getVaults()
ao.send({ Target = msg.From, Action = "Vaults-Notice", Data = json.encode(vaults) })
end)

Handlers.add(ActionMap.Vault, utils.hasMatchingTag("Action", ActionMap.Vault), function(msg)
local vault = vaults.getVault(msg.Tags.Address or msg.From)
ao.send({
Target = msg.From,
Action = "Vault-Notice",
Address = msg.Tags.Address or msg.From,
Data = json.encode(vault),
})
end)

Handlers.add("paginatedVaults", utils.hasMatchingTag("Action", "Paginated-Vaults"), function(msg)
local page = utils.parsePaginationTags(msg)
local status, result =
pcall(vaults.getPaginatedVaults, page.cursor, page.limit, page.sortBy or "startTimestamp", page.sortOrder)
if not status then
ao.send({
Target = msg.From,
Action = "Invalid-Vaults-Notice",
Error = "Pagination-Error",
Data = json.encode(result),
})
else
ao.send({ Target = msg.From, Action = "Vaults-Notice", Data = json.encode(result) })
end
end)

-- END READ HANDLERS

-- UTILITY HANDLERS USED FOR MIGRATION
Expand Down Expand Up @@ -1325,51 +1358,6 @@ Handlers.add("paginatedBalances", utils.hasMatchingTag("Action", "Paginated-Bala
end
end)

Handlers.add("fixGateways", utils.hasMatchingTag("Action", "Fix-Gateways"), function(msg)
-- if msg.From ~= Owner then
-- ao.send({ Target = msg.From, Data = "Unauthorized" })
-- return
-- end

local fixGateways = function()
-- reset any gateways marked as leaving
local gateways = gar.getGateways()
for address, gateway in pairs(gateways) do
if gateway ~= nil and gateway.status == "leaving" then
gateway.status = "joined"
gateway.endTimestamp = nil
local totalOperatorStake = 0
for _, vault in pairs(gateway.vaults) do
totalOperatorStake = totalOperatorStake + vault.balance
end
gateway.operatorStake = totalOperatorStake
gateway.vaults = {}
-- move the delegated vaults back
for delegateAddress, delegate in pairs(gateway.delegates) do
for _, vault in pairs(delegate.vaults) do
-- if the vault is delegated to the gateway, move it back
gateway.totalDelegatedStake = gateway.totalDelegatedStake + vault.balance
gateway.delegates[delegateAddress].delegatedStake = (
gateway.delegates[delegateAddress].delegatedStake or 0
) + vault.balance
end
gateway.delegates[delegateAddress].vaults = {}
end
-- now update the actual registry
gar.addGateway(address, gateway)
end
end
end

local status, result = pcall(fixGateways)
if not status then
ao.send({ Target = msg.From, Action = "Fix-Gateways-Error-Notice", Data = json.encode(result) })
else
ao.send({ Target = msg.From, Action = "Fix-Gateways-Notice", Data = json.encode(result) })
end
-- end
end)

-- END UTILITY HANDLERS USED FOR MIGRATION

return process

0 comments on commit 203e897

Please sign in to comment.