Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
refactor: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Mar 27, 2024
1 parent d1ba325 commit b64952c
Showing 1 changed file with 29 additions and 83 deletions.
112 changes: 29 additions & 83 deletions src/bridges/xtoken-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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 });
}
}
}
Expand Down

0 comments on commit b64952c

Please sign in to comment.