diff --git a/api/resolvers/chainFee.js b/api/resolvers/chainFee.js
new file mode 100644
index 00000000..a3c3728d
--- /dev/null
+++ b/api/resolvers/chainFee.js
@@ -0,0 +1,37 @@
+import lndService from 'ln-service'
+import lnd from '../lnd'
+
+const cache = new Map()
+const expiresIn = 1000 * 30 // 30 seconds in milliseconds
+
+async function fetchChainFeeRate () {
+ let chainFee = 0
+ try {
+ const fee = await lndService.getChainFeeRate({ lnd })
+ chainFee = fee.tokens_per_vbyte
+ } catch (err) {
+ console.error('fetchChainFee', err)
+ }
+ cache.set('fee', { fee: chainFee, createdAt: Date.now() })
+ return chainFee
+}
+
+async function getChainFeeRate () {
+ if (cache.has('fee')) {
+ const { fee, createdAt } = cache.get('fee')
+ const expired = createdAt + expiresIn < Date.now()
+ if (expired) fetchChainFeeRate().catch(console.error) // update cache
+ return fee
+ } else {
+ fetchChainFeeRate().catch(console.error)
+ }
+ return 0
+}
+
+export default {
+ Query: {
+ chainFee: async (parent, opts, ctx) => {
+ return await getChainFeeRate()
+ }
+ }
+}
diff --git a/api/resolvers/index.js b/api/resolvers/index.js
index b341d890..60fc8f34 100644
--- a/api/resolvers/index.js
+++ b/api/resolvers/index.js
@@ -15,6 +15,7 @@ import price from './price'
import { GraphQLJSONObject as JSONObject } from 'graphql-type-json'
import admin from './admin'
import blockHeight from './blockHeight'
+import chainFee from './chainFee'
import image from './image'
import { GraphQLScalarType, Kind } from 'graphql'
import { createIntScalar } from 'graphql-scalar'
@@ -53,4 +54,4 @@ const limit = createIntScalar({
})
export default [user, item, message, wallet, lnurl, notifications, invite, sub,
- upload, search, growth, rewards, referrals, price, admin, blockHeight, image, { JSONObject }, { Date: date }, { Limit: limit }]
+ upload, search, growth, rewards, referrals, price, admin, blockHeight, chainFee, image, { JSONObject }, { Date: date }, { Limit: limit }]
diff --git a/api/ssrApollo.js b/api/ssrApollo.js
index a34b769d..2524fd86 100644
--- a/api/ssrApollo.js
+++ b/api/ssrApollo.js
@@ -10,6 +10,7 @@ import search from './search'
import { ME } from '../fragments/users'
import { PRICE } from '../fragments/price'
import { BLOCK_HEIGHT } from '../fragments/blockHeight'
+import { CHAIN_FEE } from '../fragments/chainFee'
import { getServerSession } from 'next-auth/next'
import { getAuthOptions } from '../pages/api/auth/[...nextauth]'
@@ -94,6 +95,10 @@ export function getGetServerSideProps (
query: BLOCK_HEIGHT, variables: {}
})
+ const { data: { chainFee } } = await client.query({
+ query: CHAIN_FEE, variables: {}
+ })
+
let error = null; let data = null; let props = {}
if (query) {
try {
@@ -125,6 +130,7 @@ export function getGetServerSideProps (
me,
price,
blockHeight,
+ chainFee,
ssrData: data
}
}
diff --git a/api/typeDefs/chainFee.js b/api/typeDefs/chainFee.js
new file mode 100644
index 00000000..0bc444ee
--- /dev/null
+++ b/api/typeDefs/chainFee.js
@@ -0,0 +1,7 @@
+import { gql } from 'graphql-tag'
+
+export default gql`
+ extend type Query {
+ chainFee: Int!
+ }
+`
diff --git a/api/typeDefs/index.js b/api/typeDefs/index.js
index ef524d4e..11963a9f 100644
--- a/api/typeDefs/index.js
+++ b/api/typeDefs/index.js
@@ -16,6 +16,7 @@ import referrals from './referrals'
import price from './price'
import admin from './admin'
import blockHeight from './blockHeight'
+import chainFee from './chainFee'
import image from './image'
const common = gql`
@@ -37,4 +38,4 @@ const common = gql`
`
export default [common, user, item, itemForward, message, wallet, lnurl, notifications, invite,
- sub, upload, growth, rewards, referrals, price, admin, blockHeight, image]
+ sub, upload, growth, rewards, referrals, price, admin, blockHeight, chainFee, image]
diff --git a/components/chain-fee.js b/components/chain-fee.js
new file mode 100644
index 00000000..f61092dc
--- /dev/null
+++ b/components/chain-fee.js
@@ -0,0 +1,29 @@
+import { createContext, useContext, useMemo } from 'react'
+import { useQuery } from '@apollo/client'
+import { SSR } from '../lib/constants'
+import { CHAIN_FEE } from '../fragments/chainFee'
+
+export const ChainFeeContext = createContext({
+ fee: 0
+})
+
+export const useChainFee = () => useContext(ChainFeeContext)
+
+export const ChainFeeProvider = ({ chainFee, children }) => {
+ const { data } = useQuery(CHAIN_FEE, {
+ ...(SSR
+ ? {}
+ : {
+ pollInterval: 30000,
+ nextFetchPolicy: 'cache-and-network'
+ })
+ })
+ const value = useMemo(() => ({
+ fee: data?.chainFee ?? chainFee ?? 0
+ }), [data, chainFee])
+ return (
+