Skip to content

Commit 91e4fe7

Browse files
author
pory-gone
committed
new feature, Sats per Big Mac
1 parent 292a20b commit 91e4fe7

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

api/resolvers/price.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,42 @@ const getPrice = cachedFetcher(async function fetchPrice (fiat = 'USD') {
1818
keyGenerator: (fiat = 'USD') => fiat
1919
})
2020

21+
const getBigMacPrice = cachedFetcher(async function fetchBigMacPrice () {
22+
const csvUrl = 'https://raw.githubusercontent.com/TheEconomist/big-mac-data/master/output-data/big-mac-raw-index.csv'
23+
try {
24+
const res = await fetch(csvUrl)
25+
const csvText = await res.text()
26+
const lines = csvText.split('\n')
27+
const usaEntries = lines
28+
.filter(line => line.includes(',USA,USD,'))
29+
.map(line => {
30+
const cols = line.split(',')
31+
return {
32+
date: cols[0],
33+
price: parseFloat(cols[4])
34+
}
35+
})
36+
.filter(entry => !isNaN(entry.price))
37+
.sort((a, b) => new Date(b.date) - new Date(a.date))
38+
return usaEntries[0]?.price || 5.79
39+
} catch (err) {
40+
console.error('Big Mac price fetch error:', err)
41+
return 5.79
42+
}
43+
}, {
44+
maxSize: 1,
45+
cacheExpiry: 24 * 60 * 60 * 1000,
46+
forceRefreshThreshold: 0,
47+
keyGenerator: () => 'bigmac-usd'
48+
})
49+
2150
export default {
2251
Query: {
2352
price: async (parent, { fiatCurrency }, ctx) => {
2453
return await getPrice(fiatCurrency) || -1
54+
},
55+
bigMacPrice: async () => {
56+
return await getBigMacPrice() || 5.79
2557
}
2658
}
2759
}

api/typeDefs/price.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { gql } from 'graphql-tag'
33
export default gql`
44
extend type Query {
55
price(fiatCurrency: String): Float
6+
bigMacPrice: Float
67
}
78
`

components/nav/price-carousel.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const carousel = [
99
'1btc',
1010
'blockHeight',
1111
'chainFee',
12-
'halving'
12+
'halving',
13+
'bigmac'
1314
]
1415

1516
export const PriceCarouselContext = createContext({

components/price.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { usePriceCarousel } from './nav/price-carousel'
1212

1313
export const PriceContext = React.createContext({
1414
price: null,
15-
fiatSymbol: null
15+
fiatSymbol: null,
16+
bigMacPrice: null
1617
})
1718

1819
export function usePrice () {
@@ -34,8 +35,9 @@ export function PriceProvider ({ price, children }) {
3435

3536
const contextValue = useMemo(() => ({
3637
price: data?.price || price,
37-
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$'
38-
}), [data?.price, price, me?.privates?.fiatCurrency])
38+
fiatSymbol: CURRENCY_SYMBOLS[fiatCurrency] || '$',
39+
bigMacPrice: data?.bigMacPrice || 5.79
40+
}), [data?.price, data?.bigMacPrice, price, me?.privates?.fiatCurrency])
3941

4042
return (
4143
<PriceContext.Provider value={contextValue}>
@@ -56,7 +58,7 @@ function AccessibleButton ({ id, description, children, ...props }) {
5658
export default function Price ({ className }) {
5759
const [selection, handleClick] = usePriceCarousel()
5860

59-
const { price, fiatSymbol } = usePrice()
61+
const { price, fiatSymbol, bigMacPrice } = usePrice()
6062
const { height: blockHeight, halving } = useBlockHeight()
6163
const { fee: chainFee } = useChainFee()
6264

@@ -106,6 +108,15 @@ export default function Price ({ className }) {
106108
)
107109
}
108110

111+
if (selection === 'bigmac') {
112+
if (!price || price < 0 || !bigMacPrice) return null
113+
return (
114+
<AccessibleButton id='bigmac-hint' description='Show satoshis per Big Mac' className={compClassName} onClick={handleClick} variant='link'>
115+
{fixedDecimal(Math.round((bigMacPrice / price) * 100000000), 0)} sats/Big Mac
116+
</AccessibleButton>
117+
)
118+
}
119+
109120
if (selection === 'fiat') {
110121
if (!price || price < 0) return null
111122
return (

fragments/price.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { gql } from '@apollo/client'
33
export const PRICE = gql`
44
query price($fiatCurrency: String) {
55
price(fiatCurrency: $fiatCurrency)
6+
bigMacPrice
67
}`

0 commit comments

Comments
 (0)