mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
configurable currencies
This commit is contained in:
parent
acf233a77b
commit
e421667941
@ -4,5 +4,8 @@ module.exports = {
|
|||||||
marketFeeAddress:
|
marketFeeAddress:
|
||||||
process.env.GATSBY_MARKET_FEE_ADDRESS ||
|
process.env.GATSBY_MARKET_FEE_ADDRESS ||
|
||||||
'0x903322C7E45A60d7c8C3EA236c5beA9Af86310c7',
|
'0x903322C7E45A60d7c8C3EA236c5beA9Af86310c7',
|
||||||
marketFeeAmount: process.env.GATSBY_MARKET_FEE_AMOUNT || '0.1' // in %
|
marketFeeAmount: process.env.GATSBY_MARKET_FEE_AMOUNT || '0.1', // in %
|
||||||
|
// Used for conversion display, can be whatever coingecko API supports
|
||||||
|
// see: https://api.coingecko.com/api/v3/simple/supported_vs_currencies
|
||||||
|
currencies: ['EUR', 'USD', 'ETH', 'BTC']
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,10 @@ import styles from './Conversion.module.css'
|
|||||||
import classNames from 'classnames/bind'
|
import classNames from 'classnames/bind'
|
||||||
import { formatCurrency } from '@coingecko/cryptoformat'
|
import { formatCurrency } from '@coingecko/cryptoformat'
|
||||||
import { useUserPreferences } from '../../../providers/UserPreferences'
|
import { useUserPreferences } from '../../../providers/UserPreferences'
|
||||||
|
import { useSiteMetadata } from '../../../hooks/useSiteMetadata'
|
||||||
|
|
||||||
const cx = classNames.bind(styles)
|
const cx = classNames.bind(styles)
|
||||||
|
|
||||||
const currencies = 'EUR,USD' // comma-separated list
|
|
||||||
const url = `https://api.coingecko.com/api/v3/simple/price?ids=ocean-protocol&vs_currencies=${currencies}&include_24hr_change=true`
|
|
||||||
|
|
||||||
export default function Conversion({
|
export default function Conversion({
|
||||||
price,
|
price,
|
||||||
update = true,
|
update = true,
|
||||||
@ -20,27 +18,30 @@ export default function Conversion({
|
|||||||
update?: boolean
|
update?: boolean
|
||||||
className?: string
|
className?: string
|
||||||
}): ReactElement {
|
}): ReactElement {
|
||||||
const [priceFiat, setPriceFiat] = useState('0.00')
|
const { appConfig } = useSiteMetadata()
|
||||||
|
const tokenId = 'ocean-protocol'
|
||||||
|
const currencies = appConfig.currencies.join(',') // comma-separated list
|
||||||
|
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${tokenId}&vs_currencies=${currencies}&include_24hr_change=true`
|
||||||
const { currency } = useUserPreferences()
|
const { currency } = useUserPreferences()
|
||||||
|
|
||||||
|
const [priceConverted, setPriceConverted] = useState('0.00')
|
||||||
|
|
||||||
const styleClasses = cx({
|
const styleClasses = cx({
|
||||||
conversion: true,
|
conversion: true,
|
||||||
[className]: className
|
[className]: className
|
||||||
})
|
})
|
||||||
|
|
||||||
const onSuccess = async (data: {
|
const onSuccess = async (data: { [tokenId]: { [key: string]: number } }) => {
|
||||||
'ocean-protocol': { eur: number; usd: number }
|
|
||||||
}) => {
|
|
||||||
if (!data) return
|
if (!data) return
|
||||||
if (!price || price === '' || price === '0') {
|
if (!price || price === '' || price === '0') {
|
||||||
setPriceFiat('0.00')
|
setPriceConverted('0.00')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { eur, usd } = data['ocean-protocol']
|
const values = data[tokenId]
|
||||||
const fiatValue = currency === 'EUR' ? eur : usd
|
const fiatValue = values[currency.toLowerCase()]
|
||||||
const converted = fiatValue * Number(price)
|
const converted = fiatValue * Number(price)
|
||||||
setPriceFiat(`${formatCurrency(converted, currency, undefined, true)}`)
|
setPriceConverted(`${formatCurrency(converted, currency, undefined, true)}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -66,7 +67,7 @@ export default function Conversion({
|
|||||||
className={styleClasses}
|
className={styleClasses}
|
||||||
title="Approximation based on current spot price on Coingecko"
|
title="Approximation based on current spot price on Coingecko"
|
||||||
>
|
>
|
||||||
≈ {priceFiat} {currency}
|
≈ {priceConverted} {currency}
|
||||||
</span>
|
</span>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
import React, { ReactElement, ChangeEvent } from 'react'
|
import React, { ReactElement, ChangeEvent } from 'react'
|
||||||
|
import { useSiteMetadata } from '../../../hooks/useSiteMetadata'
|
||||||
import { useUserPreferences } from '../../../providers/UserPreferences'
|
import { useUserPreferences } from '../../../providers/UserPreferences'
|
||||||
import Input from '../../atoms/Input'
|
import Input from '../../atoms/Input'
|
||||||
|
|
||||||
export default function Currency(): ReactElement {
|
export default function Currency(): ReactElement {
|
||||||
const { currency, setCurrency } = useUserPreferences()
|
const { currency, setCurrency } = useUserPreferences()
|
||||||
|
const { appConfig } = useSiteMetadata()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li>
|
<li>
|
||||||
@ -12,7 +14,7 @@ export default function Currency(): ReactElement {
|
|||||||
label="Currency"
|
label="Currency"
|
||||||
help="Select your preferred currency."
|
help="Select your preferred currency."
|
||||||
type="select"
|
type="select"
|
||||||
options={['EUR', 'USD']}
|
options={appConfig.currencies}
|
||||||
value={currency}
|
value={currency}
|
||||||
onChange={(e: ChangeEvent<HTMLSelectElement>) =>
|
onChange={(e: ChangeEvent<HTMLSelectElement>) =>
|
||||||
setCurrency(e.target.value)
|
setCurrency(e.target.value)
|
||||||
|
@ -18,6 +18,7 @@ const query = graphql`
|
|||||||
network
|
network
|
||||||
marketFeeAddress
|
marketFeeAddress
|
||||||
marketFeeAmount
|
marketFeeAmount
|
||||||
|
currencies
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,9 @@ import React, {
|
|||||||
useEffect
|
useEffect
|
||||||
} from 'react'
|
} from 'react'
|
||||||
|
|
||||||
declare type Currency = 'EUR' | 'USD'
|
|
||||||
|
|
||||||
interface UserPreferencesValue {
|
interface UserPreferencesValue {
|
||||||
debug: boolean
|
debug: boolean
|
||||||
currency: Currency
|
currency: string
|
||||||
setDebug?: (value: boolean) => void
|
setDebug?: (value: boolean) => void
|
||||||
setCurrency?: (value: string) => void
|
setCurrency?: (value: string) => void
|
||||||
}
|
}
|
||||||
@ -45,7 +43,7 @@ function UserPreferencesProvider({
|
|||||||
const [debug, setDebug] = useState<boolean>(
|
const [debug, setDebug] = useState<boolean>(
|
||||||
(localStorage && localStorage.debug) || false
|
(localStorage && localStorage.debug) || false
|
||||||
)
|
)
|
||||||
const [currency, setCurrency] = useState<Currency>(
|
const [currency, setCurrency] = useState<string>(
|
||||||
(localStorage && localStorage.currency) || 'EUR'
|
(localStorage && localStorage.currency) || 'EUR'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user