1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

refactor network messaging

This commit is contained in:
Matthias Kretschmann 2020-10-23 13:01:29 +02:00
parent f0c6958154
commit 6e589949b4
Signed by: m
GPG Key ID: 606EEEF3C479A91F
9 changed files with 25 additions and 38 deletions

View File

@ -1,4 +1,8 @@
module.exports = { module.exports = {
// The default network and its associated config the app should connect to
// on start. App will automatically switch network configs when user switches
// networks in their wallet.
// Ocean Protocol contracts are deployed for: 'mainnet', 'rinkeby', 'development'
network: process.env.GATSBY_NETWORK || 'rinkeby', network: process.env.GATSBY_NETWORK || 'rinkeby',
infuraProjectId: process.env.GATSBY_INFURA_PROJECT_ID || 'xxx', infuraProjectId: process.env.GATSBY_INFURA_PROJECT_ID || 'xxx',
// The ETH address the marketplace fee will be sent to. // The ETH address the marketplace fee will be sent to.

View File

@ -2,7 +2,7 @@ import { useOcean } from '@oceanprotocol/react'
import { toDataUrl } from 'ethereum-blockies' import { toDataUrl } from 'ethereum-blockies'
import React, { FormEvent } from 'react' import React, { FormEvent } from 'react'
import { ReactComponent as Caret } from '../../../images/caret.svg' import { ReactComponent as Caret } from '../../../images/caret.svg'
import { accountTruncate, isCorrectNetwork } from '../../../utils/wallet' import { accountTruncate } from '../../../utils/wallet'
import Status from '../../atoms/Status' import Status from '../../atoms/Status'
import styles from './Account.module.css' import styles from './Account.module.css'
@ -24,7 +24,7 @@ const Blockies = ({ account }: { account: string | undefined }) => {
// eslint-disable-next-line // eslint-disable-next-line
const Account = React.forwardRef((props, ref: any) => { const Account = React.forwardRef((props, ref: any) => {
const { accountId, status, connect, networkId } = useOcean() const { accountId, status, connect, networkId } = useOcean()
const hasSuccess = status === 1 && isCorrectNetwork(networkId) const hasSuccess = status === 1 && networkId === 1
async function handleActivation(e: FormEvent<HTMLButtonElement>) { async function handleActivation(e: FormEvent<HTMLButtonElement>) {
// prevent accidentially submitting a form the button might be in // prevent accidentially submitting a form the button might be in

View File

@ -1,5 +1,6 @@
.details { .details {
padding: calc(var(--spacer) / 4); padding: calc(var(--spacer) / 4);
min-width: 17rem;
} }
.details > section[class*='feedback'] { .details > section[class*='feedback'] {

View File

@ -3,14 +3,13 @@ import Button from '../../atoms/Button'
import styles from './Details.module.css' import styles from './Details.module.css'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
import Web3Feedback from './Feedback' import Web3Feedback from './Feedback'
import { getNetworkName } from '../../../utils/wallet'
import { getInjectedProviderName } from 'web3modal' import { getInjectedProviderName } from 'web3modal'
import Conversion from '../../atoms/Price/Conversion' import Conversion from '../../atoms/Price/Conversion'
import { formatCurrency } from '@coingecko/cryptoformat' import { formatCurrency } from '@coingecko/cryptoformat'
import { useUserPreferences } from '../../../providers/UserPreferences' import { useUserPreferences } from '../../../providers/UserPreferences'
export default function Details(): ReactElement { export default function Details(): ReactElement {
const { balance, connect, logout, networkId } = useOcean() const { balance, connect, logout } = useOcean()
const { locale } = useUserPreferences() const { locale } = useUserPreferences()
return ( return (
@ -27,11 +26,7 @@ export default function Details(): ReactElement {
))} ))}
<li className={styles.actions}> <li className={styles.actions}>
<span title="Connected provider"> <span title="Connected provider">{getInjectedProviderName()}</span>
{getInjectedProviderName()}
<br />
{getNetworkName(networkId)}
</span>
<Button <Button
style="text" style="text"
size="small" size="small"

View File

@ -2,8 +2,7 @@ import React, { ReactElement } from 'react'
import Status from '../../atoms/Status' import Status from '../../atoms/Status'
import styles from './Feedback.module.css' import styles from './Feedback.module.css'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
import { isCorrectNetwork } from '../../../utils/wallet' import { getNetworkName } from '../../../utils/wallet'
import { useSiteMetadata } from '../../../hooks/useSiteMetadata'
export declare type Web3Error = { export declare type Web3Error = {
status: 'error' | 'warning' | 'success' status: 'error' | 'warning' | 'success'
@ -16,23 +15,18 @@ export default function Web3Feedback({
}: { }: {
isBalanceSufficient?: boolean isBalanceSufficient?: boolean
}): ReactElement { }): ReactElement {
const { appConfig } = useSiteMetadata()
const { account, status, networkId } = useOcean() const { account, status, networkId } = useOcean()
const isOceanConnectionError = status === -1 const isOceanConnectionError = status === -1
const correctNetwork = isCorrectNetwork(networkId) const isMainnet = networkId === 1
const showFeedback = const showFeedback =
!account || !account ||
isOceanConnectionError || isOceanConnectionError ||
!correctNetwork || !isMainnet ||
isBalanceSufficient === false isBalanceSufficient === false
const desiredNetworkName = appConfig.network.replace(/^\w/, (c: string) =>
c.toUpperCase()
)
const state = !account const state = !account
? 'error' ? 'error'
: !correctNetwork : !isMainnet
? 'warning' ? 'warning'
: account && isBalanceSufficient : account && isBalanceSufficient
? 'success' ? 'success'
@ -42,8 +36,8 @@ export default function Web3Feedback({
? 'No account connected' ? 'No account connected'
: isOceanConnectionError : isOceanConnectionError
? 'Error connecting to Ocean' ? 'Error connecting to Ocean'
: !correctNetwork : !isMainnet
? 'Wrong Network' ? getNetworkName(networkId)
: account : account
? isBalanceSufficient === false ? isBalanceSufficient === false
? 'Insufficient balance' ? 'Insufficient balance'
@ -54,8 +48,8 @@ export default function Web3Feedback({
? 'Please connect your Web3 wallet.' ? 'Please connect your Web3 wallet.'
: isOceanConnectionError : isOceanConnectionError
? 'Please try again.' ? 'Please try again.'
: !correctNetwork : !isMainnet
? `Please connect to ${desiredNetworkName}.` ? undefined
: isBalanceSufficient === false : isBalanceSufficient === false
? 'You do not have enough OCEAN in your wallet to purchase this asset.' ? 'You do not have enough OCEAN in your wallet to purchase this asset.'
: 'Something went wrong.' : 'Something went wrong.'
@ -64,7 +58,7 @@ export default function Web3Feedback({
<section className={styles.feedback}> <section className={styles.feedback}>
<Status state={state} aria-hidden /> <Status state={state} aria-hidden />
<h3 className={styles.title}>{title}</h3> <h3 className={styles.title}>{title}</h3>
<p className={styles.error}>{message}</p> {message && <p className={styles.error}>{message}</p>}
</section> </section>
) : null ) : null
} }

View File

@ -20,3 +20,7 @@
.hasTokens { .hasTokens {
composes: hasTokens from './index.module.css'; composes: hasTokens from './index.module.css';
} }
.feedback {
width: 100%;
}

View File

@ -56,7 +56,5 @@
.alertArea { .alertArea {
margin-left: -2rem; margin-left: -2rem;
margin-right: -2rem; margin-right: -2rem;
padding: var(--spacer) calc(var(--spacer) / 2); padding: 0 calc(var(--spacer) / 2);
padding-bottom: 0;
margin-top: var(--spacer);
} }

View File

@ -1,8 +1,6 @@
import { useOcean, usePricing } from '@oceanprotocol/react' import { useOcean, usePricing } from '@oceanprotocol/react'
import PriceUnit from '../../../../atoms/Price/PriceUnit' import PriceUnit from '../../../../atoms/Price/PriceUnit'
import React, { ReactElement, useEffect, useState } from 'react' import React, { ReactElement, useEffect, useState } from 'react'
import { useSiteMetadata } from '../../../../../hooks/useSiteMetadata'
import { isCorrectNetwork } from '../../../../../utils/wallet'
import Alert from '../../../../atoms/Alert' import Alert from '../../../../atoms/Alert'
import FormHelp from '../../../../atoms/Input/Help' import FormHelp from '../../../../atoms/Input/Help'
import Tooltip from '../../../../atoms/Tooltip' import Tooltip from '../../../../atoms/Tooltip'
@ -22,7 +20,6 @@ export default function Dynamic({
ddo: DDO ddo: DDO
content: any content: any
}): ReactElement { }): ReactElement {
const { appConfig } = useSiteMetadata()
const { account, balance, networkId, refreshBalance } = useOcean() const { account, balance, networkId, refreshBalance } = useOcean()
const { dtSymbol, dtName } = usePricing(ddo) const { dtSymbol, dtName } = usePricing(ddo)
@ -31,23 +28,17 @@ export default function Dynamic({
const { price, weightOnDataToken } = values as PriceOptionsMarket const { price, weightOnDataToken } = values as PriceOptionsMarket
const [error, setError] = useState<string>() const [error, setError] = useState<string>()
const correctNetwork = isCorrectNetwork(networkId)
const desiredNetworkName = appConfig.network.replace(/^\w/, (c: string) =>
c.toUpperCase()
)
// Check: account, network & insufficient balance // Check: account, network & insufficient balance
useEffect(() => { useEffect(() => {
if (!account) { if (!account) {
setError(`No account connected. Please connect your Web3 wallet.`) setError(`No account connected. Please connect your Web3 wallet.`)
} else if (!correctNetwork) {
setError(`Wrong Network. Please connect to ${desiredNetworkName}.`)
} else if (Number(balance.ocean) < Number(price)) { } else if (Number(balance.ocean) < Number(price)) {
setError(`Insufficient balance. You need at least ${price} OCEAN`) setError(`Insufficient balance. You need at least ${price} OCEAN`)
} else { } else {
setError(undefined) setError(undefined)
} }
}, [price, networkId, account, balance, correctNetwork, desiredNetworkName]) }, [price, networkId, account, balance])
// refetch balance periodically // refetch balance periodically
useEffect(() => { useEffect(() => {

View File

@ -50,7 +50,7 @@ export function getNetworkId(network: string): number {
} }
} }
export function isCorrectNetwork(networkId: number): boolean { export function isDefaultNetwork(networkId: number): boolean {
const configuredNetwork = getNetworkId(network) const configuredNetwork = getNetworkId(network)
return configuredNetwork === networkId return configuredNetwork === networkId
} }