From 531b31a395974f72c8ad83bf2ee57a0bc85ad6b1 Mon Sep 17 00:00:00 2001 From: Julien Genestoux Date: Fri, 1 Nov 2024 21:11:07 -0400 Subject: [PATCH] fix(unlock-app): JSON serailization fixed for crosschain purpose --- unlock-app/src/hooks/useCrossChainRoutes.ts | 104 +++++++++++--------- unlock-app/src/utils/theBox.ts | 21 ++-- 2 files changed, 68 insertions(+), 57 deletions(-) diff --git a/unlock-app/src/hooks/useCrossChainRoutes.ts b/unlock-app/src/hooks/useCrossChainRoutes.ts index ed3aaca4c29..a0f5125cb55 100644 --- a/unlock-app/src/hooks/useCrossChainRoutes.ts +++ b/unlock-app/src/hooks/useCrossChainRoutes.ts @@ -139,57 +139,65 @@ export const useCrossChainRoutes = ({ nativeBalance, ], queryFn: async () => { - if (!prices) { - return null - } - if ( - network === lock.network && - (token.address === lock.currencyContractAddress || - (!lock.currencyContractAddress && - token.address === ZeroAddress)) - ) { - return null - } - const route = await getCrossChainRoute({ - sender: account!, - lock, - prices, - recipients, - keyManagers: keyManagers || recipients, - referrers: recipients.map(() => - getReferrer(account!, paywallConfig, lock.address) - ), - purchaseData: purchaseData || recipients.map(() => '0x'), - srcToken: token.address, - srcChainId: network, - }) - if (!route) { - console.info( - `No route found from ${network} and ${token.address} to ${lock.network} and ${lock.currencyContractAddress}` - ) - return null - } - const amount = BigInt(toBigInt(route.tokenPayment.amount)) - let userTokenBalance - if (route.tokenPayment.tokenAddress === ZeroAddress) { - userTokenBalance = nativeBalance - } else { - userTokenBalance = await web3Service.getAddressBalance( - account!, - network, - route.tokenPayment.tokenAddress - ) - } + try { + if (!prices) { + return null + } + if ( + network === lock.network && + (token.address === lock.currencyContractAddress || + (!lock.currencyContractAddress && + token.address === ZeroAddress)) + ) { + return null + } + const route = await getCrossChainRoute({ + sender: account!, + lock, + prices, + recipients, + keyManagers: keyManagers || recipients, + referrers: recipients.map(() => + getReferrer(account!, paywallConfig, lock.address) + ), + purchaseData: purchaseData || recipients.map(() => '0x'), + srcToken: token.address, + srcChainId: network, + }) + if (!route) { + console.info( + `No route found from ${network} and ${token.address} to ${lock.network} and ${lock.currencyContractAddress}` + ) + return null + } + const amount = BigInt(toBigInt(route.tokenPayment.amount)) + let userTokenBalance + if (route.tokenPayment.tokenAddress === ZeroAddress) { + userTokenBalance = nativeBalance + } else { + userTokenBalance = await web3Service.getAddressBalance( + account!, + network, + route.tokenPayment.tokenAddress + ) + } - const cost = ethers.formatUnits(amount, route.tokenPayment.decimals) - if (Number(cost) > Number(userTokenBalance)) { + const cost = ethers.formatUnits( + amount, + route.tokenPayment.decimals + ) + if (Number(cost) > Number(userTokenBalance)) { + return null + } + return { + resolvedAt: new Date().getTime(), // maintaining order + userTokenBalance, + ...route, + } as CrossChainRouteWithBalance + } catch (error) { + console.error(error) return null } - return { - resolvedAt: new Date().getTime(), // maintaining order - userTokenBalance, - ...route, - } as CrossChainRouteWithBalance }, enabled: enabled && hasPrices, staleTime: 1000 * 60 * 5, // 5 minutes diff --git a/unlock-app/src/utils/theBox.ts b/unlock-app/src/utils/theBox.ts index c53f013ea81..d2362f16d10 100644 --- a/unlock-app/src/utils/theBox.ts +++ b/unlock-app/src/utils/theBox.ts @@ -29,12 +29,12 @@ export interface CrossChainRoute { currency: string } -// const bigintSerializer = (_key: string, value: unknown): unknown => { -// if (typeof value === 'bigint') { -// return value.toString() + 'n' -// } -// return value -// } +const bigintSerializer = (_key: string, value: unknown): unknown => { + if (typeof value === 'bigint') { + return value.toString() + 'n' + } + return value +} interface getCrossChainRouteParams { sender: string @@ -109,9 +109,12 @@ export const getCrossChainRoute = async ({ }, } - const query = JSON.stringify({ - ...actionRequest, - }) + const query = JSON.stringify( + { + ...actionRequest, + }, + bigintSerializer + ) const url = `${baseUrl}?arguments=${query}` const response = await axios