From fd3a436a317bf093fdbc05b79edab89a377178e8 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 22 Jul 2020 12:20:55 +0200 Subject: [PATCH] more config refactor --- app.config.js | 13 ++++------- src/components/organisms/AssetList.tsx | 7 +++--- src/components/pages/Home.tsx | 10 ++++----- src/components/pages/Publish/index.tsx | 3 +-- src/components/templates/AssetDetails.tsx | 9 +++----- src/components/templates/Search/utils.ts | 5 +++-- src/hooks/useSiteMetadata.ts | 9 -------- src/utils/getConfig.ts | 5 +++++ src/utils/getFromFaucet.ts | 27 ----------------------- tests/unit/utils/getFromFaucet.test.ts | 15 ------------- 10 files changed, 24 insertions(+), 79 deletions(-) create mode 100644 src/utils/getConfig.ts delete mode 100644 src/utils/getFromFaucet.ts delete mode 100644 tests/unit/utils/getFromFaucet.test.ts diff --git a/app.config.js b/app.config.js index 91cbdfe3f..3dd4b76a3 100644 --- a/app.config.js +++ b/app.config.js @@ -1,20 +1,15 @@ -const { ConfigHelper } = require('@oceanprotocol/lib') +const { getOceanConfig } = require('./src/utils/getConfig') -const networkConfig = new ConfigHelper().getConfig( - process.env.GATSBY_NETWORK || 'rinkeby' -) +const oceanConfig = getOceanConfig() module.exports = { oceanConfig: { - ...networkConfig, + ...oceanConfig, url: `https://rinkeby.infura.io/${process.env.GATSBY_INFURA_PROJECT_ID}`, verbose: 3 }, // Main, Rinkeby, Kovan // networks: [1, 4, 42], networks: [4], - infuraProjectId: process.env.GATSBY_INFURA_PROJECT_ID || 'xxx', - marketAddress: - process.env.GATSBY_MARKET_ADDRESS || - '0x36A7f3383A63279cDaF4DfC0F3ABc07d90252C6b' + infuraProjectId: process.env.GATSBY_INFURA_PROJECT_ID || 'xxx' } diff --git a/src/components/organisms/AssetList.tsx b/src/components/organisms/AssetList.tsx index bc38aef5e..ecb345994 100644 --- a/src/components/organisms/AssetList.tsx +++ b/src/components/organisms/AssetList.tsx @@ -7,17 +7,18 @@ import { updateQueryStringParameter } from '../../utils' import styles from './AssetList.module.css' import { MetadataMarket } from '../../@types/Metadata' import { DDO } from '@oceanprotocol/lib' -import { useSiteMetadata } from '../../hooks/useSiteMetadata' +import { getOceanConfig } from '../../utils/getConfig' declare type AssetListProps = { queryResult: QueryResult } const AssetList: React.FC = ({ queryResult }) => { - const { appConfig } = useSiteMetadata() const location = useLocation() const navigate = useNavigate() + const { metadataStoreUri } = getOceanConfig() + // Construct the urls on the pagination links. This is only for UX, // since the links are no they will not work by itself. function hrefBuilder(pageIndex: number) { @@ -55,7 +56,7 @@ const AssetList: React.FC = ({ queryResult }) => { }) ) : (
- No results found in {appConfig.oceanConfig.metadataStoreUri} + No results found in {metadataStoreUri}
)} diff --git a/src/components/pages/Home.tsx b/src/components/pages/Home.tsx index bf8de2812..5207c19ff 100644 --- a/src/components/pages/Home.tsx +++ b/src/components/pages/Home.tsx @@ -6,10 +6,11 @@ import AssetList from '../organisms/AssetList' import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore' import Container from '../atoms/Container' import Loader from '../atoms/Loader' -import { useSiteMetadata } from '../../hooks/useSiteMetadata' +import { getOceanConfig } from '../../utils/getConfig' -async function getLatestAssets(metadataStoreUri: string) { +async function getLatestAssets() { try { + const { metadataStoreUri } = getOceanConfig() const metadataStore = new MetadataStore(metadataStoreUri, Logger) const result = await metadataStore.queryMetadata({ @@ -26,15 +27,12 @@ async function getLatestAssets(metadataStoreUri: string) { } export default function HomePage(): ReactElement { - const { appConfig } = useSiteMetadata() const [queryResult, setQueryResult] = useState() const [loading, setLoading] = useState(true) useEffect(() => { async function init() { - const results = await getLatestAssets( - appConfig.oceanConfig.metadataStoreUri - ) + const results = await getLatestAssets() setQueryResult(results) setLoading(false) } diff --git a/src/components/pages/Publish/index.tsx b/src/components/pages/Publish/index.tsx index a21ef4cd1..fccf7fa4f 100644 --- a/src/components/pages/Publish/index.tsx +++ b/src/components/pages/Publish/index.tsx @@ -8,7 +8,6 @@ import PublishForm from './PublishForm' import Web3Feedback from '../../molecules/Wallet/Feedback' import { FormContent } from '../../../@types/Form' import { initialValues, validationSchema } from './validation' -import { useSiteMetadata } from '../../../hooks/useSiteMetadata' import { MetadataPublishForm } from '../../../@types/Metadata' import { transformPublishFormToMetadata } from './utils' import Preview from './Preview' @@ -20,7 +19,7 @@ export default function PublishPage({ }): ReactElement { const { publish, publishError } = usePublish() const navigate = useNavigate() - const { marketAddress } = useSiteMetadata() + const marketAddress = '0x36A7f3383A63279cDaF4DfC0F3ABc07d90252C6b' async function handleSubmit(values: MetadataPublishForm): Promise { console.log(` diff --git a/src/components/templates/AssetDetails.tsx b/src/components/templates/AssetDetails.tsx index da3d10816..6ff4ff566 100644 --- a/src/components/templates/AssetDetails.tsx +++ b/src/components/templates/AssetDetails.tsx @@ -6,7 +6,7 @@ import { MetadataMarket, ServiceMetadataMarket } from '../../@types/Metadata' import { MetadataStore, Logger, DDO } from '@oceanprotocol/lib' import Alert from '../../components/atoms/Alert' import Loader from '../../components/atoms/Loader' -import { useSiteMetadata } from '../../hooks/useSiteMetadata' +import { getOceanConfig } from '../../utils/getConfig' export default function PageTemplateAssetDetails({ did, @@ -15,7 +15,6 @@ export default function PageTemplateAssetDetails({ did: string uri: string }): ReactElement { - const { appConfig } = useSiteMetadata() const [metadata, setMetadata] = useState() const [title, setTitle] = useState() const [error, setError] = useState() @@ -24,10 +23,8 @@ export default function PageTemplateAssetDetails({ useEffect(() => { async function init() { try { - const metadataStore = new MetadataStore( - appConfig.oceanConfig.metadataStoreUri, - Logger - ) + const { metadataStoreUri } = getOceanConfig() + const metadataStore = new MetadataStore(metadataStoreUri, Logger) const ddo = await metadataStore.retrieveDDO(did) setDdo(ddo) diff --git a/src/components/templates/Search/utils.ts b/src/components/templates/Search/utils.ts index 28973e6af..24eb1ba1b 100644 --- a/src/components/templates/Search/utils.ts +++ b/src/components/templates/Search/utils.ts @@ -4,7 +4,7 @@ import { } from '@oceanprotocol/lib/dist/node/metadatastore/MetadataStore' import { priceQueryParamToWei } from '../../../utils' import { MetadataStore, Logger } from '@oceanprotocol/lib' -import { oceanConfig } from '../../../../app.config' +import { getOceanConfig } from '../../../utils/getConfig' export function getSearchQuery( page?: string | string[], @@ -52,7 +52,8 @@ export async function getResults(params: any): Promise { ]) : undefined - const metadataStore = new MetadataStore(oceanConfig.metadataStoreUri, Logger) + const { metadataStoreUri } = getOceanConfig() + const metadataStore = new MetadataStore(metadataStoreUri, Logger) const queryResult = await metadataStore.queryMetadata( getSearchQuery(page, offset, text, tag, priceQuery) ) diff --git a/src/hooks/useSiteMetadata.ts b/src/hooks/useSiteMetadata.ts index 0a642efec..7d813f309 100644 --- a/src/hooks/useSiteMetadata.ts +++ b/src/hooks/useSiteMetadata.ts @@ -16,15 +16,6 @@ const query = graphql` appConfig { infuraProjectId networks - marketAddress - oceanConfig { - url - factoryAddress - oceanTokenAddress - metadataStoreUri - providerUri - verbose - } } } } diff --git a/src/utils/getConfig.ts b/src/utils/getConfig.ts new file mode 100644 index 000000000..814025719 --- /dev/null +++ b/src/utils/getConfig.ts @@ -0,0 +1,5 @@ +import { ConfigHelper, Config } from '@oceanprotocol/lib' + +export function getOceanConfig(): Config { + return new ConfigHelper().getConfig(process.env.GATSBY_NETWORK || 'rinkeby') +} diff --git a/src/utils/getFromFaucet.ts b/src/utils/getFromFaucet.ts deleted file mode 100644 index d98cfb0f9..000000000 --- a/src/utils/getFromFaucet.ts +++ /dev/null @@ -1,27 +0,0 @@ -import axios, { AxiosResponse } from 'axios' -import { oceanConfig } from '../../app.config' - -export interface FaucetResponse { - success: boolean - message: string - trxHash?: string -} - -export default async function getFromFaucet( - account: string -): Promise { - try { - const response: AxiosResponse = await axios({ - method: 'POST', - url: `${oceanConfig.faucetUri}/faucet`, - data: { - address: account, - agent: 'market' - } - }) - - return response.data - } catch (error) { - return { success: false, message: error.message } - } -} diff --git a/tests/unit/utils/getFromFaucet.test.ts b/tests/unit/utils/getFromFaucet.test.ts deleted file mode 100644 index 6a58a401b..000000000 --- a/tests/unit/utils/getFromFaucet.test.ts +++ /dev/null @@ -1,15 +0,0 @@ -import axios, { AxiosResponse } from 'axios' -import getFromFaucet from '../../../src/utils/getFromFaucet' - -jest.mock('axios') - -describe('getFromFaucet()', () => { - ;(axios as any).mockResolvedValue({ - data: { success: true, message: 'hello' } - } as AxiosResponse) - - it('can be called', async () => { - const response = await getFromFaucet('0x0000000') - expect(response.message).toBe('hello') - }) -})