Skip to content

Commit

Permalink
fix: add chain
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 5, 2025
1 parent de51430 commit bc086a3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/core/EVM/EVMStepExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { parseEVMErrors } from './parseEVMErrors.js'
import { type PermitSignature, signPermitMessage } from './signPermitMessage.js'
import { switchChain } from './switchChain.js'
import { isRelayerStep } from './typeguards.js'
import { getMaxPriorityFeePerGas } from './utils.js'
import { convertExtendedChain, getMaxPriorityFeePerGas } from './utils.js'
import {
type WalletCallReceipt,
waitForBatchTransactionReceipt,
Expand Down Expand Up @@ -86,7 +86,9 @@ export class EVMStepExecutor extends BaseStepExecutor {
)(undefined)) as GetAddressesReturnType
accountAddress = accountAddresses?.[0]
}
if (accountAddress?.toLowerCase() !== step.action.fromAddress?.toLowerCase()) {
if (
accountAddress?.toLowerCase() !== step.action.fromAddress?.toLowerCase()
) {
let processToUpdate = process
if (!processToUpdate) {
// We need to create some process if we don't have one so we can show the error
Expand Down Expand Up @@ -533,14 +535,15 @@ export class EVMStepExecutor extends BaseStepExecutor {
sendTransaction,
'sendTransaction'
)({
to: transactionRequest.to,
to: transactionRequest.to as Address,
account: this.client.account!,
data: transactionRequest.data,
data: transactionRequest.data as Hex,
value: transactionRequest.value,
gas: transactionRequest.gas,
gasPrice: transactionRequest.gasPrice,
maxFeePerGas: transactionRequest.maxFeePerGas,
maxPriorityFeePerGas: transactionRequest.maxPriorityFeePerGas,
chain: convertExtendedChain(fromChain),
} as SendTransactionParameters)
}
}
Expand Down
55 changes: 53 additions & 2 deletions src/core/EVM/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
import type { ChainId } from '@lifi/types'
import type { Address, Client, Transaction } from 'viem'
import type { ChainId, ExtendedChain } from '@lifi/types'
import type { Address, Chain, Client, Transaction } from 'viem'
import { getBlock } from 'viem/actions'
import { config } from '../../config.js'
import { median } from '../../utils/median.js'

type ChainBlockExplorer = {
name: string
url: string
}

type ChainBlockExplorers = {
[key: string]: ChainBlockExplorer
default: ChainBlockExplorer
}

export const convertExtendedChain = (chain: ExtendedChain): Chain => ({
...chain,
...chain.metamask,
blockExplorers: chain.metamask.blockExplorerUrls.reduce(
(blockExplorers, blockExplorer, index) => {
blockExplorers[index === 0 ? 'default' : `${index}`] = {
name: blockExplorer,
url: blockExplorer,
}
return blockExplorers
},
{} as ChainBlockExplorers
),
name: chain.metamask.chainName,
rpcUrls: {
default: { http: chain.metamask.rpcUrls },
public: { http: chain.metamask.rpcUrls },
},
contracts: {
...(chain.multicallAddress
? { multicall3: { address: chain.multicallAddress as Address } }
: undefined),
},
})

export function isExtendedChain(chain: any): chain is ExtendedChain {
return (
typeof chain === 'object' &&
chain !== null &&
'key' in chain &&
'chainType' in chain &&
'coin' in chain &&
'mainnet' in chain &&
'logoURI' in chain &&
typeof chain.metamask === 'object' &&
chain.metamask !== null &&
typeof chain.nativeToken === 'object' &&
chain.nativeToken !== null
)
}

export const getMaxPriorityFeePerGas = async (
client: Client
): Promise<bigint | undefined> => {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
setTokenAllowance,
} from './core/EVM/setAllowance.js'
export { isEVM } from './core/EVM/types.js'
export { isExtendedChain, convertExtendedChain } from './core/EVM/utils.js'
export { isRelayerStep } from './core/EVM/typeguards.js'
export type {
EVMProvider,
Expand Down

0 comments on commit bc086a3

Please sign in to comment.