From f327ec0e430ec5b4ba84b504a69c806452dade0c Mon Sep 17 00:00:00 2001
From: Matthias Kretschmann <m@kretschmann.io>
Date: Wed, 22 Jul 2020 13:56:42 +0200
Subject: [PATCH] network config refactor

---
 .env.example                                 |  3 +-
 app.config.js                                |  8 ++--
 src/components/molecules/Wallet/Feedback.tsx |  6 +--
 src/hooks/useSiteMetadata.ts                 |  2 +-
 src/utils/wallet.ts                          | 39 ++++++++++++--------
 5 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/.env.example b/.env.example
index 294bbb0ce..77b9d04de 100644
--- a/.env.example
+++ b/.env.example
@@ -1,5 +1,4 @@
 # Network, possible values: development, pacific, rinkeby, mainnet
 GATSBY_NETWORK="rinkeby"
 
-#GATSBY_INFURA_PROJECT_ID="xxx"
-#GATSBY_MARKET_ADDRESS="xxx"
\ No newline at end of file
+#GATSBY_INFURA_PROJECT_ID="xxx"
\ No newline at end of file
diff --git a/app.config.js b/app.config.js
index cb865733d..9636066b9 100644
--- a/app.config.js
+++ b/app.config.js
@@ -1,8 +1,10 @@
 const { ConfigHelper } = require('@oceanprotocol/lib')
 
+const network = process.env.GATSBY_NETWORK || 'rinkeby'
+
 function getDefaultOceanConfig() {
   return new ConfigHelper().getConfig(
-    process.env.GATSBY_NETWORK || 'rinkeby',
+    network,
     process.env.GATSBY_INFURA_PROJECT_ID
   )
 }
@@ -12,9 +14,7 @@ const appConfig = {
     ...getDefaultOceanConfig(),
     verbose: 3
   },
-  // Main, Rinkeby, Kovan
-  // networks: [1, 4, 42],
-  networks: [4],
+  network,
   infuraProjectId: process.env.GATSBY_INFURA_PROJECT_ID || 'xxx'
 }
 
diff --git a/src/components/molecules/Wallet/Feedback.tsx b/src/components/molecules/Wallet/Feedback.tsx
index a2ff8fed8..53b329058 100644
--- a/src/components/molecules/Wallet/Feedback.tsx
+++ b/src/components/molecules/Wallet/Feedback.tsx
@@ -21,9 +21,7 @@ export default function Web3Feedback({
   const isOceanConnectionError = status === -1
   const correctNetwork = isCorrectNetwork(chainId)
   const showFeedback = !account || isOceanConnectionError || !correctNetwork
-  const allowedNetworkNames = appConfig.networks.map((network: number) =>
-    getNetworkName(network)
-  )
+  const networkName = getNetworkName(appConfig.network)
 
   const state = !account
     ? 'error'
@@ -50,7 +48,7 @@ export default function Web3Feedback({
     : isOceanConnectionError
     ? 'Please try again.'
     : !correctNetwork
-    ? `Please connect to ${allowedNetworkNames}.`
+    ? `Please connect to ${networkName}.`
     : isBalanceInsufficient === true
     ? 'You do not have enough OCEAN in your wallet to purchase this asset.'
     : 'Something went wrong.'
diff --git a/src/hooks/useSiteMetadata.ts b/src/hooks/useSiteMetadata.ts
index 7d813f309..acf0994cb 100644
--- a/src/hooks/useSiteMetadata.ts
+++ b/src/hooks/useSiteMetadata.ts
@@ -15,7 +15,7 @@ const query = graphql`
         }
         appConfig {
           infuraProjectId
-          networks
+          network
         }
       }
     }
diff --git a/src/utils/wallet.ts b/src/utils/wallet.ts
index e88e9b13c..7fd635192 100644
--- a/src/utils/wallet.ts
+++ b/src/utils/wallet.ts
@@ -1,7 +1,7 @@
 import { OceanProviderValue } from '@oceanprotocol/react'
 import { appConfig } from '../../app.config'
 
-const { infuraProjectId, networks } = appConfig
+const { infuraProjectId, network, oceanConfig } = appConfig
 
 const web3ModalTheme = {
   background: 'var(--brand-white)',
@@ -29,18 +29,14 @@ export async function connectWallet(
       }
     },
     torus: {
-      package: Torus
-      // options: {
-      // networkParams: {
-      // host: 'https://localhost:8545' // optional
-      //   chainId: 1337, // optional
-      //   networkId: 1337 // optional
-      // },
-      // config: {
-      //   buildEnv: 'development' // optional
-      // }
-      // }
-      // }
+      package: Torus,
+      options: {
+        networkParams: {
+          host: oceanConfig.url // optional
+          // chainId: 1337, // optional
+          // networkId: 1337 // optional
+        }
+      }
     }
   }
 
@@ -48,8 +44,8 @@ export async function connectWallet(
 }
 
 export function isCorrectNetwork(chainId: number): boolean {
-  const allowedIds = networks
-  return allowedIds.includes(chainId)
+  const configuredNetwork = getChainId(network)
+  return configuredNetwork === chainId
 }
 
 export function accountTruncate(account: string): string {
@@ -70,3 +66,16 @@ export function getNetworkName(chainId: number): string {
       return 'Unknown'
   }
 }
+
+export function getChainId(network: string): number {
+  switch (network) {
+    case 'mainnet':
+      return 1
+    case 'rinkeby':
+      return 4
+    case 'kovan':
+      return 42
+    default:
+      return 0
+  }
+}