Skip to content

Commit

Permalink
[#1951] handle scripe based DReps on FE
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dyczka committed Oct 18, 2024
1 parent daedc37 commit 6bcc584
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 120 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from "@molecules";
import {
correctAdaFormat,
formHexToBech32,
getMetadataDataMissingStatusTranslation,
openInNewTab,
} from "@utils";
Expand Down Expand Up @@ -236,9 +235,7 @@ const getDisplayedDelegationId = ({
];
if (delegateTo) {
if (!restrictedNames.includes(delegateTo)) {
return delegateTo.includes("drep")
? delegateTo
: formHexToBech32(delegateTo);
return delegateTo;
}
return undefined;
}
Expand Down
4 changes: 3 additions & 1 deletion govtool/frontend/src/context/wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,10 @@ const CardanoProvider = (props: Props) => {
targetDRep = DRep.new_always_abstain();
} else if (target === AutomatedVotingOptionDelegationId.no_confidence) {
targetDRep = DRep.new_always_no_confidence();
} else if (target.includes("drep")) {
} else if (target.includes("drep1")) {
targetDRep = DRep.new_key_hash(Ed25519KeyHash.from_bech32(target));
} else if (target.includes("drep_script1")) {
targetDRep = DRep.new_script_hash(Ed25519KeyHash.from_bech32(target));
} else {
targetDRep = DRep.new_key_hash(Ed25519KeyHash.from_hex(target));
}
Expand Down
1 change: 0 additions & 1 deletion govtool/frontend/src/hooks/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./useCreateGovernanceActionForm";
export * from "./useDelegateTodRepForm";
export * from "./useEditDRepInfoForm";
export * from "./useRegisterAsdRepForm";
export * from "./useVoteActionForm";
Expand Down
105 changes: 0 additions & 105 deletions govtool/frontend/src/hooks/forms/useDelegateTodRepForm.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions govtool/frontend/src/models/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export enum DRepListSort {
export type DrepDataDTO = {
deposit: number;
drepId: string;
isScriptBased: boolean;
latestRegistrationDate: string;
latestTxHash?: string;
metadataHash?: string;
Expand Down Expand Up @@ -198,6 +199,7 @@ export type VotedProposal = {
export type CurrentDelegation = {
dRepHash: string | null;
dRepView: string | null;
isDRepScriptBased: boolean;
txHash: string | null;
} | null;

Expand Down
6 changes: 2 additions & 4 deletions govtool/frontend/src/pages/DRepDirectoryContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@hooks";
import { DataActionsBar, EmptyStateDrepDirectory } from "@molecules";
import { AutomatedVotingOptions, DRepCard } from "@organisms";
import { correctAdaFormat, formHexToBech32, isSameDRep } from "@utils";
import { correctAdaFormat, isSameDRep } from "@utils";
import { DRepListSort, DRepStatus } from "@models";
import {
AutomatedVotingOptionCurrentDelegation,
Expand Down Expand Up @@ -59,9 +59,7 @@ export const DRepDirectoryContent: FC<DRepDirectoryContentProps> = ({

const { dRepData: myDRepList } = useGetDRepListInfiniteQuery(
{
searchPhrase: currentDelegation?.dRepView?.startsWith("drep")
? currentDelegation.dRepView
: formHexToBech32(currentDelegation?.dRepHash ?? ""),
searchPhrase: currentDelegation?.dRepView ?? "",
},
{ enabled: !!inProgressDelegation || !!currentDelegation },
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CurrentDelegation } from "@models";

import { API } from "../API";
import { fixViewForScriptBasedDRep } from "@/utils";

export const getAdaHolderCurrentDelegation = async ({
stakeKey,
Expand All @@ -11,5 +12,16 @@ export const getAdaHolderCurrentDelegation = async ({
`/ada-holder/get-current-delegation/${stakeKey}`,
);

return response.data;
if (!response.data) return response.data;

// DBSync contains wrong representation of DRep view for script based DReps
const view = response.data.dRepView && fixViewForScriptBasedDRep(
response.data.dRepView,
response.data.isDRepScriptBased,
);

return {
...response.data,
dRepView: view,
};
};
16 changes: 14 additions & 2 deletions govtool/frontend/src/services/requests/getDRepList.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { bech32 } from "bech32";

import {
type Infinite,
type DRepStatus,
Expand All @@ -22,9 +24,18 @@ export const getDRepList = async ({
filters = [],
page = 0,
pageSize = 10,
searchPhrase = "",
searchPhrase: rawSearchPhrase = "",
status = [],
}: GetDRepListArguments): Promise<Infinite<DRepData>> => {
// DBSync contains wrong representation of DRep view for script based DReps, but it's still used by BE

Check failure on line 30 in govtool/frontend/src/services/requests/getDRepList.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 104. Maximum allowed is 100
const searchPhrase = (() => {
if (rawSearchPhrase.startsWith("drep_script")) {
const { words } = bech32.decode(rawSearchPhrase);
return bech32.encode("drep", words);
}
return rawSearchPhrase;
})();

const response = await API.get<Infinite<DrepDataDTO>>("/drep/list", {
params: {
page,
Expand All @@ -39,7 +50,8 @@ export const getDRepList = async ({
const validatedResponse = {
...response.data,
elements: await Promise.all(
response.data.elements.map(async (drep) => mapDtoToDrep(drep)),
response.data.elements
.map(async (drep) => mapDtoToDrep(drep)),
),
};

Expand Down
10 changes: 10 additions & 0 deletions govtool/frontend/src/utils/dRep.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bech32 } from "bech32";
import { DRepData } from "@/models";

export const isSameDRep = (
Expand All @@ -9,3 +10,12 @@ export const isSameDRep = (
}
return drepId === dRepIdOrView || view === dRepIdOrView;
};

// DBSync contains wrong representation of DRep view for script based DReps
export const fixViewForScriptBasedDRep = (view: string, isScriptBased: boolean) => {
if (isScriptBased && !view.startsWith("drep_script")) {
const { words } = bech32.decode(view);
return bech32.encode("drep_script", words);
}
return view;
};

Check failure on line 21 in govtool/frontend/src/utils/dRep.ts

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
10 changes: 8 additions & 2 deletions govtool/frontend/src/utils/getDRepID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { bech32 } from "bech32";

import { CardanoApiWallet } from "@models";

export const formHexToBech32 = (dRepID?: string) => {
// TODO: How do we know if DRep is script based if we have no DRep data?
export const formHexToBech32 = (dRepID?: string, isScript?: boolean) => {
if (!dRepID) return;
const words = bech32.toWords(Buffer.from(dRepID, "hex"));
const dRepIDBech32 = bech32.encode("drep", words);
let dRepIDBech32;
if (isScript){

Check failure on line 12 in govtool/frontend/src/utils/getDRepID.ts

View workflow job for this annotation

GitHub Actions / lint

Missing space before opening brace
dRepIDBech32 = bech32.encode("drep_script", words);
} else {
dRepIDBech32 = bech32.encode("drep", words);
}
return dRepIDBech32;
};

Expand Down
7 changes: 7 additions & 0 deletions govtool/frontend/src/utils/mapDtoToDrep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

Check failure on line 1 in govtool/frontend/src/utils/mapDtoToDrep.ts

View workflow job for this annotation

GitHub Actions / lint

Too many blank lines at the beginning of file. Max of 0 allowed
import { DRepData, DRepMetadata, DrepDataDTO } from "@/models";
import { postValidate } from "@/services";
import { fixViewForScriptBasedDRep } from "./dRep";

export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
const emptyMetadata = {
Expand All @@ -15,6 +17,9 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
metadataValid: true,
};

// DBSync contains wrong representation of DRep view for script based DReps
const view = fixViewForScriptBasedDRep(dto.view, dto.isScriptBased);

if (dto.metadataHash && dto.url) {
const validationResponse = await postValidate<DRepMetadata>({
url: dto.url,
Expand All @@ -26,11 +31,13 @@ export const mapDtoToDrep = async (dto: DrepDataDTO): Promise<DRepData> => {
...validationResponse.metadata,
metadataStatus: validationResponse.status || null,
metadataValid: validationResponse.valid,
view,
};
}

return {
...dto,
...emptyMetadata,
view,
};
};
1 change: 1 addition & 0 deletions govtool/frontend/src/utils/tests/dRep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const EXAMPLE_DREP: DRepData = {
motivations: null,
qualifications: null,
doNotList: false,
isScriptBased: false,
};

describe("isSameDRep function", () => {
Expand Down

0 comments on commit 6bcc584

Please sign in to comment.