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

fix closing fee #475

Merged
merged 2 commits into from
Apr 6, 2023
Merged

fix closing fee #475

merged 2 commits into from
Apr 6, 2023

Conversation

taiki1frsh
Copy link
Collaborator

close #407

@taiki1frsh taiki1frsh self-assigned this Apr 5, 2023
@@ -141,6 +134,7 @@ func (k Keeper) ClosePerpetualFuturesPosition(ctx sdk.Context, position types.Pe
}

returningCoin := sdk.NewCoin(position.RemainingMargin.Denom, returningAmount)

if returningCoin.IsPositive() {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, position.Address.AccAddress(), sdk.Coins{returningCoin}); err != nil {
return err
Copy link

Choose a reason for hiding this comment

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

the code review:

  1. The code seems to be related to closing a perpetual futures positions.
  2. There is a FIXME comment in line 5 indicating that the Size_ cannot be used like this, possibly causing inconsistency between the position and token amount.
  3. A TODO comment in line 24 suggests that something is wrong with the code, which may be related to the FIXME comment above.
  4. In line 35, a MetricsRateType is declared for both base and quote denom, which could be useful for calculating the returning amount and loss to LP.
  5. Line 37 is updated to include feeAmount as a parameter, which will be deducted when closing a position.
  6. Line 42 indicates that the loss to LP should be restricted by the protocol behavior in the future.
  7. Line 45 updates the returning coin to be positive, and sends it to the account address in line 47.

}

returningAmount = returningAmount.Sub(tradingFee)

return returningAmount, lossToLP
}

Copy link

Choose a reason for hiding this comment

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

the review:

  1. The patch adds a tradingFee parameter to the CalcReturningAmountAtClose method.
  2. It also adds code that subtracts the trading fee from the returning amount and stores the difference between the trading fee and the returning amount as LossToLP.
  3. The code looks correct and there is no bug risk.
  4. As an improvement suggestion, it might be useful to add comments to explain why the trading fee is being subtracted and why the difference is stored as LossToLP.

@@ -141,6 +134,7 @@ func (k Keeper) ClosePerpetualFuturesPosition(ctx sdk.Context, position types.Pe
}

returningCoin := sdk.NewCoin(position.RemainingMargin.Denom, returningAmount)

if returningCoin.IsPositive() {
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, position.Address.AccAddress(), sdk.Coins{returningCoin}); err != nil {
return err
Copy link

Choose a reason for hiding this comment

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

The code patch adds a trading fee deduction and modifies the returning amount calculation in ClosePerpetualFuturesPosition function within the Keeper.

Improvements:

Bug Risk:

  • None identified.

Overall, the changes seem to improve the code's functionality.

@@ -885,7 +895,7 @@ func TestCalcReturningAmountAtClose(t *testing.T) {
sizeInMicro := types.NormalToMicroInt(tc.position.PositionInstance.Size_)
tc.position.PositionInstance.SizeInMicro = &sizeInMicro

returningAmount, lossToLP := tc.position.CalcReturningAmountAtClose(baseMetricsRate, quoteMetricsRate)
returningAmount, lossToLP := tc.position.CalcReturningAmountAtClose(baseMetricsRate, quoteMetricsRate, tc.tradingFee)
fmt.Println(returningAmount, lossToLP)
if !tc.expReturn.Equal(returningAmount) {
t.Error(tc, "expected %v, got %v", tc.expReturn, returningAmount)
Copy link

Choose a reason for hiding this comment

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

with the code patch. It's adding a new variable (tradingFee) to the TestCalcReturningAmountAtClose function, and it is also being used in the CalcReturningAmountAtClose function. This new variable should be properly initialized before calling CalcReturningAmountAtClose, otherwise it might lead to unexpected behaviours. Also, it should be checked that the tradingFee value is valid (non-negative).

Apart from that, I would recommend adding some documentation to the TestCalcReturningAmountAtClose function, so it is easier for other developers to understand what the code is doing.

}

returningAmount = returningAmount.Sub(tradingFee)

return returningAmount, lossToLP
}

Copy link

Choose a reason for hiding this comment

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

The code changes seem to add functionality related to trading fees calculation. The CalcReturningAmountAtClose function now takes a tradingFee parameter and subtracts it from the returningAmount before returning.

One suggestion for improvement would be to validate the tradingFee parameter to make sure it is not negative. Additionally, it may be helpful to include comments or documentation to explain how the tradingFee parameter is calculated and what units it's in.

Overall, the code changes appear to be low risk, as they do not significantly alter the existing functionality.

@@ -885,7 +895,7 @@ func TestCalcReturningAmountAtClose(t *testing.T) {
sizeInMicro := types.NormalToMicroInt(tc.position.PositionInstance.Size_)
tc.position.PositionInstance.SizeInMicro = &sizeInMicro

returningAmount, lossToLP := tc.position.CalcReturningAmountAtClose(baseMetricsRate, quoteMetricsRate)
returningAmount, lossToLP := tc.position.CalcReturningAmountAtClose(baseMetricsRate, quoteMetricsRate, tc.tradingFee)
fmt.Println(returningAmount, lossToLP)
if !tc.expReturn.Equal(returningAmount) {
t.Error(tc, "expected %v, got %v", tc.expReturn, returningAmount)
Copy link

Choose a reason for hiding this comment

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

The code patch appears to be a test case for the CalcReturningAmountAtClose() function. It sets the input parameters for different test scenarios, including trading fees, and then checks the expected return matches the actual return.

There are no obvious bug risks in this code review. However, some potential improvements could include providing more descriptive comments, renaming variables to make them clearer, or adding additional test cases to cover more edge cases.

Base automatically changed from feature/fix-handling-psize to newDevelop April 5, 2023 06:23
Copy link
Contributor

@kimurayu45z kimurayu45z left a comment

Choose a reason for hiding this comment

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

LGTM

@taiki1frsh taiki1frsh merged commit ee6b7ee into newDevelop Apr 6, 2023
@taiki1frsh taiki1frsh deleted the feature/fix-closing-fee branch April 6, 2023 06:59
taiki1frsh added a commit that referenced this pull request Apr 6, 2023
taiki1frsh added a commit that referenced this pull request Apr 10, 2023
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.

[derivatives] fix the way to retrieve fee when to close the position
2 participants