Skip to content

Commit

Permalink
Update TX Confirmations (#62)
Browse files Browse the repository at this point in the history
* Simulate tht claim of xtoken-next

* Update tx confirmation blocks to waiting
  • Loading branch information
JayJay1024 authored Dec 2, 2024
1 parent ad4de69 commit 9e1da30
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/xtoken-ui/src/bridges/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { Address, PublicClient as ViemPublicClient, TransactionReceipt, createPublicClient, http } from "viem";
import { PublicClient as WagmiPublicClient, WalletClient } from "wagmi";
import erc20Abi from "../abi/erc20";
import { TX_CONFIRMATIONS } from "../config";

export abstract class BaseBridge {
protected logo: BridgeLogo = { symbol: "", horizontal: "" };
Expand Down Expand Up @@ -254,7 +255,7 @@ export abstract class BaseBridge {
account: owner,
});
const hash = await this.walletClient.writeContract(request);
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/xtoken-ui/src/bridges/helix-lpbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BridgeConstructorArgs, GetFeeArgs, TransferOptions } from "../types/bri
import { BaseBridge } from "./base";
import { Address, TransactionReceipt } from "viem";
import { HistoryRecord } from "../types/graphql";
import { TX_CONFIRMATIONS } from "../config";

export class HelixLpBridge extends BaseBridge {
private readonly prefix = BigInt("0x6878000000000000");
Expand Down Expand Up @@ -77,7 +78,7 @@ export class HelixLpBridge extends BaseBridge {
const hash = await (this.sourceToken?.type === "native"
? this.walletClient.writeContract(nativeParams)
: this.walletClient.writeContract(defaultParams));
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand All @@ -104,7 +105,7 @@ export class HelixLpBridge extends BaseBridge {
value: await this.getBridgeFee(),
gas: this.getTxGasLimit(),
});
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down Expand Up @@ -136,7 +137,7 @@ export class HelixLpBridge extends BaseBridge {
value: fee,
})
: this.walletClient.writeContract({ address, abi, gas, functionName: "increaseFee", args: [transferId, fee] }));
return await this.publicClient.waitForTransactionReceipt({ hash });
return await this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/xtoken-ui/src/bridges/helixbridge-dvmdvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BridgeConstructorArgs, TransferOptions } from "../types/bridge";
import { BaseBridge } from "./base";
import { Address, TransactionReceipt } from "viem";
import { HistoryRecord } from "../types/graphql";
import { TX_CONFIRMATIONS } from "../config";

/**
* DVM <=> DVM
Expand Down Expand Up @@ -95,7 +96,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
const hash = await (this.sourceToken.type === "native"
? this.walletClient.writeContract(nativeParams)
: this.walletClient.writeContract(defaultParams));
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -141,7 +142,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
const hash = await (this.targetToken?.type === "native"
? this.walletClient.writeContract(nativeParams)
: this.walletClient.writeContract(defaultParams));
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -207,7 +208,7 @@ export class HelixBridgeDVMDVM extends BaseBridge {
args: [this.specVersion.source, this.gasLimit, messageNonce, sendTokenAddress, record.sender, sendAmount],
});
}
return hash && this.publicClient.waitForTransactionReceipt({ hash });
return hash && this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down
9 changes: 5 additions & 4 deletions packages/xtoken-ui/src/bridges/helixbridge-dvmevm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BridgeConstructorArgs, TransferOptions } from "../types/bridge";
import { BaseBridge } from "./base";
import { Address, TransactionReceipt } from "viem";
import { HistoryRecord } from "../types/graphql";
import { TX_CONFIRMATIONS } from "../config";

/**
* DVM <=> EVM
Expand Down Expand Up @@ -78,7 +79,7 @@ export class HelixBridgeDVMEVM extends BaseBridge {
const hash = await (this.sourceToken.type === "native"
? this.walletClient.writeContract(nativeParams)
: this.walletClient.writeContract(defaultParams));
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -124,7 +125,7 @@ export class HelixBridgeDVMEVM extends BaseBridge {
const hash = await (this.targetToken.type === "native"
? this.walletClient.writeContract(nativeParams)
: this.walletClient.writeContract(defaultParams));
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -160,7 +161,7 @@ export class HelixBridgeDVMEVM extends BaseBridge {
],
gas: this.getTxGasLimit(),
});
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down Expand Up @@ -207,7 +208,7 @@ export class HelixBridgeDVMEVM extends BaseBridge {
args: [transferId, sendTokenAddress, record.sender, sendAmount],
});
}
return hash && this.publicClient.waitForTransactionReceipt({ hash });
return hash && this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/xtoken-ui/src/bridges/l2bridge.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TX_CONFIRMATIONS } from "../config";
import { BridgeConstructorArgs, TransferOptions } from "../types/bridge";
import { BaseBridge } from "./base";
import { Address, TransactionReceipt, encodeAbiParameters } from "viem";
Expand Down Expand Up @@ -73,7 +74,7 @@ export class L2ArbitrumBridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions packages/xtoken-ui/src/bridges/xtoken-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isTronChain,
waitForTronTransactionReceipt,
} from "../utils";
import { TX_CONFIRMATIONS } from "../config";

export class XTokenNextBridge extends BaseBridge {
constructor(args: BridgeConstructorArgs) {
Expand Down Expand Up @@ -261,7 +262,7 @@ export class XTokenNextBridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
} else {
const defaultParams = {
Expand All @@ -287,7 +288,7 @@ export class XTokenNextBridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
} else if (this.crossInfo?.action === "redeem" && this.sourcePublicClient && account) {
Expand All @@ -306,7 +307,7 @@ export class XTokenNextBridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
} else {
const defaultParams = {
Expand All @@ -323,7 +324,7 @@ export class XTokenNextBridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -473,7 +474,7 @@ export class XTokenNextBridge extends BaseBridge {
const guard = await this._getTargetGuard();

if (record.recvTokenAddress && guard && this.contract && this.walletClient && this.publicClient) {
const hash = await this.walletClient.writeContract({
const { request } = await this.publicClient.simulateContract({
abi: (await import("../abi/guard-next")).default,
functionName: "claim",
args: [
Expand All @@ -488,7 +489,8 @@ export class XTokenNextBridge extends BaseBridge {
address: guard,
gas: this.getTxGasLimit(),
});
return this.publicClient.waitForTransactionReceipt({ hash });
const hash = await this.walletClient.writeContract(request);
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}

Expand Down Expand Up @@ -577,7 +579,7 @@ export class XTokenNextBridge extends BaseBridge {
gas: this.getTxGasLimit(),
value: feeAndParams.fee,
});
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
} else if (this.crossInfo?.action === "redeem") {
Expand Down Expand Up @@ -649,7 +651,7 @@ export class XTokenNextBridge extends BaseBridge {
gas: this.getTxGasLimit(),
value: feeAndParams.fee,
});
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/xtoken-ui/src/bridges/xtoken-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BridgeConstructorArgs, GetFeeArgs, HistoryRecord, Token, TransferOption
import { BaseBridge } from ".";
import { Address, Hex, TransactionReceipt, encodeFunctionData, isAddressEqual } from "viem";
import { fetchMsglineFeeAndParams, getMessagerAddress } from "../utils";
import { TX_CONFIRMATIONS } from "../config";

export class XTokenV3Bridge extends BaseBridge {
constructor(args: BridgeConstructorArgs) {
Expand Down Expand Up @@ -72,7 +73,7 @@ export class XTokenV3Bridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
} else if (this.crossInfo?.action === "redeem") {
const defaultParams = {
Expand All @@ -89,7 +90,7 @@ export class XTokenV3Bridge extends BaseBridge {
return this.sourcePublicClient.estimateContractGas(defaultParams);
} else if (this.walletClient) {
const hash = await this.walletClient.writeContract(defaultParams);
return this.sourcePublicClient.waitForTransactionReceipt({ hash });
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ export class XTokenV3Bridge extends BaseBridge {
address: guardContract,
gas: this.getTxGasLimit(),
});
return this.publicClient.waitForTransactionReceipt({ hash });
return this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
Expand Down Expand Up @@ -299,7 +300,7 @@ export class XTokenV3Bridge extends BaseBridge {
}
}

return hash && this.publicClient.waitForTransactionReceipt({ hash });
return hash && this.publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
}
}
}
3 changes: 2 additions & 1 deletion packages/xtoken-ui/src/components/faucet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import abi from "../abi/faucet";
import Tooltip from "../ui/tooltip";
import CountLoading from "../ui/count-loading";
import Modal from "../ui/modal";
import { TX_CONFIRMATIONS } from "../config";

interface Props {
sourceChain: ChainConfig;
Expand Down Expand Up @@ -40,7 +41,7 @@ export default function Faucet({ sourceChain, sourceToken, onSuccess = () => und
functionName: "faucet",
args: [1n <= allow ? allow - 1n : allow],
});
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const receipt = await publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
notifyTransaction(receipt, sourceChain);
setBusy(false);

Expand Down
3 changes: 2 additions & 1 deletion packages/xtoken-ui/src/components/wrap-unwrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ethereumChain } from "../config/chains";
import Button from "../ui/button";
import { useConnectModal } from "@rainbow-me/rainbowkit";
import { isTxSuccess, notifyError, notifyTransaction } from "../utils";
import { TX_CONFIRMATIONS } from "../config";

interface Amount {
input: string;
Expand Down Expand Up @@ -142,7 +143,7 @@ export default function WrapUnwrap() {
functionName: actionText === "Unwrap" ? "withdraw" : "deposit",
args: [amountRef.current.value],
});
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const receipt = await publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
setBusy(false);
notifyTransaction(receipt, ethereumChain, actionText === "Unwrap" ? "Withdraw" : "Deposit");
if (receipt.status === "success") {
Expand Down
1 change: 1 addition & 0 deletions packages/xtoken-ui/src/config/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const TX_CONFIRMATIONS = 2;
1 change: 1 addition & 0 deletions packages/xtoken-ui/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./gql";
export * from "./constant";
3 changes: 2 additions & 1 deletion packages/xtoken-ui/src/hooks/use-allowance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
notifyError,
waitForTronTransactionReceipt,
} from "../utils";
import { TX_CONFIRMATIONS } from "../config";

function getAllowanceEVM(chain: ChainConfig, token: Token, owner: Address, spender: Address) {
const publicClient = createPublicClient({ chain, transport: http() });
Expand Down Expand Up @@ -103,7 +104,7 @@ export function useAllowance(
functionName: "approve",
args: [spender, amount],
});
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const receipt = await publicClient.waitForTransactionReceipt({ hash, confirmations: TX_CONFIRMATIONS });
if (receipt.status === "success") {
setAllowance(await getAllowanceEVM(chain, token, owner, spender));
}
Expand Down

0 comments on commit 9e1da30

Please sign in to comment.