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

refactor: improve waiting for destination chain #229

Merged
merged 2 commits into from
Jan 7, 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
8 changes: 4 additions & 4 deletions examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"author": "LI.FI <[email protected]>",
"license": "MIT",
"dependencies": {
"@lifi/data-types": "^5.19.0",
"@lifi/sdk": "^3.4.4",
"@lifi/data-types": "^5.19.1",
"@lifi/sdk": "^3.5.0",
"@wagmi/connectors": "^5.7.3",
"@wagmi/core": "^2.16.3",
"dotenv": "^16.4.7",
"viem": "^2.22.1"
"viem": "^2.22.2"
},
"scripts": {
"example:swap": "tsx examples/swap.ts",
Expand All @@ -24,7 +24,7 @@
"example:yearn": "tsx examples/yearnDeposit.ts"
},
"devDependencies": {
"@types/node": "^22.10.3",
"@types/node": "^22.10.5",
"tsx": "^4.19.2",
"typescript": "^5.7.2"
}
Expand Down
167 changes: 105 additions & 62 deletions examples/node/pnpm-lock.yaml

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lifi/sdk",
"version": "3.5.0",
"version": "3.5.1-beta.0",
"description": "LI.FI Any-to-Any Cross-Chain-Swap SDK",
"keywords": [
"bridge",
Expand Down Expand Up @@ -62,7 +62,7 @@
"build:cjs": "tsc --project ./tsconfig.build.json --module commonjs --outDir ./src/_cjs --removeComments --verbatimModuleSyntax false && printf '{\"type\":\"commonjs\"}' > ./src/_cjs/package.json",
"build:esm": "tsc --project ./tsconfig.build.json --module es2015 --outDir ./src/_esm && printf '{\"type\": \"module\",\"sideEffects\":false}' > ./src/_esm/package.json",
"build:types": "tsc --project ./tsconfig.build.json --module esnext --declarationDir ./src/_types --emitDeclarationOnly --declaration --declarationMap",
"clean": "rm -rf dist tsconfig.tsbuildinfo tsconfig.build.tsbuildinfo src/tsconfig.build.tsbuildinfo src/_esm src/_cjs src/_types",
"clean": "rm -rf dist src/_esm src/_cjs src/_types",
"coverage": "vitest run --coverage",
"postinstall": "husky",
"prepack": "pinst --disable",
Expand Down Expand Up @@ -94,14 +94,13 @@
"dependencies": {
"@bigmi/core": "^0.1.0",
"@lifi/types": "^16.5.0",
"@noble/curves": "^1.7.0",
"@noble/hashes": "^1.6.1",
"@noble/curves": "^1.8.0",
"@solana/wallet-adapter-base": "^0.9.23",
"@solana/web3.js": "^1.98.0",
"bech32": "^2.0.0",
"bitcoinjs-lib": "^7.0.0-rc.0",
"bs58": "^6.0.0",
"viem": "^2.22.1"
"viem": "^2.22.2"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
Expand Down
39 changes: 25 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 14 additions & 74 deletions src/core/EVM/EVMStepExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
ExtendedTransactionInfo,
FullStatusData,
Process,
} from '@lifi/types'
import type { Process } from '@lifi/types'
import type {
Client,
GetAddressesReturnType,
Expand All @@ -15,18 +11,16 @@ import { config } from '../../config.js'
import { LiFiErrorCode } from '../../errors/constants.js'
import { TransactionError, ValidationError } from '../../errors/errors.js'
import { getStepTransaction } from '../../services/api.js'
import { getTransactionFailedMessage } from '../../utils/getTransactionMessage.js'
import { isZeroAddress } from '../../utils/isZeroAddress.js'
import { BaseStepExecutor } from '../BaseStepExecutor.js'
import { checkBalance } from '../checkBalance.js'
import { getSubstatusMessage } from '../processMessages.js'
import { stepComparison } from '../stepComparison.js'
import type {
LiFiStepExtended,
StepExecutorOptions,
TransactionParameters,
} from '../types.js'
import { waitForReceivingTransaction } from '../waitForReceivingTransaction.js'
import { waitForDestinationChainTransaction } from '../waitForDestinationChainTransaction.js'
import { checkAllowance } from './checkAllowance.js'
import { updateMultisigRouteProcess } from './multisig.js'
import { parseEVMErrors } from './parseEVMErrors.js'
Expand Down Expand Up @@ -109,16 +103,16 @@ export class EVMStepExecutor extends BaseStepExecutor {
executeStep = async (step: LiFiStepExtended): Promise<LiFiStepExtended> => {
step.execution = this.statusManager.initExecutionObject(step)

// Find if it's bridging and the step is waiting for a transaction on the receiving chain
const recievingChainProcess = step.execution?.process.find(
// Find if it's bridging and the step is waiting for a transaction on the destination chain
const destinationChainProcess = step.execution?.process.find(
(process) => process.type === 'RECEIVING_CHAIN'
)

// Make sure that the chain is still correct
// If the step is waiting for a transaction on the receiving chain, we do not switch the chain
// If the step is waiting for a transaction on the destination chain, we do not switch the chain
// All changes are already done from the source chain
// Return the step
if (recievingChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION') {
if (destinationChainProcess?.substatus !== 'WAIT_DESTINATION_TRANSACTION') {
const updatedClient = await this.checkClient(step)
if (!updatedClient) {
return step
Expand All @@ -138,7 +132,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
const isBridgeExecution = fromChain.id !== toChain.id
const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP'

// STEP 1: Check allowance
// Check allowance
const existingProcess = step.execution.process.find(
(p) => p.type === currentProcessType
)
Expand Down Expand Up @@ -171,7 +165,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
}
}

// STEP 2: Get transaction
let process = this.statusManager.findOrCreateProcess({
step,
type: currentProcessType,
Expand Down Expand Up @@ -245,7 +238,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
)
}

// STEP 3: Send the transaction
// Make sure that the chain is still correct
const updatedClient = await this.checkClient(step, process)
if (!updatedClient) {
Expand Down Expand Up @@ -335,7 +327,6 @@ export class EVMStepExecutor extends BaseStepExecutor {
} as SendTransactionParameters)
}

// STEP 4: Wait for the transaction
if (isMultisigClient) {
process = this.statusManager.updateProcess(
step,
Expand Down Expand Up @@ -423,8 +414,7 @@ export class EVMStepExecutor extends BaseStepExecutor {
}
}

// STEP 5: Wait for the receiving chain
const processTxHash = process.txHash
// Wait for the transaction status on the destination chain
if (isBridgeExecution) {
process = this.statusManager.findOrCreateProcess({
step,
Expand All @@ -433,63 +423,13 @@ export class EVMStepExecutor extends BaseStepExecutor {
chainId: toChain.id,
})
}
let statusResponse: FullStatusData

try {
if (!processTxHash) {
throw new Error('Transaction hash is undefined.')
}
statusResponse = (await waitForReceivingTransaction(
processTxHash,
this.statusManager,
process.type,
step
)) as FullStatusData

const statusReceiving =
statusResponse.receiving as ExtendedTransactionInfo

process = this.statusManager.updateProcess(step, process.type, 'DONE', {
substatus: statusResponse.substatus,
substatusMessage:
statusResponse.substatusMessage ||
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
txHash: statusReceiving?.txHash,
txLink: `${toChain.metamask.blockExplorerUrls[0]}tx/${statusReceiving?.txHash}`,
})

this.statusManager.updateExecution(step, 'DONE', {
fromAmount: statusResponse.sending.amount,
toAmount: statusReceiving?.amount,
toToken: statusReceiving?.token,
gasCosts: [
{
amount: statusResponse.sending.gasAmount,
amountUSD: statusResponse.sending.gasAmountUSD,
token: statusResponse.sending.gasToken,
estimate: statusResponse.sending.gasUsed,
limit: statusResponse.sending.gasUsed,
price: statusResponse.sending.gasPrice,
type: 'SEND',
},
],
})
} catch (e: unknown) {
const htmlMessage = await getTransactionFailedMessage(
step,
process.txLink
)

process = this.statusManager.updateProcess(step, process.type, 'FAILED', {
error: {
code: LiFiErrorCode.TransactionFailed,
message: 'Failed while waiting for receiving chain.',
htmlMessage,
},
})
this.statusManager.updateExecution(step, 'FAILED')
throw await parseEVMErrors(e as Error, step, process)
}
await waitForDestinationChainTransaction(
step,
process,
this.statusManager,
toChain
)

// DONE
return step
Expand Down
Loading
Loading