Implement dynamic coinbase endpoint
This commit is contained in:
parent
5d4abecf3c
commit
6f632ccc0d
|
@ -9,7 +9,7 @@ import styles from '../styles/post.module.css'
|
||||||
import { useLazyQuery, gql, useMutation } from '@apollo/client'
|
import { useLazyQuery, gql, useMutation } from '@apollo/client'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { usePrice } from './price'
|
import { CURRENCY_SYMBOLS, usePrice } from './price'
|
||||||
import Avatar from './avatar'
|
import Avatar from './avatar'
|
||||||
|
|
||||||
Yup.addMethod(Yup.string, 'or', function (schemas, msg) {
|
Yup.addMethod(Yup.string, 'or', function (schemas, msg) {
|
||||||
|
@ -34,13 +34,16 @@ function satsMin2Mo (minute) {
|
||||||
|
|
||||||
function PriceHint ({ monthly }) {
|
function PriceHint ({ monthly }) {
|
||||||
const price = usePrice()
|
const price = usePrice()
|
||||||
|
const { fiatCurrency } = useMe();
|
||||||
|
const fiatSymbol = CURRENCY_SYMBOLS[fiatCurrency]
|
||||||
|
|
||||||
if (!price) {
|
if (!price) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const fixed = (n, f) => Number.parseFloat(n).toFixed(f)
|
const fixed = (n, f) => Number.parseFloat(n).toFixed(f)
|
||||||
const fiat = fixed((price / 100000000) * monthly, 0)
|
const fiat = fixed((price / 100000000) * monthly, 0)
|
||||||
|
|
||||||
return <span className='text-muted'>{monthly} sats/mo which is ${fiat}/mo</span>
|
return <span className='text-muted'>{monthly} sats/mo which is {fiatSymbol}{fiat}/mo</span>
|
||||||
}
|
}
|
||||||
|
|
||||||
// need to recent list items
|
// need to recent list items
|
||||||
|
|
|
@ -2,6 +2,7 @@ import React, { useContext, useEffect, useState } from 'react'
|
||||||
import { Button } from 'react-bootstrap'
|
import { Button } from 'react-bootstrap'
|
||||||
import useSWR from 'swr'
|
import useSWR from 'swr'
|
||||||
import { fixedDecimal } from '../lib/format'
|
import { fixedDecimal } from '../lib/format'
|
||||||
|
import { useMe } from './me'
|
||||||
|
|
||||||
const fetcher = url => fetch(url).then(res => res.json()).catch()
|
const fetcher = url => fetch(url).then(res => res.json()).catch()
|
||||||
|
|
||||||
|
@ -9,16 +10,22 @@ export const PriceContext = React.createContext({
|
||||||
price: null
|
price: null
|
||||||
})
|
})
|
||||||
|
|
||||||
const ENDPOINT = 'https://api.coinbase.com/v2/prices/BTC-USD/spot'
|
export const CURRENCY_SYMBOLS = {
|
||||||
|
'USD': '$',
|
||||||
|
'EUR': '€'
|
||||||
|
}
|
||||||
|
|
||||||
export async function getPrice () {
|
const endpoint = (fiat) => `https://api.coinbase.com/v2/prices/BTC-${fiat}/spot`
|
||||||
const data = await fetcher(ENDPOINT)
|
|
||||||
|
export async function getPrice (fiat) {
|
||||||
|
const data = await fetcher(endpoint(fiat))
|
||||||
return data?.data?.amount
|
return data?.data?.amount
|
||||||
}
|
}
|
||||||
|
|
||||||
export function PriceProvider ({ price, children }) {
|
export function PriceProvider ({ price, children }) {
|
||||||
|
const { fiatCurrency } = useMe()
|
||||||
const { data } = useSWR(
|
const { data } = useSWR(
|
||||||
ENDPOINT,
|
endpoint(fiatCurrency),
|
||||||
fetcher,
|
fetcher,
|
||||||
{
|
{
|
||||||
refreshInterval: 30000
|
refreshInterval: 30000
|
||||||
|
@ -45,8 +52,9 @@ export default function Price () {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setAsSats(localStorage.getItem('asSats'))
|
setAsSats(localStorage.getItem('asSats'))
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const price = usePrice()
|
const price = usePrice()
|
||||||
|
const { fiatCurrency } = useMe()
|
||||||
|
const fiatSymbol = CURRENCY_SYMBOLS[fiatCurrency];
|
||||||
|
|
||||||
if (!price) return null
|
if (!price) return null
|
||||||
|
|
||||||
|
@ -66,7 +74,7 @@ export default function Price () {
|
||||||
if (asSats === 'yep') {
|
if (asSats === 'yep') {
|
||||||
return (
|
return (
|
||||||
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
|
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
|
||||||
{fixedDecimal(100000000 / price, 0) + ' sats/$'}
|
{fixedDecimal(100000000 / price, 0) + ` sats/${fiatSymbol}`}
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +89,7 @@ export default function Price () {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
|
<Button className='text-reset p-0' onClick={handleClick} variant='link'>
|
||||||
{'$' + fixedDecimal(price, 0)}
|
{fiatSymbol + fixedDecimal(price, 0)}
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue