Skip to content

Commit

Permalink
feat: add multiple choice query parsing
Browse files Browse the repository at this point in the history
Signed-off-by: david <david@umaproject.org>
  • Loading branch information
daywiss committed Jan 15, 2025
1 parent b4ad1b7 commit 1334d24
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/data/approvedIdentifiersTable.json
Original file line number Diff line number Diff line change
Expand Up @@ -1951,4 +1951,4 @@
"url": ""
}
}
}
}
80 changes: 79 additions & 1 deletion src/helpers/queryParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ export function getQueryMetaData(

return tryParseMultipleChoiceQuery(decodedQueryText, projectName);
}
if (decodedIdentifier === "MULTIPLE_VALUES") {
console.log(
decodedIdentifier,
tryParseMultipleValuesQuery(decodedQueryText, "Polymarket"),
);
return tryParseMultipleValuesQuery(decodedQueryText, "Polymarket");
}

const identifierDetails = approvedIdentifiers[decodedIdentifier];
const isApprovedIdentifier = Boolean(identifierDetails);
Expand Down Expand Up @@ -334,6 +341,77 @@ export function getQueryMetaData(
};
}

type MultipleValuesQuery = {
// The title of the request
title: string;
// Description of the request
description: string;
// Values will be encoded into the settled price in the same order as the provided labels. The oracle UI will display each Label along with an input field. 7 labels maximum.
labels: string[];
};

const isMultipleValuesQueryFormat = (
input: unknown,
): input is MultipleValuesQuery => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const $io1 = (input: any): boolean =>
"string" === typeof input?.title && //eslint-disable-line
"string" === typeof input?.description && //eslint-disable-line
Array.isArray(input?.labels) && //eslint-disable-line
input.labels.length <= 7 && //eslint-disable-line
input.labels.every((label: any) => "string" === typeof label); //eslint-disable-line
return "object" === typeof input && null !== input && $io1(input);
};

function decodeMultipleValuesQuery(decodedAncillaryData: string) {
const endOfObjectIndex = decodedAncillaryData.lastIndexOf("}");
const maybeJson =
endOfObjectIndex > 0
? decodedAncillaryData.slice(0, endOfObjectIndex + 1)
: decodedAncillaryData;

const json = JSON.parse(maybeJson);
if (!isMultipleValuesQueryFormat(json))
throw new Error("Not a valid multiple values request");
return {
title: json.title,
description: json.description,
proposeOptions: json.labels.map((opt: string) => ({
label: opt,
value: 0,
secondaryLabel: undefined,
})),
};
}
function tryParseMultipleValuesQuery(
decodedQueryText: string,
projectName: ProjectName,
): MetaData {
try {
return {
...decodeMultipleValuesQuery(decodedQueryText),
umipUrl:
"https://github.com/UMAprotocol/UMIPs/blob/master/UMIPs/umip-183.md",
umipNumber: "UMIP-183",
project: projectName,
};
} catch (err) {
let description = `Error decoding description`;
if (err instanceof Error) {
description += `: ${err.message}`;
}
console.error(`Error parsing MULTIPLE_VALUES`, decodedQueryText, err);
return {
title: `MULTIPLE_VALUES`,
description,
umipUrl:
"https://github.com/UMAprotocol/UMIPs/blob/master/UMIPs/umip-183.md",
umipNumber: "UMIP-183",
project: projectName,
proposeOptions: [],
};
}
}
type MultipleChoiceQuery = {
// Require a title string
title: string;
Expand Down Expand Up @@ -391,7 +469,7 @@ function tryParseMultipleChoiceQuery(
umipUrl:
"https://github.com/UMAprotocol/UMIPs/blob/master/UMIPs/umip-181.md",
umipNumber: "UMIP-181",
project: "Unknown",
project: projectName,
proposeOptions: makeMultipleChoiceOptions(
makeMultipleChoiceYesOrNoOptions(),
),
Expand Down
2 changes: 1 addition & 1 deletion src/types/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,5 @@ export type MetaData = {
export type DropdownItem = {
label: string;
value: string | number;
secondaryLabel?: string;
secondaryLabel?: string | undefined;
};

0 comments on commit 1334d24

Please sign in to comment.