-
Notifications
You must be signed in to change notification settings - Fork 316
fix: guarding gas config in useTransactionCallback #5328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9addb75
fix: guarding gas config in useTransactionCallback
UncleBill 4a0a842
fixup! fix: guarding gas config in useTransactionCallback
UncleBill c98784a
refactor: formatCoutdown show tailing 0 time
UncleBill c46d2ab
fixup! refactor: formatCoutdown show tailing 0 time
UncleBill ae0f8bb
refactor: compact code (#5333)
guanbinrui 7ad838b
fixup! fixup! refactor: formatCoutdown show tailing 0 time
UncleBill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
35 changes: 5 additions & 30 deletions
35
packages/mask/src/plugins/MaskBox/helpers/formatCountdown.ts
This file contains hidden or 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,32 +1,7 @@ | ||
| const msPerSecond = 1000 | ||
| const msPerMinute = msPerSecond * 60 | ||
| const msPerHour = msPerMinute * 60 | ||
| const msPerDay = msPerHour * 24 | ||
| import formatDuration from 'date-fns/formatDuration' | ||
| import intervalToDuration from 'date-fns/intervalToDuration' | ||
|
|
||
| const units = [ | ||
| ['day', 'days'], | ||
| ['hour', 'hours'], | ||
| ['minute', 'minutes'], | ||
| ['second', 'seconds'], | ||
| ] | ||
|
|
||
| export function formatCountdown(countdown: number) { | ||
| if (countdown <= 0) return '' | ||
|
|
||
| const amounts: number[] = [] | ||
|
|
||
| ;[msPerDay, msPerHour, msPerMinute, msPerSecond].reduce((accumulator, x) => { | ||
| amounts.push(Math.floor(accumulator / x)) | ||
| return accumulator % x | ||
| }, countdown) | ||
|
|
||
| return amounts | ||
| .map((x, i) => { | ||
| if (x <= 0) return '' | ||
| if (x === 1) return `${x} ${units[i][0]}` | ||
| return `${x} ${units[i][1]}` | ||
| }) | ||
| .filter(Boolean) | ||
| .join(' ') | ||
| .trim() | ||
| export function formatCountdown(from: number, to: number) { | ||
| const duration = intervalToDuration({ start: from, end: to }) | ||
| return formatDuration(duration) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -35,12 +35,13 @@ import { useMaskBoxPurchasedTokens } from './useMaskBoxPurchasedTokens' | |
| import { formatCountdown } from '../helpers/formatCountdown' | ||
| import { useOpenBoxTransaction } from './useOpenBoxTransaction' | ||
| import { useMaskBoxMetadata } from './useMaskBoxMetadata' | ||
| import { useHeartBit } from './useHeartBit' | ||
| import { useHeartBeat } from './useHeartBeat' | ||
| import { useIsWhitelisted } from './useIsWhitelisted' | ||
| import { isGreaterThanOrEqualTo, isLessThanOrEqualTo, isZero, multipliedBy } from '@masknet/web3-shared-base' | ||
|
|
||
| function useContext(initialState?: { boxId: string }) { | ||
| const heartBit = useHeartBit() | ||
| const now = new Date() | ||
| const heartBeat = useHeartBeat() | ||
| const account = useAccount() | ||
| const chainId = useChainId() | ||
| const { NATIVE_TOKEN_ADDRESS } = useTokenConstants(ChainId.Mainnet) | ||
|
|
@@ -92,8 +93,8 @@ function useContext(initialState?: { boxId: string }) { | |
| const totalComputed = total && remaining && remaining > total ? remaining : total | ||
| const sold = Math.max(0, totalComputed - remaining) | ||
| const personalRemaining = Math.max(0, personalLimit - purchasedTokens.length) | ||
| const startAt = Number.parseInt(maskBoxCreationSuccessEvent?.returnValues.start_time ?? '0', 10) | ||
| const endAt = Number.parseInt(maskBoxCreationSuccessEvent?.returnValues.end_time ?? '0', 10) | ||
| const startAt = Number.parseInt(maskBoxCreationSuccessEvent?.returnValues.start_time || '0', 10) | ||
| const endAt = Number.parseInt(maskBoxCreationSuccessEvent?.returnValues.end_time || '0', 10) | ||
| const info: BoxInfo = { | ||
| boxId, | ||
| creator: maskBoxInfo.creator, | ||
|
|
@@ -105,6 +106,7 @@ function useContext(initialState?: { boxId: string }) { | |
| availableAmount: Math.min(personalRemaining, remaining), | ||
| startAt: startAt === 0 ? subDays(new Date(), 1) : fromUnixTime(startAt), | ||
| endAt: endAt === 0 ? addDays(new Date(), 1) : fromUnixTime(endAt), | ||
| started: maskBoxStatus.started, | ||
| total: totalComputed, | ||
| sold, | ||
| canceled: maskBoxStatus.canceled, | ||
|
|
@@ -135,17 +137,18 @@ function useContext(initialState?: { boxId: string }) { | |
|
|
||
| const boxState = useMemo(() => { | ||
| if (errorMaskBoxInfo || errorMaskBoxStatus || errorBoxInfo) return BoxState.ERROR | ||
| if (loadingMaskBoxInfo || loadingMaskBoxStatus || loadingBoxInfo) return BoxState.UNKNOWN | ||
| if (loadingMaskBoxInfo || loadingMaskBoxStatus || loadingBoxInfo) { | ||
| if (!maskBoxInfo && !boxInfo) return BoxState.UNKNOWN | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we already know something, don't lie. Actually, to avoid ui flickering when call retry callback. |
||
| } | ||
| if (maskBoxInfo && !boxInfo) return BoxState.UNKNOWN | ||
| if (!maskBoxInfo || !maskBoxStatus || !boxInfo) return BoxState.NOT_FOUND | ||
| if (maskBoxStatus.canceled) return BoxState.CANCELED | ||
| const now = new Date() | ||
| if (isGreaterThanOrEqualTo(boxInfo.tokenIdsPurchased.length, boxInfo.personalLimit)) return BoxState.DRAWED_OUT | ||
| if (isLessThanOrEqualTo(boxInfo.remaining, 0)) return BoxState.SOLD_OUT | ||
| if (boxInfo.startAt > now) return BoxState.NOT_READY | ||
| if (boxInfo.startAt > now || !boxInfo.started) return BoxState.NOT_READY | ||
| if (boxInfo.endAt < now || maskBoxStatus?.expired) return BoxState.EXPIRED | ||
| return BoxState.READY | ||
| }, [boxInfo, loadingBoxInfo, errorBoxInfo, maskBoxInfo, loadingMaskBoxInfo, errorMaskBoxInfo, heartBit]) | ||
| }, [boxInfo, loadingBoxInfo, errorBoxInfo, maskBoxInfo, loadingMaskBoxInfo, errorMaskBoxInfo, heartBeat]) | ||
|
|
||
| const isWhitelisted = useIsWhitelisted(boxInfo?.qualificationAddress, account) | ||
| const isQualifiedByContract = | ||
|
|
@@ -170,10 +173,10 @@ function useContext(initialState?: { boxId: string }) { | |
| case BoxState.EXPIRED: | ||
| return 'Ended' | ||
| case BoxState.NOT_READY: | ||
| const now = Date.now() | ||
| const nowAt = now.getTime() | ||
| const startAt = boxInfo?.startAt.getTime() ?? 0 | ||
| if (startAt <= now) return 'Loading...' | ||
| const countdown = formatCountdown(startAt - now) | ||
| if (startAt <= nowAt) return 'Syncing status...' | ||
| const countdown = formatCountdown(startAt, nowAt) | ||
| return countdown ? `Start sale in ${countdown}` : 'Loading...' | ||
| case BoxState.SOLD_OUT: | ||
| return 'Sold Out' | ||
|
|
@@ -186,7 +189,16 @@ function useContext(initialState?: { boxId: string }) { | |
| default: | ||
| unreachable(boxState) | ||
| } | ||
| }, [boxState, heartBit]) | ||
| }, [boxState, boxInfo?.startAt, heartBeat]) | ||
|
|
||
| useEffect(() => { | ||
| if (!boxInfo || boxInfo.started) return | ||
|
|
||
| if (boxInfo.startAt < now) { | ||
| retryMaskBoxStatus() | ||
| } | ||
| }, [boxInfo, heartBeat]) | ||
|
|
||
| //#endregion | ||
|
|
||
| //#region the box metadata | ||
|
|
@@ -253,8 +265,8 @@ function useContext(initialState?: { boxId: string }) { | |
| const canPurchase = !isBalanceInsufficient && isQualified && !!boxInfo?.personalRemaining | ||
| const allowToPurchase = boxState === BoxState.READY | ||
| const isAllowanceEnough = isNativeToken ? true : costAmount.lte(erc20Allowance ?? '0') | ||
| const { value: openBoxTransactionGasLimit = 0 } = useAsyncRetry(async () => { | ||
| if (!openBoxTransaction || !canPurchase || !allowToPurchase || !isAllowanceEnough) return 0 | ||
| const { value: openBoxTransactionGasLimit } = useAsyncRetry(async () => { | ||
| if (!openBoxTransaction || !canPurchase || !allowToPurchase || !isAllowanceEnough) return | ||
|
Comment on lines
+268
to
+269
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unset, let next step make the decision. |
||
| const estimatedGas = await openBoxTransaction.method.estimateGas(omit(openBoxTransaction.config, 'gas')) | ||
| return new BigNumber(estimatedGas).toNumber() | ||
| }, [openBoxTransaction, canPurchase, allowToPurchase, isAllowanceEnough]) | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
.../src/plugins/MaskBox/hooks/useHeartBit.ts → ...src/plugins/MaskBox/hooks/useHeartBeat.ts
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.