From 203e8973abe9fdbe106edf4ac0b2eba1137a44f8 Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Mon, 15 Jul 2024 08:52:29 -0600 Subject: [PATCH] chore: add vault handlers --- src/main.lua | 78 ++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/src/main.lua b/src/main.lua index 67295282..c4422030 100644 --- a/src/main.lua +++ b/src/main.lua @@ -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", @@ -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 @@ -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