Skip to content

Commit

Permalink
Enhance transaction notification
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Apr 16, 2024
1 parent 240d473 commit 03bfc2a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/components/transfer-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function Component() {
const receipt = await approve(
fee?.token.type === "native" ? deferredAmount.value : deferredAmount.value + (fee?.value ?? 0n),
);
notifyTransaction(receipt, sourceChain);
notifyTransaction(receipt, sourceChain, "Approval");
} else if (actionText === "Transfer") {
setIsOpen(true);
}
Expand All @@ -203,7 +203,7 @@ function Component() {
withdrawNonce: BigInt(relayInfo?.withdrawNonce ?? 0),
depositedMargin: BigInt(relayInfo?.margin ?? 0),
});
notifyTransaction(receipt, sourceChain);
notifyTransaction(receipt, sourceChain, "Transfer");
setTxHash(receipt?.transactionHash);
if (receipt?.status === "success") {
setIsTransfering(false);
Expand Down
12 changes: 6 additions & 6 deletions src/providers/relayer-provider-v3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
if (address) {
try {
const receipt = await bridgeInstance.sourceApprove(amount, address);
notifyTransaction(receipt, bridgeInstance.getSourceChain());
notifyTransaction(receipt, bridgeInstance.getSourceChain(), "Approval");
setSourceAllowance(await bridgeInstance.getSourceAllowance(address));
return receipt;
} catch (err) {
Expand All @@ -141,7 +141,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
if (address) {
try {
const receipt = await bridgeInstance.targetApprove(amount, address);
notifyTransaction(receipt, bridgeInstance.getTargetChain());
notifyTransaction(receipt, bridgeInstance.getTargetChain(), "Approval");
setTargetAllowance(await bridgeInstance.getTargetAllowance(address));
return receipt;
} catch (err) {
Expand Down Expand Up @@ -169,7 +169,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
async (amount: bigint) => {
try {
const receipt = await bridgeInstance.depositPenaltyReserve(amount);
notifyTransaction(receipt, bridgeInstance.getSourceChain());
notifyTransaction(receipt, bridgeInstance.getSourceChain(), "Deposite");
if (receipt?.status === "success") {
await _updatePenaltyReserves();
if (address) {
Expand All @@ -190,7 +190,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
async (baseFee: bigint, feeRate: number, transferLimit: bigint) => {
try {
const receipt = await bridgeInstance.registerLnProvider(baseFee, feeRate, transferLimit);
notifyTransaction(receipt, bridgeInstance.getSourceChain());
notifyTransaction(receipt, bridgeInstance.getSourceChain(), "Register");
return receipt;
} catch (err) {
console.error(err);
Expand All @@ -204,7 +204,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
async (amount: bigint) => {
try {
const receipt = await bridgeInstance.withdrawPenaltyReserve(amount);
notifyTransaction(receipt, bridgeInstance.getSourceChain());
notifyTransaction(receipt, bridgeInstance.getSourceChain(), "Withdraw");

if (receipt?.status === "success") {
await _updatePenaltyReserves();
Expand All @@ -231,7 +231,7 @@ export default function RelayerProviderV3({ children }: PropsWithChildren<unknow
messageFee,
params ?? address,
);
notifyTransaction(receipt, bridgeInstance.getTargetChain());
notifyTransaction(receipt, bridgeInstance.getTargetChain(), "Withdraw");
return receipt;
} catch (err) {
console.error(err);
Expand Down
8 changes: 4 additions & 4 deletions src/providers/relayer-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export default function RelayerProvider({ children }: PropsWithChildren<unknown>
const sourceApprove = useCallback(async (owner: Address, amount: bigint, bridge: BaseBridge, chain: ChainConfig) => {
try {
const receipt = await bridge.sourceApprove(amount, owner);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Approval");
setSourceAllowance(await bridge.getSourceAllowance(owner));
return receipt;
} catch (err) {
Expand All @@ -238,7 +238,7 @@ export default function RelayerProvider({ children }: PropsWithChildren<unknown>
const targetApprove = useCallback(async (owner: Address, amount: bigint, bridge: BaseBridge, chain: ChainConfig) => {
try {
const receipt = await bridge.targetApprove(amount, owner);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Approval");
setTargetAllowance(await bridge.getTargetAllowance(owner));
return receipt;
} catch (err) {
Expand All @@ -254,7 +254,7 @@ export default function RelayerProvider({ children }: PropsWithChildren<unknown>
async (relayer: Address, margin: bigint, bridge: LnBridgeV2Default, chain: ChainConfig) => {
try {
const receipt = await bridge.depositMargin(margin);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Deposite");

const a = await bridge.getTargetAllowance(relayer);
const b = await bridge.getTargetBalance(relayer);
Expand Down Expand Up @@ -327,7 +327,7 @@ export default function RelayerProvider({ children }: PropsWithChildren<unknown>
) => {
try {
const receipt = await bridge.withdrawMargin(recipientOrParams, amount, fee);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Withdraw");
return receipt;
} catch (err) {
console.error(err);
Expand Down
4 changes: 2 additions & 2 deletions src/providers/transfer-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default function TransferProvider({ children }: PropsWithChildren<unknown
) => {
try {
const receipt = await bridge.transfer(sender, recipient, amount, options);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Transfer");

const a = await bridge.getSourceAllowance(sender);
const b = await bridge.getSourceBalance(sender);
Expand All @@ -155,7 +155,7 @@ export default function TransferProvider({ children }: PropsWithChildren<unknown
const sourceApprove = useCallback(async (owner: Address, amount: bigint, bridge: BaseBridge, chain: ChainConfig) => {
try {
const receipt = await bridge.sourceApprove(amount, owner);
notifyTransaction(receipt, chain);
notifyTransaction(receipt, chain, "Approval");
setSourceAllowance(await bridge.getSourceAllowance(owner));
return receipt;
} catch (err) {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ function Link({ href, children }: PropsWithChildren<{ href: string }>) {
);
}

export function notifyTransaction(receipt?: TransactionReceipt, chain?: ChainConfig) {
export function notifyTransaction(receipt?: TransactionReceipt, chain?: ChainConfig, title?: string) {
const explorer = chain?.blockExplorers?.default.url;
const txHash = receipt?.transactionHash;
const href = new URL(`tx/${txHash}`, explorer).href;

if (receipt?.status === "success" && txHash) {
notification.success({
title: "Transaction successful",
title: `${title ?? "Transaction"} successful`,
description: <Link href={href}>{txHash}</Link>,
});
} else if (receipt?.status === "reverted" && explorer) {
notification.error({
title: "Transaction failed",
title: `${title ?? "Transaction"} failed`,
description: <Link href={href}>{txHash}</Link>,
});
}
}

export function notifyError(err: unknown) {
return notification.error({ title: "An error occurred", description: (err as Error).message });
return notification.error({ title: "Oops an error occurred", description: (err as Error).message });
}

0 comments on commit 03bfc2a

Please sign in to comment.