From 0e891be3cbdeb9121abb0fa961af7071c8a9164b Mon Sep 17 00:00:00 2001 From: Vadim Tokar <52737608+vraja-nayaka@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:03:16 +0400 Subject: [PATCH] refactor(idea/frontend): update test-balance and meta requests (#1679) --- .github/workflows/CI-CD-k8s-gear-js-stage.yml | 3 ++- .../workflows/CI-CD-k8s-release-deploy.yml | 3 ++- idea/frontend/.env.example | 3 ++- idea/frontend/Dockerfile | 7 ++++--- idea/frontend/src/api/index.ts | 3 +-- idea/frontend/src/api/metadata/index.ts | 3 --- idea/frontend/src/api/metadata/requests.ts | 10 ---------- .../src/app/providers/chain/Provider.tsx | 8 ++------ idea/frontend/src/features/balance/api.ts | 20 ++++++------------- idea/frontend/src/features/balance/consts.ts | 3 +++ idea/frontend/src/features/balance/index.ts | 3 +-- .../frontend/src/features/dns/consts/index.ts | 4 ++-- idea/frontend/src/features/dns/utils.ts | 14 +++++-------- .../src/features/metadata/api/index.ts | 1 + .../src/features/metadata/api/requests.ts | 17 ++++++++++++++++ .../src/features/metadata/api/types.ts | 9 +++++++++ idea/frontend/src/features/metadata/consts.ts | 5 +++++ .../features/metadata/hooks/use-metadata.ts | 7 ++++--- idea/frontend/src/features/metadata/index.ts | 12 ++++++++++- .../frontend/src/features/sails/api/consts.ts | 8 +++++--- .../src/features/sails/api/requests.ts | 17 ++++++++++++---- idea/frontend/src/features/sails/api/types.ts | 7 ++++++- .../src/features/sails/hooks/use-sails.ts | 9 ++++----- idea/frontend/src/hooks/use-add-metadata.ts | 2 +- idea/frontend/src/shared/config/consts.ts | 4 ++-- idea/frontend/src/shared/helpers/index.ts | 16 ++++++++++++--- .../src/shared/services/rpcService/index.ts | 4 ++-- .../services/rpcService/model/RPCService.ts | 5 ++--- .../services/rpcService/model/consts.ts | 1 - 29 files changed, 125 insertions(+), 83 deletions(-) delete mode 100644 idea/frontend/src/api/metadata/index.ts delete mode 100644 idea/frontend/src/api/metadata/requests.ts create mode 100644 idea/frontend/src/features/balance/consts.ts create mode 100644 idea/frontend/src/features/metadata/api/index.ts create mode 100644 idea/frontend/src/features/metadata/api/requests.ts create mode 100644 idea/frontend/src/features/metadata/api/types.ts create mode 100644 idea/frontend/src/features/metadata/consts.ts diff --git a/.github/workflows/CI-CD-k8s-gear-js-stage.yml b/.github/workflows/CI-CD-k8s-gear-js-stage.yml index 4ea6b8d54a..5271b4a187 100644 --- a/.github/workflows/CI-CD-k8s-gear-js-stage.yml +++ b/.github/workflows/CI-CD-k8s-gear-js-stage.yml @@ -49,10 +49,11 @@ jobs: tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend:qa build-args: | VITE_NODE_ADDRESS=${{ secrets.REACT_APP_NODE_ADDRESS }} - VITE_API_URL=${{ secrets.REACT_APP_API_URL }} VITE_DEFAULT_TRANSFER_BALANCE_VALUE=${{ secrets.REACT_APP_DEFAULT_TRANSFER_BALANCE_VALUE }} VITE_HCAPTCHA_SITE_KEY=${{ secrets.REACT_APP_HCAPTCHA_SITE_KEY }} VITE_NODES_API_URL= ${{ secrets.REACT_APP_DEFAULT_NODES_URL }} + VITE_FAUCET_API_URL= ${{ secrets.VITE_FAUCET_API_URL }} + VITE_METADATA_STORAGE_API_URL= ${{ secrets.VITE_METADATA_STORAGE_API_URL }} VITE_MAINNET_VOUCHERS_API_URL=${{ secrets.VITE_MAINNET_VOUCHERS_API_URL }} VITE_TESTNET_VOUCHERS_API_URL=${{ secrets.VITE_TESTNET_VOUCHERS_API_URL }} VITE_INDEXER_API_URL=${{ secrets.VITE_INDEXER_API_URL }} diff --git a/.github/workflows/CI-CD-k8s-release-deploy.yml b/.github/workflows/CI-CD-k8s-release-deploy.yml index bcb190ccc1..5a16eadeda 100644 --- a/.github/workflows/CI-CD-k8s-release-deploy.yml +++ b/.github/workflows/CI-CD-k8s-release-deploy.yml @@ -60,10 +60,11 @@ jobs: tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend:prod build-args: | VITE_NODE_ADDRESS=${{ secrets.REACT_APP_NODE_ADDRESS }} - VITE_API_URL=${{ secrets.REACT_APP_API_URL }} VITE_DEFAULT_TRANSFER_BALANCE_VALUE=${{ secrets.REACT_APP_DEFAULT_TRANSFER_BALANCE_VALUE }} VITE_HCAPTCHA_SITE_KEY=${{ secrets.REACT_APP_HCAPTCHA_SITE_KEY }} VITE_NODES_API_URL=${{ secrets.REACT_APP_DEFAULT_NODES_URL }} + VITE_FAUCET_API_URL=${{ secrets.VITE_FAUCET_API_URL }} + VITE_METADATA_STORAGE_API_URL=${{ secrets.VITE_METADATA_STORAGE_API_URL }} VITE_GTM_ID=${{ secrets.VITE_GTM_ID}} VITE_MAINNET_VOUCHERS_API_URL=${{ secrets.VITE_MAINNET_VOUCHERS_API_URL }} VITE_TESTNET_VOUCHERS_API_URL=${{ secrets.VITE_TESTNET_VOUCHERS_API_URL }} diff --git a/idea/frontend/.env.example b/idea/frontend/.env.example index acd7362ac8..9e09aa27ce 100644 --- a/idea/frontend/.env.example +++ b/idea/frontend/.env.example @@ -1,6 +1,7 @@ -VITE_API_URL= VITE_INDEXER_API_URL= VITE_NODES_API_URL= +VITE_FAUCET_API_URL= +VITE_METADATA_STORAGE_API_URL= VITE_NODE_ADDRESS= VITE_HCAPTCHA_SITE_KEY= VITE_GTM_ID= diff --git a/idea/frontend/Dockerfile b/idea/frontend/Dockerfile index 99f8f1c0a9..c025ee1f01 100644 --- a/idea/frontend/Dockerfile +++ b/idea/frontend/Dockerfile @@ -16,8 +16,9 @@ COPY ./utils/wallet-connect ./utils/wallet-connect RUN yarn install --mode=skip-build ARG VITE_NODE_ADDRESS \ - VITE_API_URL \ VITE_NODES_API_URL \ + VITE_FAUCET_API_URL \ + VITE_METADATA_STORAGE_API_URL \ VITE_DEFAULT_TRANSFER_BALANCE_VALUE \ VITE_HCAPTCHA_SITE_KEY \ VITE_GTM_ID \ @@ -28,9 +29,9 @@ ARG VITE_NODE_ADDRESS \ VITE_TESTNET_DNS_API_URL ENV VITE_NODE_ADDRESS=${VITE_NODE_ADDRESS} \ - VITE_VOUCHERS_API_URL=${VITE_VOUCHERS_API_URL} \ - VITE_API_URL=${VITE_API_URL} \ VITE_NODES_API_URL=${VITE_NODES_API_URL} \ + VITE_FAUCET_API_URL=${VITE_FAUCET_API_URL} \ + VITE_METADATA_STORAGE_API_URL=${VITE_METADATA_STORAGE_API_URL} \ VITE_DEFAULT_TRANSFER_BALANCE_VALUE=${VITE_DEFAULT_TRANSFER_BALANCE_VALUE} \ VITE_HCAPTCHA_SITE_KEY=${VITE_HCAPTCHA_SITE_KEY} \ VITE_GTM_ID=${VITE_GTM_ID} \ diff --git a/idea/frontend/src/api/index.ts b/idea/frontend/src/api/index.ts index 17164aad04..56cf120834 100644 --- a/idea/frontend/src/api/index.ts +++ b/idea/frontend/src/api/index.ts @@ -1,12 +1,11 @@ import { NodeSection } from '@/entities/node'; import { NODES_API_URL } from '@/shared/config'; -import { fetchMetadata, addMetadata } from './metadata'; import { PaginationParameters, PaginationResponse } from './types'; import { INFINITE_QUERY } from './consts'; const getNodes = () => fetch(NODES_API_URL).then((result) => result.json() as unknown as NodeSection[]); -export { INFINITE_QUERY, getNodes, addMetadata, fetchMetadata }; +export { INFINITE_QUERY, getNodes }; export type { PaginationParameters, PaginationResponse }; diff --git a/idea/frontend/src/api/metadata/index.ts b/idea/frontend/src/api/metadata/index.ts deleted file mode 100644 index a12ca05980..0000000000 --- a/idea/frontend/src/api/metadata/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { addMetadata, fetchMetadata } from './requests'; - -export { addMetadata, fetchMetadata }; diff --git a/idea/frontend/src/api/metadata/requests.ts b/idea/frontend/src/api/metadata/requests.ts deleted file mode 100644 index 20e0761796..0000000000 --- a/idea/frontend/src/api/metadata/requests.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { HexString } from '@polkadot/util/types'; - -import { rpcService } from '@/shared/services/rpcService'; -import { RpcMethods } from '@/shared/config'; -import { IMeta } from '@/entities/metadata'; - -const addMetadata = (hash: HexString, hex: HexString) => rpcService.callRPC(RpcMethods.AddMetadata, { hash, hex }); -const fetchMetadata = (hash: HexString) => rpcService.callRPC(RpcMethods.GetMetadata, { hash }); - -export { addMetadata, fetchMetadata }; diff --git a/idea/frontend/src/app/providers/chain/Provider.tsx b/idea/frontend/src/app/providers/chain/Provider.tsx index 6ca9606171..103cb157a4 100644 --- a/idea/frontend/src/app/providers/chain/Provider.tsx +++ b/idea/frontend/src/app/providers/chain/Provider.tsx @@ -1,7 +1,6 @@ import { ProviderProps, useApi } from '@gear-js/react-hooks'; import { useEffect, useState } from 'react'; -import { getIsTestBalanceAvailable } from '@/features/balance'; import { GENESIS } from '@/shared/config'; import { ChainContext } from './Context'; @@ -21,11 +20,8 @@ const ChainProvider = ({ children }: ProviderProps) => { if (isDevChain === undefined) return; - if (isDevChain) { - setIsTestBalanceAvailable(true); - } else { - getIsTestBalanceAvailable(genesis!).then(({ result }) => setIsTestBalanceAvailable(result)); - } + setIsTestBalanceAvailable(isDevChain || genesis === GENESIS.TESTNET); + // eslint-disable-next-line react-hooks/exhaustive-deps }, [isDevChain]); diff --git a/idea/frontend/src/features/balance/api.ts b/idea/frontend/src/features/balance/api.ts index b6947860fb..1f505501a3 100644 --- a/idea/frontend/src/features/balance/api.ts +++ b/idea/frontend/src/features/balance/api.ts @@ -1,20 +1,12 @@ -import { HexString } from '@gear-js/api'; - -import { rpcService } from '@/shared/services/rpcService'; - -const METHOD = { - GET_TEST_BALANCE: 'testBalance.get', - IS_TEST_BALANCE_AVAILABLE: 'testBalance.available', -} as const; +import { GENESIS } from '@/shared/config'; +import { fetchWithGuard } from '@/shared/helpers'; +import { FAUCET_API_URL } from './consts'; type GetTestBalanceParameters = { token: string; address: string; }; -const getIsTestBalanceAvailable = (genesis: HexString) => - rpcService.callRPC(METHOD.IS_TEST_BALANCE_AVAILABLE, { genesis }); - -const getTestBalance = (params: GetTestBalanceParameters) => rpcService.callRPC(METHOD.GET_TEST_BALANCE, params); - -export { getIsTestBalanceAvailable, getTestBalance }; +const getTestBalance = ({ token, address }: GetTestBalanceParameters) => + fetchWithGuard(`${FAUCET_API_URL}/balance`, 'POST', { token, payload: { address, genesis: GENESIS.TESTNET } }); +export { getTestBalance }; diff --git a/idea/frontend/src/features/balance/consts.ts b/idea/frontend/src/features/balance/consts.ts new file mode 100644 index 0000000000..5837c8cf59 --- /dev/null +++ b/idea/frontend/src/features/balance/consts.ts @@ -0,0 +1,3 @@ +const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API_URL as string; + +export { FAUCET_API_URL }; diff --git a/idea/frontend/src/features/balance/index.ts b/idea/frontend/src/features/balance/index.ts index d0f579ac59..0367802fd1 100644 --- a/idea/frontend/src/features/balance/index.ts +++ b/idea/frontend/src/features/balance/index.ts @@ -1,4 +1,3 @@ import { ProgramBalance, BalanceDropdown, TransferBalanceModal } from './ui'; -import { getIsTestBalanceAvailable } from './api'; -export { ProgramBalance, BalanceDropdown, getIsTestBalanceAvailable, TransferBalanceModal }; +export { ProgramBalance, BalanceDropdown, TransferBalanceModal }; diff --git a/idea/frontend/src/features/dns/consts/index.ts b/idea/frontend/src/features/dns/consts/index.ts index 65504f2836..0b3cc4a9ff 100644 --- a/idea/frontend/src/features/dns/consts/index.ts +++ b/idea/frontend/src/features/dns/consts/index.ts @@ -5,7 +5,7 @@ import { GENESIS } from '@/shared/config'; import { Values } from '../types'; import { Program } from './sails'; -const API_URL = { +const DNS_API_URL = { [GENESIS.MAINNET]: import.meta.env.VITE_MAINNET_DNS_API_URL as string, [GENESIS.TESTNET]: import.meta.env.VITE_TESTNET_DNS_API_URL as string, } as const; @@ -31,4 +31,4 @@ const FUNCTION_NAME = { REMOVE_ADMIN: 'removeAdminFromProgram', } as const; -export { API_URL, FIELD_NAME, DEFAULT_VALUES, NAME_SCHEMA, FUNCTION_NAME, Program }; +export { DNS_API_URL, FIELD_NAME, DEFAULT_VALUES, NAME_SCHEMA, FUNCTION_NAME, Program }; diff --git a/idea/frontend/src/features/dns/utils.ts b/idea/frontend/src/features/dns/utils.ts index af1092e9fc..6c35fb5f21 100644 --- a/idea/frontend/src/features/dns/utils.ts +++ b/idea/frontend/src/features/dns/utils.ts @@ -4,35 +4,31 @@ import { DEFAULT_LIMIT } from '@/shared/config'; import { fetchWithGuard } from '@/shared/helpers'; import { Dns, DnsParams, DnsResponse } from './types'; -import { API_URL } from './consts'; +import { DNS_API_URL } from './consts'; type Genesis = { genesis: HexString; }; -const getUrl = (genesis: HexString, path: string) => `${API_URL[genesis as keyof typeof API_URL]}/${path}`; +const getUrl = (genesis: HexString, path: string) => `${DNS_API_URL[genesis as keyof typeof DNS_API_URL]}/${path}`; const getDnsProgramId = async ({ genesis }: Genesis) => { - const method = 'GET'; - - return (await fetchWithGuard<{ contract: HexString }>(getUrl(genesis, 'dns/contract'), { method })).contract; + return (await fetchWithGuard<{ contract: HexString }>(getUrl(genesis, 'dns/contract'), 'GET')).contract; }; const getDns = ({ genesis, ...params }: DnsParams & Genesis) => { - const method = 'GET'; const url = new URL(getUrl(genesis, 'dns')); Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, String(value))); - return fetchWithGuard(url.toString(), { method }); + return fetchWithGuard(url, 'GET'); }; const getSingleDns = ({ genesis, ...params }: ({ address: HexString } | { name: string }) & Genesis) => { - const method = 'GET'; const [key, value] = Object.entries(params)[0]; const url = new URL(getUrl(genesis, `dns/by_${key}/${value}`)); - return fetchWithGuard(url.toString(), { method }); + return fetchWithGuard(url, 'GET'); }; const getNextPageParam = (lastPage: DnsResponse, allPages: DnsResponse[]) => { diff --git a/idea/frontend/src/features/metadata/api/index.ts b/idea/frontend/src/features/metadata/api/index.ts new file mode 100644 index 0000000000..33bf692f70 --- /dev/null +++ b/idea/frontend/src/features/metadata/api/index.ts @@ -0,0 +1 @@ +export { addMetadata } from './requests'; diff --git a/idea/frontend/src/features/metadata/api/requests.ts b/idea/frontend/src/features/metadata/api/requests.ts new file mode 100644 index 0000000000..ab2e92cd7b --- /dev/null +++ b/idea/frontend/src/features/metadata/api/requests.ts @@ -0,0 +1,17 @@ +import { HexString } from '@polkadot/util/types'; + +import { METADATA_STORAGE_API_URL } from '@/shared/config'; +import { fetchWithGuard } from '@/shared/helpers'; +import { GetMetaResponse } from './types'; + +const fetchMetadata = (hash: HexString) => { + const url = new URL(`${METADATA_STORAGE_API_URL}/meta`); + url.searchParams.append('hash', hash); + + return fetchWithGuard(url, 'GET').then(({ hex }) => ({ result: { hex } })); +}; + +const addMetadata = (hash: HexString, hex: HexString) => + fetchWithGuard(`${METADATA_STORAGE_API_URL}/meta`, 'POST', { hash, hex }); + +export { addMetadata, fetchMetadata }; diff --git a/idea/frontend/src/features/metadata/api/types.ts b/idea/frontend/src/features/metadata/api/types.ts new file mode 100644 index 0000000000..752a98962c --- /dev/null +++ b/idea/frontend/src/features/metadata/api/types.ts @@ -0,0 +1,9 @@ +import { HexString } from '@gear-js/api'; + +type GetMetaResponse = { + hash: string; + hex: HexString; + hasState: boolean; +}; + +export type { GetMetaResponse }; diff --git a/idea/frontend/src/features/metadata/consts.ts b/idea/frontend/src/features/metadata/consts.ts new file mode 100644 index 0000000000..a904d716cf --- /dev/null +++ b/idea/frontend/src/features/metadata/consts.ts @@ -0,0 +1,5 @@ +const errorMessage = { + metadataNotFound: 'MetadataNotFound', +}; + +export { errorMessage }; diff --git a/idea/frontend/src/features/metadata/hooks/use-metadata.ts b/idea/frontend/src/features/metadata/hooks/use-metadata.ts index 1c8752fbc8..5d3755f61e 100644 --- a/idea/frontend/src/features/metadata/hooks/use-metadata.ts +++ b/idea/frontend/src/features/metadata/hooks/use-metadata.ts @@ -2,11 +2,12 @@ import { HexString, ProgramMetadata } from '@gear-js/api'; import { useAlert } from '@gear-js/react-hooks'; import { useEffect, useMemo, useState } from 'react'; -import { fetchMetadata } from '@/api'; -import { RPCError, RPCErrorCode } from '@/shared/services/rpcService'; import { useChain } from '@/hooks'; import { getLocalMetadata } from '@/features/local-indexer'; +import { fetchMetadata } from '../api/requests'; +import { errorMessage } from '../consts'; + function useMetadata(hash?: HexString | null | undefined) { const alert = useAlert(); const { isDevChain } = useChain(); @@ -29,7 +30,7 @@ function useMetadata(hash?: HexString | null | undefined) { getMetadata(hash) .then(({ result }) => result.hex && setMetadataHex(result.hex)) - .catch(({ message, code }: RPCError) => code !== RPCErrorCode.MetadataNotFound && alert.error(message)) + .catch(({ message }) => message !== errorMessage.metadataNotFound && alert.error(message)) .finally(() => setIsMetadataReady(true)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [hash]); diff --git a/idea/frontend/src/features/metadata/index.ts b/idea/frontend/src/features/metadata/index.ts index 594c04af6c..c6f8f927a1 100644 --- a/idea/frontend/src/features/metadata/index.ts +++ b/idea/frontend/src/features/metadata/index.ts @@ -1,5 +1,15 @@ import { useMetadata, useMetadataHash, useMetadataWithFile } from './hooks'; import { MetadataTable, MetadataPreview } from './ui'; import { isHumanTypesRepr, isState } from './utils'; +import { addMetadata } from './api'; -export { useMetadata, useMetadataHash, useMetadataWithFile, MetadataTable, MetadataPreview, isHumanTypesRepr, isState }; +export { + useMetadata, + useMetadataHash, + useMetadataWithFile, + MetadataTable, + MetadataPreview, + isHumanTypesRepr, + isState, + addMetadata, +}; diff --git a/idea/frontend/src/features/sails/api/consts.ts b/idea/frontend/src/features/sails/api/consts.ts index 1e3c381278..12e4862e2d 100644 --- a/idea/frontend/src/features/sails/api/consts.ts +++ b/idea/frontend/src/features/sails/api/consts.ts @@ -1,8 +1,10 @@ const METHOD = { GET_EVENT: 'event.data', GET_EVENTS: 'event.all', - ADD_SAILS: 'sails.add', - GET_SAILS: 'sails.get', } as const; -export { METHOD }; +const errorMessage = { + sailsIdlNotFound: 'SailsIdlNotFound', +}; + +export { METHOD, errorMessage }; diff --git a/idea/frontend/src/features/sails/api/requests.ts b/idea/frontend/src/features/sails/api/requests.ts index d89ef7f77f..747facb4bd 100644 --- a/idea/frontend/src/features/sails/api/requests.ts +++ b/idea/frontend/src/features/sails/api/requests.ts @@ -1,13 +1,22 @@ import { HexString } from '@gear-js/api'; import { PaginationResponse } from '@/api'; -import { INDEXER_RPC_SERVICE, rpcService } from '@/shared/services/rpcService'; +import { METADATA_STORAGE_API_URL } from '@/shared/config'; +import { fetchWithGuard } from '@/shared/helpers'; +import { INDEXER_RPC_SERVICE } from '@/shared/services/rpcService'; -import { EventType, GetEventsParameters } from './types'; +import { EventType, GetEventsParameters, GetIdlResponse } from './types'; import { METHOD } from './consts'; -const addIdl = (codeId: HexString, data: string) => rpcService.callRPC(METHOD.ADD_SAILS, { codeId, data }); -const getIdl = (codeId: HexString) => rpcService.callRPC(METHOD.GET_SAILS, { codeId }); +const getIdl = (codeId: HexString) => { + const url = new URL(`${METADATA_STORAGE_API_URL}/sails`); + url.searchParams.append('codeId', codeId); + + return fetchWithGuard(url, 'GET'); +}; + +const addIdl = (codeId: HexString, data: string) => + fetchWithGuard(`${METADATA_STORAGE_API_URL}/sails`, 'POST', { codeId, data }); const getEvents = (parameters: GetEventsParameters) => INDEXER_RPC_SERVICE.callRPC>(METHOD.GET_EVENTS, parameters); diff --git a/idea/frontend/src/features/sails/api/types.ts b/idea/frontend/src/features/sails/api/types.ts index 592db16438..b2fc27fcaf 100644 --- a/idea/frontend/src/features/sails/api/types.ts +++ b/idea/frontend/src/features/sails/api/types.ts @@ -17,4 +17,9 @@ type EventType = IBase & { name?: string | null; }; -export type { GetEventsParameters, EventType }; +type GetIdlResponse = { + codeId: string; + data: string; +}; + +export type { GetEventsParameters, EventType, GetIdlResponse }; diff --git a/idea/frontend/src/features/sails/hooks/use-sails.ts b/idea/frontend/src/features/sails/hooks/use-sails.ts index ea47b8353a..17379c60cd 100644 --- a/idea/frontend/src/features/sails/hooks/use-sails.ts +++ b/idea/frontend/src/features/sails/hooks/use-sails.ts @@ -3,10 +3,9 @@ import { useAlert } from '@gear-js/react-hooks'; import { useQuery } from '@tanstack/react-query'; import { useEffect } from 'react'; -import { RPCError, RPCErrorCode } from '@/shared/services/rpcService'; - import { getIdl } from '../api'; import { useSailsInit } from './use-sails-init'; +import { errorMessage } from '../api/consts'; function useSails(codeId: HexString | null | undefined) { const sails = useSailsInit(); @@ -16,9 +15,9 @@ function useSails(codeId: HexString | null | undefined) { if (!sails) throw new Error('Sails is not initialized'); if (!codeId) throw new Error('Code ID is not found'); - const { result } = await getIdl(codeId); + const { data } = await getIdl(codeId); - return sails.parseIdl(result); + return sails.parseIdl(data); }; const { data, isPending, error, refetch } = useQuery({ @@ -29,7 +28,7 @@ function useSails(codeId: HexString | null | undefined) { useEffect(() => { if (!error) return; - if (error instanceof RPCError && error.code === RPCErrorCode.MetadataNotFound) return; + if (error.message === errorMessage.sailsIdlNotFound) return; alert.error(error.message); // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/idea/frontend/src/hooks/use-add-metadata.ts b/idea/frontend/src/hooks/use-add-metadata.ts index 2937ffccde..c4a15eff4f 100644 --- a/idea/frontend/src/hooks/use-add-metadata.ts +++ b/idea/frontend/src/hooks/use-add-metadata.ts @@ -1,7 +1,7 @@ import { HexString } from '@gear-js/api'; import { useAlert } from '@gear-js/react-hooks'; -import { addMetadata as addStorageMetadata } from '@/api'; +import { addMetadata as addStorageMetadata } from '@/features/metadata'; import { addLocalMetadata } from '@/features/local-indexer'; import { getErrorMessage } from '@/shared/helpers'; diff --git a/idea/frontend/src/shared/config/consts.ts b/idea/frontend/src/shared/config/consts.ts index 5c78edd4be..60a1a66321 100644 --- a/idea/frontend/src/shared/config/consts.ts +++ b/idea/frontend/src/shared/config/consts.ts @@ -3,9 +3,9 @@ import { z } from 'zod'; import { isAccountAddressValid } from '../helpers'; -const API_URL = import.meta.env.VITE_API_URL as string; const INDEXER_API_URL = import.meta.env.VITE_INDEXER_API_URL as string; const NODES_API_URL = import.meta.env.VITE_NODES_API_URL as string; +const METADATA_STORAGE_API_URL = import.meta.env.VITE_METADATA_STORAGE_API_URL as string; const NODE_ADDRESS = import.meta.env.VITE_NODE_ADDRESS as string; const HCAPTCHA_SITE_KEY = import.meta.env.VITE_HCAPTCHA_SITE_KEY as string; const GTM_ID = import.meta.env.VITE_GTM_ID as string | undefined; @@ -96,9 +96,9 @@ const ACCOUNT_ADDRESS_SCHEMA = z .transform((value) => decodeAddress(value)); export { - API_URL, INDEXER_API_URL, NODES_API_URL, + METADATA_STORAGE_API_URL, NODE_ADDRESS, HCAPTCHA_SITE_KEY, GTM_ID, diff --git a/idea/frontend/src/shared/helpers/index.ts b/idea/frontend/src/shared/helpers/index.ts index 34116fdd8e..fc9968752f 100644 --- a/idea/frontend/src/shared/helpers/index.ts +++ b/idea/frontend/src/shared/helpers/index.ts @@ -125,10 +125,20 @@ const isHex = (value: unknown): value is HexString => { return isString(value) && (value === '0x' || (HEX_REGEX.test(value) && value.length % 2 === 0)); }; -const fetchWithGuard = async (...args: Parameters) => { - const response = await fetch(...args); +const fetchWithGuard = async (url: URL | string, method: 'GET' | 'POST', params?: object) => { + const request = { method } as RequestInit; - if (!response.ok) throw new Error(response.statusText); + if (method === 'POST') { + request.headers = { 'Content-Type': 'application/json;charset=utf-8' }; + request.body = JSON.stringify(params); + } + + const response = await fetch(url, request); + + if (!response.ok) { + const result = await response.json().catch(() => ({})); + throw new Error('error' in result ? result.error : response.statusText); + } return response.json() as T; }; diff --git a/idea/frontend/src/shared/services/rpcService/index.ts b/idea/frontend/src/shared/services/rpcService/index.ts index 6288d8e574..ff94cfa75f 100644 --- a/idea/frontend/src/shared/services/rpcService/index.ts +++ b/idea/frontend/src/shared/services/rpcService/index.ts @@ -1,7 +1,7 @@ import { RPCRequest, RPCResponse, RPCErrorResponse, RPCSuccessResponse } from './model/types'; import { RPC_VERSION, RPCErrorCode } from './model/consts'; import { RPCError } from './model/RPCError'; -import { rpcService, INDEXER_RPC_SERVICE } from './model/RPCService'; +import { INDEXER_RPC_SERVICE } from './model/RPCService'; -export { rpcService, INDEXER_RPC_SERVICE, RPCError, RPCErrorCode, RPC_VERSION }; +export { INDEXER_RPC_SERVICE, RPCError, RPCErrorCode, RPC_VERSION }; export type { RPCRequest, RPCResponse, RPCErrorResponse, RPCSuccessResponse }; diff --git a/idea/frontend/src/shared/services/rpcService/model/RPCService.ts b/idea/frontend/src/shared/services/rpcService/model/RPCService.ts index 2bdc4d63fb..f53a04363c 100644 --- a/idea/frontend/src/shared/services/rpcService/model/RPCService.ts +++ b/idea/frontend/src/shared/services/rpcService/model/RPCService.ts @@ -1,7 +1,7 @@ import ky, { Options } from 'ky'; import { generateRandomId } from '@/shared/helpers'; -import { API_URL, INDEXER_API_URL, LocalStorage } from '@/shared/config'; +import { INDEXER_API_URL, LocalStorage } from '@/shared/config'; import { RPCError } from './RPCError'; import { RPCRequest, RPCResponse, RPCSuccessResponse } from './types'; @@ -48,7 +48,6 @@ class RPCService { } } -const rpcService = new RPCService(API_URL); const INDEXER_RPC_SERVICE = new RPCService(INDEXER_API_URL); -export { rpcService, INDEXER_RPC_SERVICE }; +export { INDEXER_RPC_SERVICE }; diff --git a/idea/frontend/src/shared/services/rpcService/model/consts.ts b/idea/frontend/src/shared/services/rpcService/model/consts.ts index 99cd36861f..cbfb33ac61 100644 --- a/idea/frontend/src/shared/services/rpcService/model/consts.ts +++ b/idea/frontend/src/shared/services/rpcService/model/consts.ts @@ -2,7 +2,6 @@ enum RPCErrorCode { ParseError = -32700, InvalidRequest = -32600, MethodNotFound = -32601, - MetadataNotFound = -32404, InvalidParams = -32602, InternalError = -32603, }