-
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.
Merge pull request #4811 from thematters/feat/ad
- Loading branch information
Showing
7 changed files
with
217 additions
and
34 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
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,111 @@ | ||
import { readContract } from '@wagmi/core' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { STORAGE_KEY_BILLBOARD } from '~/common/enums' | ||
import { | ||
BillboardOperatorABI, | ||
BillboardRegistryABI, | ||
storage, | ||
} from '~/common/utils' | ||
|
||
// custom hook level enums | ||
enum QueryStatus { | ||
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<QueryStatus>(QueryStatus.IDLE) | ||
|
||
const data = storage.get(STORAGE_KEY_BILLBOARD) as Record<string, any> | ||
const ttl = 3 * 60 * 1000 | ||
|
||
const isLoading = status === QueryStatus.LOADING | ||
const isError = status === QueryStatus.ERROR | ||
|
||
const resetData = () => { | ||
storage.set(STORAGE_KEY_BILLBOARD, { | ||
contentURI: '', | ||
redirectURI: '', | ||
expired: Date.now() + ttl, | ||
}) | ||
} | ||
|
||
// fetch board cotent if data is expired | ||
useEffect(() => { | ||
if (isLoading) { | ||
return | ||
} | ||
|
||
if (data?.expired >= Date.now()) { | ||
return | ||
} | ||
|
||
;(async () => { | ||
try { | ||
setStatus(QueryStatus.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) { | ||
storage.set(STORAGE_KEY_BILLBOARD, { | ||
contentURI: bid.contentURI, | ||
redirectURI: bid.redirectURI, | ||
expired: Date.now() + ttl, | ||
}) | ||
} else { | ||
// if no running ad or it hasn't been cleared yet | ||
resetData() | ||
} | ||
|
||
setStatus(QueryStatus.LOADED) | ||
} catch (error) { | ||
resetData() | ||
setStatus(QueryStatus.ERROR) | ||
} | ||
})() | ||
}, []) | ||
|
||
return { data, isLoading, isError } | ||
} |