Skip to content

Commit

Permalink
test(decrease delegate stake): update unit tests PE-7352
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Melendez committed Dec 27, 2024
1 parent 3551c35 commit 7b304f9
Showing 1 changed file with 89 additions and 47 deletions.
136 changes: 89 additions & 47 deletions spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,11 @@ describe("gar", function()
weights = testGateway.weights,
}, result)
end)
end)

it("should decrease delegated stake if the remaining stake is greater than the minimum stake", function()
local totalDelegatedStake = 750000000
describe("decreaseDelegateStake", function()
it("should decrease delegated stake if the remaining stake is at least the minimum stake", function()
local totalDelegatedStake = minDelegatedStake + 100000000
local decreaseAmount = 100000000
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
Expand All @@ -1141,13 +1143,13 @@ describe("gar", function()
vaults = {},
}

local expectation = {
local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = totalDelegatedStake - decreaseAmount,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
delegatedStake = totalDelegatedStake - decreaseAmount,
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
Expand All @@ -1166,6 +1168,25 @@ describe("gar", function()
observerAddress = testGateway.observerAddress,
weights = testGateway.weights,
}

local expectation = {
amountWithdrawn = 0,
delegatePruned = false,
expeditedWithdrawalFee = 0,
gatewayTotalDelegatedStake = minDelegatedStake,
penaltyRate = 0,
updatedDelegate = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
balance = decreaseAmount,
startTimestamp = startTimestamp,
endTimestamp = startTimestamp + (90 * 24 * 60 * 60 * 1000), -- 90 days
},
},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
Expand All @@ -1175,58 +1196,79 @@ describe("gar", function()
stubMessageId
)
assert.is_true(status)
assert.are.same(expectation, result.gateway)
assert.are.same(expectation, _G.GatewayRegistry[stubGatewayAddress])
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
end)

it("should decrease delegated stake with instant withdrawal and apply penalty and remove delegate", function()
_G.Balances[ao.id] = 0
local expeditedWithdrawalFee = 1000 * 0.50
local withdrawalAmount = 1000 - expeditedWithdrawalFee
_G.GatewayRegistry[stubGatewayAddress] = {
operatorStake = minOperatorStake,
totalDelegatedStake = minDelegatedStake + 1000,
vaults = {},
startTimestamp = startTimestamp,
stats = testGateway.stats,
services = testGateway.services,
settings = testGateway.settings,
status = testGateway.status,
observerAddress = testGateway.observerAddress,
delegates = {
[stubRandomAddress] = {
delegatedStake = minDelegatedStake + 1000,
it(
"should decrease delegated stake with instant withdrawal if the remaining stake is at least the minimum stake",
function()
local totalDelegatedStake = minDelegatedStake + 100000000
local decreaseAmount = 100000000
local expeditedWithdrawalFee = decreaseAmount * 0.50
local withdrawalAmount = decreaseAmount - expeditedWithdrawalFee
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
_G.GatewayRegistry[stubGatewayAddress].delegates[stubRandomAddress] = {
delegatedStake = totalDelegatedStake,
startTimestamp = 0,
vaults = {},
}

local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {},
},
},
startTimestamp = testGateway.startTimestamp,
stats = testGateway.stats,
services = testGateway.services,
settings = testGateway.settings,
status = testGateway.status,
observerAddress = testGateway.observerAddress,
weights = testGateway.weights,
}

local expectation = {
amountWithdrawn = withdrawalAmount,
delegatePruned = false,
expeditedWithdrawalFee = expeditedWithdrawalFee,
gatewayTotalDelegatedStake = minDelegatedStake,
penaltyRate = constants.MAX_EXPEDITED_WITHDRAWAL_PENALTY_RATE,
updatedDelegate = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {},
},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
stubRandomAddress,
1000,
startTimestamp,
stubMessageId,
true -- instant withdrawal
)

assert.is_true(status)
assert.are.same(result.gateway.delegates[stubRandomAddress].delegatedStake, minDelegatedStake)
assert.are.equal(result.gateway.totalDelegatedStake, minDelegatedStake)
assert.are.equal(withdrawalAmount, result.amountWithdrawn)
assert.are.equal(withdrawalAmount, _G.Balances[stubRandomAddress])
assert.are.equal(expeditedWithdrawalFee, result.expeditedWithdrawalFee)
assert.are.equal(expeditedWithdrawalFee, _G.Balances[ao.id])
assert.are.equal(constants.MAX_EXPEDITED_WITHDRAWAL_PENALTY_RATE, result.penaltyRate)
assert.are.equal(minDelegatedStake, _G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake)
end)
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
stubRandomAddress,
decreaseAmount,
startTimestamp,
stubMessageId,
true -- Instant withdrawal flag
)
assert.is_true(status)
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
assert.are.equal(withdrawalAmount, _G.Balances[stubRandomAddress])
assert.are.equal(expeditedWithdrawalFee, _G.Balances[ao.id])
end
)

it("should error if the remaining delegate stake is less than the minimum stake", function()
local delegatedStake = minDelegatedStake
_G.GatewayRegistry[stubGatewayAddress] = {
operatorStake = minOperatorStake,
totalDelegatedStake = minDelegatedStake - 1,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
Expand Down

0 comments on commit 7b304f9

Please sign in to comment.