Skip to content

Commit

Permalink
chore: remove points, refactorise withdraw for phase-1 delegation (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab authored Dec 6, 2024
1 parent 753c2f4 commit efbb76b
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 686 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
NEXT_PUBLIC_MEMPOOL_API=https://mempool.space
NEXT_PUBLIC_API_URL=https://staking-api.phase-2-devnet.babylonlabs.io
NEXT_PUBLIC_POINTS_API_URL=https://points.testnet.babylonchain.io
NEXT_PUBLIC_NETWORK=signet
NEXT_PUBLIC_DISPLAY_TESTING_MESSAGES=true
NEXT_PUBLIC_SENTRY_DSN=http://[email protected]:9000/1
1 change: 0 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ jobs:
build-args: |
NEXT_PUBLIC_MEMPOOL_API=${{ vars.NEXT_PUBLIC_MEMPOOL_API }}
NEXT_PUBLIC_API_URL=${{ vars.NEXT_PUBLIC_API_URL }}
NEXT_PUBLIC_POINTS_API_URL=${{ vars.NEXT_PUBLIC_POINTS_API_URL }}
NEXT_PUBLIC_NETWORK=${{ vars.NEXT_PUBLIC_NETWORK }}
NEXT_PUBLIC_DISPLAY_TESTING_MESSAGES=${{ vars.NEXT_PUBLIC_DISPLAY_TESTING_MESSAGES }}
NEXT_PUBLIC_SENTRY_DSN=${{ vars.NEXT_PUBLIC_SENTRY_DSN }}
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ where,
node queries
- `NEXT_PUBLIC_API_URL` specifies the back-end API to use for the staking
system queries
- `NEXT_PUBLIC_POINTS_API_URL` specifies the Points API to use for the points
system (Optional)
- `NEXT_PUBLIC_NETWORK` specifies the BTC network environment
- `NEXT_PUBLIC_DISPLAY_TESTING_MESSAGES` boolean value to indicate whether display
testing network related message. Default to true
Expand Down
3 changes: 0 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ ENV NEXT_PUBLIC_MEMPOOL_API=${NEXT_PUBLIC_MEMPOOL_API}
ARG NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}

ARG NEXT_PUBLIC_POINTS_API_URL
ENV NEXT_PUBLIC_POINTS_API_URL=${NEXT_PUBLIC_POINTS_API_URL}

ARG NEXT_PUBLIC_NETWORK
ENV NEXT_PUBLIC_NETWORK=${NEXT_PUBLIC_NETWORK}

Expand Down
73 changes: 0 additions & 73 deletions src/app/api/getPoints.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/app/api/pointsApiWrapper.ts

This file was deleted.

118 changes: 44 additions & 74 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Heading } from "@babylonlabs-io/bbn-core-ui";
import type { networks } from "bitcoinjs-lib";
import { useEffect, useMemo, useState } from "react";
import { useEffect, useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";
import { useLocalStorage } from "usehooks-ts";

Expand All @@ -10,90 +9,44 @@ import {
MODE_WITHDRAW,
WithdrawModal,
} from "@/app/components/Modals/WithdrawModal";
import { DelegationsPointsProvider } from "@/app/context/api/DelegationsPointsProvider";
import { useError } from "@/app/context/Error/ErrorContext";
import { useBTCWallet } from "@/app/context/wallet/BTCWalletProvider";
import { useDelegations } from "@/app/hooks/client/api/useDelegations";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { useNetworkFees } from "@/app/hooks/client/api/useNetworkFees";
import { useV1TransactionService } from "@/app/hooks/services/useV1TransactionService";
import { useDelegationState } from "@/app/state/DelegationState";
import {
Delegation as DelegationInterface,
DelegationState,
} from "@/app/types/delegations";
import { ErrorState } from "@/app/types/errors";
import { shouldDisplayPoints } from "@/config";
import { signWithdrawalTx } from "@/utils/delegations/signWithdrawalTx";
import { getIntermediateDelegationsLocalStorageKey } from "@/utils/local_storage/getIntermediateDelegationsLocalStorageKey";
import { toLocalStorageIntermediateDelegation } from "@/utils/local_storage/toLocalStorageIntermediateDelegation";

import { Delegation } from "./Delegation";

export const Delegations = () => {
const { data: delegationsAPI } = useDelegations();
const { address, publicKeyNoCoord, connected, network } = useBTCWallet();

if (!connected || !delegationsAPI || !network) {
return;
}

return (
<DelegationsPointsProvider
publicKeyNoCoord={publicKeyNoCoord}
delegationsAPI={delegationsAPI.delegations}
isWalletConnected={connected}
address={address}
>
{/* If there are no delegations, don't render the content */}
{delegationsAPI.delegations.length > 0 && (
<DelegationsContent
delegationsAPI={delegationsAPI.delegations}
address={address}
btcWalletNetwork={network}
publicKeyNoCoord={publicKeyNoCoord}
isWalletConnected={connected}
/>
)}
</DelegationsPointsProvider>
);
};

interface DelegationsContentProps {
delegationsAPI: DelegationInterface[];
publicKeyNoCoord: string;
btcWalletNetwork: networks.Network;
address: string;
isWalletConnected: boolean;
}

const DelegationsContent: React.FC<DelegationsContentProps> = ({
delegationsAPI,
address,
btcWalletNetwork,
publicKeyNoCoord,
}) => {
export const Delegations = ({}) => {
const { publicKeyNoCoord, connected, network } = useBTCWallet();
const [modalOpen, setModalOpen] = useState(false);
const [txID, setTxID] = useState("");
const [modalMode, setModalMode] = useState<MODE>();
const { showError, captureError } = useError();
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const { showError } = useError();
const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false);
const { data: delegationsAPI } = useDelegations();
const {
delegations = [],
fetchMoreDelegations,
hasMoreDelegations,
isLoading,
} = useDelegationState();

const { signPsbt, getNetworkFees, pushTx } = useBTCWallet();
const { submitWithdrawalTx } = useV1TransactionService();
const { data: networkFees } = useNetworkFees();

const delegation = useMemo(
() =>
delegationsAPI.find((delegation) => delegation.stakingTxHashHex === txID),
[delegationsAPI, txID],
const selectedDelegation = delegationsAPI?.delegations.find(
(delegation) => delegation.stakingTxHashHex === txID,
);

const shouldShowPoints =
isApiNormal && !isGeoBlocked && shouldDisplayPoints();
// Local storage state for intermediate delegations (transitioning, withdrawing)
const intermediateDelegationsLocalStorageKey =
getIntermediateDelegationsLocalStorageKey(publicKeyNoCoord);
Expand Down Expand Up @@ -141,24 +94,37 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
};

// Handles withdrawing requests for delegations that have expired timelocks
// It constructs a withdrawal transaction, creates a signature for it, and submits it to the Bitcoin network
// It constructs a withdrawal transaction, creates a signature for it,
// and submits it to the Bitcoin network
const handleWithdraw = async (id: string) => {
try {
if (!networkFees) {
throw new Error("Network fees not found");
}
// Prevent the modal from closing
setAwaitingWalletResponse(true);

if (selectedDelegation?.stakingTxHashHex != id) {
throw new Error("Wrong delegation selected for withdrawal");
}
// Sign the withdrawal transaction
const { delegation } = await signWithdrawalTx(
id,
delegationsAPI,
publicKeyNoCoord,
btcWalletNetwork,
signPsbt,
address,
getNetworkFees,
pushTx,
const { stakingTx, finalityProviderPkHex, stakingValueSat, unbondingTx } =
selectedDelegation;
submitWithdrawalTx(
{
stakingTimelock: stakingTx.timelock,
finalityProviderPkNoCoordHex: finalityProviderPkHex,
stakingAmountSat: stakingValueSat,
},
stakingTx.startHeight,
stakingTx.txHex,
unbondingTx?.txHex,
);
// Update the local state with the new intermediate delegation
updateLocalStorage(delegation, DelegationState.INTERMEDIATE_WITHDRAWAL);
updateLocalStorage(
selectedDelegation,
DelegationState.INTERMEDIATE_WITHDRAWAL,
);
} catch (error: Error | any) {
showError({
error: {
Expand Down Expand Up @@ -192,7 +158,7 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
}

return intermediateDelegations.filter((intermediateDelegation) => {
const matchingDelegation = delegationsAPI.find(
const matchingDelegation = delegationsAPI.delegations.find(
(delegation) =>
delegation?.stakingTxHashHex ===
intermediateDelegation?.stakingTxHashHex,
Expand Down Expand Up @@ -223,7 +189,7 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
}, [delegationsAPI, setIntermediateDelegationsLocalStorage]);

useEffect(() => {
if (modalOpen && !delegation) {
if (modalOpen && !selectedDelegation) {
showError({
error: {
message: "Delegation not found",
Expand All @@ -235,11 +201,15 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
setTxID("");
setModalMode(undefined);
}
}, [modalOpen, delegation, showError]);
}, [modalOpen, selectedDelegation, showError]);

if (!connected || !delegationsAPI || !network) {
return;
}

// combine delegations from the API and local storage, prioritizing API data
const combinedDelegationsData = delegationsAPI
? [...delegations, ...delegationsAPI]
? [...delegations, ...delegationsAPI.delegations]
: // if no API data, fallback to using only local storage delegations
delegations;

Expand Down Expand Up @@ -296,7 +266,7 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
</InfiniteScroll>
</div>
</div>
{modalMode && txID && delegation && (
{modalMode && txID && selectedDelegation && (
<WithdrawModal
isOpen={modalOpen}
onClose={() => setModalOpen(false)}
Expand Down
17 changes: 1 addition & 16 deletions src/app/components/FAQ/data/questions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactNode } from "react";

import { shouldDisplayPoints, shouldDisplayTestingMsg } from "@/config";
import { shouldDisplayTestingMsg } from "@/config";

export interface Question {
title: string;
Expand Down Expand Up @@ -291,21 +291,6 @@ export const questions = (
),
},
];
if (shouldDisplayPoints()) {
questionList.push({
title: "What are the points for?",
content: (
<p>
We use points to track staking activity. Points are not blockchain
tokens. Points do not, and may never, convert to, accrue to, be used
as a basis to calculate, or become tokens, other digital assets, or
distributions thereof. Points are virtual calculations with no
monetary value. Points do not constitute any currency or property of
any type and are not redeemable, refundable, or transferable.
</p>
),
});
}
if (shouldDisplayTestingMsg()) {
questionList.push({
title: "What is the goal of this testnet?",
Expand Down
Loading

0 comments on commit efbb76b

Please sign in to comment.