diff --git a/types/api/contract.ts b/types/api/contract.ts index de0a470007..9e22a980cf 100644 --- a/types/api/contract.ts +++ b/types/api/contract.ts @@ -110,9 +110,14 @@ export interface SmartContractQueryMethodReadError { result: { code: number; message: string; - raw?: string; } | { error: string; + } | { + raw: string; + } | { + method_call: string; + method_id: string; + parameters: Array<{ 'name': string; 'type': string; 'value': string }>; }; } diff --git a/ui/address/contract/ContractRead.pw.tsx b/ui/address/contract/ContractRead.pw.tsx index 8d772203c1..71fe9fcc3c 100644 --- a/ui/address/contract/ContractRead.pw.tsx +++ b/ui/address/contract/ContractRead.pw.tsx @@ -44,29 +44,3 @@ test('base view +@mobile +@dark-mode', async({ mount, page }) => { await expect(component).toHaveScreenshot(); }); - -test('error result', async({ mount, page }) => { - await page.route(CONTRACT_READ_METHODS_API_URL, (route) => route.fulfill({ - status: 200, - body: JSON.stringify(contractMethodsMock.read), - })); - await page.route(CONTRACT_QUERY_METHOD_API_URL, (route) => route.fulfill({ - status: 200, - body: JSON.stringify(contractMethodsMock.readResultError), - })); - - const component = await mount( - - - , - { hooksConfig }, - ); - - await component.getByText(/expand all/i).click(); - await component.getByPlaceholder(/address/i).type('address-hash'); - await component.getByText(/query/i).click(); - - const section = page.locator('section', { hasText: 'FLASHLOAN_PREMIUM_TOTAL' }); - - await expect(section).toHaveScreenshot(); -}); diff --git a/ui/address/contract/ContractReadResult.pw.tsx b/ui/address/contract/ContractReadResult.pw.tsx new file mode 100644 index 0000000000..8cd3341a70 --- /dev/null +++ b/ui/address/contract/ContractReadResult.pw.tsx @@ -0,0 +1,101 @@ +import { test, expect } from '@playwright/experimental-ct-react'; +import React from 'react'; + +import type { ContractMethodReadResult } from './types'; + +import * as contractMethodsMock from 'mocks/contract/methods'; +import TestApp from 'playwright/TestApp'; + +import ContractReadResult from './ContractReadResult'; + +const item = contractMethodsMock.read[0]; +const onSettle = () => Promise.resolve(); + +test.use({ viewport: { width: 500, height: 500 } }); + +test('default error', async({ mount }) => { + const result: ContractMethodReadResult = { + is_error: true, + result: { + error: 'I am an error', + }, + }; + const component = await mount( + + + , + ); + + await expect(component).toHaveScreenshot(); +}); + +test('error with code', async({ mount }) => { + const result: ContractMethodReadResult = { + is_error: true, + result: { + message: 'I am an error', + code: -32017, + }, + }; + const component = await mount( + + + , + ); + + await expect(component).toHaveScreenshot(); +}); + +test('raw error', async({ mount }) => { + const result: ContractMethodReadResult = { + is_error: true, + result: { + raw: '49276d20616c7761797320726576657274696e67207769746820616e206572726f72', + }, + }; + const component = await mount( + + + , + ); + + await expect(component).toHaveScreenshot(); +}); + +test('complex error', async({ mount }) => { + const result: ContractMethodReadResult = { + is_error: true, + result: { + method_call: 'SomeCustomError(address addr, uint256 balance)', + method_id: '50289a9f', + parameters: [ + { name: 'addr', type: 'address', value: '0x850e73b42f48e91ebaedf8f00a74f6147e485c5a' }, + { name: 'balance', type: 'uint256', value: '14' }, + ], + }, + }; + const component = await mount( + + + , + ); + + await expect(component).toHaveScreenshot(); +}); + +test('success', async({ mount }) => { + const result: ContractMethodReadResult = { + is_error: false, + result: { + names: [ 'address' ], + output: [ { type: 'address', value: '0x0000000000000000000000000000000000000000' } ], + }, + }; + const component = await mount( + + + , + ); + + await expect(component).toHaveScreenshot(); +}); diff --git a/ui/address/contract/ContractReadResult.tsx b/ui/address/contract/ContractReadResult.tsx index e72df96c92..9ed7f5f481 100644 --- a/ui/address/contract/ContractReadResult.tsx +++ b/ui/address/contract/ContractReadResult.tsx @@ -6,6 +6,15 @@ import type { SmartContractReadMethod } from 'types/api/contract'; import hexToUtf8 from 'lib/hexToUtf8'; +const ContractReadResultError = ({ children }: {children: React.ReactNode}) => { + return ( + + { children } + + ); + +}; + interface Props { item: SmartContractReadMethod; result: ContractMethodReadResult; @@ -20,19 +29,27 @@ const ContractReadResult = ({ item, result, onSettle }: Props) => { }, [ onSettle ]); if ('status' in result) { - return { result.statusText }; + return { result.statusText }; } if (result.is_error) { - const message = 'error' in result.result ? result.result.error : result.result.message; - const decoded = 'raw' in result.result && result.result.raw ? `\nRevert reason: ${ hexToUtf8(result.result.raw) }` : ''; - - return ( - - { message } - { decoded } - - ); + if ('error' in result.result) { + return { result.result.error }; + } + + if ('message' in result.result) { + return [{ result.result.code }] { result.result.message }; + } + + if ('raw' in result.result) { + return { `Revert reason: ${ hexToUtf8(result.result.raw) }` }; + } + + if ('method_id' in result.result) { + return { JSON.stringify(result.result, undefined, 2) }; + } + + return Something went wrong.; } return ( diff --git a/ui/address/contract/__screenshots__/ContractRead.pw.tsx_default_error-result-1.png b/ui/address/contract/__screenshots__/ContractRead.pw.tsx_default_error-result-1.png deleted file mode 100644 index 66552cc739..0000000000 Binary files a/ui/address/contract/__screenshots__/ContractRead.pw.tsx_default_error-result-1.png and /dev/null differ diff --git a/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_complex-error-1.png b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_complex-error-1.png new file mode 100644 index 0000000000..80741a58e9 Binary files /dev/null and b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_complex-error-1.png differ diff --git a/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_default-error-1.png b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_default-error-1.png new file mode 100644 index 0000000000..31c2a142c1 Binary files /dev/null and b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_default-error-1.png differ diff --git a/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_error-with-code-1.png b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_error-with-code-1.png new file mode 100644 index 0000000000..680fbde860 Binary files /dev/null and b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_error-with-code-1.png differ diff --git a/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_raw-error-1.png b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_raw-error-1.png new file mode 100644 index 0000000000..dfbf8524ab Binary files /dev/null and b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_raw-error-1.png differ diff --git a/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_success-1.png b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_success-1.png new file mode 100644 index 0000000000..9ed2619edd Binary files /dev/null and b/ui/address/contract/__screenshots__/ContractReadResult.pw.tsx_default_success-1.png differ