From 8aca9e7e5e8c3d97ad4c23b4f81b82a2fd72aa9a Mon Sep 17 00:00:00 2001 From: isstuev Date: Thu, 8 Feb 2024 12:27:48 +0100 Subject: [PATCH] check template summary --- ui/tx/interpretation/TxInterpretation.tsx | 6 +++++- ui/tx/interpretation/utils.test.ts | 14 +++++++++++++- ui/tx/interpretation/utils.ts | 13 +++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/ui/tx/interpretation/TxInterpretation.tsx b/ui/tx/interpretation/TxInterpretation.tsx index 56f81f9e9b..a7fccf074e 100644 --- a/ui/tx/interpretation/TxInterpretation.tsx +++ b/ui/tx/interpretation/TxInterpretation.tsx @@ -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; @@ -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); diff --git a/ui/tx/interpretation/utils.test.ts b/ui/tx/interpretation/utils.test.ts index c3089594b7..be1c7d3380 100644 --- a/ui/tx/interpretation/utils.test.ts +++ b/ui/tx/interpretation/utils.test.ts @@ -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}'; @@ -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); +}); diff --git a/ui/tx/interpretation/utils.ts b/ui/tx/interpretation/utils.ts index 7f5519b200..8cccd15f27 100644 --- a/ui/tx/interpretation/utils.ts +++ b/ui/tx/interpretation/utils.ts @@ -20,6 +20,19 @@ export function getStringChunks(template: string) { return template.split(VAR_REGEXP); } +export function checkSummary(template: string, variables: Record) { + 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) { const variablesNames = extractVariables(template);