From 2687aec9fc8c5e5761556596d136ea393a0013d9 Mon Sep 17 00:00:00 2001 From: Mohammad Reza Lotfi Date: Wed, 9 Oct 2024 13:55:48 +0330 Subject: [PATCH] Check if already unlocked for posted order hashes (#16) Co-authored-by: mrlotfi --- src/driver/state-closer.ts | 4 +-- src/driver/unlocker.ts | 54 +++++++++++++++++++++++++++----------- src/utils/solana-trx.ts | 3 ++- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/driver/state-closer.ts b/src/driver/state-closer.ts index fe3a270..e830e28 100644 --- a/src/driver/state-closer.ts +++ b/src/driver/state-closer.ts @@ -41,7 +41,7 @@ export class StateCloser { const serializedTrx = trx.serialize(); logger.info(`Closing ${destSolanaStates.length} dest swap states`); - await this.solanaTxSender.sendAndConfirmTransaction(serializedTrx, 10); + await this.solanaTxSender.sendAndConfirmTransaction(serializedTrx, 10, 'confirmed', 60, 100, false); logger.info(`Closed ${destSolanaStates.length} dest swap states`); } @@ -73,7 +73,7 @@ export class StateCloser { const serializedTrx = trx.serialize(); logger.info(`Closing ${auctionStates.length} auction states`); - await this.solanaTxSender.sendAndConfirmTransaction(serializedTrx, 10); + await this.solanaTxSender.sendAndConfirmTransaction(serializedTrx, 10, 'confirmed', 30, 100, false); logger.info(`Closed ${auctionStates.length} auction states`); } diff --git a/src/driver/unlocker.ts b/src/driver/unlocker.ts index 7359ffd..b538b13 100644 --- a/src/driver/unlocker.ts +++ b/src/driver/unlocker.ts @@ -90,22 +90,6 @@ export class Unlocker { this.driverAddresses.push(this.walletConfig.evm.address); } - private async getOwnedUnlockableSwaps(driverAddresses: string[]): Promise<{ - singleData: UnlockableSwapSingleItem[]; - batchData: UnlockableSwapBatchItem[]; - postedBatchData: UnlockableSwapBatchItem[]; - }> { - const rawData = await axios.get(this.endpoints.explorerApiUrl + '/v3/unlockable-swaps', { - params: { - batchUnlockThreshold: this.gConf.batchUnlockThreshold, - singleBatchChainIds: this.gConf.singleBatchChainIds.join(','), - driverAddresses: driverAddresses.join(','), - }, - }); - - return rawData.data; - } - scheduleUnlockJobs() { if (this.gConf.disableUnlocker) { logger.info(`Unlocker is disabled and ignored.`); @@ -164,6 +148,21 @@ export class Unlocker { } for (let postedUnlockData of freshExplorerData.postedBatchData) { + const orderHashs = postedUnlockData.orders.map((order) => order.orderHash); + let alreadyUnlocked: Set; + if (+postedUnlockData.fromChain === CHAIN_ID_SOLANA) { + alreadyUnlocked = await this.getAlreadyUnlockedOrPendingOrderSolana(orderHashs); + } else { + alreadyUnlocked = await this.getAlreadyUnlockedOrPendingOrderEvm( + postedUnlockData.fromChain, + orderHashs, + ); + } + + postedUnlockData.orders = postedUnlockData.orders.filter( + (order) => !alreadyUnlocked.has(order.orderHash), + ); + this.scheduleAlreadyPostedUnlocks(postedUnlockData); } @@ -366,6 +365,8 @@ export class Unlocker { this.rpcConfig.solana.sendCount, 'confirmed', 30, + 100, + false, ); return txHash; @@ -433,6 +434,9 @@ export class Unlocker { serializedTrx, this.rpcConfig.solana.sendCount, 'confirmed', + 60, + 100, + false, ); return txHash; @@ -601,6 +605,8 @@ export class Unlocker { this.rpcConfig.solana.sendCount, 'confirmed', 30, + 100, + false, ); logger.verbose(`Batch posted Successfully Solana, getting batch sequence`); @@ -746,6 +752,22 @@ export class Unlocker { return result; } + private async getOwnedUnlockableSwaps(driverAddresses: string[]): Promise<{ + singleData: UnlockableSwapSingleItem[]; + batchData: UnlockableSwapBatchItem[]; + postedBatchData: UnlockableSwapBatchItem[]; + }> { + const rawData = await axios.get(this.endpoints.explorerApiUrl + '/v3/unlockable-swaps', { + params: { + batchUnlockThreshold: this.gConf.batchUnlockThreshold, + singleBatchChainIds: this.gConf.singleBatchChainIds.join(','), + driverAddresses: driverAddresses.join(','), + }, + }); + + return rawData.data; + } + private async getSignedVaa(sequence: string, destChainId: number, deadlineSeconds?: number): Promise { const contractAddress = this.contracts.contracts[destChainId]; let mayanBridgeEmitterAddress; diff --git a/src/utils/solana-trx.ts b/src/utils/solana-trx.ts index 536dc79..6603325 100644 --- a/src/utils/solana-trx.ts +++ b/src/utils/solana-trx.ts @@ -260,13 +260,14 @@ export class SolanaMultiTxSender { confirmationLevel: 'confirmed' | 'finalized' = 'confirmed', timeoutSeconds: number = 59, maxTotalSendCount: number = 150, + skipPreflightFirst: boolean = true, ): Promise { const sendInterval = this.rpcConfig.solana.sendInterval; const otherSendInterval = this.rpcConfig.solana.otherSendInterval; let ongoingSends: any[] = []; let done = false; - const trxHash = await this.connection.sendRawTransaction(rawTrx, { skipPreflight: true }); + const trxHash = await this.connection.sendRawTransaction(rawTrx, { skipPreflight: skipPreflightFirst }); let startTime = new Date().getTime(); const backgroundSend = async () => {