Skip to content

Commit

Permalink
Transaction fee
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Dec 18, 2023
1 parent 017ee93 commit 54b4165
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
106 changes: 66 additions & 40 deletions src/bridges/xtoken-v3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BridgeConstructorArgs, TransferOptions } from "@/types";
import { BridgeConstructorArgs, GetFeeArgs, Token, TransferOptions } from "@/types";
import { BaseBridge } from ".";
import { Address, TransactionReceipt } from "viem";
import { Address, Hex, TransactionReceipt } from "viem";

export class XTokenV3Bridge extends BaseBridge {
constructor(args: BridgeConstructorArgs) {
Expand All @@ -27,19 +27,66 @@ export class XTokenV3Bridge extends BaseBridge {
amount: bigint,
options?: TransferOptions & { askEstimateGas?: boolean },
): Promise<bigint | TransactionReceipt | undefined> {
const sourceMessager = this.sourceChain?.messager?.msgline;
const targetMessager = this.targetChain?.messager?.msgline;
const feeAndParams = await this._getMsgportFeeAndParams(sender, recipient, amount);
const account = await this.getSigner();

if (
account &&
feeAndParams &&
this.contract &&
this.targetChain &&
this.sourceToken &&
this.sourcePublicClient &&
this.walletClient
) {
const askEstimateGas = options?.askEstimateGas ?? false;

if (this.crossInfo?.action === "issue") {
const defaultParams = {
address: this.contract.sourceAddress,
abi: (await import("@/abi/xtoken-backing")).default,
functionName: "lockAndRemoteIssuing",
args: [BigInt(this.targetChain.id), this.sourceToken.address, recipient, amount, feeAndParams.extParams],
account,
} as const;

if (askEstimateGas) {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
}
} else if (this.crossInfo?.action === "redeem") {
const defaultParams = {
address: this.contract.sourceAddress,
abi: (await import("@/abi/xtoken-issuing")).default,
functionName: "burnAndRemoteUnlock",
args: [this.sourceToken.address, recipient, amount, feeAndParams.extParams],
account,
} as const;

if (askEstimateGas) {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
}
}
}
return;
}

private async _getMsgportFeeAndParams(sender: Address, recipient: Address, amount: bigint) {
const sourceMessager = this.sourceChain?.messager?.msgline;
const targetMessager = this.targetChain?.messager?.msgline;

if (
sourceMessager &&
targetMessager &&
this.contract &&
this.sourceToken &&
this.targetToken &&
this.sourcePublicClient &&
this.walletClient
this.sourcePublicClient
) {
const message = await (this.crossInfo?.action === "issue"
? this.sourcePublicClient.readContract({
Expand Down Expand Up @@ -67,42 +114,21 @@ export class XTokenV3Bridge extends BaseBridge {
);
const feeJson = await feeData.json();
if (feeData.ok && feeJson.code === 0) {
const askEstimateGas = options?.askEstimateGas ?? false;
const extParams = feeJson.data.params;

if (this.crossInfo?.action === "issue") {
const defaultParams = {
address: this.contract.sourceAddress,
abi: (await import("@/abi/xtoken-backing")).default,
functionName: "lockAndRemoteIssuing",
args: [BigInt(this.targetChain.id), this.sourceToken.address, recipient, amount, extParams],
account,
} as const;

if (askEstimateGas) {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
}
} else if (this.crossInfo?.action === "redeem") {
const defaultParams = {
address: this.contract.sourceAddress,
abi: (await import("@/abi/xtoken-issuing")).default,
functionName: "burnAndRemoteUnlock",
args: [this.sourceToken.address, recipient, amount, extParams],
account,
} as const;
const fee = BigInt(feeJson.data.fee);
const extParams = feeJson.data.params as Hex;
return { fee, extParams };
}
}
}

if (askEstimateGas) {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
}
}
async getFee(args?: GetFeeArgs | undefined): Promise<{ value: bigint; token: Token } | undefined> {
if (this.sourceToken) {
const sender = args?.sender ?? "0x0000000000000000000000000000000000000000";
const recipient = args?.recipient ?? "0x0000000000000000000000000000000000000000";
const feeAndParams = await this._getMsgportFeeAndParams(sender, recipient, args?.transferAmount ?? 0n);
if (feeAndParams) {
return { value: feeAndParams.fee, token: this.sourceToken };
}
}
return;
}
}
4 changes: 3 additions & 1 deletion src/components/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export default function Transfer() {
setIsLoadingFee(true);
sub$$ = from(
bridgeInstance.getFee({
sender: address,
recipient,
baseFee: BigInt(relayer?.baseFee || 0),
protocolFee: BigInt(relayer?.protocolFee || 0),
liquidityFeeRate: BigInt(relayer?.liquidityFeeRate || 0),
Expand All @@ -144,7 +146,7 @@ export default function Transfer() {
}

return () => sub$$?.unsubscribe();
}, [bridgeInstance, relayersData, deferredTransferAmount, setBridgeFee]);
}, [address, recipient, bridgeInstance, relayersData, deferredTransferAmount, setBridgeFee]);

useEffect(() => {
let sub$$: Subscription | undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface GetFeeArgs {
protocolFee?: bigint;
liquidityFeeRate?: bigint;
transferAmount?: bigint;
sender?: Address;
recipient?: Address;
}

export interface TransferOptions {
Expand Down

0 comments on commit 54b4165

Please sign in to comment.