Skip to content

Commit

Permalink
Refactor approve
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyounglee committed Sep 26, 2024
1 parent 40d313d commit 2d88bf1
Showing 1 changed file with 10 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,20 @@ export const useExecute = () => {
const approve = async (
provider: Web3Provider,
routeResponse: RouteResponse,
): Promise<boolean> => {
): Promise<void> => {
try {
if (isSquidNativeToken(routeResponse?.route?.params.fromToken)) {
return true;
}
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 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 transactionRequestTarget = routeResponse?.route?.transactionRequest?.target;
const fromAmount = routeResponse?.route.params.fromAmount;
const tx = await tokenContract.approve(transactionRequestTarget, fromAmount);
await tx.wait();
return true;
} catch (error) {
return false;
}
} catch (e) {
throw Error('Error approving tokens');
Expand Down

0 comments on commit 2d88bf1

Please sign in to comment.