Skip to content

Commit

Permalink
chore: remove balance check before executing route
Browse files Browse the repository at this point in the history
  • Loading branch information
odcey committed Dec 3, 2024
1 parent 1ae87bc commit 39cf7bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 56 deletions.
59 changes: 6 additions & 53 deletions src/handlers/evm/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EthersAdapter } from "../../adapter/EthersAdapter";
import erc20Abi from "../../abi/erc20.json";
import { EthersAdapter } from "../../adapter/EthersAdapter";

import {
Contract,
Expand All @@ -20,8 +20,8 @@ import {
NATIVE_EVM_TOKEN_ADDRESS,
uint256MaxValue,
} from "../../constants";
import { Utils } from "./utils";
import { TokensChains } from "../../utils/TokensChains";
import { Utils } from "./utils";

const ethersAdapter = new EthersAdapter();

Expand All @@ -45,13 +45,10 @@ export class EvmHandler extends Utils {
overrides,
});

await this.validateBalanceAndApproval({
data: {
...data,
overrides: gasData,
},
params,
});
const hasAllowance = await this.validateTokenAllowance({ data, params });
if (!hasAllowance) {
await this.approveRoute({ data, params });
}

const tx = {
to: target,
Expand Down Expand Up @@ -94,50 +91,6 @@ export class EvmHandler extends Utils {
}
}

async validateBalanceAndApproval({
data,
params,
}: {
data: ExecuteRoute;
params: RouteParamsPopulated;
}): Promise<boolean> {
const wallet = data.signer as EvmWallet;

// support of multiple signers type and versions
let address = (wallet as any).address;

// ethers v5 & v6 support
try {
address = await wallet.getAddress();
} catch (error) {
// do nothing
}

// validate balance
await this.validateBalance({
sender: address,
params,
});

if (params.fromIsNative) {
return true;
}

const hasAllowance = await this.validateAllowance({
fromTokenContract: params.fromTokenContract as Contract,
sender: address,
router: (data.route.transactionRequest as OnChainExecutionData).target,
amount: BigInt(params.fromAmount),
});

// approve token spent if necessary
if (!hasAllowance) {
await this.approveRoute({ data, params });
}

return true;
}

async approveRoute({
data,
params,
Expand Down
43 changes: 40 additions & 3 deletions src/handlers/evm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { ChainData, OnChainExecutionData, Token } from "@0xsquid/squid-types";

import { OverrideParams, Contract, GasData, RpcProvider, TokenBalance } from "../../types";
import { MulticallWrapper } from "ethers-multicall-provider";
import { Provider, ethers } from "ethers";
import { multicallAbi, MULTICALL_ADDRESS, NATIVE_EVM_TOKEN_ADDRESS } from "../../constants";
import { MulticallWrapper } from "ethers-multicall-provider";
import { MULTICALL_ADDRESS, NATIVE_EVM_TOKEN_ADDRESS, multicallAbi } from "../../constants";
import {
Contract,
EvmWallet,
ExecuteRoute,
GasData,
OverrideParams,
RouteParamsPopulated,
RpcProvider,
TokenBalance,
} from "../../types";

export class Utils {
async validateNativeBalance({
Expand Down Expand Up @@ -248,4 +257,32 @@ export class Utils {
return null;
}
}

async validateTokenAllowance({
params,
data,
}: {
params: RouteParamsPopulated;
data: ExecuteRoute;
}): Promise<boolean> {
if (params.fromIsNative) {
return true;
}

const wallet = data.signer as EvmWallet;
let address = (wallet as any).address;

try {
address = await wallet.getAddress();
} catch (error) {
// do nothing
}

return await this.validateAllowance({
fromTokenContract: params.fromTokenContract as Contract,
sender: address,
router: (data.route.transactionRequest as OnChainExecutionData).target,
amount: BigInt(params.fromAmount),
});
}
}

0 comments on commit 39cf7bb

Please sign in to comment.