Skip to content

Commit

Permalink
fix(gar): do not remove delegate before decreasing stake (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler authored Dec 18, 2024
2 parents d7132d9 + 7b55fd5 commit 4de7fd4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/gar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ end
--- @param gateway Gateway
--- @param quantity mARIO
function increaseDelegateStakeAtGateway(delegate, gateway, quantity)
assert(delegate, "Delegate is required")
assert(gateway, "Gateway is required")
-- zero is allowed as it is a no-op
assert(
quantity and utils.isInteger(quantity) and quantity >= 0,
"Quantity is required and must be an integer greater than or equal to 0: " .. quantity
)
delegate.delegatedStake = delegate.delegatedStake + quantity
gateway.totalDelegatedStake = gateway.totalDelegatedStake + quantity
end
Expand All @@ -489,8 +496,15 @@ end
--- @param ban boolean|nil do not add the delegate back to the gateway allowlist if their delegation is over
function decreaseDelegateStakeAtGateway(delegateAddress, gateway, quantity, ban)
local delegate = gateway.delegates[delegateAddress]
-- use this in an inverse way
increaseDelegateStakeAtGateway(delegate, gateway, -quantity)
assert(delegate, "Delegate is required")
-- zero is allowed as it is a no-op
assert(
quantity and utils.isInteger(quantity) and quantity >= 0,
"Quantity is required and must be an integer greater than or equal to 0: " .. quantity
)
assert(gateway, "Gateway is required")
delegate.delegatedStake = delegate.delegatedStake - quantity
gateway.totalDelegatedStake = gateway.totalDelegatedStake - quantity
gar.pruneDelegateFromGatewayIfNecessary(delegateAddress, gateway)
if ban and gateway.settings.allowedDelegatesLookup then
gateway.settings.allowedDelegatesLookup[delegateAddress] = nil
Expand Down Expand Up @@ -2055,8 +2069,9 @@ function unlockGatewayDelegateVault(gateway, delegateAddress, vaultId)
assert(vault, "Vault not found")

balances.increaseBalance(delegateAddress, vault.balance)
gateway.delegates[delegateAddress] = nil
decreaseDelegateStakeAtGateway(delegateAddress, gateway, vault.balance)
-- delete the delegate's vault and prune the delegate if necessary
gateway.delegates[delegateAddress].vaults[vaultId] = nil
gar.pruneDelegateFromGatewayIfNecessary(delegateAddress, gateway)
end

--- @param gateway Gateway
Expand Down
1 change: 0 additions & 1 deletion src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ local function assertAndSanitizeInputs(msg)
.. msg.Timestamp
)
assert(msg.From, "From is required")
assert(msg.Id, "Id is required")
assert(msg.Tags and type(msg.Tags) == "table", "Tags are required")

msg.Tags = utils.validateAndSanitizeInputs(msg.Tags)
Expand Down

0 comments on commit 4de7fd4

Please sign in to comment.