Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance transaction notification #695

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/ui/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const createContainer = () => {

const createItem = (config: Config, status: Status, onClose: () => void) => {
const domNode = document.createElement("div");
domNode.className = `rounded-medium border-component border bg-inner p-medium lg:p-5 flex items-center gap-medium mb-medium animate-notification-enter relative w-[82vw] lg:w-96 ${config.className}`;
domNode.className = `rounded-xl shadow-lg border-white/5 border lg:border-none bg-secondary p-medium lg:p-5 flex items-center gap-medium mb-medium animate-notification-enter relative w-[82vw] lg:w-96 ${config.className}`;

const root = createRoot(domNode);
root.render(
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 });
}
Loading