-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG] [Add Funds Widget] Get token to calculate USD amount (#…
- Loading branch information
1 parent
244530c
commit ffd8678
Showing
2 changed files
with
39 additions
and
6 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/widgets/add-funds/functions/convertToUsd.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,26 @@ | ||
import { ChainId, type TokenInfo } from '@imtbl/checkout-sdk'; | ||
import { Token } from '../types'; | ||
import { SQUID_NATIVE_TOKEN } from '../utils/config'; | ||
|
||
export function convertToUsd( | ||
tokens: Token[] | null, | ||
amount: string, | ||
token: TokenInfo | undefined, | ||
): number { | ||
if (!tokens || !amount || !token?.address) { | ||
return 0; | ||
} | ||
|
||
const address = token.address === 'native' ? SQUID_NATIVE_TOKEN : token.address; | ||
|
||
const toToken = tokens.find( | ||
(value) => value.address.toLowerCase() === address.toLowerCase() | ||
&& value.chainId === ChainId.IMTBL_ZKEVM_MAINNET.toString(), | ||
); | ||
|
||
if (!toToken) { | ||
return 0; | ||
} | ||
|
||
return Number(amount) * toToken.usdPrice; | ||
} |
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