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 Oct 1, 2024
1 parent 60de770 commit 2da4c65
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 206 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.
88 changes: 52 additions & 36 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,10 @@ import { notFound } from 'next/navigation'
import { stripPrefix, truncate } from '@siafoundation/design-system'
import { to } from '@siafoundation/request'
import { explored } from '../../../config/explored'
import {
ExplorerFileContract,
FileContractElement,
} from '@siafoundation/explored-types'

export function generateMetadata({ params }): Metadata {
const id = decodeURIComponent((params?.id as string) || '')
Expand All @@ -25,14 +28,7 @@ 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 [[rate, rateError]] = await Promise.all([
to(
siaCentral.exchangeRates({
params: {
Expand All @@ -42,44 +38,58 @@ export default async function Page({ params }) {
),
])

if (error) {
throw error
}
if (rateError) throw rateError
if (!rate) return notFound()

const contract = c?.contract
// Grab the contract and previous revisions data.
const [
[contract, contractError],
[previousRevisions, previousRevisionsError],
] = await Promise.all([
to(explored.contractByID({ params: { id } })),
to<
FileContractElement[] // Not sure why this is necessary here.
>(explored.contractRevisions({ params: { id } })),
])

if (!contract) {
return notFound()
}
if (contractError) throw contractError
if (previousRevisionsError) throw previousRevisionsError
if (!contract || !previousRevisions) return notFound()

const formationTxnId = getFormationTxnId(contract)
const finalRevisionTxnId = contract?.transaction_id || ''
// What should we do here? What's going on now is wrong, I'm sure, and
// lots of things below here in the file and children hinge on this.
const formationTxnId = getFormationTxnId(contract, previousRevisions)
const finalRevisionTxnId = contract.confirmationTransactionID || ''

const [[ft], [rt]] = await Promise.all([
// Fetch our formation and finalRevision transactions
const [
[formationTransaction, formationTransactionError],
[renewalTransaction, renewalTransactionError],
] = await Promise.all([
to(
siaCentral.transaction({
explored.transactionByID({
params: {
id: formationTxnId,
},
})
),
to(
siaCentral.transaction({
explored.transactionByID({
params: {
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 (renewalTransactionError) throw renewalTransactionError
if (!formationTransaction || !renewalTransaction) return notFound()

const renewedFrom = formationTransaction.fileContractRevisions[0]
const renewedTo = renewalTransaction.fileContracts[0]

// The following is a temporary addition to satisfy new Transaction
// component requirements for the Sia Central phase out on the
// transaction route.
// Do our transaction fetching, chain indices, and tip fetching.
const [
[transaction, transactionError],
[transactionChainIndices, transactionChainIndicesError],
Expand All @@ -101,6 +111,7 @@ export default async function Page({ params }) {
if (transactionError) throw transactionError
if (transactionChainIndicesError) throw transactionChainIndicesError
if (currentTipError) throw currentTipError

if (!transaction || !transactionChainIndices || !currentTip) return notFound()

// Use the first chainIndex from the above call to get our parent block.
Expand All @@ -113,8 +124,10 @@ export default async function Page({ params }) {

return (
<ContractView
previousRevisions={previousRevisions}
currentHeight={currentTip.height}
contract={contract}
rates={r?.rates}
rates={rate.rates}
renewedFrom={renewedFrom}
renewedTo={renewedTo}
formationTransaction={transaction}
Expand All @@ -128,12 +141,15 @@ export default async function Page({ params }) {
)
}

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
}
function getFormationTxnId(
contract: ExplorerFileContract,
previousRevisions: FileContractElement[]
) {
const id = contract.confirmationTransactionID
// if (previousRevisions.length) {
// id =
// previousRevisions[previousRevisions.length - 1]
// .transaction_id // Do we have an analague here?
// }
return id
}
49 changes: 27 additions & 22 deletions apps/explorer/components/Contract/ContractHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import {
Badge,
ContractTimeline,
// ContractTimeline,
LinkButton,
} from '@siafoundation/design-system'
import { ArrowLeft16, ArrowRight16 } from '@siafoundation/react-icons'
import { SiaCentralContract } from '@siafoundation/sia-central-types'
import { lowerCase } from '@technically/lodash'
import { routes } from '../../config/routes'
import { EntityHeading } from '../EntityHeading'
import { siaCentral } from '../../config/siaCentral'
import { to } from '@siafoundation/request'
import { ExplorerFileContract } from '@siafoundation/explored-types'

type Props = {
contract: SiaCentralContract
currentHeight: number
contract: ExplorerFileContract
renewedToId?: string
renewedFromId?: string
}

// This is dummy logic and I'm not even sure it makes sense, given the values.
function contractStatus(valid: boolean, resolved: boolean) {
if (!valid) return 'Invalid'
if (valid) {
if (!resolved) return 'Unresolved'
return 'Resolved'
}
}

export async function ContractHeader({
currentHeight,
contract,
renewedFromId,
renewedToId,
}: Props) {
const { id } = contract
const [latest, error] = await to(siaCentral.blockLatest())
if (error) {
console.error(error.stack)
}
return (
<div className="flex flex-col gap-x-4 gap-y-4 pb-4">
<div className="flex flex-wrap gap-x-4 gap-y-4 items-center justify-between">
Expand All @@ -50,10 +54,10 @@ export async function ContractHeader({
<Badge
interactive={false}
variant={
contract.status === 'obligationSucceeded' ? 'green' : 'gray'
contract.valid && contract.resolved ? 'green' : 'gray' // This is an assumption about how this is derived.
}
>
{lowerCase(contract.status)}
{contractStatus(contract.valid, contract.resolved)}
</Badge>
{renewedToId && renewedToId !== id && (
<LinkButton
Expand All @@ -66,26 +70,27 @@ export async function ContractHeader({
)}
</div>
</div>
{latest?.block && (
{currentHeight && (
<div className="px-1">
<ContractTimeline
currentHeight={latest.block.height || 0}
{/* <ContractTimeline
currentHeight={currentHeight || 0}
contractHeightStart={contract.negotiation_height}
contractHeightEnd={contract.expiration_height}
proofWindowHeightStart={contract.expiration_height}
proofWindowHeightEnd={contract.proof_deadline}
contractHeightEnd={contract.fileContract.windowStart}
proofWindowHeightStart={contract.fileContract.windowStart}
proofWindowHeightEnd={contract.fileContract.windowEnd}
proofHeight={contract.proof_height}
range={{
startHeight: contract.negotiation_height,
endHeight: Math.max(
latest.block.height || 0,
currentHeight || 0,
Math.round(
contract.proof_deadline +
(contract.expiration_height - contract.negotiation_height)
contract.fileContract.windowEnd +
(contract.fileContract.windowStart -
contract.negotiation_height)
)
),
}}
/>
/> */}
</div>
)}
</div>
Expand Down
Loading

0 comments on commit 2da4c65

Please sign in to comment.