Skip to content

Commit

Permalink
trade: throttle price streams for better ui
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Apr 3, 2024
1 parent 1147bc5 commit 51ac0dd
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions trade.renegade.fi/contexts/PriceContext/price-context.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Exchange, PriceReporterWs, Token } from "@renegade-fi/renegade-js"
import React, {
createContext,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from "react"

Expand All @@ -29,16 +29,14 @@ const PriceContext = createContext<{
) => number | undefined
} | null>(null)

const UPDATE_THRESHOLD_MS = 1000

const invalid = ["USDT", "BUSD", "CBETH", "RNG"]

export const PriceProvider = ({ children }: { children: React.ReactNode }) => {
const [priceReporter, setPriceReporter] = useState<PriceReporterWs | null>(
null
)
const [prices, setPrices] = useState<Record<string, number>>({})
const [lastUpdate, setLastUpdate] = useState<Record<string, number>>({})
const lastUpdateRef = useRef<Record<string, number>>({})
const [attempted, setAttempted] = useState<Record<string, boolean>>({})
useEffect(() => {
const priceReporter = new PriceReporterWs(
Expand All @@ -50,41 +48,44 @@ export const PriceProvider = ({ children }: { children: React.ReactNode }) => {
}
}, [])

const handleSubscribe = useCallback(
(exchange: Exchange, base: string, quote: string, decimals: number) => {
if (!priceReporter || invalid.includes(base)) return
const handleSubscribe = (
exchange: Exchange,
base: string,
quote: string,
decimals: number
) => {
if (!priceReporter || invalid.includes(base)) return

const topic = getTopic(exchange, base, quote)
if (attempted[topic]) return
const topic = getTopic(exchange, base, quote)
if (attempted[topic]) return

let lastUpdate = 0
const now = Date.now()
if (now - lastUpdate <= UPDATE_THRESHOLD_MS) {
return
}
lastUpdate = now
priceReporter.subscribeToTokenPair(
exchange,
new Token({ ticker: base }),
new Token({ ticker: quote || "USDT" }),
(price) => {
const now = Date.now()
const lastUpdateTime = lastUpdateRef.current[topic] || 0

priceReporter.subscribeToTokenPair(
exchange,
new Token({ ticker: base }),
new Token({ ticker: quote || "USDT" }),
(price) => {
setPrices((prevPrices) => {
if (
prevPrices[topic]?.toFixed(decimals) !==
Number(price).toFixed(decimals)
) {
return { ...prevPrices, [topic]: Number(price) }
}
return prevPrices
})
setLastUpdate((prev) => ({ ...prev, [topic]: Date.now() }))
const randomThreshold = Math.random() * 1000 + 200
if (now - lastUpdateTime < randomThreshold) {
return
}
)
setAttempted((prev) => ({ ...prev, [topic]: true }))
},
[attempted, priceReporter]
)

setPrices((prevPrices) => {
if (
prevPrices[topic]?.toFixed(decimals) !==
Number(price).toFixed(decimals)
) {
return { ...prevPrices, [topic]: Number(price) }
}
return prevPrices
})
lastUpdateRef.current[topic] = now
}
)
setAttempted((prev) => ({ ...prev, [topic]: true }))
}

const handleGetPrice = (exchange: Exchange, base: string, quote: string) => {
const topic = getTopic(exchange, base, quote)
Expand All @@ -97,7 +98,7 @@ export const PriceProvider = ({ children }: { children: React.ReactNode }) => {
quote: string
) => {
const topic = getTopic(exchange, base, quote)
return lastUpdate[topic]
return lastUpdateRef.current[topic]
}

return (
Expand Down

0 comments on commit 51ac0dd

Please sign in to comment.