-
Notifications
You must be signed in to change notification settings - Fork 58
fix(tickers): select the most liquid ICP Swap pool for a token price #8042
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
Open
yhabib
wants to merge
6
commits into
main
Choose a base branch
from
fix/icp-swap-pool-liquidity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92c6169
fix(tickers): use the most liquid ICPSwap pool for a token price
yhabib fc4c914
test(tickers): add an e2e spec for the ICPSwap pool selection
yhabib 339488e
fix(tickers): skip an ICPSwap pool with no usable price
yhabib e0ce558
fix(tests): correct the e2e ICP price format expectation
yhabib 696356b
chore(spelling): add ICPSwap to the custom dictionary
yhabib 40665b3
test(e2e): wait for the tickers request instead of a fixed sleep
yhabib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| 86 | ||
| 87 | ||
| ICPSwap | ||
| Monterey | ||
| IC | ||
| ICP | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import type { IcpSwapTicker } from "$lib/types/icp-swap"; | ||
| import { AppPo } from "$tests/page-objects/App.page-object"; | ||
| import { PlaywrightPageObjectElement } from "$tests/page-objects/playwright.page-object"; | ||
| import { | ||
| dfxCanisterId, | ||
| disableCssAnimations, | ||
| signInWithNewUser, | ||
| step, | ||
| } from "$tests/utils/e2e.test-utils"; | ||
| import { expect, test } from "@playwright/test"; | ||
|
|
||
| // The ICP price in USD that the banner shows is the `last_price` of the | ||
| // ckUSDC ticker. The app formats it with `formatNumber`, which replaces the | ||
| // decimal comma from `Intl.NumberFormat("fr-FR")` with a dot, so 4 becomes | ||
| // "4.00" and 400 becomes "400.00". | ||
| const REAL_ICP_PRICE = "4"; | ||
| const ATTACKER_ICP_PRICE = "400"; | ||
| const REAL_ICP_PRICE_SHOWN = "4.00"; | ||
|
|
||
| const ticker = (overrides: Partial<IcpSwapTicker>): IcpSwapTicker => ({ | ||
| ticker_id: "ne2vj-6yaaa-aaaag-qb3ia-cai", | ||
| ticker_name: "CKUSDC_ICP", | ||
| base_id: "2ouva-viaaa-aaaaq-aaamq-cai", | ||
| base_currency: "ckUSDC", | ||
| target_id: "ryjl3-tyaaa-aaaaa-aaaba-cai", | ||
| target_currency: "ICP", | ||
| last_price: "1", | ||
| base_volume: "0", | ||
| target_volume: "0", | ||
| base_volume_24H: "0", | ||
| target_volume_24H: "0", | ||
| total_volume_usd: "0", | ||
| volume_usd_24H: "0", | ||
| fee_usd: "0", | ||
| liquidity_in_usd: "0", | ||
| ...overrides, | ||
| }); | ||
|
|
||
| test("The ICP price comes from the most liquid ICPSwap pool", async ({ | ||
| page, | ||
| context, | ||
| }) => { | ||
| const icpLedgerCanisterId = await dfxCanisterId("nns-ledger"); | ||
| const ckusdcLedgerCanisterId = await dfxCanisterId("ckusdc_ledger"); | ||
|
|
||
| // The real ckUSDC pool holds value but nobody traded it in the last 24 | ||
| // hours. This is true of 296 of the 407 ICP pairs in the live feed. | ||
| const realCkusdcTicker = ticker({ | ||
| base_id: ckusdcLedgerCanisterId, | ||
| target_id: icpLedgerCanisterId, | ||
| last_price: REAL_ICP_PRICE, | ||
| liquidity_in_usd: "617000", | ||
| volume_usd_24H: "0", | ||
| }); | ||
|
|
||
| // Anybody can create a second ICPSwap pool for the same pair. This one holds | ||
| // almost nothing, carries one wash trade and reports an outlier price. The | ||
| // old code selected it, because it was the first ticker with a volume. | ||
| const attackerCkusdcTicker = ticker({ | ||
| base_id: ckusdcLedgerCanisterId, | ||
| target_id: icpLedgerCanisterId, | ||
| last_price: ATTACKER_ICP_PRICE, | ||
| liquidity_in_usd: "5", | ||
| volume_usd_24H: "1", | ||
| }); | ||
|
|
||
| let tickersRequested = false; | ||
| await page.route("**/tickers", async (route) => { | ||
| tickersRequested = true; | ||
| await route.fulfill({ | ||
| // The attacker pool comes first, the order an attacker would want. | ||
| json: [attackerCkusdcTicker, realCkusdcTicker], | ||
| }); | ||
| }); | ||
|
|
||
| await page.goto("/accounts"); | ||
| await disableCssAnimations(page); | ||
| await expect(page).toHaveTitle("Account | Network Nervous System"); | ||
|
|
||
| await signInWithNewUser({ page, context }); | ||
|
|
||
| const pageElement = PlaywrightPageObjectElement.fromPage(page); | ||
| const appPo = new AppPo(pageElement); | ||
| const usdValueBannerPo = appPo | ||
| .getAccountsPo() | ||
| .getNnsAccountsPo() | ||
| .getUsdValueBannerPo(); | ||
| await usdValueBannerPo.waitFor(); | ||
|
|
||
| // The app asks ICPSwap for the tickers only when the network configures an | ||
| // ICPSwap URL. `dfx.json` gives that URL to mainnet, app and beta, not to | ||
| // the local network, so the local app shows no price at all. | ||
| await page.waitForTimeout(5_000); | ||
| test.skip( | ||
| !tickersRequested, | ||
| "This network configures no ICPSwap URL, so the app requests no tickers." | ||
| ); | ||
|
|
||
| step("The banner shows the price of the most liquid pool"); | ||
|
|
||
| const icpPrice = await usdValueBannerPo.getIcpPrice(); | ||
| expect(icpPrice).toBe(REAL_ICP_PRICE_SHOWN); | ||
| expect(await usdValueBannerPo.hasError()).toBe(false); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.