-
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.
[NO CHANGELOG][Add Funds Widget] Implement useExecute (#2194)
- Loading branch information
1 parent
a154af5
commit 77a9ec7
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { RouteResponse } from '@0xsquid/squid-types'; | ||
import { Squid } from '@0xsquid/sdk'; | ||
import { TransactionResponses } from '@0xsquid/sdk/dist/types'; | ||
|
||
export const useExecute = async () => { | ||
const checkProviderChain = async (provider: Web3Provider, chainId: string) => { | ||
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, | ||
}, | ||
], | ||
}); | ||
} | ||
}; | ||
|
||
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 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, | ||
}); | ||
}; | ||
return { | ||
approve, execute, | ||
}; | ||
}; |