From 39cf7bba4966a1dd8bd7671be00f69df0276b4e3 Mon Sep 17 00:00:00 2001 From: pilot <8565879+odcey@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:14:16 +0100 Subject: [PATCH 1/4] chore: remove balance check before executing route --- src/handlers/evm/index.ts | 59 ++++----------------------------------- src/handlers/evm/utils.ts | 43 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index 0486798..3e0093f 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -1,5 +1,5 @@ -import { EthersAdapter } from "../../adapter/EthersAdapter"; import erc20Abi from "../../abi/erc20.json"; +import { EthersAdapter } from "../../adapter/EthersAdapter"; import { Contract, @@ -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(); @@ -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, @@ -94,50 +91,6 @@ export class EvmHandler extends Utils { } } - async validateBalanceAndApproval({ - data, - params, - }: { - data: ExecuteRoute; - params: RouteParamsPopulated; - }): Promise { - 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, diff --git a/src/handlers/evm/utils.ts b/src/handlers/evm/utils.ts index 371392d..5afebbe 100644 --- a/src/handlers/evm/utils.ts +++ b/src/handlers/evm/utils.ts @@ -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({ @@ -248,4 +257,32 @@ export class Utils { return null; } } + + async validateTokenAllowance({ + params, + data, + }: { + params: RouteParamsPopulated; + data: ExecuteRoute; + }): Promise { + 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), + }); + } } From 01f4a79b477f93d2f8da2dcab488ae8e4db8647a Mon Sep 17 00:00:00 2001 From: pilot <8565879+odcey@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:35:18 +0100 Subject: [PATCH 2/4] Revert "chore: remove balance check before executing route" This reverts commit 39cf7bba4966a1dd8bd7671be00f69df0276b4e3. --- src/handlers/evm/index.ts | 59 +++++++++++++++++++++++++++++++++++---- src/handlers/evm/utils.ts | 43 ++-------------------------- 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index 3e0093f..0486798 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -1,5 +1,5 @@ -import erc20Abi from "../../abi/erc20.json"; import { EthersAdapter } from "../../adapter/EthersAdapter"; +import erc20Abi from "../../abi/erc20.json"; import { Contract, @@ -20,8 +20,8 @@ import { NATIVE_EVM_TOKEN_ADDRESS, uint256MaxValue, } from "../../constants"; -import { TokensChains } from "../../utils/TokensChains"; import { Utils } from "./utils"; +import { TokensChains } from "../../utils/TokensChains"; const ethersAdapter = new EthersAdapter(); @@ -45,10 +45,13 @@ export class EvmHandler extends Utils { overrides, }); - const hasAllowance = await this.validateTokenAllowance({ data, params }); - if (!hasAllowance) { - await this.approveRoute({ data, params }); - } + await this.validateBalanceAndApproval({ + data: { + ...data, + overrides: gasData, + }, + params, + }); const tx = { to: target, @@ -91,6 +94,50 @@ export class EvmHandler extends Utils { } } + async validateBalanceAndApproval({ + data, + params, + }: { + data: ExecuteRoute; + params: RouteParamsPopulated; + }): Promise { + 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, diff --git a/src/handlers/evm/utils.ts b/src/handlers/evm/utils.ts index 5afebbe..371392d 100644 --- a/src/handlers/evm/utils.ts +++ b/src/handlers/evm/utils.ts @@ -1,18 +1,9 @@ import { ChainData, OnChainExecutionData, Token } from "@0xsquid/squid-types"; -import { Provider, ethers } from "ethers"; +import { OverrideParams, Contract, GasData, RpcProvider, TokenBalance } from "../../types"; 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"; +import { Provider, ethers } from "ethers"; +import { multicallAbi, MULTICALL_ADDRESS, NATIVE_EVM_TOKEN_ADDRESS } from "../../constants"; export class Utils { async validateNativeBalance({ @@ -257,32 +248,4 @@ export class Utils { return null; } } - - async validateTokenAllowance({ - params, - data, - }: { - params: RouteParamsPopulated; - data: ExecuteRoute; - }): Promise { - 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), - }); - } } From fd5012e3ca452f531098db0da4a7860cdd121327 Mon Sep 17 00:00:00 2001 From: pilot <8565879+odcey@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:44:26 +0100 Subject: [PATCH 3/4] chore: introduce bypassBalanceChecks var for route execution --- src/handlers/evm/index.ts | 21 ++++++++++++--------- src/index.ts | 5 ++++- src/types/index.ts | 9 +++++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index 0486798..537d8af 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -1,5 +1,5 @@ -import { EthersAdapter } from "../../adapter/EthersAdapter"; import erc20Abi from "../../abi/erc20.json"; +import { EthersAdapter } from "../../adapter/EthersAdapter"; import { Contract, @@ -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(); @@ -32,6 +32,7 @@ export class EvmHandler extends Utils { }: { data: ExecuteRoute; params: RouteParamsPopulated; + bypassBalanceChecks?: boolean; }): Promise { const { route: { transactionRequest }, @@ -45,13 +46,15 @@ export class EvmHandler extends Utils { overrides, }); - await this.validateBalanceAndApproval({ - data: { - ...data, - overrides: gasData, - }, - params, - }); + if (!data.bypassBalanceChecks) { + await this.validateBalanceAndApproval({ + data: { + ...data, + overrides: gasData, + }, + params, + }); + } const tx = { to: target, diff --git a/src/index.ts b/src/index.ts index e3e279f..fddc56a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -158,7 +158,10 @@ export class Squid extends TokensChains { data.signer as EvmWallet, ); - return this.handlers.evm.executeRoute({ data, params: evmParams }); + return this.handlers.evm.executeRoute({ + data, + params: evmParams, + }); case ChainType.COSMOS: const cosmosParams = this.handlers.cosmos.populateRouteParams(this, data.route.params); diff --git a/src/types/index.ts b/src/types/index.ts index cf975c9..7556580 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,14 +1,14 @@ import { - ChainData, - Token, - RouteRequest, RouteResponse as _RouteResponse, + ChainData, DepositAddressResponse, + RouteRequest, + Token, } from "@0xsquid/squid-types"; import { SigningStargateClient } from "@cosmjs/stargate"; -import { EvmWallet, TransactionResponse, RpcProvider, Contract, GasData } from "./ethers"; import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx"; +import { Contract, EvmWallet, GasData, RpcProvider, TransactionResponse } from "./ethers"; export * from "@0xsquid/squid-types"; export * from "./cosmos"; @@ -42,6 +42,7 @@ export type ExecuteRoute = { route: _RouteResponse["route"]; executionSettings?: ExecutionSettings; overrides?: OverrideParams; + bypassBalanceChecks?: boolean; signerAddress?: string; // cosmos specific }; From b3e96522c4ae36496745c1cf983e1e8ad1c671b9 Mon Sep 17 00:00:00 2001 From: pilot <8565879+odcey@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:54:26 +0100 Subject: [PATCH 4/4] chore: remove unused param --- src/handlers/evm/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index 537d8af..94ad4bb 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -32,7 +32,6 @@ export class EvmHandler extends Utils { }: { data: ExecuteRoute; params: RouteParamsPopulated; - bypassBalanceChecks?: boolean; }): Promise { const { route: { transactionRequest },