Skip to content
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
2 changes: 1 addition & 1 deletion icons/advanced-filter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions icons/files/csv.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions lib/api/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export const RESOURCES = {
userOps: USER_OPS_API_RESOURCES,
visualize: VISUALIZE_API_RESOURCES,
zetachain: ZETA_CHAIN_API_RESOURCES,
// external API resources
// there is no type definition for them, use valibot to parse the response
external: {
gas_hawk_saving_potential: {
path: '/api/v2/gas-hawk-saving-potential',
},
safe_transaction_api: {
path: '',
},
},
} satisfies Record<ApiName, Record<string, ApiResource>>;

export const resourceKey = (x: ResourceName) => x;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ApiName =
'general' | 'admin' | 'bens' | 'contractInfo' | 'clusters' |
'general' | 'admin' | 'bens' | 'contractInfo' | 'clusters' | 'external' |
'metadata' | 'multichain' | 'rewards' | 'stats' | 'tac' |
'userOps' | 'visualize' | 'zetachain';

Expand Down
6 changes: 3 additions & 3 deletions lib/api/useQueryClientConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ export default function useQueryClientConfig() {
return false;
}

const EXTERNAL_API_RESOURCES: Array<ResourceName | 'safe_transaction_api' | 'gas_hawk_saving_potential'> = [
const EXTERNAL_API_RESOURCES: Array<ResourceName> = [
'general:contract_solidity_scan_report',
'general:address_xstar_score',
'general:address_3rd_party_info',
'general:noves_transaction',
'general:noves_address_history',
'general:noves_describe_txs',
// these resources are not proxied by the backend
'safe_transaction_api',
'gas_hawk_saving_potential',
'external:safe_transaction_api',
'external:gas_hawk_saving_potential',
];
const isExternalApiResource = EXTERNAL_API_RESOURCES.some((resource) => query.queryKey[0] === resource);

Expand Down
2 changes: 1 addition & 1 deletion lib/hooks/useIsSafeAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function useIsSafeAddress(hash: string | undefined): boolean {
const fetch = useFetch();

const { data } = useQuery({
queryKey: [ 'safe_transaction_api', hash ],
queryKey: [ 'external:safe_transaction_api', hash ],
queryFn: async() => {
if (!feature.isEnabled || !hash) {
return Promise.reject();
Expand Down
38 changes: 29 additions & 9 deletions pages/api/monitoring/invalid-api-schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import * as v from 'valibot';

import metrics from 'lib/monitoring/metrics';
import type { ApiName } from 'lib/api/types';

import { httpLogger } from 'nextjs/utils/logger';

const PayloadSchema = v.object({
resource: v.string(),
url: v.string(),
});
import { RESOURCES } from 'lib/api/resources';
import getErrorMessage from 'lib/errors/getErrorMessage';
import metrics from 'lib/monitoring/metrics';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const payload = JSON.parse(req.body);
metrics?.invalidApiSchema.inc(v.parse(PayloadSchema, payload));
} catch (error) {}
const payload: { resource?: string; url?: string } = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;

if (!payload.resource) {
throw new Error('Resource not found');
}

const [ apiName, resourceName ] = payload.resource.split(':');
const api = RESOURCES[apiName as ApiName];
const resource = api?.[resourceName as keyof typeof api];

if (!resource) {
throw new Error(`Resource not found: ${ payload.resource }`);
}

const url = payload.url ? new URL(payload.url) : undefined;

metrics?.invalidApiSchema.inc({
resource: payload.resource,
url: url?.toString(),
});
} catch (error) {
httpLogger.logger.error({ message: 'Unable to process invalid API schema', error: getErrorMessage(error) || 'Unknown error' });
}
res.status(200).json({ status: 'ok' });
}
13 changes: 7 additions & 6 deletions ui/address/details/AddressSaveOnGas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import config from 'configs/app';
import { Image } from 'toolkit/chakra/image';
import { Link } from 'toolkit/chakra/link';
import { Skeleton } from 'toolkit/chakra/skeleton';
import { HEX_REGEXP_WITH_0X } from 'toolkit/utils/regexp';
import TextSeparator from 'ui/shared/TextSeparator';

const feature = config.features.saveOnGas;
Expand All @@ -26,9 +27,9 @@ const AddressSaveOnGas = ({ gasUsed, address }: Props) => {
const gasUsedNumber = Number(gasUsed);

const query = useQuery({
queryKey: [ 'gas_hawk_saving_potential', { address } ],
queryKey: [ 'external:gas_hawk_saving_potential', { address } ],
queryFn: async() => {
if (!feature.isEnabled) {
if (!feature.isEnabled || !HEX_REGEXP_WITH_0X.test(address)) {
return;
}

Expand All @@ -40,7 +41,7 @@ const AddressSaveOnGas = ({ gasUsed, address }: Props) => {
const parsedResponse = v.safeParse(responseSchema, response);

if (!parsedResponse.success) {
throw Error('Invalid response schema');
throw Error(ERROR_NAME);
}

return parsedResponse.output;
Expand All @@ -52,12 +53,12 @@ const AddressSaveOnGas = ({ gasUsed, address }: Props) => {
const errorMessage = query.error && 'message' in query.error ? query.error.message : undefined;

React.useEffect(() => {
if (errorMessage === ERROR_NAME) {
if (feature.isEnabled && ERROR_NAME === errorMessage) {
fetch('/node-api/monitoring/invalid-api-schema', {
method: 'POST',
body: JSON.stringify({
resource: 'gas_hawk_saving_potential',
url: feature.isEnabled ? feature.apiUrlTemplate.replace('<address>', address) : undefined,
resource: 'external:gas_hawk_saving_potential',
url: feature.isEnabled && HEX_REGEXP_WITH_0X.test(address) ? feature.apiUrlTemplate.replace('<address>', address) : undefined,
}),
});
}
Expand Down
2 changes: 1 addition & 1 deletion ui/txs/noves/useDescribeTxs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function useDescribeTxs(items: Array<Transaction> | undefined, vi
};

const describeQuery = useQuery({
queryKey: [ 'noves_describe_txs', queryKey ],
queryKey: [ 'general:noves_describe_txs', queryKey ],
queryFn: async() => {
const queries = txChunks.map((hashes) => {
if (hashes.length === 0) {
Expand Down
Loading