Skip to content

Commit

Permalink
Merge pull request #1399 from galacticcouncil/default-era-fix
Browse files Browse the repository at this point in the history
Default era fix
  • Loading branch information
jvonasek authored Jun 12, 2024
2 parents 95ea94a + 9b4b4d4 commit f5a052f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
41 changes: 28 additions & 13 deletions src/api/era.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { ExtrinsicEra } from "@polkadot/types/interfaces/extrinsics"
import { bnToBn } from "@polkadot/util"
import { useTimestamp } from "./timestamp"
import { useMemo } from "react"
import { BLOCK_TIME } from "utils/constants"
import { PARACHAIN_BLOCK_TIME } from "utils/constants"

const DEFAULT_PERIOD = 900

export const useEra = (
era: ExtrinsicEra,
Expand All @@ -14,21 +16,27 @@ export const useEra = (
const blockNumber = bnToBn(hexBlockNumber)

const mortal = useMemo(() => {
if (blockNumber.isZero()) return null
if (era.isMortalEra) {
const mortal = era.asMortalEra
const period = new BN(mortal.period.toHex()) // Blocks validity

if (!blockNumber.isZero()) {
const birth = new BN(mortal.birth(blockNumber))
const death = new BN(mortal.death(blockNumber))
return {
birth,
death,
period,
}
const birth = new BN(mortal.birth(blockNumber))
const death = new BN(mortal.death(blockNumber))
return {
birth,
death,
period,
}
}
return null

const birth = new BN(blockNumber.toString())
const period = new BN(DEFAULT_PERIOD)
return {
birth,
death: birth.plus(period),
period,
}
}, [era, blockNumber])

const timestamp = useTimestamp(mortal?.birth, enabled)
Expand All @@ -38,14 +46,21 @@ export const useEra = (
const birthDate = new Date(timestamp.data)
const deathDate = addSeconds(
birthDate,
mortal.period.times(BLOCK_TIME).toNumber(),
mortal.period.times(PARACHAIN_BLOCK_TIME).toNumber(),
)
return {
birthDate,
deathDate,
period: mortal.period,
isLoading: false,
}
}

return null
}, [timestamp.data, mortal?.period])
return {
birthDate: null,
deathDate: null,
period: null,
isLoading: timestamp.isLoading,
}
}, [timestamp.data, timestamp.isLoading, mortal?.period])
}
9 changes: 5 additions & 4 deletions src/api/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export function useTimestamp(
enabled = true,
) {
const { api } = useRpcProvider()
const queryEnabled = !!blockNumber && enabled

return useQuery(
QUERY_KEYS.timestamp(blockNumber),
() =>
blockNumber !== null ? getTimestamp(api, blockNumber) : undefinedNoop(),
() => (queryEnabled ? getTimestamp(api, blockNumber) : undefinedNoop()),
{
enabled: !!blockNumber && enabled,
enabled: queryEnabled,
},
)
}
Expand All @@ -25,7 +26,7 @@ export async function getTimestamp(
api: ApiPromise,
blockNumber?: u32 | BigNumber,
) {
if (blockNumber != null) {
if (blockNumber) {
const blockHash = await api.rpc.chain.getBlockHash(blockNumber.toString())
const apiAt = await api.at(blockHash)
const now = await apiAt.query.timestamp.now()
Expand Down
7 changes: 4 additions & 3 deletions src/i18n/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@
"liquidity.reviewTransaction.modal.title": "Review transaction details",
"liquidity.reviewTransaction.modal.desc": "Please review your transaction",
"liquidity.reviewTransaction.modal.detail.cost": "Transaction cost:",
"liquidity.reviewTransaction.modal.detail.lifetime": "Transaction lifetime:",
"liquidity.reviewTransaction.modal.detail.lifetime": "Transaction expiration:",
"liquidity.reviewTransaction.modal.detail.lifetime.tooltip": "If the transaction is not included by this time, it will be cancelled.",
"liquidity.reviewTransaction.modal.detail.nonce": "Nonce:",
"liquidity.reviewTransaction.modal.detail.tip":"Tip the block author:",
"liquidity.reviewTransaction.modal.detail.tip.name":"Display user tip",
Expand Down Expand Up @@ -377,7 +378,7 @@
"otc.offers.table.header.status": "Order status",
"otc.offers.table.actions.fill": "Fill order",
"otc.offers.table.actions.cancel": "Cancel order",
"transaction.mortal.expire": "{{ date, dd/MM/yyyy hh:mm:ss }}",
"transaction.mortal.expire": "{{ date, dd/MM/yyyy HH:mm:ss }}",
"transaction.immortal.expire": "Never",
"walletConnect.provider.title": "Connect wallet",
"walletConnect.provider.description.default": "Select your wallet of choice.",
Expand Down Expand Up @@ -909,7 +910,7 @@
"migration.export.question": "Would you like to migrate all past app settings to the new app?",
"migration.export.button": "Migrate Settings",
"migration.import.overwrite.title": "Do you wish to overwrite your settings?",
"migration.import.overwrite.description": "You already transferred your settings on {{ date, dd/MM/yyyy hh:mm:ss }}. Do you wish to overwrite your current settings?",
"migration.import.overwrite.description": "You already transferred your settings on {{ date, dd/MM/yyyy HH:mm:ss }}. Do you wish to overwrite your current settings?",
"migration.import.overwrite.button": "Overwrite Settings",
"migration.import.confirm.title": "You have opened a migration link",
"migration.import.confirm.description": "Do you want to import new app settings?",
Expand Down
2 changes: 2 additions & 0 deletions src/sections/transaction/ReviewTransactionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const ReviewTransactionForm: FC<Props> = (props) => {
isLinkedAccount,
storedReferralCode,
tx,
era,
} = transactionValues.data

const isLinking = !isLinkedAccount && storedReferralCode
Expand Down Expand Up @@ -121,6 +122,7 @@ export const ReviewTransactionForm: FC<Props> = (props) => {
}

const signature = await tx.signAsync(address, {
era: era?.period?.toNumber(),
tip: tipAmount?.gte(0) ? tipAmount.toString() : undefined,
signer: wallet.signer,
// defer to polkadot/api to handle nonce w/ regard to mempool
Expand Down
1 change: 0 additions & 1 deletion src/sections/transaction/ReviewTransactionForm.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ export const useTransactionValues = ({
const era = useEra(
boundedTx.era,
bestNumber.data?.parachainBlockNumber.toString(),
boundedTx.era.isMortalEra,
)

const feePaymentValue = paymentInfo?.partialFee.toBigNumber() ?? BN_NAN
Expand Down
30 changes: 24 additions & 6 deletions src/sections/transaction/ReviewTransactionSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { useRpcProvider } from "providers/rpcProvider"
import { ReviewTransactionAuthorTip } from "sections/transaction/ReviewTransactionAuthorTip"
import { NATIVE_EVM_ASSET_SYMBOL } from "utils/evm"
import { Transaction } from "state/store"
import { InfoTooltip } from "components/InfoTooltip/InfoTooltip"
import { SInfoIcon } from "components/InfoTooltip/InfoTooltip.styled"

type ReviewTransactionSummaryProps = {
tx: SubmittableExtrinsic<"promise">
Expand All @@ -24,7 +26,6 @@ type ReviewTransactionSummaryProps = {
}

export const ReviewTransactionSummary: FC<ReviewTransactionSummaryProps> = ({
tx,
transactionValues,
xcallMeta,
editFeePaymentAssetEnabled,
Expand Down Expand Up @@ -116,11 +117,28 @@ export const ReviewTransactionSummary: FC<ReviewTransactionSummaryProps> = ({
rows={[
{
label: t("liquidity.reviewTransaction.modal.detail.lifetime"),
content: tx.era.isMortalEra
? t("transaction.mortal.expire", {
date: era?.deathDate,
})
: t("transaction.immortal.expire"),
content: (
<Text fs={14} sx={{ flex: "row", gap: 4, align: "center" }}>
{era?.isLoading ? (
<Skeleton width={100} height={16} />
) : era?.deathDate ? (
<>
{t("transaction.mortal.expire", {
date: era.deathDate,
})}
<InfoTooltip
text={t(
"liquidity.reviewTransaction.modal.detail.lifetime.tooltip",
)}
>
<SInfoIcon />
</InfoTooltip>
</>
) : (
t("transaction.immortal.expire")
)}
</Text>
),
},
{
label: t("liquidity.reviewTransaction.modal.detail.nonce"),
Expand Down

0 comments on commit f5a052f

Please sign in to comment.