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 dafdce2
Show file tree
Hide file tree
Showing 69 changed files with 530 additions and 683 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilly-pants-provide.md
@@ -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
@@ -0,0 +1,5 @@
---
'@siafoundation/sia-central-js': minor
---

The SDK has been updated to use the request library.
19 changes: 9 additions & 10 deletions apps/explorer/app/address/[id]/opengraph-image.tsx
@@ -1,8 +1,8 @@
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'
import { to } from '@siafoundation/request'

export const revalidate = 0

Expand All @@ -16,14 +16,13 @@ export const contentType = 'image/png'

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

if (!a) {
return getOGImage(
Expand Down
21 changes: 10 additions & 11 deletions apps/explorer/app/address/[id]/page.tsx
@@ -1,11 +1,11 @@
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'
import { to } from '@siafoundation/request'

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

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

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

if (a?.unspent_siacoins == undefined) {
Expand Down
19 changes: 9 additions & 10 deletions apps/explorer/app/block/[id]/opengraph-image.tsx
@@ -1,8 +1,8 @@
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'
import { to } from '@siafoundation/request'

export const revalidate = 0

Expand All @@ -16,14 +16,13 @@ export const contentType = 'image/png'

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

if (!b || !b.block) {
return getOGImage(
Expand Down
21 changes: 10 additions & 11 deletions apps/explorer/app/block/[id]/page.tsx
Expand Up @@ -2,10 +2,10 @@ 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'
import { to } from '@siafoundation/request'

export function generateMetadata({ params }): Metadata {
const id = decodeURIComponent((params?.id as string) || '')
Expand Down Expand Up @@ -34,17 +34,16 @@ export const revalidate = 0

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

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

if (!b?.block) {
Expand Down
36 changes: 17 additions & 19 deletions apps/explorer/app/contract/[id]/opengraph-image.tsx
@@ -1,14 +1,11 @@
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'
import { CurrencyOption, currencyOptions } from '@siafoundation/react-core'
import { to } from '@siafoundation/request'

export const revalidate = 0

Expand All @@ -25,20 +22,21 @@ 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({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
},
}),
const [[c], [r]] = await Promise.all([
to(
siaCentral.contract({
params: {
id,
},
})
),
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
])

if (!c || !c.contract) {
Expand Down
76 changes: 36 additions & 40 deletions apps/explorer/app/contract/[id]/page.tsx
@@ -1,16 +1,12 @@
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'
import { to } from '@siafoundation/request'

export function generateMetadata({ params }): Metadata {
const id = decodeURIComponent((params?.id as string) || '')
Expand All @@ -28,51 +24,51 @@ 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({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
},
}),
const [[c, error], [r]] = await Promise.all([
to(
siaCentral.contract({
params: {
id,
},
})
),
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
])

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

if (!c?.contract) {
const contract = c?.contract

if (!contract) {
return notFound()
}

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

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

const formationTransaction = ft?.transaction
Expand Down
36 changes: 17 additions & 19 deletions apps/explorer/app/host/[id]/opengraph-image.tsx
@@ -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 @@ -14,6 +10,7 @@ import {
} from '@siafoundation/units'
import { truncate } from '@siafoundation/design-system'
import { CurrencyOption, currencyOptions } from '@siafoundation/react-core'
import { to } from '@siafoundation/request'

export const revalidate = 0

Expand All @@ -29,20 +26,21 @@ 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({
params: {
id,
},
config: {
api: siaCentralApi,
},
}),
getSiaCentralExchangeRates({
config: {
api: siaCentralApi,
},
}),
const [[h], [r]] = await Promise.all([
to(
siaCentral.host({
params: {
id,
},
})
),
to(
siaCentral.exchangeRates({
params: {
currencies: 'sc',
},
})
),
])

if (!h || !h.host) {
Expand Down

0 comments on commit dafdce2

Please sign in to comment.