From b64952ccaa1fd8474eb663e5ccf5cc80de621123 Mon Sep 17 00:00:00 2001 From: JayJay1024 Date: Wed, 27 Mar 2024 16:55:35 +0800 Subject: [PATCH] refactor: optimize code --- src/bridges/xtoken-next.ts | 112 ++++++++++--------------------------- 1 file changed, 29 insertions(+), 83 deletions(-) diff --git a/src/bridges/xtoken-next.ts b/src/bridges/xtoken-next.ts index 9d0586f3..cd2fc6df 100644 --- a/src/bridges/xtoken-next.ts +++ b/src/bridges/xtoken-next.ts @@ -51,22 +51,11 @@ export class XTokenNextBridge extends BaseBridge { this.publicClient ) { const value = this.sourceToken.type === "native" ? amount + feeAndParams.fee : feeAndParams.fee; - const guard = await this._getTargetGuard(); const gas = this.getTxGasLimit(); if (this.crossInfo?.action === "issue") { if (this.sourceToken?.type === "native") { - if (guard && this.sourceChain?.convertor) { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/wtoken-convertor")).default, - functionName: "lockAndXIssue", - args: [BigInt(this.targetChain.id), pRecipient, sender, amount, nonce, extData, feeAndParams.extParams], - address: this.sourceChain.convertor, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } else if (this.sourceChain?.convertor) { + if (this.sourceChain?.convertor) { const hash = await this.walletClient.writeContract({ abi: (await import("@/abi/wtoken-convertor")).default, functionName: "lockAndXIssue", @@ -78,59 +67,28 @@ export class XTokenNextBridge extends BaseBridge { return this.publicClient.waitForTransactionReceipt({ hash }); } } else { - if (guard) { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/xtoken-backing-next")).default, - functionName: "lockAndXIssue", - args: [ - BigInt(this.targetChain.id), - this.sourceToken.address, - pRecipient, - sender, - amount, - nonce, - extData, - feeAndParams.extParams, - ], - address: this.contract.sourceAddress, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } else { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/xtoken-backing-next")).default, - functionName: "lockAndXIssue", - args: [ - BigInt(this.targetChain.id), - this.sourceToken.address, - pRecipient, - sender, - amount, - nonce, - extData, - feeAndParams.extParams, - ], - address: this.contract.sourceAddress, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } + const hash = await this.walletClient.writeContract({ + abi: (await import("@/abi/xtoken-backing-next")).default, + functionName: "lockAndXIssue", + args: [ + BigInt(this.targetChain.id), + this.sourceToken.address, + pRecipient, + sender, + amount, + nonce, + extData, + feeAndParams.extParams, + ], + address: this.contract.sourceAddress, + value, + gas, + }); + return this.publicClient.waitForTransactionReceipt({ hash }); } } else if (this.crossInfo?.action === "redeem") { if (this.targetToken?.type === "native") { - if (guard && this.sourceChain?.convertor) { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/xtoken-convertor")).default, - functionName: "burnAndXUnlock", - args: [pRecipient, sender, amount, nonce, extData, feeAndParams.extParams], - address: this.sourceChain.convertor, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } else if (this.sourceChain?.convertor) { + if (this.sourceChain?.convertor) { const hash = await this.walletClient.writeContract({ abi: (await import("@/abi/xtoken-convertor")).default, functionName: "burnAndXUnlock", @@ -142,27 +100,15 @@ export class XTokenNextBridge extends BaseBridge { return this.publicClient.waitForTransactionReceipt({ hash }); } } else { - if (guard) { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/xtoken-issuing-next")).default, - functionName: "burnAndXUnlock", - args: [this.sourceToken.inner, pRecipient, sender, amount, nonce, extData, feeAndParams.extParams], - address: this.contract.sourceAddress, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } else { - const hash = await this.walletClient.writeContract({ - abi: (await import("@/abi/xtoken-issuing-next")).default, - functionName: "burnAndXUnlock", - args: [this.sourceToken.inner, pRecipient, sender, amount, nonce, extData, feeAndParams.extParams], - address: this.contract.sourceAddress, - value, - gas, - }); - return this.publicClient.waitForTransactionReceipt({ hash }); - } + const hash = await this.walletClient.writeContract({ + abi: (await import("@/abi/xtoken-issuing-next")).default, + functionName: "burnAndXUnlock", + args: [this.sourceToken.inner, pRecipient, sender, amount, nonce, extData, feeAndParams.extParams], + address: this.contract.sourceAddress, + value, + gas, + }); + return this.publicClient.waitForTransactionReceipt({ hash }); } } }