Skip to content

Commit

Permalink
fix(web): deposit payment button
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamParkhi committed Sep 27, 2024
1 parent d94be62 commit 84605c6
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { usePublicClient } from "wagmi";
import { isUndefined } from "utils/index";
import { wrapWithToast } from "utils/wrapWithToast";
import { useTransactionDetailsContext } from "context/TransactionDetailsContext";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useSimulateEscrowUniversalAcceptSettlement,
useWriteEscrowUniversalAcceptSettlement,
Expand All @@ -16,7 +15,6 @@ const AcceptButton: React.FC = () => {
const publicClient = usePublicClient();
const { id } = useTransactionDetailsContext();
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: acceptSettlementConfig } = useSimulateEscrowUniversalAcceptSettlement({
args: [BigInt(id)],
Expand Down Expand Up @@ -44,7 +42,7 @@ const AcceptButton: React.FC = () => {
return (
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
disabled={isSending}
text="Accept"
onClick={handleAcceptSettlement}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { usePublicClient } from "wagmi";
import { useTransactionDetailsContext } from "context/TransactionDetailsContext";
import { isUndefined } from "utils/index";
import { wrapWithToast } from "utils/wrapWithToast";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useSimulateEscrowUniversalExecuteTransaction,
useWriteEscrowUniversalExecuteTransaction,
Expand All @@ -16,7 +15,6 @@ const ExecuteTransactionButton: React.FC = () => {
const publicClient = usePublicClient();
const { id } = useTransactionDetailsContext();
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: executeTransactionConfig } = useSimulateEscrowUniversalExecuteTransaction({
args: [BigInt(id)],
Expand Down Expand Up @@ -45,7 +43,7 @@ const ExecuteTransactionButton: React.FC = () => {
return (
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
disabled={isSending}
text="Execute Transaction"
onClick={handleExecuteTransaction}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { parseEther } from "viem";
import { isUndefined } from "utils/index";
import { wrapWithToast } from "utils/wrapWithToast";
import { useTransactionDetailsContext } from "context/TransactionDetailsContext";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useSimulateEscrowUniversalProposeSettlement,
useWriteEscrowUniversalProposeSettlement,
Expand All @@ -29,7 +28,6 @@ const ProposeSettlementButton: React.FC<IProposeSettlementButton> = ({
const publicClient = usePublicClient();
const { id } = useTransactionDetailsContext();
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: proposeSettlementConfig } = useSimulateEscrowUniversalProposeSettlement({
args: [BigInt(id), parseEther(amountProposed)],
Expand Down Expand Up @@ -59,7 +57,7 @@ const ProposeSettlementButton: React.FC<IProposeSettlementButton> = ({
return (
<Button
isLoading={isSending}
disabled={isSending || !isAmountValid || !hasSufficientNativeBalance}
disabled={isSending || !isAmountValid}
text={buttonText}
onClick={handleProposeSettlement}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useMemo, useState } from "react";
import { Button } from "@kleros/ui-components-library";
import { useAccount, usePublicClient } from "wagmi";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useWriteEscrowUniversalPayArbitrationFeeByBuyer,
useWriteEscrowUniversalPayArbitrationFeeBySeller,
Expand All @@ -26,23 +25,29 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button
const { buyer, id } = useTransactionDetailsContext();
const isBuyer = useMemo(() => address?.toLowerCase() === buyer?.toLowerCase(), [address, buyer]);
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: payArbitrationFeeByBuyerConfig } = useSimulateEscrowUniversalPayArbitrationFeeByBuyer({
args: [BigInt(id)],
value: arbitrationCost,
});
const { data: payArbitrationFeeByBuyerConfig, isLoading: isPreparingBuyerConfig } =
useSimulateEscrowUniversalPayArbitrationFeeByBuyer({
query: {
enabled: isBuyer,
},
args: [BigInt(id)],
value: arbitrationCost,
});

const { data: payArbitrationFeeBySellerConfig } = useSimulateEscrowUniversalPayArbitrationFeeBySeller({
args: [BigInt(id)],
value: arbitrationCost,
});
const { data: payArbitrationFeeBySellerConfig, isLoading: isPreparingSellerConfig } =
useSimulateEscrowUniversalPayArbitrationFeeBySeller({
query: {
enabled: !isBuyer,
},
args: [BigInt(id)],
value: arbitrationCost,
});

const { writeContractAsync: payArbitrationFeeByBuyer } =
useWriteEscrowUniversalPayArbitrationFeeByBuyer(payArbitrationFeeByBuyerConfig);
const { writeContractAsync: payArbitrationFeeBySeller } = useWriteEscrowUniversalPayArbitrationFeeBySeller(
payArbitrationFeeBySellerConfig
);
const { writeContractAsync: payArbitrationFeeBySeller } =
useWriteEscrowUniversalPayArbitrationFeeBySeller(payArbitrationFeeBySellerConfig);

const handleRaiseDispute = () => {
if (isBuyer && !isUndefined(payArbitrationFeeByBuyer)) {
Expand Down Expand Up @@ -80,8 +85,8 @@ const RaiseDisputeButton: React.FC<IRaiseDisputeButton> = ({ toggleModal, button

return (
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
isLoading={isSending || isPreparingBuyerConfig || isPreparingSellerConfig}
disabled={isSending || isPreparingBuyerConfig || isPreparingSellerConfig}
text={buttonText}
onClick={handleRaiseDispute}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useMemo, useState } from "react";
import { Button } from "@kleros/ui-components-library";
import { useAccount, usePublicClient } from "wagmi";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useWriteEscrowUniversalTimeOutByBuyer,
useWriteEscrowUniversalTimeOutBySeller,
Expand All @@ -20,7 +19,6 @@ const TimeOutButton: React.FC = () => {
const { buyer, id } = useTransactionDetailsContext();
const isBuyer = useMemo(() => address?.toLowerCase() === buyer?.toLowerCase(), [address, buyer]);
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: timeOutByBuyerConfig } = useSimulateEscrowUniversalTimeOutByBuyer({
args: [BigInt(id)],
Expand Down Expand Up @@ -66,7 +64,7 @@ const TimeOutButton: React.FC = () => {
return (
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
disabled={isSending}
text="Claim full payment back"
onClick={handleTimeout}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState } from "react";
import { Button } from "@kleros/ui-components-library";
import { useToggle } from "react-use";
import PaymentReleased from "pages/MyTransactions/Modal/PaymentReleased";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useWriteEscrowUniversalExecuteTransaction,
useSimulateEscrowUniversalExecuteTransaction,
Expand All @@ -17,7 +16,6 @@ const ClaimFullPaymentButton: React.FC = () => {
const [isSending, setIsSending] = useState<boolean>(false);
const publicClient = usePublicClient();
const { id } = useTransactionDetailsContext();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: executeTransactionConfig } = useSimulateEscrowUniversalExecuteTransaction({
args: [id],
Expand Down Expand Up @@ -49,7 +47,7 @@ const ClaimFullPaymentButton: React.FC = () => {
<>
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
disabled={isSending}
text={"No. Claim full payment"}
onClick={handleExecuteTransaction}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState } from "react";
import { Button } from "@kleros/ui-components-library";
import { useToggle } from "react-use";
import PaymentReleased from "pages/MyTransactions/Modal/PaymentReleased";
import { useNewTransactionContext } from "context/NewTransactionContext";
import { useWriteEscrowUniversalPay, useSimulateEscrowUniversalPay } from "hooks/contracts/generated";
import { isUndefined } from "utils/index";
import { wrapWithToast } from "utils/wrapWithToast";
Expand All @@ -16,7 +15,6 @@ const ReleasePaymentButton: React.FC = () => {
const publicClient = usePublicClient();
const { id, amount } = useTransactionDetailsContext();
const refetchQuery = useQueryRefetch();
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: releaseFullPaymentConfig } = useSimulateEscrowUniversalPay({
args: [id, amount],
Expand Down Expand Up @@ -47,7 +45,7 @@ const ReleasePaymentButton: React.FC = () => {
<>
<Button
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
disabled={isSending}
text={"Yes. Release full payment"}
onClick={handleReleasePayment}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useState, useMemo } from "react";
import styled from "styled-components";
import { Button } from "@kleros/ui-components-library";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
useWriteEscrowUniversalCreateNativeTransaction,
useSimulateEscrowUniversalCreateNativeTransaction,
Expand All @@ -18,6 +17,7 @@ import {
useReadContract,
useWriteContract,
useSimulateContract,
useBalance,
} from "wagmi";
import { parseEther, parseUnits } from "viem";
import { isUndefined } from "utils/index";
Expand All @@ -39,6 +39,7 @@ const DepositPaymentButton: React.FC = () => {
deadline,
sendingToken,
resetContext,
setHasSufficientNativeBalance,
} = useNewTransactionContext();

const [finalRecipientAddress, setFinalRecipientAddress] = useState(sellerAddress);
Expand All @@ -55,12 +56,34 @@ const DepositPaymentButton: React.FC = () => {
() => (isNativeTransaction ? parseEther(sendingQuantity) : parseUnits(sendingQuantity, 18)),
[isNativeTransaction, sendingQuantity]
);
const { hasSufficientNativeBalance } = useNewTransactionContext();

const { data: balanceData } = useBalance({
address: address as `0x${string}` | undefined,
token: isNativeTransaction ? undefined : sendingToken?.address as `0x${string}` | undefined,
});

const [error, setError] = useState<string | null>(null);

useEffect(() => {
setFinalRecipientAddress(ensResult.data || sellerAddress);
}, [sellerAddress, ensResult.data]);

useEffect(() => {
const balance = parseFloat(balanceData?.formatted || "0");
const quantity = parseFloat(sendingQuantity);

if (quantity > balance) {
setError("Insufficient balance");
setHasSufficientNativeBalance(false);
} else if (quantity === 0) {
setError("Amount cannot be zero");
setHasSufficientNativeBalance(false);
} else {
setError(null);
setHasSufficientNativeBalance(true);
}
}, [balanceData, sendingQuantity, setHasSufficientNativeBalance]);

const { data: allowance, refetch: refetchAllowance } = useReadContract({
query: { enabled: !isNativeTransaction && chain?.id },
address: sendingToken?.address,
Expand Down Expand Up @@ -151,12 +174,15 @@ const DepositPaymentButton: React.FC = () => {
};

return (
<StyledButton
isLoading={isSending}
disabled={isSending || !hasSufficientNativeBalance}
text={isNativeTransaction || isApproved ? "Deposit the Payment" : "Approve Token"}
onClick={isNativeTransaction || isApproved ? handleCreateTransaction : handleApproveToken}
/>
<>
<StyledButton
isLoading={isSending}
disabled={isSending || !!error}
text={isNativeTransaction || isApproved ? "Deposit the Payment" : "Approve Token"}
onClick={isNativeTransaction || isApproved ? handleCreateTransaction : handleApproveToken}
/>
<p>{error}</p>
</>
);
};

Expand Down

0 comments on commit 84605c6

Please sign in to comment.