Skip to content

Commit

Permalink
fix: mf-6335 remove opensea,gem prioritize nftscan then x2y2 (#11904)
Browse files Browse the repository at this point in the history
* fix: mf-6335 remove opensea,gem prioritize nftscan then x2y2

* fixup! fix: mf-6335 remove opensea,gem prioritize nftscan then x2y2
  • Loading branch information
swkatmask authored Nov 6, 2024
1 parent 9f2fe5b commit 00e2e62
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
60 changes: 59 additions & 1 deletion packages/web3-providers/src/SimpleHash/apis/EVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ import {
TokenType,
leftShift,
type NonFungibleCollectionOverview,
type NonFungibleTokenEvent,
} from '@masknet/web3-shared-base'
import { formatBalance } from '@masknet/web3-shared-base'
import { subSeconds, isAfter, secondsToMilliseconds, millisecondsToSeconds } from 'date-fns'
import { ChainId, SchemaType, isValidChainId, ZERO_ADDRESS, isValidAddress } from '@masknet/web3-shared-evm'
import {
ChainId,
SchemaType,
isValidChainId,
ZERO_ADDRESS,
isValidAddress,
createERC20Token,
} from '@masknet/web3-shared-evm'
import {
fetchFromSimpleHash,
createNonFungibleAsset,
Expand All @@ -35,6 +43,7 @@ import {
resolveSimpleHashRange,
checkBlurToken,
isLensFollower,
resolveActivityType,
} from '../helpers.js'
import { LooksRare } from '../../LooksRare/index.js'
import { OpenSea } from '../../OpenSea/index.js'
Expand All @@ -45,6 +54,7 @@ import type { BaseHubOptions, NonFungibleTokenAPI, TrendingAPI } from '../../ent
import { historicalPriceState } from '../historicalPriceState.js'
import { SIMPLE_HASH_HISTORICAL_PRICE_START_TIME, SPAM_SCORE } from '../constants.js'
import { SimpleHash } from '../../types/SimpleHash.js'
import { createPermalink } from '../../NFTScan/helpers/EVM.js'

class SimpleHashAPI_EVM implements NonFungibleTokenAPI.Provider<ChainId, SchemaType> {
async getCollectionByContractAddress(
Expand Down Expand Up @@ -142,6 +152,54 @@ class SimpleHashAPI_EVM implements NonFungibleTokenAPI.Provider<ChainId, SchemaT
}
}

async getEvents(address: string, tokenId: string, options?: BaseHubOptions<ChainId>) {
const path = urlcat('/api/v0/nfts/transfers/ethereum/:address/:tokenId', {
address,
tokenId,
cursor: options?.indicator?.id,
})
const response = await fetchFromSimpleHash<SimpleHash.TransfersResponse>(path)
const events: Array<NonFungibleTokenEvent<ChainId, SchemaType>> = response.transfers.map((transfer) => {
const chainId = resolveChainId(transfer.chain) || ChainId.Mainnet
const sale_details = transfer.sale_details
const from_address = transfer.from_address!
const to_address = transfer.to_address!
return {
id: transfer.transaction,
chainId,
type: resolveActivityType(transfer.event_type),
assetPermalink: createPermalink(chainId || ChainId.Mainnet, address, tokenId),
quantity: '1',
hash: transfer.transaction,
from: {
address: from_address,
},
to: {
address: to_address,
},
send: {
address: from_address,
},
receive: {
address: to_address,
},
timestamp: new Date(transfer.timestamp).getTime(),
PaymentToken:
sale_details?.payment_token.address ?
createERC20Token(chainId, sale_details.payment_token.address)
: EVMChainResolver.nativeCurrency(chainId),
source: SourceType.SimpleHash,
}
})
return createPageable(
events,
createIndicator(options?.indicator),
events.length && response.next_cursor ?
createNextIndicator(options?.indicator, response.next_cursor)
: undefined,
)
}

async getAssets(account: string, { chainId = ChainId.Mainnet, indicator }: BaseHubOptions<ChainId> = {}) {
const chain = resolveChain(NetworkPluginID.PLUGIN_EVM, chainId)
if (!account || !isValidChainId(chainId) || !chain) {
Expand Down
10 changes: 10 additions & 0 deletions packages/web3-providers/src/SimpleHash/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ export const resolveChainId: (chain: string) => ChainId | undefined = memoize(fu
}
})

export const resolveActivityType = (eventType: string) => {
const map: Record<string, ActivityType> = {
transfer: ActivityType.Transfer,
sale: ActivityType.Sale,
mint: ActivityType.Mint,
list: ActivityType.List,
}
return map[eventType] || ActivityType.Transfer
}

const ChainNameMap: Record<NetworkPluginID, Record<number, string>> = {
[NetworkPluginID.PLUGIN_EVM]: {
[ChainId.Mainnet]: 'ethereum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export abstract class BaseHubNonFungible<ChainId, SchemaType> extends AbstractBa
return attemptUntil(
providers.map((x) => () => x.getEvents?.(address, tokenId, options)),
createPageable(EMPTY_LIST, createIndicator(options.indicator)),
(res) => !res?.data.length,
)
}

Expand Down
18 changes: 5 additions & 13 deletions packages/web3-providers/src/Web3/EVM/apis/HubNonFungibleAPI.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { SourceType } from '@masknet/web3-shared-base'
import { ChainId, type SchemaType } from '@masknet/web3-shared-evm'
import { BaseHubNonFungible } from '../../Base/apis/HubNonFungible.js'
import { EVMHubOptionsProvider } from './HubOptionsAPI.js'
import type { EVMHubOptions } from '../types/index.js'
import type { AuthorizationAPI, NonFungibleTokenAPI, TokenListAPI } from '../../../entry-types.js'
import * as AlchemyEVM from /* webpackDefer: true */ '../../../Alchemy/index.js'
import * as ChainbaseNonFungibleToken from /* webpackDefer: true */ '../../../Chainbase/index.js'
import * as Gem from /* webpackDefer: true */ '../../../Gem/index.js'
import type { AuthorizationAPI, NonFungibleTokenAPI, TokenListAPI } from '../../../entry-types.js'
import * as GoPlusAuthorization from /* webpackDefer: true */ '../../../GoPlusLabs/index.js'
import * as NFTScanNonFungibleTokenEVM from /* webpackDefer: true */ '../../../NFTScan/index.js'
import * as OpenSea from /* webpackDefer: true */ '../../../OpenSea/index.js'
import * as R2D2TokenList from /* webpackDefer: true */ '../../../R2D2/index.js'
import * as Rabby from /* webpackDefer: true */ '../../../Rabby/index.js'
import * as SimpleHashEVM from /* webpackDefer: true */ '../../../SimpleHash/index.js'
import * as X2Y2 from /* webpackDefer: true */ '../../../X2Y2/index.js'
import * as ZerionNonFungibleToken from /* webpackDefer: true */ '../../../Zerion/index.js'
import * as Zora from /* webpackDefer: true */ '../../../Zora/index.js'
import { BaseHubNonFungible } from '../../Base/apis/HubNonFungible.js'
import type { EVMHubOptions } from '../types/index.js'
import { EVMHubOptionsProvider } from './HubOptionsAPI.js'

export class HubNonFungibleAPI extends BaseHubNonFungible<ChainId, SchemaType> {
protected override HubOptions = new EVMHubOptionsProvider(this.options)
Expand All @@ -32,25 +30,21 @@ export class HubNonFungibleAPI extends BaseHubNonFungible<ChainId, SchemaType> {
[SourceType.Chainbase]: ChainbaseNonFungibleToken.ChainbaseNonFungibleToken,
[SourceType.Zerion]: ZerionNonFungibleToken.ZerionNonFungibleToken,
[SourceType.NFTScan]: NFTScanNonFungibleTokenEVM.NFTScanNonFungibleTokenEVM,
[SourceType.OpenSea]: OpenSea.OpenSea,
[SourceType.Alchemy_EVM]: AlchemyEVM.AlchemyEVM,
[SourceType.Zora]: Zora.Zora,
[SourceType.Gem]: Gem.Gem,
[SourceType.GoPlus]: GoPlusAuthorization.GoPlusAuthorization,
[SourceType.Rabby]: Rabby.Rabby,
[SourceType.R2D2]: R2D2TokenList.R2D2TokenList,
[SourceType.SimpleHash]: SimpleHashEVM.SimpleHashEVM,
},
options.chainId === ChainId.Mainnet ?
[
X2Y2.X2Y2,
SimpleHashEVM.SimpleHashEVM,
NFTScanNonFungibleTokenEVM.NFTScanNonFungibleTokenEVM,
X2Y2.X2Y2,
ZerionNonFungibleToken.ZerionNonFungibleToken,
OpenSea.OpenSea,
AlchemyEVM.AlchemyEVM,
Zora.Zora,
Gem.Gem,
GoPlusAuthorization.GoPlusAuthorization,
Rabby.Rabby,
R2D2TokenList.R2D2TokenList,
Expand All @@ -60,9 +54,7 @@ export class HubNonFungibleAPI extends BaseHubNonFungible<ChainId, SchemaType> {
NFTScanNonFungibleTokenEVM.NFTScanNonFungibleTokenEVM,
ZerionNonFungibleToken.ZerionNonFungibleToken,
AlchemyEVM.AlchemyEVM,
OpenSea.OpenSea,
Zora.Zora,
Gem.Gem,
GoPlusAuthorization.GoPlusAuthorization,
Rabby.Rabby,
R2D2TokenList.R2D2TokenList,
Expand Down
36 changes: 36 additions & 0 deletions packages/web3-providers/src/types/SimpleHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,40 @@ export namespace SimpleHash {
first_acquired_date: string
last_acquired_date: string
}

export interface SaleDetails {
marketplace_id: string
marketplace_name: string
is_bundle_sale: boolean
payment_token: PaymentToken
unit_price: number
total_price: number
}

export interface Transfer {
nft_id: string
chain: string
contract_address: string
token_id: string
collection_id: string
event_type: LiteralUnion<'transfer' | 'sale'>
from_address: string | null
to_address: string
quantity: number
timestamp: string
block_number: number
block_hash: string
transaction: string
transaction_initiator: string
log_index: number
batch_transfer_index: number
sale_details: SaleDetails | null
}

export interface TransfersResponse {
next_cursor: string | null
next: string | null
previous: string | null
transfers: Transfer[]
}
}

0 comments on commit 00e2e62

Please sign in to comment.