Skip to content

Commit

Permalink
Use deposit time after hedge
Browse files Browse the repository at this point in the history
  • Loading branch information
KMKoushik committed Jan 25, 2023
1 parent 559bb7a commit 811b3f2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useUserCrabTxHistory } from '../useUserCrabTxHistory'
import { useUserCrabV2TxHistory } from '../useUserCrabV2TxHistory'
import { CrabStrategyTxType, CrabStrategyV2TxType } from '../../types'
import { toTokenAmount } from '@utils/calculations'
import { useAtom, useAtomValue } from 'jotai'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
import { indexAtom } from 'src/state/controller/atoms'
import useAppCallback from '../useAppCallback'
import useAppMemo from '../useAppMemo'
Expand All @@ -16,6 +16,8 @@ import {
crabQueuedInEthAtom,
currentCrabPositionValueInETHAtom,
currentCrabPositionValueInETHAtomV2,
firstDepositBlockAtom,
firstDepositTimeAtom,
} from 'src/state/crab/atoms'
import BigNumber from 'bignumber.js'

Expand Down Expand Up @@ -112,6 +114,8 @@ export const useCrabPositionV2 = (user: string) => {
const currentEthValue = useAtomValue(currentCrabPositionValueInETHAtomV2)
const [txToSearch, setTxToSearch] = useState<string | undefined>(undefined)
const currentQueuedCrabEth = useAtomValue(crabQueuedInEthAtom)
const setFirstDepositTime = useSetAtom(firstDepositTimeAtom)
const setFirstDepositBlock = useSetAtom(firstDepositBlockAtom)

const { loading: txHistoryLoading, data: txHistoryData, startPolling, stopPolling } = useUserCrabV2TxHistory(user)

Expand All @@ -129,6 +133,8 @@ export const useCrabPositionV2 = (user: string) => {
return { remainingDepositUsd: BIG_ZERO, remainingDepositEth: BIG_ZERO }
}

setFirstDepositTime(Number(txHistoryData[0].timestamp))
setFirstDepositBlock(Number(txHistoryData[0].blockNumber))
const { totalSharesDeposited, totalSharesWithdrawn, totalUSDDeposit, totalETHDeposit } = txHistoryData?.reduce(
(acc, tx) => {
if (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/frontend/src/queries/squeeth/userCrabV2TxQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const USER_CRAB_V2_TX_QUERY = gql`
erc20Token
erc20Amount
transaction
blockNumber
}
}
`
Expand Down
3 changes: 3 additions & 0 deletions packages/frontend/src/state/crab/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export const crabLoadingAtomV2 = atom((get) => {
export const crabv2StrategyFilterStartDateAtom = atom<Date>(new Date(CRABV2_START_DATE))
export const crabv2StrategyFilterEndDateAtom = atom<Date>(new Date())

export const firstDepositTimeAtom = atom(0)
export const firstDepositBlockAtom = atom(0)

export const useCrabPnLV2ChartData = () => {
const startDate = useAtomValue(crabv2StrategyFilterStartDateAtom)
const endDate = useAtomValue(crabv2StrategyFilterEndDateAtom)
Expand Down
12 changes: 10 additions & 2 deletions packages/frontend/src/state/crab/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import {
crabTotalSupplyV2Atom,
totalUsdcQueuedAtom,
totalCrabQueuedAtom,
firstDepositTimeAtom,
firstDepositBlockAtom,
} from './atoms'
import { addressesAtom } from '../positions/atoms'
import {
Expand Down Expand Up @@ -1232,6 +1234,8 @@ export const useCrabProfitData = () => {
})
const crabPosition = useAtomValue(currentCrabPositionValueAtomV2)
const currentEthPrice = useOnChainETHPrice()
const firstDepositTime = useAtomValue(firstDepositTimeAtom)
const firstDepositBlock = useAtomValue(firstDepositBlockAtom)

const getVault = useGetVault()
const { getTwapSafe } = useOracle()
Expand Down Expand Up @@ -1272,12 +1276,16 @@ export const useCrabProfitData = () => {
useEffect(() => {
if (!loading && data && data.strategy) {
if ( crabPosition.isGreaterThan(0)) {
setData(data.strategy.lastHedgeBlockNumber , data.strategy.lastHedgeTimestamp)
if (data.strategy.lastHedgeBlockNumber < firstDepositBlock) {
setData(firstDepositBlock , firstDepositTime)
} else {
setData(data.strategy.lastHedgeBlockNumber , data.strategy.lastHedgeTimestamp)
}
} else {
setData()
}
}
}, [loading, data, crabPosition])
}, [loading, data, crabPosition, firstDepositBlock, firstDepositTime])

return { profitData, loading }
}

0 comments on commit 811b3f2

Please sign in to comment.