Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/checkout/widgets-lib/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ describe('utils', () => {
expect(isNativeToken('NATIVE')).toBeTruthy();
});

it('should return true if address is zero address (used by some APIs for native token)', () => {
expect(isNativeToken('0x0000000000000000000000000000000000000000')).toBeTruthy();
});

it('should return false if address is not NATIVE', () => {
expect(isNativeToken('0x123')).toBeFalsy();
});
Expand Down
7 changes: 6 additions & 1 deletion packages/checkout/widgets-lib/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,14 @@ export const isZkEvmChainId = (chainId: ChainId) => chainId === ChainId.IMTBL_ZK
export const isL1EthChainId = (chainId: ChainId) => chainId === ChainId.SEPOLIA
|| chainId === ChainId.ETHEREUM;

/** Zero address used by some APIs (e.g. primary-sales) to denote native token (e.g. tIMX on zkEVM) */
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

export const isNativeToken = (
address: string | undefined,
): boolean => !address || address.toLocaleLowerCase() === NATIVE;
): boolean => !address
|| address.toLocaleLowerCase() === NATIVE
|| address.toLocaleLowerCase() === ZERO_ADDRESS;

export function getRemoteImage(environment: Environment | undefined, path: string) {
return `${CHECKOUT_CDN_BASE_URL[environment ?? Environment.PRODUCTION]}/v1/blob/img${path}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Checkout, ItemBalance, WrappedBrowserProvider, TokenBalance, TransactionRequirement,
} from '@imtbl/checkout-sdk';
import { Environment } from '@imtbl/config';
import { compareStr } from '../../../lib/utils';
import { compareStr, isNativeToken } from '../../../lib/utils';
import {
OrderQuoteCurrency,
FundingBalance,
Expand All @@ -11,6 +11,7 @@ import {
import {
getAlternativeFundingSteps,
getERC20ItemRequirement,
getNativeItemRequirement,
getFnToPushAndSortFundingBalances,
getFundingBalances,
getGasEstimate,
Expand Down Expand Up @@ -75,11 +76,9 @@ export const fetchFundingBalances = async (
return null;
}

const itemRequirements = getERC20ItemRequirement(
amount,
spenderAddress,
currency.address,
);
const itemRequirements = isNativeToken(currency.address)
? getNativeItemRequirement(amount)
: getERC20ItemRequirement(amount, spenderAddress, currency.address);

const transactionOrGasAmount = getIsGasless()
? undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TransactionOrGasType,
TokenInfo,
ERC20ItemRequirement,
NativeItemRequirement,
FundingRoute,
RoutingOutcomeType,
FundingStep,
Expand Down Expand Up @@ -41,6 +42,13 @@ export const getERC20ItemRequirement = (
},
];

export const getNativeItemRequirement = (amount: string): NativeItemRequirement[] => [
{
type: ItemType.NATIVE,
amount,
},
];

export const getGasEstimate = (): GasAmount => ({
type: TransactionOrGasType.GAS,
gasToken: {
Expand Down
Loading