Skip to content

Commit

Permalink
feat(events): track supply changes more closely PE-6796
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Melendez committed Oct 18, 2024
1 parent 0e7c9a1 commit f1c203e
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 52 deletions.
5 changes: 4 additions & 1 deletion spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1557,12 +1557,15 @@ describe("gar", function()
local protocolBalanceBefore = _G.Balances[ao.id] or 0
local status, result = pcall(gar.pruneGateways, currentTimestamp, msgId)
assert.is_true(status)
local expectedSlashedStake = math.floor(gar.getSettings().operators.minStake * 0.2)
assert.are.same({
prunedGateways = { "address1" },
slashedGateways = { "address3" },
stakeSlashed = expectedSlashedStake,
delegateStakeReturned = 0,
gatewayStakeReturned = 0,
}, result)

local expectedSlashedStake = math.floor(gar.getSettings().operators.minStake * 0.2)
local expectedRemainingStake = math.floor(gar.getSettings().operators.minStake * 0.8) + 10000
assert.is_nil(GatewayRegistry["address1"]) -- removed
assert.is_not_nil(GatewayRegistry["address2"]) -- not removed
Expand Down
7 changes: 7 additions & 0 deletions src/gar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function gar.leaveNetwork(from, currentTimestamp, msgId)
}

-- Reduce gateway stake and set this delegate stake to 0
-- TODO: It's an invariant if totalDelegatedStake isn't 0 at the end of this loop
gateway.totalDelegatedStake = gateway.totalDelegatedStake - delegate.delegatedStake
gateway.delegates[address].delegatedStake = 0
end
Expand Down Expand Up @@ -654,6 +655,9 @@ function gar.pruneGateways(currentTimestamp, msgId)
local result = {
prunedGateways = {},
slashedGateways = {},
gatewayStakeReturned = 0,
delegateStakeReturned = 0,
stakeSlashed = 0,
}

if next(gateways) == nil then
Expand All @@ -667,6 +671,7 @@ function gar.pruneGateways(currentTimestamp, msgId)
for vaultId, vault in pairs(gateway.vaults) do
if vault.endTimestamp <= currentTimestamp then
balances.increaseBalance(address, vault.balance)
result.gatewayStakeReturned = result.gatewayStakeReturned + vault.balance
gateway.vaults[vaultId] = nil
end
end
Expand All @@ -675,6 +680,7 @@ function gar.pruneGateways(currentTimestamp, msgId)
for vaultId, vault in pairs(delegate.vaults) do
if vault.endTimestamp <= currentTimestamp then
balances.increaseBalance(delegateAddress, vault.balance)
result.delegateStakeReturned = result.delegateStakeReturned + vault.balance
delegate.vaults[vaultId] = nil
end
end
Expand All @@ -701,6 +707,7 @@ function gar.pruneGateways(currentTimestamp, msgId)
gar.slashOperatorStake(address, slashAmount)
gar.leaveNetwork(address, currentTimestamp, msgId)
table.insert(result.slashedGateways, address)
result.stakeSlashed = result.stakeSlashed + slashAmount
else
if gateway.status == "leaving" and gateway.endTimestamp <= currentTimestamp then
-- if the timestamp is after gateway end timestamp, mark the gateway as nil
Expand Down
Loading

0 comments on commit f1c203e

Please sign in to comment.