Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute send transaction from Viem client #222

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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