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

feat: update totalFee calculation #197

Merged
merged 2 commits into from
Jan 16, 2024
Merged

Conversation

alfetopito
Copy link
Contributor

Summary

As per this internal discussion, the backend updated how the fees are returned by the api.

In this PR the calculation is updated to return the sum of executedSurplusFee and executedFeeAmount.

Context

Take as example this order: https://explorer.cow.fi/orders/0xdedb65b51a6f81493f8e6e1d811b56a57297dbb6fb11716d85e646a66a5d041f85cc3c8da85612013acabe9b5d954d578860b3c165a22c43?tab=overview

Currently it shows as 0 fee, even though executedFeeAmount is set.

The issue is that this order has both fields executedSurplusFee and executedFeeAmount returned, but executedSurplusFee is set to 0.
In the current implementation, it takes precedence over executedFeeAmount.

const totalFee = executedSurplusFee ?? executedFeeAmount

This logic was introduced over 1y ago and according to this comment from Nick:

tl;dr: the “real” fee is executedSurplusFee ?? executedFeeAmount, and not (executedSurplusFee ?? 0) + executedFeeAmount.

According to Dusan:

... probably caused by this change: cowprotocol/services#2103
We extracted the executed_surplus_fee out of the parent struct which caused it to be always serialized (even for market orders).
Considering we will probably deliver protocol fees (which are expressed through executed_surplus_fee ) before we completely remove market orders (orders with signed fee_amount), and in that case we will have orders that have both executed_fee_amount and executed_surplus_fee different from zero => I would go with summarizing the values for UI.

Test

Unit tests

@alfetopito alfetopito self-assigned this Jan 15, 2024
@alfetopito alfetopito requested a review from anxolin January 15, 2024 16:32
@coveralls
Copy link
Collaborator

coveralls commented Jan 15, 2024

Coverage Status

coverage: 78.995% (+0.1%) from 78.899%
when pulling cac5a4d on update-totalFee-calculation
into 755a01b on main.

Comment on lines +32 to +33
const _executedFeeAmount = BigInt(executedFeeAmount || '0')
const _executedSurplusFee = BigInt(executedSurplusFee || '0')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using native BigInt to avoid adding a new dependency just for this.

Or should we?

Copy link
Contributor

Choose a reason for hiding this comment

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

this solution looks good.

why the underscore in the consts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because those names already exist in the scope.
Could call then name+BigInt but I didn't find it a good choice.
Suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

okok, not big deal. works for me. Also the BigInt works too

const rawOrder = { ...ORDER, executedFeeAmount: '1', executedSurplusFee: '0' }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual(rawOrder.executedFeeAmount)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Why asserting the transformedOrder totalFee matches the rawOrder executedFeeAmount?
I would find more readable that given the above, the result is '1' (setting the actual value, instead of picking the property name)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(transformedOrder.totalFee).toEqual(rawOrder.executedFeeAmount)
expect(transformedOrder.totalFee).toEqual('1') // 1 + 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's meant to be: it's the unaltered value coming from the original order obj.
I feel like that's a better way to ready it than using an actual value.

But sure, will update it.

const rawOrder = { ...ORDER, executedFeeAmount: '0', executedSurplusFee: '1' }
const transformedOrder = transformOrder(rawOrder)

expect(transformedOrder.totalFee).toEqual(rawOrder.executedSurplusFee)
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment

Comment on lines +32 to +33
const _executedFeeAmount = BigInt(executedFeeAmount || '0')
const _executedSurplusFee = BigInt(executedSurplusFee || '0')
Copy link
Contributor

Choose a reason for hiding this comment

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

this solution looks good.

why the underscore in the consts?

Copy link
Contributor

@anxolin anxolin left a comment

Choose a reason for hiding this comment

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

just some nits

@alfetopito alfetopito merged commit bfa4c63 into main Jan 16, 2024
@alfetopito alfetopito deleted the update-totalFee-calculation branch January 16, 2024 10:50
@github-actions github-actions bot locked and limited conversation to collaborators Jan 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants