-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ad): create react hook for fetching billboard content
- Loading branch information
Showing
6 changed files
with
191 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,119 @@ | ||
export const BillboardABI = [ | ||
export const BillboardOperatorABI = [ | ||
{ | ||
type: 'function', | ||
name: 'getLatestEpoch', | ||
inputs: [ | ||
{ | ||
name: 'tokenId_', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: 'epoch', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'getBid', | ||
inputs: [ | ||
{ | ||
name: 'tokenId_', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
name: 'epoch_', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
name: 'bidder_', | ||
type: 'address', | ||
internalType: 'address', | ||
}, | ||
], | ||
name: 'getBoard', | ||
outputs: [ | ||
{ | ||
name: 'bid', | ||
type: 'tuple', | ||
internalType: 'struct IBillboardRegistry.Bid', | ||
components: [ | ||
{ | ||
internalType: 'address', | ||
name: 'creator', | ||
type: 'address', | ||
name: 'price', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
internalType: 'string', | ||
name: 'name', | ||
type: 'string', | ||
name: 'tax', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
internalType: 'string', | ||
name: 'description', | ||
name: 'contentURI', | ||
type: 'string', | ||
internalType: 'string', | ||
}, | ||
{ | ||
internalType: 'string', | ||
name: 'location', | ||
name: 'redirectURI', | ||
type: 'string', | ||
internalType: 'string', | ||
}, | ||
{ | ||
internalType: 'string', | ||
name: 'contentURI', | ||
type: 'string', | ||
name: 'placedAt', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
internalType: 'string', | ||
name: 'redirectURI', | ||
type: 'string', | ||
name: 'updatedAt', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
name: 'isWon', | ||
type: 'bool', | ||
internalType: 'bool', | ||
}, | ||
{ | ||
name: 'isWithdrawn', | ||
type: 'bool', | ||
internalType: 'bool', | ||
}, | ||
], | ||
internalType: 'struct IBillboardRegistry.Board', | ||
name: 'board', | ||
type: 'tuple', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
}, | ||
] as const | ||
|
||
export const BillboardRegistryABI = [ | ||
{ | ||
type: 'function', | ||
name: 'highestBidder', | ||
inputs: [ | ||
{ | ||
name: '', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
{ | ||
name: '', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: '', | ||
type: 'address', | ||
internalType: 'address', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
}, | ||
] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { readContract } from '@wagmi/core' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { BillboardOperatorABI, BillboardRegistryABI } from '~/common/utils' | ||
|
||
// custom hook level enums | ||
enum BillboardQueryStatus { | ||
IDLE = 'idle', | ||
LOADING = 'loading', | ||
LOADED = 'loaded', | ||
ERROR = 'error', | ||
} | ||
|
||
type Props = { | ||
id: number | ||
chainId: number | ||
operatorAddress: `0x${string}` | ||
registryAddress: `0x${string}` | ||
} | ||
|
||
export const useBillboard = ({ | ||
id, | ||
chainId, | ||
operatorAddress, | ||
registryAddress, | ||
}: Props) => { | ||
const [status, setStatus] = useState<BillboardQueryStatus>( | ||
BillboardQueryStatus.IDLE | ||
) | ||
const [data, setData] = useState<Record<string, any>>({}) | ||
|
||
const isLoading = status === BillboardQueryStatus.LOADING | ||
const isError = status === BillboardQueryStatus.ERROR | ||
|
||
useEffect(() => { | ||
if (isLoading) { | ||
return | ||
} | ||
|
||
;(async () => { | ||
try { | ||
setStatus(BillboardQueryStatus.LOADING) | ||
|
||
const tokenId = BigInt(id) | ||
const currEpoch = await readContract({ | ||
abi: BillboardOperatorABI, | ||
address: operatorAddress, | ||
chainId, | ||
functionName: 'getLatestEpoch', | ||
args: [tokenId], | ||
}) | ||
|
||
if (currEpoch < 2n) { | ||
return | ||
} | ||
|
||
const epoch = currEpoch - 2n | ||
const bidder = await readContract({ | ||
abi: BillboardRegistryABI, | ||
address: registryAddress, | ||
chainId, | ||
functionName: 'highestBidder', | ||
args: [tokenId, epoch], | ||
}) | ||
const bid = await readContract({ | ||
abi: BillboardOperatorABI, | ||
address: operatorAddress, | ||
chainId, | ||
functionName: 'getBid', | ||
args: [tokenId, epoch, bidder], | ||
}) | ||
|
||
if (bid && bid.isWon) { | ||
setData({ | ||
...(bid.contentURI ? { contentURI: bid.contentURI } : {}), | ||
...(bid.redirectURI ? { redirectURI: bid.redirectURI } : {}), | ||
}) | ||
} | ||
|
||
setStatus(BillboardQueryStatus.LOADED) | ||
} catch (error) { | ||
setStatus(BillboardQueryStatus.ERROR) | ||
} | ||
})() | ||
}, []) | ||
|
||
return { data, isLoading, isError } | ||
} |