From 8b2632407cb06321682a2946f9a574bb9d9af28b Mon Sep 17 00:00:00 2001 From: Ji Young Lee <641712+jiyounglee@users.noreply.github.com> Date: Thu, 26 Sep 2024 17:00:01 +1000 Subject: [PATCH] [NO CHANGELOG] Add funds/update use execute (#2237) --- .../add-funds/functions/isSquidNativeToken.ts | 3 + .../src/widgets/add-funds/hooks/useExecute.ts | 96 +++++++++++++------ 2 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 packages/checkout/widgets-lib/src/widgets/add-funds/functions/isSquidNativeToken.ts diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/functions/isSquidNativeToken.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/functions/isSquidNativeToken.ts new file mode 100644 index 0000000000..25054dcbf1 --- /dev/null +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/functions/isSquidNativeToken.ts @@ -0,0 +1,3 @@ +const SQUID_NATIVE_TOKEN = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'; + +export const isSquidNativeToken = (token: string) => token === SQUID_NATIVE_TOKEN; diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useExecute.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useExecute.ts index ed3fd387a0..841c46b5ba 100644 --- a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useExecute.ts +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useExecute.ts @@ -1,51 +1,87 @@ import { Web3Provider } from '@ethersproject/providers'; import { RouteResponse } from '@0xsquid/squid-types'; import { Squid } from '@0xsquid/sdk'; -import { TransactionResponses } from '@0xsquid/sdk/dist/types'; +import { ethers } from 'ethers'; +import { isSquidNativeToken } from '../functions/isSquidNativeToken'; -export const useExecute = async () => { - const checkProviderChain = async (provider: Web3Provider, chainId: string) => { +export const useExecute = () => { + const convertToNetworkChangeableProvider = async ( + provider: Web3Provider, + ): Promise => new ethers.providers.Web3Provider( + provider.provider, + 'any', + ); + + const checkProviderChain = async ( + provider: Web3Provider, + chainId: string, + ): Promise => { if (!provider.provider.request) { throw Error('provider does not have request method'); } - - const fromChainHex = `0x${parseInt(chainId, 10).toString(16)}`; - const providerChainId = await provider.provider.request({ - method: 'eth_chainId', - }); - - if (fromChainHex !== providerChainId) { - await provider.provider.request({ - method: 'wallet_switchEthereumChain', - params: [ - { - chainId: fromChainHex, - }, - ], + try { + const fromChainHex = `0x${parseInt(chainId, 10).toString(16)}`; + const providerChainId = await provider.provider.request({ + method: 'eth_chainId', }); + if (fromChainHex !== providerChainId) { + await provider.provider.request({ + method: 'wallet_switchEthereumChain', + params: [ + { + chainId: fromChainHex, + }, + ], + }); + } + } catch (e) { + throw Error('Error checking provider'); } }; - const approve = async (squid: Squid, provider: Web3Provider, routeResponse: RouteResponse): Promise => { - await checkProviderChain(provider, routeResponse.route.params.fromChain); - return squid.approveRoute({ - signer: provider.getSigner(), - route: routeResponse.route, - }); + const approve = async ( + provider: Web3Provider, + routeResponse: RouteResponse, + ): Promise => { + try { + if (!isSquidNativeToken(routeResponse?.route?.params.fromToken)) { + const fromToken = routeResponse?.route.params.fromToken; + const erc20Abi = [ + 'function approve(address spender, uint256 amount) public returns (bool)', + ]; + const signer = provider.getSigner(); + const tokenContract = new ethers.Contract(fromToken, erc20Abi, signer); + + const transactionRequestTarget = routeResponse?.route?.transactionRequest?.target; + const fromAmount = routeResponse?.route.params.fromAmount; + const tx = await tokenContract.approve(transactionRequestTarget, fromAmount); + await tx.wait(); + } + } catch (e) { + throw Error('Error approving tokens'); + } }; const execute = async ( squid: Squid, provider: Web3Provider, routeResponse: RouteResponse, - ): Promise => { - await checkProviderChain(provider, routeResponse.route.params.fromChain); - return squid.executeRoute({ - signer: provider.getSigner(), - route: routeResponse.route, - }); + ): Promise => { + if (!provider.provider.request) { + throw Error('provider does not have request method'); + } + try { + const tx = (await squid.executeRoute({ + signer: provider.getSigner(), + route: routeResponse.route, + })) as unknown as ethers.providers.TransactionResponse; + return tx.wait(); + } catch (e) { + throw Error('Error executing route'); + } }; + return { - approve, execute, + convertToNetworkChangeableProvider, checkProviderChain, approve, execute, }; };