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

Read verified contract: error of query execution is not displayed #1190

Merged
merged 1 commit into from
Sep 19, 2023
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
7 changes: 6 additions & 1 deletion types/api/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }>;
};
}

Expand Down
26 changes: 0 additions & 26 deletions ui/address/contract/ContractRead.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<TestApp>
<ContractRead addressHash={ addressHash }/>
</TestApp>,
{ 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();
});
101 changes: 101 additions & 0 deletions ui/address/contract/ContractReadResult.pw.tsx
Original file line number Diff line number Diff line change
@@ -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(
<TestApp>
<ContractReadResult item={ item } onSettle={ onSettle } result={ result }/>
</TestApp>,
);

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(
<TestApp>
<ContractReadResult item={ item } onSettle={ onSettle } result={ result }/>
</TestApp>,
);

await expect(component).toHaveScreenshot();
});

test('raw error', async({ mount }) => {
const result: ContractMethodReadResult = {
is_error: true,
result: {
raw: '49276d20616c7761797320726576657274696e67207769746820616e206572726f72',
},
};
const component = await mount(
<TestApp>
<ContractReadResult item={ item } onSettle={ onSettle } result={ result }/>
</TestApp>,
);

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(
<TestApp>
<ContractReadResult item={ item } onSettle={ onSettle } result={ result }/>
</TestApp>,
);

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(
<TestApp>
<ContractReadResult item={ item } onSettle={ onSettle } result={ result }/>
</TestApp>,
);

await expect(component).toHaveScreenshot();
});
37 changes: 27 additions & 10 deletions ui/address/contract/ContractReadResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import type { SmartContractReadMethod } from 'types/api/contract';

import hexToUtf8 from 'lib/hexToUtf8';

const ContractReadResultError = ({ children }: {children: React.ReactNode}) => {
return (
<Alert status="error" mt={ 3 } p={ 4 } borderRadius="md" fontSize="sm" wordBreak="break-word" whiteSpace="pre-wrap">
{ children }
</Alert>
);

};

interface Props {
item: SmartContractReadMethod;
result: ContractMethodReadResult;
Expand All @@ -20,19 +29,27 @@ const ContractReadResult = ({ item, result, onSettle }: Props) => {
}, [ onSettle ]);

if ('status' in result) {
return <Alert status="error" mt={ 3 } p={ 4 } borderRadius="md" fontSize="sm" wordBreak="break-word">{ result.statusText }</Alert>;
return <ContractReadResultError>{ result.statusText }</ContractReadResultError>;
}

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 (
<Alert status="error" mt={ 3 } p={ 4 } borderRadius="md" fontSize="sm" wordBreak="break-word" whiteSpace="pre-wrap">
{ message }
{ decoded }
</Alert>
);
if ('error' in result.result) {
return <ContractReadResultError>{ result.result.error }</ContractReadResultError>;
}

if ('message' in result.result) {
return <ContractReadResultError>[{ result.result.code }] { result.result.message }</ContractReadResultError>;
}

if ('raw' in result.result) {
return <ContractReadResultError>{ `Revert reason: ${ hexToUtf8(result.result.raw) }` }</ContractReadResultError>;
}

if ('method_id' in result.result) {
return <ContractReadResultError>{ JSON.stringify(result.result, undefined, 2) }</ContractReadResultError>;
}

return <ContractReadResultError>Something went wrong.</ContractReadResultError>;
}

return (
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.