Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Amm#574

Merged
sanchaymittal merged 54 commits into
mainfrom
amm
Apr 24, 2021
Merged

Amm#574
sanchaymittal merged 54 commits into
mainfrom
amm

Conversation

@rhlsthrm

Copy link
Copy Markdown

The Problem

The Solution

Comment thread modules/router/src/services/config.ts Outdated
@sanchaymittal sanchaymittal marked this pull request as ready for review April 23, 2021 17:04
const marketPrice = parseEther("1");
const estimatedPrice = parseEther("1.2");
const res = getPriceImpact(marketPrice, estimatedPrice);
console.log(res.toString());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should actually ensure desired behavior here for above three methods, no? If this is just a TODO item, I'll take care of it


expect(res.isError).to.be.true;
expect(res.getError()!.message).to.be.eq(ConfigServiceError.reasons.UnableToGetSwapRate);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Think we need one more test here to check for desired behavior in a valid use case. I will add shortly

return uniquePairs;
};

export const getPriceImpact = (marketPrice: BigNumber, estimatedPrice: BigNumber): BigNumber => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we return price impact as a percentage integer, as opposed to a decimal? Seems a bit dangerous - we are assuming method callers/users will know it's returning a perc integer.

I'm adding a method header just to clarify for now. We should be doing better docstrings soon, so this might not be worth concern rn

@jakekidd jakekidd Apr 23, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also - isn't price impact the percentage difference between the value of a token before and after a swap?

If so, math should be:

// Assuming user is swapping token B for (-->) token A in router:
// Get token's current prices.
priceTokenA = ..
priceTokenB = ..
// Get amounts for each token after executing the desired swap.
amountTokenA = <amount of token A in liquidity pool> - <amount being withdrawn>
amountTokenB = <amount of token B in liquidity pool> + <amount being deposited>
// Calculate new projected price of token after execution.
projectedPriceTokenB = amountTokenA * priceTokenA / amountTokenB
// Convert to percentage difference from previous value.
1 - projectedPriceTokenB / priceTokenB

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Alternatively, I could be misunderstanding this method's purpose, and it could just be a case of a misnomer here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you wanted to account for fees, assuming we take those in tokenA, we would change this one line:

// Assuming a 3% fee:
projectedPriceTokenB = 0.97 * amountTokenA * priceTokenA / amountTokenB

@sanchaymittal sanchaymittal merged commit 7cd13ea into main Apr 24, 2021
@sanchaymittal sanchaymittal deleted the amm branch April 25, 2021 13:01
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