Skip to content

Commit

Permalink
use fetch instead of ws
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Feb 26, 2024
1 parent f285641 commit 44cd134
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
33 changes: 17 additions & 16 deletions trade.renegade.fi/hooks/use-balance.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { renegade } from "@/app/providers"
import { useRenegade } from "@/contexts/Renegade/renegade-context"
import { Balance, BalanceId } from "@renegade-fi/renegade-js"
import { useEffect, useState } from "react"

export const useBalance = () => {
// const [balances, setBalances] = useState<Record<BalanceId, Balance>>({})
// const { accountId } = useRenegade()
const [balances, setBalances] = useState<Record<BalanceId, Balance>>({})
const { accountId } = useRenegade()

// useEffect(() => {
// if (!accountId) return
// const interval = setInterval(async () => {
// const fetchedBalances = await renegade
// .queryWallet(accountId)
// .then(() => renegade.getBalances(accountId))
// setBalances(fetchedBalances)
// }, 5000)
useEffect(() => {
if (!accountId) return
const interval = setInterval(async () => {
const fetchedBalances = await renegade
.queryWallet(accountId)
.then(() => renegade.getBalances(accountId))
setBalances(fetchedBalances)
}, 5000)

// return () => {
// clearInterval(interval)
// }
// }, [accountId])
return () => {
clearInterval(interval)
}
}, [accountId])

// return balances
const { balances } = useRenegade()
return balances
}
69 changes: 36 additions & 33 deletions trade.renegade.fi/hooks/use-order.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { renegade } from "@/app/providers"
import { useRenegade } from "@/contexts/Renegade/renegade-context"
import { safeLocalStorageGetItem, safeLocalStorageSetItem } from "@/lib/utils"
import { Order, OrderId } from "@renegade-fi/renegade-js"
import { useEffect, useState } from "react"

export const useOrders = () => {
// const { accountId } = useRenegade()
// const [orders, setOrders] = useState<Record<OrderId, Order>>({})

// useEffect(() => {
// if (!accountId) return
// const interval = setInterval(async () => {
// const existingOrders = safeLocalStorageGetItem(`orders-${accountId}`)
// const existingOrdersArray = existingOrders
// ? existingOrders.split(",")
// : []

// const fetchedOrders = await renegade
// .queryWallet(accountId)
// .then(() => renegade.getOrders(accountId))
// setOrders(fetchedOrders)

// const uniqueNewOrderIds = Object.keys(fetchedOrders).filter(
// (orderId) => !existingOrdersArray.includes(orderId)
// )

// existingOrdersArray.push(...uniqueNewOrderIds)
// const updatedOrders = existingOrdersArray.join(",")

// safeLocalStorageSetItem(`orders-${accountId}`, updatedOrders)
// }, 1000)

// return () => {
// clearInterval(interval)
// }
// }, [accountId])

// return orders
const { orders } = useRenegade()
const { accountId } = useRenegade()
const [orders, setOrders] = useState<Record<OrderId, Order>>({})

useEffect(() => {
if (!accountId) return
const interval = setInterval(async () => {
const existingOrders = safeLocalStorageGetItem(`orders-${accountId}`)
const existingOrdersArray = existingOrders
? existingOrders.split(",")
: []

const fetchedOrders = await renegade
.queryWallet(accountId)
.then(() => renegade.getOrders(accountId))
setOrders(fetchedOrders)

const uniqueNewOrderIds = Object.keys(fetchedOrders).filter(
(orderId) => !existingOrdersArray.includes(orderId)
)

existingOrdersArray.push(...uniqueNewOrderIds)
const updatedOrders = existingOrdersArray.join(",")

safeLocalStorageSetItem(`orders-${accountId}`, updatedOrders)
}, 1000)

return () => {
clearInterval(interval)
}
}, [accountId])

// const { orders } = useRenegade()
return orders
}

0 comments on commit 44cd134

Please sign in to comment.