-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Check token price before sending transaction
- Loading branch information
1 parent
5e97e6c
commit 5df66de
Showing
3 changed files
with
68 additions
and
17 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
33 changes: 33 additions & 0 deletions
33
packages/canonical-bridge-widget/src/modules/transfer/hooks/usePriceValidation.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,33 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { useBridgeConfig } from '@/index'; | ||
import { useTokenPrice } from '@/modules/aggregator/providers/TokenPricesProvider'; | ||
|
||
export const usePriceValidation = () => { | ||
const { fetchApiTokenPrices } = useTokenPrice(); | ||
const bridgeConfig = useBridgeConfig(); | ||
|
||
const validateTokenPrice = useCallback( | ||
async ({ tokenSymbol, tokenAddress }: { tokenSymbol: string; tokenAddress: string }) => { | ||
const { cmcPrices, llamaPrices } = await fetchApiTokenPrices(bridgeConfig); | ||
|
||
const key1 = `${tokenSymbol?.toLowerCase()}:${tokenAddress?.toLowerCase()}`; | ||
const key3 = tokenSymbol?.toLowerCase(); | ||
const key2 = `ethereum:${key3}`; | ||
let price = | ||
cmcPrices?.[key1]?.price ?? | ||
llamaPrices?.[key1]?.price ?? | ||
cmcPrices?.[key2]?.price ?? | ||
llamaPrices?.[key2]?.price ?? | ||
cmcPrices?.[key3]?.price ?? | ||
llamaPrices?.[key3]?.price; | ||
if (price !== undefined) { | ||
price = Number(price); | ||
} | ||
return price; | ||
}, | ||
[bridgeConfig, fetchApiTokenPrices], | ||
); | ||
|
||
return { validateTokenPrice }; | ||
}; |