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

fix: withdraw & remove vouch #93

Merged
merged 1 commit into from
Oct 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
88 changes: 40 additions & 48 deletions src/app/[pohid]/[chain]/[request]/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ export default withClientConnected<ActionBarProps>(function ActionBar({

const [action, setAction] = useState(ActionType.NONE);

const [didIVouchFor, setDidIVouchFor] = useState(false);
const [isVouchOnchain, setIsVouchOnchain] = useState(false);

const handleDidIVouchFor = () => {
return (
onChainVouches.length + offChainVouches.length >= 0 &&
(onChainVouches.some((voucherAddress) => {
if (voucherAddress.toLocaleLowerCase() === address?.toLocaleLowerCase()) {
setIsVouchOnchain(true);
return true;
}
setIsVouchOnchain(false);
return false;
}) ||
offChainVouches.some(
(voucher) => voucher.voucher.toLocaleLowerCase() === address?.toLocaleLowerCase()
))
);
};

useEffect(() => {
setDidIVouchFor(handleDidIVouchFor());
}, [address, action, requester, revocation, chain, userChainId]);

useEffectOnce(() => {
const checkVouchStatus = async () => {
if (status === "resolved" || status === "withdrawn")
Expand All @@ -110,7 +134,11 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
contractData.requiredNumberOfVouches
)
setAction(ActionType.ADVANCE);
else if (onChainVouches.length + offChainVouches.length >= 0)
else if (
(onChainVouches.length + offChainVouches.length >= 0) &&
didIVouchFor &&
isVouchOnchain
)
setAction(ActionType.REMOVE_VOUCH);
else setAction(ActionType.VOUCH);
} else if (status == "resolving")
Expand Down Expand Up @@ -164,7 +192,7 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
[loading]
)
);
const [prepareWithdraw, withdraw, withdrawState] = usePoHWrite(
const [prepareWithdraw, withdraw] = usePoHWrite(
"withdrawRequest",
useMemo(
() => ({
Expand All @@ -191,35 +219,6 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
]
);

const [isVouchGranted, setIsVouchGranted] = useState({
didIVouchFor: false,
isVouchOnchain: false,
});

useEffect(() => {
const didIVouchFor = () => {
return (
onChainVouches.length + offChainVouches.length >= 0 &&
(onChainVouches.some((voucherAddress) => {
if (voucherAddress === address?.toLocaleLowerCase()) {
setIsVouchGranted((prevState) => ({
...prevState,
isVouchOnchain: true,
}));
return true;
}
return false;
}) ||
offChainVouches.some(
(voucher) => voucher.voucher === address?.toLocaleLowerCase()
))
);
};

if (didIVouchFor())
setIsVouchGranted((prevState) => ({ ...prevState, didIVouchFor: true }));
}, [address, action, requester, revocation, chain, userChainId]);

useEffect(() => {
//useEffectOnce(() => {
if (action === ActionType.ADVANCE && !revocation) {
Expand Down Expand Up @@ -276,13 +275,6 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
prepareWithdraw();
}, [address, prepareWithdraw, action, requester, revocation, chain, userChainId]);


const [withdrawDisabled, setWithdrawDisabled] = useState(true);

useEffect(() => {
setWithdrawDisabled(pending || withdrawState.prepare !== "success");
}, [withdrawState.prepare])

const totalCost = BigInt(contractData.baseDeposit) + arbitrationCost;
const statusColor = colorForStatus(status, revocation, expired);

Expand Down Expand Up @@ -337,18 +329,18 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
/>
)}

{requester === address?.toLowerCase() ? (
{requester.toLocaleLowerCase() === address?.toLowerCase() ? (
<button
disabled={withdrawDisabled}
disabled={userChainId!==chain.id}
className="btn-main mb-2"
onClick={withdraw}
>
Withdraw
</button>
) : !isVouchGranted.didIVouchFor ? (
) : !didIVouchFor ? (
<Vouch pohId={pohId} claimer={requester} web3Loaded={web3Loaded} me={me} chain={chain} address={address} />
) : isVouchGranted.isVouchOnchain ? (
<RemoveVouch requester={requester} pohId={pohId} web3Loaded={web3Loaded} me={me} chain={chain} address={address} />
) : isVouchOnchain ? (
<RemoveVouch requester={requester} pohId={pohId} web3Loaded={web3Loaded} chain={chain} userChainId={userChainId}/>
) : null}
</div>
</>
Expand All @@ -359,18 +351,18 @@ export default withClientConnected<ActionBarProps>(function ActionBar({
<span className="text-slate-400">Ready to advance</span>

<div className="flex gap-4">
{requester === address?.toLowerCase() ? (
{requester.toLocaleLowerCase() === address?.toLowerCase() ? (
<button
disabled={withdrawDisabled}
disabled={userChainId!==chain.id}
className="btn-sec mb-2"
onClick={withdraw}
>
Withdraw
</button>
) : !isVouchGranted.didIVouchFor ? (
) : !didIVouchFor ? (
<Vouch pohId={pohId} claimer={requester} web3Loaded={web3Loaded} me={me} chain={chain} address={address} />
) : isVouchGranted.isVouchOnchain ? (
<RemoveVouch requester={requester} pohId={pohId} web3Loaded={web3Loaded} me={me} chain={chain} address={address} />
) : isVouchOnchain ? (
<RemoveVouch requester={requester} pohId={pohId} web3Loaded={web3Loaded} chain={chain} userChainId={userChainId}/>
) : null}
<button
disabled={pending}
Expand Down
9 changes: 4 additions & 5 deletions src/app/[pohid]/[chain]/[request]/RemoveVouch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ interface RemoveVouchProps {
pohId: Hash;
requester: Address;
web3Loaded: any;
me: any;
chain: SupportedChain;
address: Address | undefined;
userChainId: number;
}

export default function RemoveVouch({ pohId, requester, web3Loaded, me, chain, address }: RemoveVouchProps) {
export default function RemoveVouch({ pohId, requester, web3Loaded, chain, userChainId }: RemoveVouchProps) {
const loading = useLoading();
const [pending] = loading.use();

Expand Down Expand Up @@ -47,8 +46,8 @@ export default function RemoveVouch({ pohId, requester, web3Loaded, me, chain, a

return (
web3Loaded &&
me && me.homeChain?.id === chain.id &&
me.pohId && (
userChainId === chain.id &&
(
<div className="flex gap-4">
<button
disabled={pending}
Expand Down