Skip to content

Alternative savings fix#9675

Merged
danjm merged 15 commits intodevelopfrom
alternative-savings-fix
Nov 9, 2020
Merged

Alternative savings fix#9675
danjm merged 15 commits intodevelopfrom
alternative-savings-fix

Conversation

@danjm
Copy link
Copy Markdown
Contributor

@danjm danjm commented Oct 22, 2020

This PR replaces #9662, and fixes some additional issues

The most fundamental change made in this PR is an update to the basic formula used for calculating total savings:

average savings = (how much your net worth will change if you use the metaswap best quote) - (how much your net worth would have changed if you had swapped the median quote directly via the aggregator)

where:
(how much your net worth will change if you use the metaswap best quote) = (usd value of tokens received) - (usd network fees) 
and where:

(how much your net worth would have changed if you had swapped the average quote directly via the aggregator) = ((usd value of tokens that would be received from the average quote) / (1 - metamask rate)) - (usd network fees of the average quote)

And so the the savings.total property is now calculated directly from the overall eth value of the best trade to the overall eth value of the median trade. Related to this is that savings.performance is now calculated by subtracting fee savings from total savings.

Another important fix is that what was previously called ethValueOfTrade is no longer used in calculations of values that will be shown to the user on the front end. It has been renamed to tokenValueOfQuoteForSorting to reflect its purpose. This variable assumes a tokenConversionRate of 1 if none is available, which is fine for comparing quotes to calculate the top quote in such cases, but would be meaningless or misleading if used to calculate user facing values. A new ethValueOfQuote variable has been created that defaults the conversion rate to 0 if not available, which will result in ethValueOfQuote being zero (in this case, the UI will have to choose not to render savings data).

Also important change is that savings.fee is now calculated by comparing the fee of the best quote to the fee of the quote with the media value, as opposed to comparing the fee of the best quote to the median fee. To support this, the getMedian function has been refactored to a function that takes a list of quotes and returns an object with relevant savings information from the quote with the media ethValueOfQuote

Finally, this PR ensures that the metamask fee is accounted for in the savings calculations and makes that information available in the savings property for use in the UI

There are some other code refactors made to improve readability given the above changes.

@danjm danjm requested a review from a team as a code owner October 22, 2020 07:22
@danjm danjm requested review from Gudahtt and rekmarks October 22, 2020 07:22
@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [4cbdce7]
Page Load Metrics (460 ± 72 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint3110343178
domContentLoaded30185245915072
load30385346015072
domInteractive30185245915072

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [609b0d7]
Page Load Metrics (427 ± 59 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint29453542
domContentLoaded29160942512259
load29361242712259
domInteractive29160942512259

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [24a011e]
Page Load Metrics (460 ± 61 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint298337126
domContentLoaded29562645912661
load29762746012661
domInteractive29562545812661

@danjm danjm force-pushed the alternative-savings-fix branch from 24a011e to 6dc0b6d Compare November 4, 2020 16:27
@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented Nov 4, 2020

I just pushed a number of changes, including a rebase onto develop. I will reply to previous comments and add further commentary on latest changes in a little while.

@danjm danjm marked this pull request as draft November 4, 2020 17:03
@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented Nov 4, 2020

6dc0b6d Added unit tests for _findTopQuoteAndCalculateSavings for the case of ERC-20 -> ERC-20 swaps when the destination conversion rate is known. We should also add unit tests for:

  • ETH -> ERC-20 swaps, for both when the destination conversion rate is known and unknown
  • ERC-20 -> ERC-20 swaps, for when the destination conversion rate is unknown
  • both swaps where ETH and where ERC-20 is being received, and trade.value includes some amount for a fee

So 5 sets of unit tests to add.

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [d1f4ddc]
Page Load Metrics (456 ± 59 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint3711550199
domContentLoaded29180245412259
load29280345612259
domInteractive29080245412259

@danjm danjm force-pushed the alternative-savings-fix branch from d1f4ddc to e4b9cd3 Compare November 5, 2020 15:05
@danjm danjm marked this pull request as ready for review November 5, 2020 15:06
danjm added 2 commits November 5, 2020 11:48
…alculateSavings

Cleaner structuring of conditionals for setting tokenValueOfQuoteForSorting, ethValueOfQuote and metaMaskFeeInEth in swaps controller

Stop subtracting medianMetaMaskFee from savings, but include it in savings data

Another fix and refactor to savings calculations in _findTopQuoteAndCalculateSavings
@danjm danjm force-pushed the alternative-savings-fix branch from e4b9cd3 to 2217b02 Compare November 5, 2020 15:19
@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented Nov 5, 2020

@Gudahtt @rekmarks This is again ready for review.

Some notables changes that have been made:

  • I have removed logic that attempts to determine cost "if you had swapped the average quote directly via the aggregator", and have gone back to the old savings calculation based on comparing received tokens and network fees of our best quote to our median quote. Whether we eventually modify this calculation is a UX decision that we are not yet ready to make. For now, for the purposes of analytics on savings, this gives us all the data we need. The metamask fee that would be extracted on the median quote is also now captured, so that we can calculate cost "if you had swapped the average quote directly via the aggregator" from our analytics data if we want. (These changes made in 333a41a)
  • In response to some of @Gudahtt's comments, and in general to try to make the _findTopQuoteAndCalculateSavings more understand I have reorganized some of the logic and renamed some variables (These changes made in 333a41a)
  • The _findTopQuoteAndCalculateSavings no longer mutates the quote object it is passed (These changes made in 333a41a)
  • I have now added many more unit tests for the different scenarios that have to be handled by _findTopQuoteAndCalculateSavings (These changes made in 2217b02)
  • getMedianEthValueQuote also no longer mutates the array it is passed (e826431)

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [2217b02]
Page Load Metrics (494 ± 72 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint308240126
domContentLoaded29785049215072
load29885149415072
domInteractive29684949215072

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [e52eb94]
Page Load Metrics (375 ± 59 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint2910040157
domContentLoaded24669137312359
load24769237512359
domInteractive24669137312359

@danjm
Copy link
Copy Markdown
Contributor Author

danjm commented Nov 6, 2020

@Gudahtt comments have been addressed

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [1458ad7]
Page Load Metrics (543 ± 74 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint3110549168
domContentLoaded38088854115474
load38289454315474
domInteractive38088854115474

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [e4bdc81]
Page Load Metrics (421 ± 63 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint309038136
domContentLoaded24366841913263
load24467042113263
domInteractive24266841913263

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [ec9f935]
Page Load Metrics (571 ± 80 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint32116682612
domContentLoaded35283456916780
load35583557116880
domInteractive35183456816780

Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [59faf71]
Page Load Metrics (448 ± 58 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint299237136
domContentLoaded30068544712158
load30168744812158
domInteractive30068544612158

@danjm danjm merged commit c044b6f into develop Nov 9, 2020
@danjm danjm deleted the alternative-savings-fix branch November 9, 2020 17:09
@github-actions github-actions bot locked and limited conversation to collaborators Nov 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants