Skip to content

Commit

Permalink
replacing last few pieces of feeder calls
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaysngh-mudrex committed Apr 9, 2024
1 parent b44d82f commit 62a2fb9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
56 changes: 47 additions & 9 deletions src/hooks/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TokenAddressMap } from 'lib/hooks/useTokenList/utils'
import { useMemo } from 'react'
import { useAppSelector } from 'state/hooks'
import { isL2ChainId } from 'utils/chains'
import ERC20_ABI from 'abis/erc20.json'

import { useAllLists, useCombinedActiveList, useCombinedTokenMapFromUrls } from '../state/lists/hooks'
import { WrappedTokenInfo } from '../state/lists/wrappedTokenInfo'
Expand All @@ -16,6 +17,8 @@ import { isAddressValidForStarknet } from 'utils/addresses'
import { useTokenContract } from './useContractV2'
import { NEVER_RELOAD, useSingleCallResult } from 'state/multicall/hooks'
import { ETH_ADDRESS, WETH } from 'constants/tokens'
import { useContractRead } from '@starknet-react/core'
import { num } from 'starknet'

type Maybe<T> = T | null | undefined

Expand Down Expand Up @@ -191,28 +194,63 @@ export function useToken(tokenAddress?: string | null): Token | null | undefined
const { chainId } = useAccountDetails()
const tokens = useDefaultActiveTokens(chainId)
const address = isAddressValidForStarknet(tokenAddress)
const validTokenAddress = address ? address : undefined
const token: Token | undefined = address ? tokens[address] : undefined

const tokenContract = useTokenContract(address ? address : undefined)
const { data: rpc_tokenName } = useContractRead({
functionName: 'name',
args: [],
abi: ERC20_ABI,
address: validTokenAddress,
watch: true,
})

const tokenName = useSingleCallResult(token ? undefined : tokenContract, 'name', undefined, NEVER_RELOAD)
const { data: rpc_symbol } = useContractRead({
functionName: 'symbol',
args: [],
abi: ERC20_ABI,
address: validTokenAddress,
watch: true,
})

const symbol = useSingleCallResult(token ? undefined : tokenContract, 'symbol', undefined, NEVER_RELOAD)
const { data: rpc_decimals } = useContractRead({
functionName: 'decimals',
args: [],
abi: ERC20_ABI,
address: validTokenAddress,
watch: true,
})

const decimals = useSingleCallResult(token ? undefined : tokenContract, 'decimals', undefined, NEVER_RELOAD)
const tokenName = useMemo(() => {
const nameResult: any = rpc_tokenName
if (!nameResult || !nameResult?.name) return undefined
return num.toHex(nameResult.name)
}, [rpc_tokenName])

const symbol = useMemo(() => {
const symbolResult: any = rpc_symbol
if (!symbolResult || !symbolResult?.symbol) return undefined
return num.toHex(symbolResult.symbol)
}, [rpc_symbol])

const decimals = useMemo(() => {
const decimalResult: any = rpc_decimals
if (!decimalResult || !decimalResult?.decimals) return undefined
return num.toHex(decimalResult.decimals)
}, [rpc_decimals])

return useMemo(() => {
if (token) return token
if (!chainId || !address) return undefined
if (address === ETH_ADDRESS) return WETH[chainId]
if (decimals.loading || symbol.loading || tokenName.loading) return null
if (decimals.result) {
// if (decimals.loading || symbol.loading || tokenName.loading) return null
if (decimals) {
const token = new Token(
chainId,
address,
parseInt(decimals.result[0]),
parseStringFromArgs(symbol.result?.[0]),
parseStringFromArgs(symbol.result?.[0])
parseInt(decimals),
parseStringFromArgs(symbol),
parseStringFromArgs(symbol)
)
return token
}
Expand Down
25 changes: 21 additions & 4 deletions src/hooks/useCurrentBlockTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import { useMemo } from 'react'
import { useInterfaceMulticall } from './useContract'
import { useMulticallContract } from './useContractV2'
import { useSingleCallResult } from 'state/multicall/hooks'
import { useAccountDetails } from './starknet-react'
import { useContractRead } from '@starknet-react/core'
import { MULTICALL_NETWORKS } from 'contracts/multicall'
import { DEFAULT_CHAIN_ID } from 'constants/tokens'
import { MULTICALL_ABI } from 'contracts/multicall'

// gets the current timestamp from the blockchain
export default function useCurrentBlockTimestamp(): BigNumber | undefined {
const multicall = useMulticallContract()
return useSingleCallResult(multicall, 'get_current_block_timestamp')?.result?.[0]
const useCurrentBlockTimestamp = () => {
const { chainId } = useAccountDetails()
const { data: blockTimeStamp } = useContractRead({
functionName: 'get_current_block_timestamp',
args: [],
abi: MULTICALL_ABI,
address: MULTICALL_NETWORKS[chainId ?? DEFAULT_CHAIN_ID],
watch: true,
})

if (!blockTimeStamp) return undefined
const { block_timestamp } = blockTimeStamp as any

return BigNumber.from(block_timestamp.toString())
}

export default useCurrentBlockTimestamp

0 comments on commit 62a2fb9

Please sign in to comment.