forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Wallet]: Refactor deepbook (MystenLabs#14395)
## Description - Refactor `useDeepbook` https://mysten.atlassian.net/browse/APPS-1819 ## Test Plan How did you test the new or updated feature? --- If your changes are not user-facing and not a breaking change, you can skip the following section. Otherwise, please indicate what changed, and then add to the Release Notes section as highlighted during the release process. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes
- Loading branch information
Showing
14 changed files
with
303 additions
and
278 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './useDeepBookConfigs'; | ||
export * from './useRecognizedCoins'; | ||
export * from './useDeepbookPools'; | ||
export * from './useBalanceConversion'; | ||
export * from './useGetEstimate'; |
97 changes: 97 additions & 0 deletions
97
apps/wallet/src/ui/app/hooks/deepbook/useBalanceConversion.ts
This file contains 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,97 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { Coins, useDeepBookConfigs } from '_app/hooks'; | ||
import { DEEPBOOK_KEY } from '_pages/swap/constants'; | ||
import { useDeepBookContext } from '_shared/deepBook/context'; | ||
import { type DeepBookClient } from '@mysten/deepbook'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
async function getDeepBookPriceForCoin( | ||
coin: Coins, | ||
pools: Record<string, string[]>, | ||
isAsk: boolean, | ||
deepBookClient: DeepBookClient, | ||
) { | ||
if (coin === Coins.USDC) { | ||
return 1n; | ||
} | ||
|
||
const poolName = `${coin}_USDC`; | ||
const poolIds = pools[poolName]; | ||
const promises = poolIds.map(async (poolId) => { | ||
const { bestBidPrice, bestAskPrice } = await deepBookClient.getMarketPrice(poolId); | ||
|
||
return isAsk ? bestBidPrice : bestAskPrice; | ||
}); | ||
|
||
const prices = await Promise.all(promises); | ||
|
||
const filter: bigint[] = prices.filter((price): price is bigint => { | ||
return typeof price === 'bigint' && price !== 0n; | ||
}); | ||
|
||
const total = filter.reduce((acc, price) => { | ||
return acc + price; | ||
}, 0n); | ||
|
||
return total / BigInt(filter.length); | ||
} | ||
|
||
export function useBalanceConversion({ | ||
balance, | ||
from, | ||
to, | ||
conversionRate = 1, | ||
}: { | ||
balance: BigInt | BigNumber | null; | ||
from: Coins; | ||
to: Coins; | ||
conversionRate: number; | ||
}) { | ||
const deepBookClient = useDeepBookContext().client; | ||
const deepbookPools = useDeepBookConfigs().pools; | ||
const isAsk = to === Coins.USDC; | ||
|
||
return useQuery({ | ||
queryKey: [DEEPBOOK_KEY, 'get-prices-usd', isAsk, from, to], | ||
queryFn: async () => { | ||
const coins = [from, to]; | ||
const promises = coins.map((coin) => | ||
getDeepBookPriceForCoin(coin, deepbookPools, isAsk, deepBookClient), | ||
); | ||
return Promise.all(promises); | ||
}, | ||
select: (prices) => { | ||
const basePrice = new BigNumber((prices?.[0] ?? 1n).toString()); | ||
const quotePrice = new BigNumber((prices?.[1] ?? 1n).toString()); | ||
|
||
const basePriceBigNumber = new BigNumber(basePrice.toString()); | ||
const quotePriceBigNumber = new BigNumber(quotePrice.toString()); | ||
|
||
let avgPrice; | ||
if (to === Coins.USDC) { | ||
avgPrice = basePriceBigNumber; | ||
} else { | ||
avgPrice = basePriceBigNumber.dividedBy(quotePriceBigNumber); | ||
} | ||
|
||
const averagePriceWithConversion = avgPrice?.shiftedBy(conversionRate); | ||
|
||
if (!averagePriceWithConversion || !balance) return null; | ||
|
||
const rawUsdValue = new BigNumber(balance.toString()) | ||
.multipliedBy(averagePriceWithConversion) | ||
.toNumber(); | ||
|
||
if (isNaN(rawUsdValue)) { | ||
return null; | ||
} | ||
|
||
return { | ||
rawValue: rawUsdValue, | ||
averagePrice: averagePriceWithConversion, | ||
}; | ||
}, | ||
}); | ||
} |
39 changes: 39 additions & 0 deletions
39
apps/wallet/src/ui/app/hooks/deepbook/useDeepBookConfigs.ts
This file contains 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,39 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { FEATURES } from '_shared/experimentation/features'; | ||
import { useFeatureValue } from '@growthbook/growthbook-react'; | ||
|
||
export enum Coins { | ||
SUI = 'SUI', | ||
USDC = 'USDC', | ||
USDT = 'USDT', | ||
WETH = 'WETH', | ||
TBTC = 'TBTC', | ||
} | ||
|
||
export const mainnetDeepBook: { | ||
pools: Record<string, string[]>; | ||
coinsMap: Record<Coins, string>; | ||
} = { | ||
pools: { | ||
SUI_USDC: [ | ||
'0x4405b50d791fd3346754e8171aaab6bc2ed26c2c46efdd033c14b30ae507ac33', | ||
'0x7f526b1263c4b91b43c9e646419b5696f424de28dda3c1e6658cc0a54558baa7', | ||
], | ||
WETH_USDC: ['0xd9e45ab5440d61cc52e3b2bd915cdd643146f7593d587c715bc7bfa48311d826'], | ||
TBTC_USDC: ['0xf0f663cf87f1eb124da2fc9be813e0ce262146f3df60bc2052d738eb41a25899'], | ||
USDT_USDC: ['0x5deafda22b6b86127ea4299503362638bea0ca33bb212ea3a67b029356b8b955'], | ||
}, | ||
coinsMap: { | ||
[Coins.SUI]: '0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI', | ||
[Coins.USDC]: '0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN', | ||
[Coins.USDT]: '0xc060006111016b8a020ad5b33834984a437aaa7d3c74c18e09a95d48aceab08c::coin::COIN', | ||
[Coins.WETH]: '0xaf8cd5edc19c4512f4259f0bee101a40d41ebed738ade5874359610ef8eeced5::coin::COIN', | ||
[Coins.TBTC]: '0xbc3a676894871284b3ccfb2eec66f428612000e2a6e6d23f592ce8833c27c973::coin::COIN', | ||
}, | ||
}; | ||
|
||
export function useDeepBookConfigs() { | ||
return useFeatureValue(FEATURES.DEEP_BOOK_CONFIGS, mainnetDeepBook); | ||
} |
This file contains 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,14 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { DEEPBOOK_KEY } from '_pages/swap/constants'; | ||
import { useDeepBookContext } from '_shared/deepBook/context'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
export function useDeepbookPools() { | ||
const deepBookClient = useDeepBookContext().client; | ||
|
||
return useQuery({ | ||
queryKey: [DEEPBOOK_KEY, 'get-all-pools'], | ||
queryFn: () => deepBookClient.getAllPools({}), | ||
}); | ||
} |
Oops, something went wrong.