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

check template summary #1585

Merged
merged 1 commit into from
Feb 12, 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
6 changes: 5 additions & 1 deletion ui/tx/interpretation/TxInterpretation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import EnsEntity from 'ui/shared/entities/ens/EnsEntity';
import TokenEntity from 'ui/shared/entities/token/TokenEntity';
import IconSvg from 'ui/shared/IconSvg';

import { extractVariables, getStringChunks, fillStringVariables, NATIVE_COIN_SYMBOL_VAR_NAME } from './utils';
import { extractVariables, getStringChunks, fillStringVariables, checkSummary, NATIVE_COIN_SYMBOL_VAR_NAME } from './utils';

type Props = {
summary?: TxInterpretationSummary;
Expand Down Expand Up @@ -116,6 +116,10 @@ const TxInterpretation = ({ summary, isLoading, className }: Props) => {
const template = summary.summary_template;
const variables = summary.summary_template_variables;

if (!checkSummary(template, variables)) {
return null;
}

const intermediateResult = fillStringVariables(template, variables);

const variablesNames = extractVariables(intermediateResult);
Expand Down
14 changes: 13 additions & 1 deletion ui/tx/interpretation/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extractVariables, getStringChunks } from './utils';
import { extractVariables, getStringChunks, checkSummary } from './utils';

const template = '{action_type} {source_amount} {native} into {destination_amount} {destination_token}';

Expand All @@ -11,3 +11,15 @@ it('split string without capturing variables', () => {
const result = getStringChunks(template);
expect(result).toEqual([ '', ' ', ' ', ' into ', ' ', '' ]);
});

it('checks that summary is valid', () => {
const result = checkSummary('{foo} {bar}', { foo: { type: 'string', value: 'foo' }, bar: { type: 'string', value: 'bar' } });
expect(result).toBe(true);
});

it('checks that summary is invalid', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:
const result = checkSummary('{foo} {bar}', { foo: { type: 'string', value: null }, bar: { type: 'string', value: 'bar' } });
expect(result).toBe(false);
});
13 changes: 13 additions & 0 deletions ui/tx/interpretation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ export function getStringChunks(template: string) {
return template.split(VAR_REGEXP);
}

export function checkSummary(template: string, variables: Record<string, TxInterpretationVariable>) {
const variablesNames = extractVariables(template);
let result = true;
for (const name of variablesNames) {
if (!variables[name] || variables[name].value === undefined || variables[name].value === null) {
result = false;
break;
}
}

return result;
}

export function fillStringVariables(template: string, variables: Record<string, TxInterpretationVariable>) {
const variablesNames = extractVariables(template);

Expand Down
Loading