Skip to content

Commit

Permalink
feat: improve transaction execution for smart account clients (#222)
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Chybisov <[email protected]>
Co-authored-by: Eugene Chybisov <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent d107759 commit 2580dbe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/core/EVM/EVMStepExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import type {
FullStatusData,
Process,
} from '@lifi/types'
import type { Client, Hash, SendTransactionParameters } from 'viem'
import type {
Client,
GetAddressesReturnType,
Hash,
SendTransactionParameters,
} from 'viem'
import { getAddresses, sendTransaction } from 'viem/actions'
import { getAction } from 'viem/utils'
import { config } from '../../config.js'
import { LiFiErrorCode } from '../../errors/constants.js'
import { TransactionError, ValidationError } from '../../errors/errors.js'
Expand Down Expand Up @@ -63,7 +69,11 @@ export class EVMStepExecutor extends BaseStepExecutor {
// Prevent execution of the quote by wallet different from the one which requested the quote
let accountAddress = this.client.account?.address
if (!accountAddress) {
const accountAddresses = await getAddresses(this.client)
const accountAddresses = (await getAction(
this.client,
getAddresses,
'getAddresses'
)(undefined)) as GetAddressesReturnType
accountAddress = accountAddresses?.[0]
}
if (accountAddress !== step.action.fromAddress) {
Expand Down Expand Up @@ -308,7 +318,11 @@ export class EVMStepExecutor extends BaseStepExecutor {
)
}
} else {
txHash = await sendTransaction(this.client, {
txHash = await getAction(
this.client,
sendTransaction,
'sendTransaction'
)({
to: transactionRequest.to,
account: this.client.account!,
data: transactionRequest.data,
Expand Down
7 changes: 6 additions & 1 deletion src/core/EVM/setAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Client, Hash, SendTransactionParameters } from 'viem'
import { encodeFunctionData } from 'viem'
import { sendTransaction } from 'viem/actions'
import { getAction } from 'viem/utils'
import { isNativeTokenAddress } from '../../utils/isZeroAddress.js'
import type { ExecutionOptions, TransactionParameters } from '../types.js'
import { approveAbi } from './abi.js'
Expand Down Expand Up @@ -48,7 +49,11 @@ export const setAllowance = async (
}
}

return sendTransaction(client, {
return getAction(
client,
sendTransaction,
'sendTransaction'
)({
to: transactionRequest.to,
account: client.account!,
data: transactionRequest.data,
Expand Down
15 changes: 12 additions & 3 deletions src/core/EVM/switchChain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Client } from 'viem'
import type { Client, GetChainIdReturnType } from 'viem'
import { getChainId } from 'viem/actions'
import { getAction } from 'viem/utils'
import { LiFiErrorCode } from '../../errors/constants.js'
import { ProviderError } from '../../errors/errors.js'
import type { StatusManager } from '../StatusManager.js'
Expand Down Expand Up @@ -30,7 +31,11 @@ export const switchChain = async (
switchChainHook?: SwitchChainHook
): Promise<Client | undefined> => {
// if we are already on the correct chain we can proceed directly
const currentChainId = await getChainId(client)
const currentChainId = (await getAction(
client,
getChainId,
'getChainId'
)(undefined)) as GetChainIdReturnType
if (currentChainId === step.action.fromChainId) {
return client
}
Expand All @@ -53,7 +58,11 @@ export const switchChain = async (
const updatedClient = await switchChainHook?.(step.action.fromChainId)
let updatedChainId: number | undefined
if (updatedClient) {
updatedChainId = await getChainId(updatedClient)
updatedChainId = (await getAction(
updatedClient,
getChainId,
'getChainId'
)(undefined)) as GetChainIdReturnType
}
if (updatedChainId !== step.action.fromChainId) {
throw new ProviderError(
Expand Down

0 comments on commit 2580dbe

Please sign in to comment.