Skip to content

Commit

Permalink
Fix/improvements (#266)
Browse files Browse the repository at this point in the history
* fix(apr): hotfix

* format

* fix(liquidity-from-coinhall): liquidity provided from coinhall

* fix(improvements): pools initialized after incentive info available

My positions now properly shown

Multiple code improvements

* fix(improvements): multiple

Removed migaloo from chain list

Resolved recurring query console error

Code cleaning and resolving compiling errors
  • Loading branch information
worrex authored Jun 21, 2023
1 parent a3b9203 commit 9db2c58
Show file tree
Hide file tree
Showing 34 changed files with 392 additions and 627 deletions.
2 changes: 1 addition & 1 deletion components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const links = [
link: 'https://tfm.com/bridge',
},
]
const Navbar = ({}) => {
const Navbar = () => {
const { disconnect } = useWallet()
const [{ key, chainId, network }, setWalletState] =
useRecoilState(walletState)
Expand Down
6 changes: 4 additions & 2 deletions components/Pages/Incentivize/hooks/useFactoryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ type FactoryConfig = {
amount: string
denom: string
}
minUnbondingDuration: any
maxUnbondingDuration: any
}

const useFactoryConfig = (incentiveFactory: string) => {
const { address, client } = useRecoilValue(walletState)
const { client } = useRecoilValue(walletState)

const { data: config = {} } = useQuery<FactoryConfig>({
const { data: config } = useQuery<FactoryConfig>({
queryKey: ['factoryConfig', incentiveFactory],
queryFn: () =>
client
Expand Down
10 changes: 4 additions & 6 deletions components/Pages/Incentivize/hooks/useIncentivePoolInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface FlowData {
tokenSymbol: string
}
export interface IncentivePoolInfo {
flowData: FlowData[]
flowData?: FlowData[]

poolId: string
}
Expand All @@ -71,7 +71,7 @@ export const useIncentivePoolInfo = (
setPoolsWithAprAnd24HrVolume(poolData)
}
fetchPoolData()
}, [currentChain, pools?.length])
}, [currentChain, pools?.length, client])

let poolAssets = []

Expand All @@ -81,7 +81,7 @@ export const useIncentivePoolInfo = (
.filter((v, i, a) => a.findIndex((t) => t.denom === v.denom) === i)
}
const { data: flowPoolData } = useQuery(
['apr', currentEpochData, prices],
['apr', currentEpochData, prices, pools, poolsWithAprAnd24HrVolume],
() =>
getPoolFlowData(
client,
Expand Down Expand Up @@ -125,7 +125,7 @@ const getPoolFlowData = async (
prices,
poolsWithAprAnd24HrVolume
): Promise<IncentivePoolInfo[]> => {
const poolFlowData = pools
return pools
? await Promise.all(
pools?.map(async (pool) => {
if (pool.staking_address === '') {
Expand Down Expand Up @@ -214,6 +214,4 @@ const getPoolFlowData = async (
})
)
: []

return poolFlowData
}
13 changes: 6 additions & 7 deletions components/Pages/Liquidity/Overview.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Text, VStack } from '@chakra-ui/react'
import { useMemo } from 'react'
import { Action } from './Action'
import usePositions from './hooks/usePositions'
import useLockedPositions from 'components/Pages/Liquidity/hooks/useLockedPositions'
import useRewards from './hooks/useRewards'
import { Positions } from './Positions'
import { Rewards } from './Rewards'
Expand All @@ -13,14 +13,13 @@ type Props = {

const Overview = ({ poolId, dailyEmissions }: Props) => {
const { rewards, totalValue } = useRewards(poolId)
const { data: positions = [] } = usePositions(poolId)

const tableData = useMemo(() => {
return positions?.map((item) => ({
const { data: positionData = [] } = useLockedPositions(poolId)
const positions = useMemo(() => {
return positionData?.map((item) => ({
...item,
action: <Action item={item} poolId={poolId} />,
}))
}, [positions])
}, [positionData])

return (
<VStack alignItems="flex-start" gap="16px" py={5}>
Expand All @@ -32,7 +31,7 @@ const Overview = ({ poolId, dailyEmissions }: Props) => {

{positions.length > 0 ? (
<Box backgroundColor="#151515" width="full" borderRadius="15px">
<Positions positions={tableData} />
<Positions positions={positions} />
</Box>
) : (
<Box width="full" textAlign="center">
Expand Down
3 changes: 1 addition & 2 deletions components/Pages/Liquidity/Positions.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { HStack, Divider, Button, Box } from '@chakra-ui/react'
import { useState } from 'react'
import { Position } from './hooks/usePositions'
import { PositionsTable } from './PositionsTable'

const STATES = ['all', 'active', 'unbonding']

type Props = Position & {
type Props = {
positions: any[]
}

Expand Down
9 changes: 5 additions & 4 deletions components/Pages/Liquidity/PositionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type TableProps = {
weight: string
action: JSX.Element
state: string
assets: any[]
}

const columnHelper = createColumnHelper<TableProps>()

const columns = (totlaValue, totalDollarValue) => {
const columns = (totalValue, totalDollarValue) => {
return [
columnHelper.accessor('duration', {
header: () => (
Expand All @@ -51,13 +52,13 @@ const columns = (totlaValue, totalDollarValue) => {
enableSorting: true,
}),
columnHelper.accessor('value', {
header: (info) => {
header: () => {
return (
<TooltipWithChildren
label={`Value($${totalDollarValue})`}
isHeading={true}
>
<AvailableRewards data={totlaValue} />
<AvailableRewards data={totalValue} />
</TooltipWithChildren>
)
},
Expand Down Expand Up @@ -174,7 +175,7 @@ export const PositionsTable = ({ columnFilters, positions }) => {
<TableContainer color="white" width="full">
<Table size="md" variant="unstyled" margin="auto" width="full">
<Thead borderBottom="1px solid rgba(255, 255, 255, 0.1)" color="gray">
{table.getHeaderGroups().map((headerGroup, index) => (
{table.getHeaderGroups().map((headerGroup) => (
<Tr key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<Th
Expand Down
4 changes: 2 additions & 2 deletions components/Pages/Liquidity/hooks/useClaimableLP.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMemo } from 'react'
import usePositions from './usePositions'
import useLockedPositions from 'components/Pages/Liquidity/hooks/useLockedPositions'

const useClaimableLP = ({ poolId }) => {
const { data: positions = [] } = usePositions(poolId)
const { data: positions = [] } = useLockedPositions(poolId)

return useMemo(() => {
//filter out positions that are not open and are not unbound and add total of amount
Expand Down
4 changes: 2 additions & 2 deletions components/Pages/Liquidity/hooks/useIsNewPosition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useMemo } from 'react'
import usePositions from '../../Liquidity/hooks/usePositions'
import useLockedPositions from 'components/Pages/Liquidity/hooks/useLockedPositions'
import { NUMBER_OF_SECONDS_IN_DAY } from 'constants/index'

const useIsNewPosition = ({ bondingDays, poolId }) => {
const { data: positions = [] } = usePositions(poolId)
const { data: positions = [] } = useLockedPositions(poolId)

return useMemo(() => {
const match = positions?.filter((p) => {
Expand Down
143 changes: 143 additions & 0 deletions components/Pages/Liquidity/hooks/useLockedPositions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import dayjs from 'dayjs'
import usePrices from 'hooks/usePrices'
import { useTokenList } from 'hooks/useTokenList'
import { fromChainAmount, num } from 'libs/num'
import { useQueryPoolLiquidity } from 'queries/useQueryPools'
import { useQuery } from 'react-query'
import { useRecoilValue } from 'recoil'
import { walletState } from 'state/atoms/walletAtoms'
import { formatSeconds } from 'util/formatSeconds'
import { TokenInfo } from 'queries/usePoolsListQuery'

export type Position = {
amount: number
weight: string
duration: string
unbonding_duration: number
assets: TokenInfo & { dollarValue: number; assetAmount: number }[]
value: number
state: string
action: null
isOpen: boolean
formattedTime: string
}

export const lpPositionToAssets = ({
totalReserve,
totalLpSupply,
providedLp,
}) => {
return [
num(totalReserve?.[0])
.times(providedLp)
.div(totalLpSupply)
.dp(6)
.toNumber(),
num(totalReserve?.[1])
.times(providedLp)
.div(totalLpSupply)
.dp(6)
.toNumber(),
]
}
export const fetchPositions = async (
client,
prices,
incentiveAddress,
address,
pool_assets,
totalReserve,
totalLpSupply
) => {
const data = await client?.queryContractSmart(incentiveAddress, {
positions: { address },
})
return data.positions
.map((p) => {
const positions = []

// open position
const open = { ...(p?.open_position || {}) }
open.formatedTime = formatSeconds(open.unbonding_duration)
open.isOpen = true
if (p?.open_position) positions.push(open)

// closed position
const close = { ...(p?.closed_position || {}) }
const today = dayjs(new Date())
const unbonding = dayjs.unix(close.unbonding_timestamp)
const diff = unbonding.diff(today, 'second')
close.formatedTime = formatSeconds(diff)
close.isOpen = false
if (p?.closed_position) positions.push(close)

return positions.map((position) => {
const lpAssets = lpPositionToAssets({
totalReserve,
totalLpSupply,
providedLp: position.amount,
})
const assets = pool_assets.map((asset, i) => {
const assetAmount = fromChainAmount(lpAssets[i], asset.decimals)
const dollarValue = num(assetAmount)
.times(prices?.[asset.symbol] || 0)
.toNumber()
return {
...asset,
assetAmount: parseFloat(assetAmount),
dollarValue,
}
})
return {
...position,
duration: position.formatedTime,
weight: position.weight,
assets,
value: assets.reduce((acc, asset) => {
return acc + Number(asset.dollarValue)
}, 0),
state: position.isOpen
? 'active'
: diff <= 0
? 'unbound'
: 'unbonding',
action: null,
}
})
})
.flat()
}
const useLockedPositions = (poolId: string) => {
const [{ liquidity = {}, pool_assets = [], staking_address = null } = {}] =
useQueryPoolLiquidity({ poolId })
const totalLpSupply = liquidity?.available?.total?.tokenAmount || 0
const totalReserve = liquidity?.reserves?.total || [0, 0]
const { address, client } = useRecoilValue(walletState)
const tokens = useTokenList()
const prices = usePrices()

return useQuery({
queryKey: [
'positions',
address,
staking_address,
poolId,
tokens,
pool_assets,
prices,
],
queryFn: (): Promise<Position[]> =>
fetchPositions(
client,
prices,
staking_address,
address,
pool_assets,
totalReserve,
totalLpSupply
),
enabled: !!address && !!client && !!staking_address,
})
}

export default useLockedPositions
28 changes: 0 additions & 28 deletions components/Pages/Liquidity/hooks/useMultiplicator.ts

This file was deleted.

Loading

0 comments on commit 9db2c58

Please sign in to comment.