Skip to content

Commit

Permalink
add terms acceptance
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs committed Oct 23, 2024
1 parent cea522a commit 8174610
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/app/api/postTerms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { apiWrapper } from "./apiWrapper";

interface TermsPayload {
address: string;
termsAccepted: boolean;
publicKey: string;
}

export const postTerms = async (
address: string,
termsAccepted: boolean,
publicKey: string,
) => {
const payload: TermsPayload = {
address: address,
termsAccepted: termsAccepted,
publicKey: publicKey,
};

const response = await apiWrapper(
"POST",
"/terms_acceptance",
"Error submitting terms acceptance request",
payload,
);

return response.status === 200;
};
4 changes: 4 additions & 0 deletions src/app/components/Modals/ErrorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const ErrorModal: React.FC<ErrorModalProps> = ({
return "Stake Error";
case ErrorState.UNBONDING:
return "Unbonding Error";
case ErrorState.TERMS:
return "Terms Error";
default:
return "Unknown Error";
}
Expand All @@ -71,6 +73,8 @@ export const ErrorModal: React.FC<ErrorModalProps> = ({
return `Failed to stake due to: ${errorMessage}`;
case ErrorState.WALLET:
return `Failed to switch network due to: ${errorMessage}`;
case ErrorState.TERMS:
return `Failed to accept terms due to: ${errorMessage}`;
default:
return errorMessage;
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/components/Points/StakerPoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export const StakerPoints: React.FC<StakerPointsProps> = ({

const points = stakerPoints?.[0]?.points;

console.log(points !== undefined || points !== 0);

return (
<div className="flex items-center justify-end gap-1">
<p className="whitespace-nowrap font-semibold">
Expand Down
32 changes: 32 additions & 0 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Network, WalletProvider } from "@/utils/wallet/wallet_provider";

import { getDelegations, PaginatedDelegations } from "./api/getDelegations";
import { getGlobalParams } from "./api/getGlobalParams";
import { postTerms } from "./api/postTerms";
import { UTXO_KEY } from "./common/constants";
import { signPsbtTransaction } from "./common/utils/psbt";
import { Delegations } from "./components/Delegations/Delegations";
Expand Down Expand Up @@ -191,6 +192,8 @@ const Home: React.FC<HomeProps> = () => {
const [delegationsLocalStorage, setDelegationsLocalStorage] = useLocalStorage<
Delegation[]
>(delegationsLocalStorageKey, []);
const [termsAcceptedLocalStorage, setTermsAcceptedLocalStorage] =
useLocalStorage("termsAccepted", false);

const [connectModalOpen, setConnectModalOpen] = useState(false);

Expand All @@ -204,6 +207,9 @@ const Home: React.FC<HomeProps> = () => {
setBTCWalletNetwork(undefined);
setPublicKeyNoCoord("");
setAddress("");
if (termsAcceptedLocalStorage) {
setTermsAcceptedLocalStorage(false);
}
};

const handleConnectBTC = useCallback(
Expand Down Expand Up @@ -314,6 +320,32 @@ const Home: React.FC<HomeProps> = () => {
0,
);

useEffect(() => {
const acceptTerms = async () => {
if (address && publicKeyNoCoord && !termsAcceptedLocalStorage) {
try {
await postTerms(address, true, publicKeyNoCoord);
setTermsAcceptedLocalStorage(true);
} catch (error: Error | any) {
handleError({
error: error,
hasError: true,
errorState: ErrorState.TERMS,
refetchFunction: () => acceptTerms(),
});
}
}
};

acceptTerms();
}, [
address,
publicKeyNoCoord,
termsAcceptedLocalStorage,
setTermsAcceptedLocalStorage,
handleError,
]);

return (
<main
className={`relative h-full min-h-svh w-full ${network === Network.MAINNET ? "main-app-mainnet" : "main-app-testnet"}`}
Expand Down
1 change: 1 addition & 0 deletions src/app/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export enum ErrorState {
WALLET = "WALLET",
WITHDRAW = "WITHDRAW",
STAKING = "STAKING",
TERMS = "TERMS",
}

export interface ErrorType {
Expand Down

0 comments on commit 8174610

Please sign in to comment.