Skip to content

Commit

Permalink
[ONC-7] Move to opensea api v2 (#7449)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikki Kang <kangaroo233@gmail.com>
  • Loading branch information
raymondjacobson and nicoback committed Feb 6, 2024
1 parent 2642104 commit 5ec9672
Show file tree
Hide file tree
Showing 20 changed files with 331 additions and 253 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/hooks/useCoinflowAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const useCoinflowWithdrawalAdapter = () => {
})
}
initWallet()
}, [audiusBackend])
}, [audiusBackend, feePayerOverride])

return adapter
}
Expand Down
57 changes: 35 additions & 22 deletions packages/common/src/hooks/useDownloadTrackButtons.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useEffect, useMemo, useState } from 'react'

import type { AudiusSdk } from '@audius/sdk'
import { isEqual } from 'lodash'
import { shallowEqual, useSelector } from 'react-redux'
import { usePrevious } from 'react-use'

import dayjs from '~/utils/dayjs'
import { encodeHashId } from '~/utils/hashIds'

import { ID } from '../models/Identifiers'
import { stemCategoryFriendlyNames, StemCategory } from '../models/Stems'
Expand All @@ -11,10 +15,6 @@ import { getHasAccount } from '../store/account/selectors'
import { getTrack, getTracks } from '../store/cache/tracks/selectors'
import { CommonState } from '../store/commonStore'
import { getCurrentUploads } from '../store/stems-upload/selectors'
import { usePrevious } from 'react-use'
import type { AudiusSdk } from '@audius/sdk'
import { encodeHashId } from '~/utils/hashIds'
import { isEqual } from 'lodash'

export type DownloadButtonConfig = {
state: ButtonState
Expand Down Expand Up @@ -101,30 +101,43 @@ export const useCurrentStems = ({ trackId }: { trackId: ID }) => {
return { stemTracks, track }
}

export const useFileSizes = ({ audiusSdk, trackIds }: {audiusSdk: () => Promise<AudiusSdk>, trackIds: ID[] }) => {
export const useFileSizes = ({
audiusSdk,
trackIds
}: {
audiusSdk: () => Promise<AudiusSdk>
trackIds: ID[]
}) => {
const previousTrackIds = usePrevious(trackIds)
const [sizes, setSizes] = useState<{[trackId: ID]: number}>({})
const [sizes, setSizes] = useState<{ [trackId: ID]: number }>({})
useEffect(() => {
if (!isEqual(previousTrackIds, trackIds)) {
const asyncFn = async () => {
const sdk = await audiusSdk()
const sizeResults = await Promise.all(trackIds.map(async trackId => {
if (sizes[trackId]) {
return ({ trackId, size: sizes[trackId] })
}
try {
const res = await sdk.tracks.inspectTrack({ trackId: encodeHashId(trackId) })
const size = res?.data?.size ?? null
return ({ trackId, size })
} catch (e) {
console.error(e)
return ({ trackId, size: null })
}
const sizeResults = await Promise.all(
trackIds.map(async (trackId) => {
if (sizes[trackId]) {
return { trackId, size: sizes[trackId] }
}
try {
const res = await sdk.tracks.inspectTrack({
trackId: encodeHashId(trackId)
})
const size = res?.data?.size ?? null
return { trackId, size }
} catch (e) {
console.error(e)
return { trackId, size: null }
}
})
)
setSizes((sizes) => ({
...sizes,
...sizeResults.reduce((acc, curr) => {
acc[curr.trackId] = curr.size
return acc
}, {} as { trackId: ID; size: number })
}))
setSizes(sizes => ({ ...sizes, ...sizeResults.reduce((acc, curr) => {
acc[curr.trackId] = curr.size
return acc
}, {} as { trackId: ID, size: number }) }) )
}
asyncFn()
}
Expand Down
81 changes: 48 additions & 33 deletions packages/common/src/models/OpenSea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type AssetContract = {
payout_address: Nullable<string>
}

type AssetCollection = {
slug: string
export type OpenSeaCollection = {
collection: string
name: string
description: string
external_url: string
project_url: string
image_url: string
}

Expand All @@ -43,41 +43,56 @@ type AssetPerson = {
} | null
type AssetOwner = AssetPerson
type AssetCreator = AssetPerson
export type OpenSeaAsset = {
token_id: string
name: Nullable<string>
description: Nullable<string>
external_link: Nullable<string>
permalink: Nullable<string>
image_url: Nullable<string>
image_preview_url: Nullable<string>
image_thumbnail_url: Nullable<string>
image_original_url: Nullable<string>
animation_url: Nullable<string>
animation_original_url: Nullable<string>
youtube_url: Nullable<string>
background_color: Nullable<string>
owner: Nullable<AssetOwner>
creator: Nullable<AssetCreator>
asset_contract: Nullable<AssetContract>
collection: Nullable<AssetCollection>

// This metadata object is absurd. It is the combination of
// some real standards and some just random fields we get back.
// Use it to try to display whatever we can. Yay NFTs.
export type OpenSeaNftMetadata = {
token_id?: string
name?: string
description?: string
external_url?: string
permalink?: string
image?: string
image_url?: string
image_preview_url?: string
image_thumbnail_url?: string
image_original_url?: string
animation_url?: string
animation_original_url?: string
youtube_url?: string
background_color?: string
owner?: AssetOwner
creator?: AssetCreator
asset_contract?: AssetContract
}

export type OpenSeaAssetExtended = OpenSeaAsset & { wallet: string }
export type OpenSeaNft = {
identifier: string
collection: string
contract: string
token_standard: string
name: string
description: string
image_url: string
metadata_url: string
opensea_url: string
// Audius added fields
wallet: string
}

export type OpenSeaNftExtended = OpenSeaNft &
OpenSeaNftMetadata & { collectionMetadata?: OpenSeaCollection }

export type OpenSeaEvent = {
id: number
created_date: string
from_account: {
address: string
}
to_account: {
address: string
}
asset: OpenSeaAsset
event_timestamp: number
from_address: string
to_address: string
nft: OpenSeaNft
wallet: string
}

export type OpenSeaEventExtended = Omit<OpenSeaEvent, 'asset'> & {
asset: OpenSeaAssetExtended
wallet: string
export type OpenSeaEventExtended = Omit<OpenSeaEvent, 'nft'> & {
nft: OpenSeaNftExtended
}
Loading

0 comments on commit 5ec9672

Please sign in to comment.