Skip to content

Commit

Permalink
return error instead of panic
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed May 22, 2024
1 parent c780c73 commit b68ea8a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions app/upgrades/v19/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (s *UpgradeTestSuite) TestUpgrade() {
s.Require().NoError(err)
})

synthLockedPreV18 := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
synthLockedPreV18, err := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
s.Require().NoError(err)

// run v18 upgrade
// by doing this, we should be having incorrect state of superfluid staking accumulator
Expand All @@ -77,7 +78,8 @@ func (s *UpgradeTestSuite) TestUpgrade() {

// broken states (current status):
// synth lock accumulator is set to 0
totalSynthLocked := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
totalSynthLocked, err := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
s.Require().NoError(err)
s.Require().True(totalSynthLocked.Equal(osmomath.ZeroInt()))

// superfluid delegated tokens have been undelegated from validator,
Expand All @@ -98,7 +100,8 @@ func (s *UpgradeTestSuite) TestUpgrade() {

// synth lock accumulator should have been fixed after v19 upgrade,
// and went back to normal state(pre-v18)
synthLockAfterV19 := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
synthLockAfterV19, err := s.App.SuperfluidKeeper.GetTotalSyntheticAssetsLocked(s.Ctx, stakingSyntheticDenom(lockDenom, superfluidVal.String()))
s.Require().NoError(err)
s.Require().True(synthLockAfterV19.Equal(synthLockedPreV18))

// also check that we have the correct superfluid staked delegation back
Expand Down
11 changes: 7 additions & 4 deletions x/superfluid/keeper/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
)

// GetTotalSyntheticAssetsLocked returns the total amount of the given denom locked.
func (k Keeper) GetTotalSyntheticAssetsLocked(ctx sdk.Context, denom string) osmomath.Int {
func (k Keeper) GetTotalSyntheticAssetsLocked(ctx sdk.Context, denom string) (osmomath.Int, error) {
unbondingTime, err := k.sk.UnbondingTime(ctx)
if err != nil {
panic(err)
return osmomath.Int{}, err
}
return k.lk.GetPeriodLocksAccumulation(ctx, lockuptypes.QueryCondition{
LockQueryType: lockuptypes.ByDuration,
Denom: denom,
Duration: unbondingTime,
})
}), nil
}

// GetExpectedDelegationAmount returns the total number of osmo the intermediary account
Expand All @@ -38,7 +38,10 @@ func (k Keeper) GetTotalSyntheticAssetsLocked(ctx sdk.Context, denom string) osm
// lead rounding errors from the true delegated amount.
func (k Keeper) GetExpectedDelegationAmount(ctx sdk.Context, acc types.SuperfluidIntermediaryAccount) (osmomath.Int, error) {
// (1) Find how many tokens total T are locked for (denom, validator) pair
totalSuperfluidDelegation := k.GetTotalSyntheticAssetsLocked(ctx, stakingSyntheticDenom(acc.Denom, acc.ValAddr))
totalSuperfluidDelegation, err := k.GetTotalSyntheticAssetsLocked(ctx, stakingSyntheticDenom(acc.Denom, acc.ValAddr))
if err != nil {
return osmomath.Int{}, err
}
// (2) Multiply the T tokens, by the number of superfluid osmo per token, to get the total amount
// of osmo we expect.
refreshedAmount, err := k.GetSuperfluidOSMOTokens(ctx, acc.Denom, totalSuperfluidDelegation)
Expand Down

0 comments on commit b68ea8a

Please sign in to comment.