Skip to content

Commit

Permalink
refactor: request api, renterd and sia central sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Apr 16, 2024
1 parent f97a2c6 commit 859a281
Show file tree
Hide file tree
Showing 67 changed files with 372 additions and 612 deletions.
6 changes: 6 additions & 0 deletions .changeset/breezy-bees-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/request': minor
---

The request API has been updated to return [data, error, response].
6 changes: 6 additions & 0 deletions .changeset/chilly-pants-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'explorer': minor
'website': minor
---

The site now uses the updated Sia Central SDK.
5 changes: 5 additions & 0 deletions .changeset/odd-pets-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@siafoundation/sia-central-js': minor
---

The SDK has been updated to use the request library.
8 changes: 2 additions & 6 deletions apps/explorer/app/address/[id]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getSiaCentralAddress } from '@siafoundation/sia-central-js'
import { humanSiacoin, humanSiafund } from '@siafoundation/units'
import { getOGImage } from '../../../components/OGImageEntity'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { truncate } from '@siafoundation/design-system'

export const revalidate = 0
Expand All @@ -16,13 +15,10 @@ export const contentType = 'image/png'

export default async function Image({ params }) {
const id = params?.id as string
const { data: a } = await getSiaCentralAddress({
const [a] = await siaCentral.address({
params: {
id,
},
config: {
api: siaCentralApi,
},
})

if (!a) {
Expand Down
12 changes: 2 additions & 10 deletions apps/explorer/app/address/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getSiaCentralAddress } from '@siafoundation/sia-central-js'
import { Address } from '../../../components/Address'
import { Metadata } from 'next'
import { routes } from '../../../config/routes'
import { buildMetadata } from '../../../lib/utils'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { notFound } from 'next/navigation'
import { truncate } from '@siafoundation/design-system'

Expand All @@ -23,19 +22,12 @@ export const revalidate = 0

export default async function Page({ params }) {
const id = params?.id as string
const { data: a, error } = await getSiaCentralAddress({
const [a] = await siaCentral.address({
params: {
id,
},
config: {
api: siaCentralApi,
},
})

if (error) {
throw Error(error)
}

if (a?.unspent_siacoins == undefined) {
return notFound()
}
Expand Down
8 changes: 2 additions & 6 deletions apps/explorer/app/block/[id]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getSiaCentralBlock } from '@siafoundation/sia-central-js'
import { humanDate } from '@siafoundation/units'
import { getOGImage } from '../../../components/OGImageEntity'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { truncate } from '@siafoundation/design-system'

export const revalidate = 0
Expand All @@ -16,13 +15,10 @@ export const contentType = 'image/png'

export default async function Image({ params }) {
const id = params?.id as string
const { data: b } = await getSiaCentralBlock({
const [b] = await siaCentral.block({
params: {
id,
},
config: {
api: siaCentralApi,
},
})

if (!b || !b.block) {
Expand Down
12 changes: 2 additions & 10 deletions apps/explorer/app/block/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { getTitleId } from '@siafoundation/design-system'
import { Block } from '../../../components/Block'
import { routes } from '../../../config/routes'
import { Metadata } from 'next'
import { getSiaCentralBlock } from '@siafoundation/sia-central-js'
import { buildMetadata } from '../../../lib/utils'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { notFound } from 'next/navigation'

export function generateMetadata({ params }): Metadata {
Expand Down Expand Up @@ -34,19 +33,12 @@ export const revalidate = 0

export default async function Page({ params }) {
const id = params?.id as string
const { data: b, error } = await getSiaCentralBlock({
const [b] = await siaCentral.block({
params: {
id,
},
config: {
api: siaCentralApi,
},
})

if (error) {
throw Error(error)
}

if (!b?.block) {
return notFound()
}
Expand Down
19 changes: 6 additions & 13 deletions apps/explorer/app/contract/[id]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {
getSiaCentralContract,
getSiaCentralExchangeRates,
} from '@siafoundation/sia-central-js'
import { humanBytes, humanDate } from '@siafoundation/units'
import { getOGImage } from '../../../components/OGImageEntity'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { truncate } from '@siafoundation/design-system'
import { lowerCase } from '@technically/lodash'
import { siacoinToFiat } from '../../../lib/currency'
Expand All @@ -25,18 +21,15 @@ const currency = currencyOptions.find((c) => c.id === 'usd') as CurrencyOption
export default async function Image({ params }) {
const id = params?.id as string

const [{ data: c }, { data: r }] = await Promise.all([
getSiaCentralContract({
const [[c], [r]] = await Promise.all([
siaCentral.contract({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
}),
])
Expand Down
41 changes: 13 additions & 28 deletions apps/explorer/app/contract/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { SiaCentralContract } from '@siafoundation/sia-central-types'
import {
getSiaCentralContract,
getSiaCentralExchangeRates,
getSiaCentralTransaction,
} from '@siafoundation/sia-central-js'
import { ContractView } from '../../../components/ContractView'
import { Metadata } from 'next'
import { routes } from '../../../config/routes'
import { buildMetadata } from '../../../lib/utils'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import { notFound } from 'next/navigation'
import { truncate } from '@siafoundation/design-system'

Expand All @@ -28,50 +23,40 @@ export const revalidate = 0

export default async function Page({ params }) {
const id = params?.id as string
const [{ data: c, error }, { data: r }] = await Promise.all([
getSiaCentralContract({
const [[c, error], [r]] = await Promise.all([
siaCentral.contract({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
}),
])

if (error) {
throw Error(error)
throw error
}

if (!c?.contract) {
return notFound()
}

const contract = c.contract
const formationTxnId = getFormationTxnId(contract)
const finalRevisionTxnId = contract?.transaction_id || ''
const formationTxnId = getFormationTxnId(c.contract)
const finalRevisionTxnId = c.contract?.transaction_id || ''

const [{ data: ft }, { data: rt }] = await Promise.all([
getSiaCentralTransaction({
const [[ft], [rt]] = await Promise.all([
siaCentral.transaction({
params: {
id: formationTxnId,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralTransaction({
siaCentral.transaction({
params: {
id: finalRevisionTxnId,
},
config: {
api: siaCentralApi,
},
}),
])

Expand All @@ -82,7 +67,7 @@ export default async function Page({ params }) {

return (
<ContractView
contract={contract}
contract={c.contract}
rates={r?.rates}
renewedFrom={renewedFrom}
renewedTo={renewedTo}
Expand Down
19 changes: 6 additions & 13 deletions apps/explorer/app/host/[id]/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import {
getSiaCentralExchangeRates,
getSiaCentralHost,
} from '@siafoundation/sia-central-js'
import { getOGImage } from '../../../components/OGImageEntity'
import { siaCentralApi } from '../../../config'
import { siaCentral } from '../../../config/siaCentral'
import {
getDownloadCost,
getDownloadSpeed,
Expand All @@ -29,18 +25,15 @@ export const contentType = 'image/png'

export default async function Image({ params }) {
const id = params?.id as string
const [{ data: h }, { data: r }] = await Promise.all([
getSiaCentralHost({
const [[h], [r]] = await Promise.all([
siaCentral.host({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
}),
])
Expand Down
21 changes: 7 additions & 14 deletions apps/explorer/app/host/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
getSiaCentralExchangeRates,
getSiaCentralHost,
} from '@siafoundation/sia-central-js'
import { Metadata } from 'next'
import { routes } from '../../../config/routes'
import { buildMetadata } from '../../../lib/utils'
import { Host } from '../../../components/Host'
import { siaCentralApi } from '../../../config'
import { notFound } from 'next/navigation'
import { truncate } from '@siafoundation/design-system'
import { siaCentral } from '../../../config/siaCentral'

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

export default async function Page({ params }) {
const id = params?.id as string
const [{ data: h, error }, { data: r }] = await Promise.all([
getSiaCentralHost({
const [[h, error], [r]] = await Promise.all([
siaCentral.host({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
}),
])

if (error) {
throw Error(error)
throw error
}

if (!h?.host) {
Expand Down
23 changes: 6 additions & 17 deletions apps/explorer/app/opengraph-image.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
getSiaCentralBlockLatest,
getSiaCentralHostsNetworkMetrics,
} from '@siafoundation/sia-central-js'
import { getOGImage } from '../components/OGImage'
import { network, siaCentralApi } from '../config'
import { network } from '../config'
import { humanBytes } from '@siafoundation/units'
import { PreviewValue } from '../components/OGImage/Preview'
import { siaCentral } from '../config/siaCentral'

export const revalidate = 0

Expand All @@ -18,20 +15,12 @@ export const size = {
export const contentType = 'image/png'

export default async function Image() {
const [{ data: metrics }, { data: latestBlock }] = await Promise.all([
getSiaCentralHostsNetworkMetrics({
config: {
api: siaCentralApi,
},
}),
getSiaCentralBlockLatest({
config: {
api: siaCentralApi,
},
}),
const [[metrics], [latestBlock]] = await Promise.all([
siaCentral.hostsNetworkMetrics(),
siaCentral.blockLatest(),
])

const lastBlockHeight = Number(latestBlock?.block.height || 0)
const lastBlockHeight = Number(latestBlock?.block?.height || 0)

const values: PreviewValue[] = []
if (latestBlock) {
Expand Down
Loading

0 comments on commit 859a281

Please sign in to comment.