Skip to content

Commit

Permalink
Update useExecute
Browse files Browse the repository at this point in the history
- 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
jiyounglee committed Sep 26, 2024
1 parent b372044 commit 40d313d
Showing 1 changed file with 72 additions and 30 deletions.
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,
};
};

0 comments on commit 40d313d

Please sign in to comment.