drop the card in the position page
This commit is contained in:
parent
c5424d2dcc
commit
e39e5908e7
75
src/hooks/usePositionTokenURI.ts
Normal file
75
src/hooks/usePositionTokenURI.ts
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import { BigNumber } from 'ethers'
|
||||||
|
import JSBI from 'jsbi'
|
||||||
|
import { useMemo } from 'react'
|
||||||
|
import { NEVER_RELOAD, useSingleCallResult } from '../state/multicall/hooks'
|
||||||
|
import { useV3NFTPositionManagerContract } from './useContract'
|
||||||
|
|
||||||
|
type TokenId = number | JSBI | BigNumber
|
||||||
|
|
||||||
|
const STARTS_WITH = 'data:application/json;base64,'
|
||||||
|
|
||||||
|
type UsePositionTokenURIResult =
|
||||||
|
| {
|
||||||
|
valid: true
|
||||||
|
loading: false
|
||||||
|
result: {
|
||||||
|
name: string
|
||||||
|
description: string
|
||||||
|
image: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
valid: false
|
||||||
|
loading: false
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
valid: true
|
||||||
|
loading: true
|
||||||
|
}
|
||||||
|
|
||||||
|
export function usePositionTokenURI(tokenId: TokenId | undefined): UsePositionTokenURIResult {
|
||||||
|
const contract = useV3NFTPositionManagerContract()
|
||||||
|
const inputs = useMemo(() => [tokenId instanceof BigNumber ? tokenId.toHexString() : tokenId?.toString(16)], [
|
||||||
|
tokenId,
|
||||||
|
])
|
||||||
|
const { result, error, loading, valid } = useSingleCallResult(contract, 'tokenURI', inputs, NEVER_RELOAD, 1_600_000)
|
||||||
|
|
||||||
|
return useMemo(() => {
|
||||||
|
if (error || !valid || !tokenId) {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
loading: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (loading) {
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
loading: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!result) {
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
loading: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const [tokenURI] = result as [string]
|
||||||
|
if (!tokenURI || !tokenURI.startsWith(STARTS_WITH))
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
loading: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(atob(tokenURI.slice(STARTS_WITH.length)))
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
loading: false,
|
||||||
|
result: json,
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return { valid: false, loading: false }
|
||||||
|
}
|
||||||
|
}, [error, loading, result, tokenId, valid])
|
||||||
|
}
|
@ -5,10 +5,11 @@ import { useToken } from 'hooks/Tokens'
|
|||||||
import { useV3PositionFromTokenId } from 'hooks/useV3Positions'
|
import { useV3PositionFromTokenId } from 'hooks/useV3Positions'
|
||||||
import { Link, RouteComponentProps } from 'react-router-dom'
|
import { Link, RouteComponentProps } from 'react-router-dom'
|
||||||
import { unwrappedToken } from 'utils/wrappedCurrency'
|
import { unwrappedToken } from 'utils/wrappedCurrency'
|
||||||
|
import { usePositionTokenURI } from '../../hooks/usePositionTokenURI'
|
||||||
import { LoadingRows } from './styleds'
|
import { LoadingRows } from './styleds'
|
||||||
import styled from 'styled-components'
|
import styled from 'styled-components'
|
||||||
import { AutoColumn } from 'components/Column'
|
import { AutoColumn } from 'components/Column'
|
||||||
import { RowBetween, RowFixed } from 'components/Row'
|
import Row, { RowBetween, RowFixed } from 'components/Row'
|
||||||
import DoubleCurrencyLogo from 'components/DoubleLogo'
|
import DoubleCurrencyLogo from 'components/DoubleLogo'
|
||||||
import { ButtonText, TYPE } from 'theme'
|
import { ButtonText, TYPE } from 'theme'
|
||||||
import Badge, { BadgeVariant } from 'components/Badge'
|
import Badge, { BadgeVariant } from 'components/Badge'
|
||||||
@ -140,6 +141,8 @@ export function PositionPage({
|
|||||||
const token0 = useToken(token0Address)
|
const token0 = useToken(token0Address)
|
||||||
const token1 = useToken(token1Address)
|
const token1 = useToken(token1Address)
|
||||||
|
|
||||||
|
const metadata = usePositionTokenURI(parsedTokenId)
|
||||||
|
|
||||||
const currency0 = token0 ? unwrappedToken(token0) : undefined
|
const currency0 = token0 ? unwrappedToken(token0) : undefined
|
||||||
const currency1 = token1 ? unwrappedToken(token1) : undefined
|
const currency1 = token1 ? unwrappedToken(token1) : undefined
|
||||||
|
|
||||||
@ -312,6 +315,12 @@ export function PositionPage({
|
|||||||
</BadgeWrapper>
|
</BadgeWrapper>
|
||||||
</RowBetween>
|
</RowBetween>
|
||||||
</AutoColumn>
|
</AutoColumn>
|
||||||
|
<Row align="stretch">
|
||||||
|
{'result' in metadata ? (
|
||||||
|
<div style={{ marginRight: 12 }}>
|
||||||
|
<img src={metadata.result.image} />
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
<DarkCard>
|
<DarkCard>
|
||||||
<AutoColumn gap="lg">
|
<AutoColumn gap="lg">
|
||||||
<ResponsiveGrid>
|
<ResponsiveGrid>
|
||||||
@ -358,6 +367,7 @@ export function PositionPage({
|
|||||||
</ResponsiveGrid>
|
</ResponsiveGrid>
|
||||||
</AutoColumn>
|
</AutoColumn>
|
||||||
</DarkCard>
|
</DarkCard>
|
||||||
|
</Row>
|
||||||
<DarkCard>
|
<DarkCard>
|
||||||
<AutoColumn gap="lg">
|
<AutoColumn gap="lg">
|
||||||
<TYPE.label display="flex">
|
<TYPE.label display="flex">
|
||||||
|
Loading…
Reference in New Issue
Block a user