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

[#1951] Handle script based DReps #2182

Merged
merged 5 commits into from
Oct 21, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ changes.
- Fix unwanted horizontal page scroll on Governance Actions page [Issue 1897](https://github.com/IntersectMBO/govtool/issues/1897)
- Fix duplicate testIds for reference errors and hints in DRep metadata form [Issue 1965](https://github.com/IntersectMBO/govtool/issues/1965)
- Eliminate duplicate DReps in the DRep Directory [Issue 2171](https://github.com/IntersectMBO/govtool/issues/2171)
- Handle script based DReps [Issue 1951](https://github.com/IntersectMBO/govtool/issues/1951)

### Changed

Expand Down
7 changes: 7 additions & 0 deletions govtool/backend/sql/get-current-delegation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ select
else encode(drep_hash.raw,'hex')
end as drep_raw,
drep_hash.view as drep_view,
EXISTS (
SELECT dh.has_script
FROM drep_hash as dh
WHERE drep_hash.raw = dh.raw
AND dh.has_script = true
LIMIT 1
) AS has_script,
encode(tx.hash, 'hex')
from delegation_vote
join tx on tx.id = delegation_vote.tx_id
Expand Down
14 changes: 14 additions & 0 deletions govtool/backend/sql/get-drep-info.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ LatestRegistrationEntry AS (
drep_registration.tx_id DESC
LIMIT 1
),
IsScriptHash AS (
SELECT EXISTS(
SELECT
drep_hash.has_script
FROM
drep_hash
CROSS JOIN DRepId
WHERE
drep_hash.raw = DRepId.raw
AND
drep_hash.has_script = true
) AS has_script),
IsRegisteredAsDRep AS (
SELECT
(LatestRegistrationEntry.deposit IS NULL
Expand Down Expand Up @@ -165,6 +177,7 @@ SoleVoterRetire AS (
LIMIT 1
)
SELECT
IsScriptHash.has_script,
IsRegisteredAsDRep.value,
WasRegisteredAsDRep.value,
IsRegisteredAsSoleVoter.value,
Expand Down Expand Up @@ -197,5 +210,6 @@ FROM
CROSS JOIN SoleVoterRegister
CROSS JOIN SoleVoterRetire
CROSS JOIN LatestRegistrationEntry
CROSS JOIN IsScriptHash
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = LatestRegistrationEntry.voting_anchor_id
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id
2 changes: 2 additions & 0 deletions govtool/backend/sql/list-dreps.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ DRepActivity AS (
SELECT
encode(dh.raw, 'hex'),
dh.view,
dh.has_script,
va.url,
encode(va.data_hash, 'hex'),
dr_deposit.deposit,
Expand Down Expand Up @@ -137,6 +138,7 @@ GROUP BY
dh.raw,
second_to_newest_drep_registration.voting_anchor_id,
dh.view,
dh.has_script,
va.url,
va.data_hash,
dr_deposit.deposit,
Expand Down
7 changes: 5 additions & 2 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ mapDRepStatus Types.Inactive = Inactive
drepRegistrationToDrep :: Types.DRepRegistration -> DRep
drepRegistrationToDrep Types.DRepRegistration {..} =
DRep
{ dRepDrepId = DRepHash dRepRegistrationDRepHash,
{ dRepIsScriptBased = dRepRegistrationIsScriptBased,
dRepDrepId = DRepHash dRepRegistrationDRepHash,
dRepView = dRepRegistrationView,
dRepUrl = dRepRegistrationUrl,
dRepMetadataHash = dRepRegistrationDataHash,
Expand All @@ -131,6 +132,7 @@ delegationToResponse Types.Delegation {..} =
DelegationResponse
{ delegationResponseDRepHash = HexText <$> delegationDRepHash,
delegationResponseDRepView = delegationDRepView,
delegationResponseIsDRepScriptBased = delegationIsDRepScriptBased,
delegationResponseTxHash = HexText delegationTxHash
}

Expand Down Expand Up @@ -286,7 +288,8 @@ drepInfo (unHexText -> dRepId) = do
CacheEnv {dRepInfoCache} <- asks vvaCache
Types.DRepInfo {..} <- cacheRequest dRepInfoCache dRepId $ DRep.getDRepInfo dRepId
return $ DRepInfoResponse
{ dRepInfoResponseIsRegisteredAsDRep = dRepInfoIsRegisteredAsDRep
{ dRepInfoResponseIsScriptBased = dRepInfoIsScriptBased
, dRepInfoResponseIsRegisteredAsDRep = dRepInfoIsRegisteredAsDRep
, dRepInfoResponseWasRegisteredAsDRep = dRepInfoWasRegisteredAsDRep
, dRepInfoResponseIsRegisteredAsSoleVoter = dRepInfoIsRegisteredAsSoleVoter
, dRepInfoResponseWasRegisteredAsSoleVoter = dRepInfoWasRegisteredAsSoleVoter
Expand Down
13 changes: 8 additions & 5 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ instance ToSchema VoteResponse where

data DRepInfoResponse
= DRepInfoResponse
{ dRepInfoResponseIsRegisteredAsDRep :: Bool
{ dRepInfoResponseIsScriptBased :: Bool
, dRepInfoResponseIsRegisteredAsDRep :: Bool
, dRepInfoResponseWasRegisteredAsDRep :: Bool
, dRepInfoResponseIsRegisteredAsSoleVoter :: Bool
, dRepInfoResponseWasRegisteredAsSoleVoter :: Bool
Expand Down Expand Up @@ -758,7 +759,8 @@ instance ToSchema DRepType where

data DRep
= DRep
{ dRepDrepId :: DRepHash
{ dRepIsScriptBased :: Bool
, dRepDrepId :: DRepHash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, dRepDrepId :: DRepHash
, dRepID :: DRepHash

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are returning drepId not just id

, dRepView :: Text
, dRepUrl :: Maybe Text
, dRepMetadataHash :: Maybe Text
Expand Down Expand Up @@ -855,9 +857,10 @@ instance ToSchema ListDRepsResponse where

data DelegationResponse
= DelegationResponse
{ delegationResponseDRepHash :: Maybe HexText
, delegationResponseDRepView :: Text
, delegationResponseTxHash :: HexText
{ delegationResponseDRepHash :: Maybe HexText
, delegationResponseDRepView :: Text
, delegationResponseIsDRepScriptBased :: Bool
, delegationResponseTxHash :: HexText
}
deriveJSON (jsonOptions "delegationResponse") ''DelegationResponse

Expand Down
6 changes: 3 additions & 3 deletions govtool/backend/src/VVA/AdaHolder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ getCurrentDelegation ::
getCurrentDelegation stakeKey = withPool $ \conn -> do
result <- liftIO $ SQL.query conn getCurrentDelegationSql (SQL.Only stakeKey)
case result of
[] -> return Nothing
[(mDRepHash, dRepView, txHash)] -> return $ Just $ Delegation mDRepHash dRepView txHash
_ -> error ("multiple delegations for stake key: " <> unpack stakeKey)
[] -> return Nothing
[(mDRepHash, dRepView, isDRepScriptBased, txHash)] -> return $ Just $ Delegation mDRepHash dRepView isDRepScriptBased txHash
_ -> error ("multiple delegations for stake key: " <> unpack stakeKey)

getVotingPowerSql :: SQL.Query
getVotingPowerSql = sqlFrom $(embedFile "sql/get-stake-key-voting-power.sql")
Expand Down
11 changes: 7 additions & 4 deletions govtool/backend/src/VVA/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ listDReps = withPool $ \conn -> do
results <- liftIO $ SQL.query_ conn listDRepsSql
timeZone <- liftIO getCurrentTimeZone
return
[ DRepRegistration drepHash drepView url dataHash (floor @Scientific deposit) votingPower status drepType txHash (localTimeToUTC timeZone date) metadataError paymentAddress givenName objectives motivations qualifications imageUrl imageHash
[ DRepRegistration drepHash drepView isScriptBased url dataHash (floor @Scientific deposit) votingPower status drepType txHash (localTimeToUTC timeZone date) metadataError paymentAddress givenName objectives motivations qualifications imageUrl imageHash
| ( drepHash
, drepView
, isScriptBased
, url
, dataHash
, deposit
Expand Down Expand Up @@ -132,7 +133,8 @@ getDRepInfo
getDRepInfo drepId = withPool $ \conn -> do
result <- liftIO $ SQL.query conn getDRepInfoSql (SQL.Only drepId)
case result of
[ ( isRegisteredAsDRep
[ ( isScriptBased
, isRegisteredAsDRep
, wasRegisteredAsDRep
, isRegisteredAsSoleVoter
, wasRegisteredAsSoleVoter
Expand All @@ -153,7 +155,8 @@ getDRepInfo drepId = withPool $ \conn -> do
, imageHash
)] ->
return $ DRepInfo
{ dRepInfoIsRegisteredAsDRep = fromMaybe False isRegisteredAsDRep
{ dRepInfoIsScriptBased = isScriptBased
, dRepInfoIsRegisteredAsDRep = fromMaybe False isRegisteredAsDRep
, dRepInfoWasRegisteredAsDRep = fromMaybe False wasRegisteredAsDRep
, dRepInfoIsRegisteredAsSoleVoter = fromMaybe False isRegisteredAsSoleVoter
, dRepInfoWasRegisteredAsSoleVoter = fromMaybe False wasRegisteredAsSoleVoter
Expand All @@ -173,4 +176,4 @@ getDRepInfo drepId = withPool $ \conn -> do
, dRepInfoImageUrl = imageUrl
, dRepInfoImageHash = imageHash
}
[] -> return $ DRepInfo False False False False Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
[] -> return $ DRepInfo False False False False False Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
11 changes: 7 additions & 4 deletions govtool/backend/src/VVA/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ data Vote

data DRepInfo
= DRepInfo
{ dRepInfoIsRegisteredAsDRep :: Bool
{ dRepInfoIsScriptBased :: Bool
, dRepInfoIsRegisteredAsDRep :: Bool
, dRepInfoWasRegisteredAsDRep :: Bool
, dRepInfoIsRegisteredAsSoleVoter :: Bool
, dRepInfoWasRegisteredAsSoleVoter :: Bool
Expand Down Expand Up @@ -101,6 +102,7 @@ data DRepRegistration
= DRepRegistration
{ dRepRegistrationDRepHash :: Text
, dRepRegistrationView :: Text
, dRepRegistrationIsScriptBased :: Bool
, dRepRegistrationUrl :: Maybe Text
, dRepRegistrationDataHash :: Maybe Text
, dRepRegistrationDeposit :: Integer
Expand Down Expand Up @@ -215,7 +217,8 @@ data NetworkMetrics

data Delegation
= Delegation
{ delegationDRepHash :: Maybe Text
, delegationDRepView :: Text
, delegationTxHash :: Text
{ delegationDRepHash :: Maybe Text
, delegationDRepView :: Text
, delegationIsDRepScriptBased :: Bool
, delegationTxHash :: Text
}
12 changes: 9 additions & 3 deletions govtool/frontend/src/components/molecules/DRepInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import { CopyButton } from "@atoms";
import { useTranslation } from "@hooks";
import { Card } from "./Card";
import { gray } from "@/consts";
import { useGetDRepDetailsQuery } from "@/hooks";

export const DRepInfoCard = () => {
const { dRepIDBech32 } = useCardano();
const { dRepID } = useCardano();
const { dRep } = useGetDRepDetailsQuery(dRepID);
const { t } = useTranslation();

if (!dRep) {
return null;
}

return (
<Card border elevation={0} sx={{ p: 1.5 }}>
<Box sx={{ display: "flex", justifyContent: "space-between" }}>
<Typography color={gray.c300} fontSize={12} fontWeight={500}>
{t("myDRepId")}
</Typography>
<CopyButton text={dRepIDBech32} variant="blue" />
<CopyButton text={dRep.view} variant="blue" />
</Box>
<Box display="flex" flexDirection="row" mt={0.5} alignItems="center">
<Typography
Expand All @@ -27,7 +33,7 @@ export const DRepInfoCard = () => {
fontSize={14}
fontWeight={500}
>
{dRepIDBech32}
{dRep.view}
</Typography>
</Box>
</Card>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ListGovActionsDashboardCards } from "./DashboardCards/ListGovActionsDas
import { ProposeGovActionDashboardCard } from "./DashboardCards/ProposeGovActionDashboardCard";

export const DashboardCards = () => {
const { dRepID, dRepIDBech32, pendingTransaction, stakeKey } = useCardano();
const { dRepID, pendingTransaction, stakeKey } = useCardano();
const { screenWidth } = useScreenDimension();

const { currentDelegation } = useGetAdaHolderCurrentDelegationQuery(stakeKey);
Expand Down Expand Up @@ -65,7 +65,7 @@ export const DashboardCards = () => {
/>

<DRepDashboardCard
dRepIDBech32={dRepIDBech32}
dRepID={dRepID}
pendingTransaction={pendingTransaction}
voter={voter}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Trans } from "react-i18next";

import { IMAGES, PATHS } from "@consts";
import { PendingTransaction } from "@context";
import { useTranslation } from "@hooks";
import { useGetDRepDetailsQuery, useTranslation } from "@hooks";
import { VoterInfo } from "@models";
import {
CopyableInfo,
Expand All @@ -13,19 +13,21 @@ import {
import { correctAdaFormat, openInNewTab } from "@utils";

type DRepDashboardCardProps = {
dRepIDBech32: string;
dRepID: string;
pendingTransaction: PendingTransaction;
voter: VoterInfo;
};

export const DRepDashboardCard = ({
dRepIDBech32,
dRepID,
pendingTransaction,
voter,
}: DRepDashboardCardProps) => {
const navigate = useNavigate();
const { t } = useTranslation();

const { dRep } = useGetDRepDetailsQuery(dRepID);

const inProgress = !!(
pendingTransaction.registerAsDrep ||
pendingTransaction.retireAsDrep ||
Expand All @@ -42,10 +44,10 @@ export const DRepDashboardCard = ({
};

const navigateToDrepDirectory = () =>
navigate(
PATHS.dashboardDRepDirectoryDRep.replace(":dRepId", dRepIDBech32),
{ state: { enteredFromWithinApp: true } },
);
dRep &&
navigate(PATHS.dashboardDRepDirectoryDRep.replace(":dRepId", dRep.view), {
state: { enteredFromWithinApp: true },
});

const cardProps: Partial<DashboardActionCardProps> = (() => {
// transaction in progress
Expand Down Expand Up @@ -165,14 +167,16 @@ export const DRepDashboardCard = ({
type="d-rep"
{...cardProps}
>
{voter?.isRegisteredAsDRep && !pendingTransaction?.retireAsDrep && (
<CopyableInfo
dataTestId="dRep-id-display-card-dashboard"
label={t("dashboard.cards.drep.yourDRepId")}
sx={{ mt: 1 }}
value={dRepIDBech32}
/>
)}
{voter?.isRegisteredAsDRep &&
!pendingTransaction?.retireAsDrep &&
dRep && (
<CopyableInfo
dataTestId="dRep-id-display-card-dashboard"
label={t("dashboard.cards.drep.yourDRepId")}
sx={{ mt: 1 }}
value={dRep.view}
/>
)}
</DashboardActionCard>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Trans } from "react-i18next";

import { IMAGES, PATHS } from "@consts";
import { PendingTransaction } from "@context";
import { useGetDRepListInfiniteQuery, useTranslation } from "@hooks";
import { useGetDRepDetailsQuery, useTranslation } from "@hooks";
import { CurrentDelegation, VoterInfo } from "@models";
import {
DashboardActionCard,
Expand All @@ -13,7 +13,6 @@ import {
} from "@molecules";
import {
correctAdaFormat,
formHexToBech32,
getMetadataDataMissingStatusTranslation,
openInNewTab,
} from "@utils";
Expand All @@ -39,17 +38,10 @@ export const DelegateDashboardCard = ({
const navigate = useNavigate();
const { t } = useTranslation();

const { dRepData, isDRepListFetching } = useGetDRepListInfiniteQuery(
{
searchPhrase: delegateTx?.resourceId ?? currentDelegation?.dRepHash ?? "",
},
{
enabled: !!currentDelegation?.dRepHash || !!delegateTx?.resourceId,
},
const { dRep: myDRepDelegationData, isLoading } = useGetDRepDetailsQuery(
delegateTx?.resourceId ?? currentDelegation?.dRepHash,
);

const myDRepDelegationData = dRepData?.[0];

const learnMoreButton = {
children: t("learnMore"),
dataTestId: "delegate-learn-more-button",
Expand Down Expand Up @@ -161,7 +153,7 @@ export const DelegateDashboardCard = ({
// eslint-disable-next-line react/jsx-indent
<DelegationAction
drepName={
isDRepListFetching
isLoading
? "Loading..."
: myDRepDelegationData?.metadataStatus
? getMetadataDataMissingStatusTranslation(
Expand Down Expand Up @@ -236,9 +228,7 @@ const getDisplayedDelegationId = ({
];
if (delegateTo) {
if (!restrictedNames.includes(delegateTo)) {
return delegateTo.includes("drep")
? delegateTo
: formHexToBech32(delegateTo);
return delegateTo;
}
return undefined;
}
Expand Down
Loading
Loading