Skip to content

Commit

Permalink
Set era to 900 blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jvonasek committed Jun 12, 2024
1 parent 754f331 commit 7954e51
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 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
4 changes: 2 additions & 2 deletions src/i18n/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,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 +909,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
19 changes: 13 additions & 6 deletions src/sections/transaction/ReviewTransactionSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type ReviewTransactionSummaryProps = {
}

export const ReviewTransactionSummary: FC<ReviewTransactionSummaryProps> = ({
tx,
transactionValues,
xcallMeta,
editFeePaymentAssetEnabled,
Expand Down Expand Up @@ -116,11 +115,19 @@ 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}>
{era?.isLoading ? (
<Skeleton width={100} height={16} />
) : era?.deathDate ? (
t("transaction.mortal.expire", {
date: era.deathDate,
})
) : (
t("transaction.immortal.expire")
)}
</Text>
),
},
{
label: t("liquidity.reviewTransaction.modal.detail.nonce"),
Expand Down

0 comments on commit 7954e51

Please sign in to comment.