-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Approve transaction manually due to bug in squid sdk - add convertToNetworkChangeableProvider to be used to change network without error - Execute transaction to return Transaction Receipt
- Loading branch information
1 parent
b372044
commit 40d313d
Showing
1 changed file
with
72 additions
and
30 deletions.
There are no files selected for viewing
102 changes: 72 additions & 30 deletions
102
packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useExecute.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 |
---|---|---|
@@ -1,51 +1,93 @@ | ||
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<Web3Provider> => new ethers.providers.Web3Provider( | ||
provider.provider, | ||
'any', | ||
); | ||
|
||
const checkProviderChain = async ( | ||
provider: Web3Provider, | ||
chainId: string, | ||
): Promise<void> => { | ||
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<boolean> => { | ||
await checkProviderChain(provider, routeResponse.route.params.fromChain); | ||
return squid.approveRoute({ | ||
signer: provider.getSigner(), | ||
route: routeResponse.route, | ||
}); | ||
const approve = async ( | ||
provider: Web3Provider, | ||
routeResponse: RouteResponse, | ||
): Promise<boolean> => { | ||
try { | ||
if (isSquidNativeToken(routeResponse?.route?.params.fromToken)) { | ||
return true; | ||
} | ||
|
||
const transactionRequestTarget = routeResponse?.route?.transactionRequest?.target; | ||
const fromToken = routeResponse?.route.params.fromToken; | ||
const fromAmount = routeResponse?.route.params.fromAmount; | ||
const signer = provider.getSigner(); | ||
const erc20Abi = [ | ||
'function approve(address spender, uint256 amount) public returns (bool)', | ||
]; | ||
const tokenContract = new ethers.Contract(fromToken, erc20Abi, signer); | ||
try { | ||
const tx = await tokenContract.approve(transactionRequestTarget, fromAmount); | ||
await tx.wait(); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} catch (e) { | ||
throw Error('Error approving tokens'); | ||
} | ||
}; | ||
|
||
const execute = async ( | ||
squid: Squid, | ||
provider: Web3Provider, | ||
routeResponse: RouteResponse, | ||
): Promise<TransactionResponses> => { | ||
await checkProviderChain(provider, routeResponse.route.params.fromChain); | ||
return squid.executeRoute({ | ||
signer: provider.getSigner(), | ||
route: routeResponse.route, | ||
}); | ||
): Promise<ethers.providers.TransactionReceipt> => { | ||
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, | ||
}; | ||
}; |