Skip to content

Commit

Permalink
feat(explorer): use explored for the contracts route
Browse files Browse the repository at this point in the history
  • Loading branch information
telestrial committed Nov 13, 2024
1 parent 73bbbf2 commit bac6213
Show file tree
Hide file tree
Showing 8 changed files with 327 additions and 249 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-goats-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'explorer': minor
---

The contracts route now uses explored except for the rates request.
59 changes: 28 additions & 31 deletions apps/explorer/app/contract/[id]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { humanBytes, humanDate } from '@siafoundation/units'
import { humanBytes } from '@siafoundation/units'
import { getOGImage } from '../../../components/OGImageEntity'
import { siaCentral } from '../../../config/siaCentral'
import { truncate } from '@siafoundation/design-system'
import { lowerCase } from '@technically/lodash'
import { siacoinToFiat } from '../../../lib/currency'
import { CurrencyOption, currencyOptions } from '@siafoundation/react-core'
import { to } from '@siafoundation/request'
import { explored } from '../../../config/explored'
import { blockHeightToHumanDate } from '../../../lib/time'
import { determineContractStatus } from '../../../lib/contracts'

export const revalidate = 0

Expand All @@ -22,24 +24,20 @@ const currency = currencyOptions.find((c) => c.id === 'usd') as CurrencyOption
export default async function Image({ params }) {
const id = params?.id as string

const [[c], [r]] = await Promise.all([
to(
siaCentral.contract({
params: {
id,
},
})
),
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
])
const [[contract, contractError], [currentTip, currentTipError], [r]] =
await Promise.all([
to(explored.contractByID({ params: { id } })),
to(explored.consensusTip()),
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
])

if (!c || !c.contract) {
if (contractError || !contract || currentTipError || !currentTip) {
return getOGImage(
{
id,
Expand All @@ -54,19 +52,16 @@ export default async function Image({ params }) {
const values = [
{
label: 'data size',
value: humanBytes(c.contract.file_size),
value: humanBytes(contract.filesize),
},
{
label: 'expiration',
value: humanDate(c.contract.expiration_timestamp, {
dateStyle: 'short',
timeStyle: 'short',
}),
value: blockHeightToHumanDate(currentTip.height, contract.windowStart),
},
{
label: 'payout',
value: siacoinToFiat(
c.contract.payout,
contract.payout,
r && {
currency,
rate: r.rates.sc.usd,
Expand All @@ -75,18 +70,20 @@ export default async function Image({ params }) {
},
]

const contractStatus = determineContractStatus(contract)

return getOGImage(
{
id,
title: truncate(c.contract.id, 30),
title: truncate(contract.id, 30),
subtitle: 'contract',
status: lowerCase(c.contract.status),
status: contractStatus,
statusColor:
c.contract.status === 'obligationSucceeded'
contractStatus === 'in progress'
? 'amber'
: contractStatus === 'obligation succeeded'
? 'green'
: c.contract.status === 'obligationFailed'
? 'red'
: 'amber',
: 'red',
initials: 'C',
values,
},
Expand Down
141 changes: 70 additions & 71 deletions apps/explorer/app/contract/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SiaCentralContract } from '@siafoundation/sia-central-types'
import { ContractView } from '../../../components/ContractView'
import { Metadata } from 'next'
import { routes } from '../../../config/routes'
Expand All @@ -8,6 +7,7 @@ import { notFound } from 'next/navigation'
import { stripPrefix, truncate } from '@siafoundation/design-system'
import { to } from '@siafoundation/request'
import { explored } from '../../../config/explored'
import { ChainIndex, ExplorerFileContract } from '@siafoundation/explored-types'

export function generateMetadata({ params }): Metadata {
const id = decodeURIComponent((params?.id as string) || '')
Expand All @@ -24,116 +24,115 @@ export function generateMetadata({ params }): Metadata {
export const revalidate = 0

export default async function Page({ params }) {
const id = params?.id as string
const [[c, error], [r]] = await Promise.all([
to(
siaCentral.contract({
params: {
id,
},
})
),
const id = params?.id

// Grab the contract and previous revisions data.
const [
[rate, rateError],
[contract, contractError],
[previousRevisions, previousRevisionsError],
[currentTip, currentTipError],
] = await Promise.all([
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
to(explored.contractByID({ params: { id } })),
to<ExplorerFileContract[]>(explored.contractRevisions({ params: { id } })),
to(explored.consensusTip()),
])

if (error) {
throw error
}
if (rateError) throw rateError

if (contractError) throw contractError
if (!contract) return notFound()

const contract = c?.contract
if (previousRevisionsError) throw previousRevisionsError
if (!previousRevisions)
throw new Error('No previousRevisions found in successful request')

if (!contract) {
return notFound()
}
if (currentTipError) throw currentTipError
if (!currentTip) throw new Error('No currentTip found in successful request')

const formationTxnId = getFormationTxnId(contract)
const finalRevisionTxnId = contract?.transaction_id || ''
const formationTxnID =
previousRevisions[0].transactionID ?? contract.transactionID
const finalRevisionTxnID =
previousRevisions[previousRevisions.length - 1].transactionID ??
contract.transactionID

const [[ft], [rt]] = await Promise.all([
// Fetch our formation and finalRevision transaction information.
const [
[formationTransaction, formationTransactionError],
[renewalTransaction, renewalTransactionError],
] = await Promise.all([
to(
siaCentral.transaction({
explored.transactionByID({
params: {
id: formationTxnId,
id: formationTxnID,
},
})
),
to(
siaCentral.transaction({
explored.transactionByID({
params: {
id: finalRevisionTxnId,
id: finalRevisionTxnID,
},
})
),
])

const formationTransaction = ft?.transaction
const renewedFrom = formationTransaction?.contract_revisions?.[0]
const renewalTransaction = rt?.transaction
const renewedTo = renewalTransaction?.storage_contracts?.[0]
if (formationTransactionError) throw formationTransactionError
if (!formationTransaction)
throw new Error('No formation transaction found in successful reqeust')

// The following is a temporary addition to satisfy new Transaction
// component requirements for the Sia Central phase out on the
// transaction route.
const [
[transaction, transactionError],
[transactionChainIndices, transactionChainIndicesError],
[currentTip, currentTipError],
] = await Promise.all([
to(
explored.transactionByID({
params: { id: formationTransaction?.id || '' },
})
),
to(
explored.transactionChainIndices({
params: { id: formationTransaction?.id || '' },
})
),
to(explored.consensusTip()),
])
if (renewalTransactionError) throw renewalTransactionError
if (!renewalTransaction)
throw new Error('No renewal transaction found in successful reqeust')

if (transactionError) throw transactionError
if (transactionChainIndicesError) throw transactionChainIndicesError
if (currentTipError) throw currentTipError
if (!transaction || !transactionChainIndices || !currentTip) return notFound()
const renewedFromID =
formationTransaction.fileContractRevisions?.[0].id ?? null
const renewedToID = renewalTransaction.fileContracts?.[0].id ?? null

const [formationTxnChainIndices, formationTxnChainIndicesError] = await to<
ChainIndex[]
>(
explored.transactionChainIndices({
params: { id: formationTransaction.id },
})
)

if (formationTxnChainIndicesError) throw formationTxnChainIndicesError
if (!formationTxnChainIndices)
throw new Error('No formationTxnChainIndices found in successful request')

// Use the first chainIndex from the above call to get our parent block.
const [parentBlock, parentBlockError] = await to(
explored.blockByID({ params: { id: transactionChainIndices[0].id } })
explored.blockByID({ params: { id: formationTxnChainIndices[0].id } })
)

if (parentBlockError) throw parentBlockError
if (!parentBlock) return notFound()
if (!parentBlock)
throw new Error('No parentBlock found in successful request')

return (
<ContractView
previousRevisions={previousRevisions}
currentHeight={currentTip.height}
contract={contract}
rates={r?.rates}
renewedFrom={renewedFrom}
renewedTo={renewedTo}
formationTransaction={transaction}
rates={rate?.rates}
renewedFromID={renewedFromID ? stripPrefix(renewedFromID) : renewedFromID}
renewedToID={renewedToID ? stripPrefix(renewedToID) : renewedToID}
formationTransaction={formationTransaction}
formationTxnChainIndex={formationTxnChainIndices}
formationTransactionHeaderData={{
id: stripPrefix(transaction.id),
blockHeight: transactionChainIndices[0].height,
confirmations: currentTip.height - transactionChainIndices[0].height,
id: stripPrefix(formationTransaction.id),
blockHeight: formationTxnChainIndices[0].height,
confirmations: currentTip.height - formationTxnChainIndices[0].height,
timestamp: parentBlock.timestamp,
}}
/>
)
}

function getFormationTxnId(contract: SiaCentralContract) {
let id = contract?.transaction_id
if (contract?.previous_revisions?.length) {
id =
contract.previous_revisions[contract.previous_revisions?.length - 1]
.transaction_id
}
return id
}
Loading

0 comments on commit bac6213

Please sign in to comment.