Skip to content

Commit

Permalink
fix: get attack gas
Browse files Browse the repository at this point in the history
  • Loading branch information
lance10030 committed Sep 13, 2024
1 parent 2e26b88 commit c76918e
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/components/Charts/ClaimChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import { ConnectButton, useConnectModal } from "@rainbow-me/rainbowkit";
import { Button } from "@/components/Button";
import { Input } from "@/components/Input";
import { useCalculateClaim } from "@/hooks/useCalculateClaim";
import { useAccount, useReadContract } from "wagmi";
import { abi } from "@/utils/abi";
import { Abi, Address, ContractFunctionExecutionError, UserRejectedRequestError, formatUnits, parseUnits } from "viem";
import { useAccount } from "wagmi";
import Spinner from "@/components/Spinner";
import { toast } from "react-toastify";
import { useEthersSigner } from "@/hooks/useEthersSigner";
Expand Down Expand Up @@ -214,18 +212,7 @@ const ClaimChart: FC<{ claimData: ClaimData[], address: string }> = ({ claimData
return 2 * (Number(modalData.position) + 1)
}
}, [modalData])
const { data: attackGas } = useReadContract({
abi: abi as Abi,
address: address as Address,
functionName: 'getRequiredBond',
args: [attackPosition]
})
const { data: defendGas } = useReadContract({
abi: abi as Abi,
address: address as Address,
functionName: 'getRequiredBond',
args: [defendPosition]
})

const handleClick = (e: any) => {
if (isConnected) {
setShowModal(true);
Expand All @@ -243,16 +230,16 @@ const ClaimChart: FC<{ claimData: ClaimData[], address: string }> = ({ claimData
}
}, [attackPosition])
const handleAttack = async () => {
if (!signer) return;
if (!val) {
toast.error('Challenge claim required!')
return
};
if (!attackGas) return;
if (!signer) return;
const contract = getChallengeContract(address, signer)
try {
setAttackLoading(true)
const tx = await contract.attack('0x' + modalData?.claim, modalData?.parentIndex, val, { value: parseUnits(formatUnits(attackGas as bigint, 18), 18) })
const gas = await contract.getRequiredBond(attackPosition)
const tx = await contract.attack('0x' + modalData?.claim, modalData?.parentIndex, val, { value: gas })
const res = await tx.wait()
setAttackLoading(false)
if (res.status === 1) {
Expand All @@ -269,12 +256,12 @@ const ClaimChart: FC<{ claimData: ClaimData[], address: string }> = ({ claimData
toast.error('Challenge claim required!')
return
};
if (!defendGas) return;
if (!signer) return;
const contract = getChallengeContract(address, signer)
try {
setDefendLoading(true)
const tx = await contract.defend('0x' + modalData?.claim, modalData?.parentIndex, val, { value: parseUnits(formatUnits(defendGas as bigint, 18), 18) })
const gas = await contract.getRequiredBond(defendPosition)
const tx = await contract.defend('0x' + modalData?.claim, modalData?.parentIndex, val, { value: gas })
const res = await tx.wait()
if (res.status === 1) {
toast.success('Transaction receipt!')
Expand Down Expand Up @@ -329,15 +316,15 @@ const ClaimChart: FC<{ claimData: ClaimData[], address: string }> = ({ claimData
</div>
<div>
<div className="text-sm font-semibold text-contentSecondary-light dark:text-warmGray-300 mb-1 mt-4">
challenge claim
Challenge claim
</div>
<Input
type="text"
name="search"
id="search"
value={val}
onChange={(e) => setVal(e.target.value)}
className={"rounded-none rounded-l-md text-black h-10"}
className={"rounded-none rounded-l-md text-black dark:text-warmGray-300 h-10 "}
placeholder={"challenge string"}
/>
</div>
Expand Down

0 comments on commit c76918e

Please sign in to comment.