Skip to content

Commit

Permalink
fix(unlock-app): JSON serailization fixed for crosschain purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
julien51 committed Nov 2, 2024
1 parent c043ba4 commit 531b31a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
104 changes: 56 additions & 48 deletions unlock-app/src/hooks/useCrossChainRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions unlock-app/src/utils/theBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 531b31a

Please sign in to comment.