Skip to content

Commit

Permalink
Merge pull request #1585 from blockscout/check-summary
Browse files Browse the repository at this point in the history
check template summary
  • Loading branch information
isstuev authored Feb 12, 2024
2 parents fec9e20 + 8aca9e7 commit 34d8a57
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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

0 comments on commit 34d8a57

Please sign in to comment.