Skip to content

Commit

Permalink
Add decrement prepaid bad debt method (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
NibiruHeisenberg authored May 22, 2022
1 parent bcfb80a commit 40e064d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions x/perp/keeper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,21 @@ func (pbd PrepaidBadDebtState) Increment(ctx sdk.Context, denom string, incremen

return amount
}

/*
Decrements the amount of bad debt prepaid by denom.
The lowest it can be decremented to is zero. Trying to decrement a prepaid bad
debt balance to below zero will clip it at zero.
*/
func (pbd PrepaidBadDebtState) Decrement(ctx sdk.Context, denom string, decrement sdk.Int) (
amount sdk.Int,
) {
kv := pbd.getKVStore(ctx)
amount = sdk.MaxInt(pbd.Get(ctx, denom).Sub(decrement), sdk.ZeroInt())

kv.Set([]byte(denom), sdk.Uint64ToBigEndian(amount.Uint64()))

return amount
}
14 changes: 14 additions & 0 deletions x/perp/keeper/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ func TestPrepaidBadDebtState(t *testing.T) {

amount = perpKeeper.PrepaidBadDebtState().Get(ctx, "NUSD")
assert.EqualValues(t, sdk.NewInt(150), amount)

t.Log("decrement and check")
amount = perpKeeper.PrepaidBadDebtState().Decrement(ctx, "NUSD", sdk.NewInt(75))
assert.EqualValues(t, sdk.NewInt(75), amount)

amount = perpKeeper.PrepaidBadDebtState().Get(ctx, "NUSD")
assert.EqualValues(t, sdk.NewInt(75), amount)

t.Log("decrement to below zero and check")
amount = perpKeeper.PrepaidBadDebtState().Decrement(ctx, "NUSD", sdk.NewInt(1000))
assert.EqualValues(t, sdk.ZeroInt(), amount)

amount = perpKeeper.PrepaidBadDebtState().Get(ctx, "NUSD")
assert.EqualValues(t, sdk.ZeroInt(), amount)
}

0 comments on commit 40e064d

Please sign in to comment.