Skip to content

Commit

Permalink
refactor(idea/frontend): update test-balance and meta requests (#1679)
Browse files Browse the repository at this point in the history
  • Loading branch information
vraja-nayaka authored Dec 2, 2024
1 parent 08f3f25 commit 0e891be
Show file tree
Hide file tree
Showing 29 changed files with 125 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CI-CD-k8s-gear-js-stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/CI-CD-k8s-release-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion idea/frontend/.env.example
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
7 changes: 4 additions & 3 deletions idea/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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} \
Expand Down
3 changes: 1 addition & 2 deletions idea/frontend/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
3 changes: 0 additions & 3 deletions idea/frontend/src/api/metadata/index.ts

This file was deleted.

10 changes: 0 additions & 10 deletions idea/frontend/src/api/metadata/requests.ts

This file was deleted.

8 changes: 2 additions & 6 deletions idea/frontend/src/app/providers/chain/Provider.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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]);

Expand Down
20 changes: 6 additions & 14 deletions idea/frontend/src/features/balance/api.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>(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 };
3 changes: 3 additions & 0 deletions idea/frontend/src/features/balance/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const FAUCET_API_URL = import.meta.env.VITE_FAUCET_API_URL as string;

export { FAUCET_API_URL };
3 changes: 1 addition & 2 deletions idea/frontend/src/features/balance/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ProgramBalance, BalanceDropdown, TransferBalanceModal } from './ui';
import { getIsTestBalanceAvailable } from './api';

export { ProgramBalance, BalanceDropdown, getIsTestBalanceAvailable, TransferBalanceModal };
export { ProgramBalance, BalanceDropdown, TransferBalanceModal };
4 changes: 2 additions & 2 deletions idea/frontend/src/features/dns/consts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
14 changes: 5 additions & 9 deletions idea/frontend/src/features/dns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DnsResponse>(url.toString(), { method });
return fetchWithGuard<DnsResponse>(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<Dns>(url.toString(), { method });
return fetchWithGuard<Dns>(url, 'GET');
};

const getNextPageParam = (lastPage: DnsResponse, allPages: DnsResponse[]) => {
Expand Down
1 change: 1 addition & 0 deletions idea/frontend/src/features/metadata/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { addMetadata } from './requests';
17 changes: 17 additions & 0 deletions idea/frontend/src/features/metadata/api/requests.ts
Original file line number Diff line number Diff line change
@@ -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<GetMetaResponse>(url, 'GET').then(({ hex }) => ({ result: { hex } }));
};

const addMetadata = (hash: HexString, hex: HexString) =>
fetchWithGuard(`${METADATA_STORAGE_API_URL}/meta`, 'POST', { hash, hex });

export { addMetadata, fetchMetadata };
9 changes: 9 additions & 0 deletions idea/frontend/src/features/metadata/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { HexString } from '@gear-js/api';

type GetMetaResponse = {
hash: string;
hex: HexString;
hasState: boolean;
};

export type { GetMetaResponse };
5 changes: 5 additions & 0 deletions idea/frontend/src/features/metadata/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const errorMessage = {
metadataNotFound: 'MetadataNotFound',
};

export { errorMessage };
7 changes: 4 additions & 3 deletions idea/frontend/src/features/metadata/hooks/use-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]);
Expand Down
12 changes: 11 additions & 1 deletion idea/frontend/src/features/metadata/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};
8 changes: 5 additions & 3 deletions idea/frontend/src/features/sails/api/consts.ts
Original file line number Diff line number Diff line change
@@ -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 };
17 changes: 13 additions & 4 deletions idea/frontend/src/features/sails/api/requests.ts
Original file line number Diff line number Diff line change
@@ -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<string>(METHOD.GET_SAILS, { codeId });
const getIdl = (codeId: HexString) => {
const url = new URL(`${METADATA_STORAGE_API_URL}/sails`);
url.searchParams.append('codeId', codeId);

return fetchWithGuard<GetIdlResponse>(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<PaginationResponse<EventType>>(METHOD.GET_EVENTS, parameters);
Expand Down
7 changes: 6 additions & 1 deletion idea/frontend/src/features/sails/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
9 changes: 4 additions & 5 deletions idea/frontend/src/features/sails/hooks/use-sails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion idea/frontend/src/hooks/use-add-metadata.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions idea/frontend/src/shared/config/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 0e891be

Please sign in to comment.