Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that empty tranches are deleted #698

Merged
merged 3 commits into from
Oct 4, 2024
Merged

Conversation

jcompagni10
Copy link
Contributor

@jcompagni10 jcompagni10 commented Aug 29, 2024

There are some cases when a tranche is canceled where is not deleted and instead is saved as an inactive tranche.
This doesn't pose any immediate problem, but in the long term would result in unnecessary state bloat.

this PR changes the k.saveTranche logic to ensure that empty tranches are fully deleted.

@jcompagni10
Copy link
Contributor Author

@pr0n00gler
Copy link
Collaborator

Please attach a successful testrun 🙃

@jcompagni10
Copy link
Contributor Author

@@ -88,8 +88,8 @@ func (k Keeper) ExecuteCancelLimitOrder(
return sdk.Coin{}, sdk.Coin{}, nil, sdkerrors.Wrapf(types.ErrCancelEmptyLimitOrder, "%s", tranche.Key.TrancheKey)
}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveTranche(ctx, tranche)
k.SaveOrRemoveTrancheUser(ctx, trancheUser)
Copy link
Collaborator

@pr0n00gler pr0n00gler Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, this is much more confusing for me now (it's not obvious at all when the method removes a tranche and when it saves a tranche)

Can't it be two separate methods?

Copy link
Collaborator

@pr0n00gler pr0n00gler Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like, i understand there is a pretty simple condition inside the method, but i think the code would be much easier to read if the condition would be located outside of the method

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I'm used to combined methods and functions like GetOrCreate and CreateOrUpdate, but SaveOrRemove are totally opposite operations, and the meaning is unclear for me. I'm still quite bad at the dex module, could you please explain the meaning to me? Even better: could you please add a comprehensive explanation to the comment of the method?

@@ -88,8 +88,8 @@ func (k Keeper) ExecuteCancelLimitOrder(
return sdk.Coin{}, sdk.Coin{}, nil, sdkerrors.Wrapf(types.ErrCancelEmptyLimitOrder, "%s", tranche.Key.TrancheKey)
}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveTranche(ctx, tranche)
k.SaveOrRemoveTrancheUser(ctx, trancheUser)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I'm used to combined methods and functions like GetOrCreate and CreateOrUpdate, but SaveOrRemove are totally opposite operations, and the meaning is unclear for me. I'm still quite bad at the dex module, could you please explain the meaning to me? Even better: could you please add a comprehensive explanation to the comment of the method?

Comment on lines 54 to 61
func (k Keeper) SaveTranche(ctx sdk.Context, tranche *types.LimitOrderTranche) {
func (k Keeper) SaveOrMakeInactiveTranche(ctx sdk.Context, tranche *types.LimitOrderTranche) {
if tranche.HasTokenIn() {
k.SetLimitOrderTranche(ctx, tranche)
} else {
k.SetInactiveLimitOrderTranche(ctx, tranche)
k.SaveOrRemoveInactiveTranche(ctx, tranche)
k.RemoveLimitOrderTranche(ctx, tranche.Key)
ctx.EventManager().EmitEvents(types.GetEventsDecTotalOrders(tranche.Key.TradePairId))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method is called SaveOrMakeInactiveTranche, but inside we either

  • SetLimitOrderTranche which sound like the Save, or
  • SaveOrRemoveInactiveTranche which doesn't sound like MakeInactive

This is confusing. Could you please add comprehensive comments here and probably give these better names?


totalIn = totalIn.Add(amountLeft)
sharesIssued = amountLeft
}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveOrRemoveTrancheUser(ctx, trancheUser)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're placing a limit order here. can it be the case that we go to the first branch of the SaveOrRemoveTrancheUser that does RemoveLimitOrderTrancheUser in case if trancheUser.IsEmpty()?

@@ -98,7 +98,7 @@ func (k Keeper) ExecuteWithdrawFilledLimitOrder(

}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveOrRemoveTrancheUser(ctx, trancheUser)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're withdrawing a filled limit order here. can it be the case that we go to the second branch of the SaveOrRemoveTrancheUser that does SetLimitOrderTrancheUser in case if !trancheUser.IsEmpty()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if answers for this and this are "no", then we definitely can follow this Mike's suggestion and move the logic from SaveOrRemoveTrancheUser outside and use pure Save and Remove methods instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also could you please have a look at all the combined methods and

  1. check whether they are applied correctly or there can only be one of the conditions met;
  2. if combination is applied correctly in several places, which would imply that we need that method to avoid code duplication, cover the method and its usage with comments so it's clear for the reader how it works and why its called there

@jcompagni10 jcompagni10 force-pushed the fix/save_tranche_logic branch from a4a6f9f to 865fba0 Compare September 24, 2024 14:45
@jcompagni10
Copy link
Contributor Author

@pr0n00gler pr0n00gler merged commit d8f24d7 into main Oct 4, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants