Skip to content

Commit

Permalink
fix: review code
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Jun 11, 2022
1 parent cd62621 commit ef4e8ee
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 42 deletions.
13 changes: 8 additions & 5 deletions packages/mask/background/services/helper/r2d2Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const matchers: R2d2WorkerMatchTuple[] = [
export async function r2d2Fetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
const url = init instanceof Request ? init.url : (input as string)

// r2d2
if (url.includes('r2d2.to')) return globalThis.fetch(input, init)

// ipfs
if (url.startsWith('ipfs://'))
return globalThis.fetch(
Expand All @@ -39,7 +36,13 @@ export async function r2d2Fetch(input: RequestInfo, init?: RequestInit): Promise
init,
)

// r2d2
if (url.includes('r2d2.to')) return globalThis.fetch(input, init)

// r2d2 worker
const r2deWorkerType = matchers.find((x) => url.startsWith(x[0]))?.[1]
if (!r2deWorkerType) return globalThis.fetch(input, init)
return globalThis.fetch(url.replace(new URL(url).origin, `https://${r2deWorkerType}.${r2d2URL}`), init)
if (r2deWorkerType) globalThis.fetch(url.replace(new URL(url).origin, `https://${r2deWorkerType}.${r2d2URL}`), init)

// fallback
return globalThis.fetch(input, init)
}
2 changes: 1 addition & 1 deletion packages/plugin-infra/src/web3-state/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ConnectionState<
>
{
constructor(
private context: Plugin.Shared.SharedContext,
protected context: Plugin.Shared.SharedContext,
protected createConnection: (
context: Plugin.Shared.SharedContext,
options?: {
Expand Down
10 changes: 2 additions & 8 deletions packages/plugin-infra/src/web3/useFungibleAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ import {
pageableToIterator,
toZero,
} from '@masknet/web3-shared-base'
import { useTokenConstants } from '@masknet/web3-shared-evm'
import type { Web3Helper } from '../web3-helpers'
import { useAccount } from './useAccount'
import { useChainId } from './useChainId'
import { useWeb3Hub } from './useWeb3Hub'
import { useWeb3State } from './useWeb3State'
import { useTrustedFungibleTokens } from './useTrustedFungibleTokens'
import { useBlockedFungibleTokens } from './useBlockedFungibleTokens'
import { useFungibleToken, useNativeTokenBalance } from '../entry-web3'

export function useFungibleAssets<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>(
pluginID?: T,
Expand All @@ -32,11 +30,6 @@ export function useFungibleAssets<S extends 'all' | void = void, T extends Netwo
const trustedTokens = useTrustedFungibleTokens(pluginID)
const blockedTokens = useBlockedFungibleTokens(pluginID)
const { Others } = useWeb3State(pluginID)
const { NATIVE_TOKEN_ADDRESS } = useTokenConstants(chainId)
const { value: balance = 0 } = useNativeTokenBalance()
const { value: nativeToken } = useFungibleToken(pluginID, NATIVE_TOKEN_ADDRESS, {
chainId,
})

return useAsyncRetry<Array<Web3Helper.FungibleAssetScope<S, T>>>(async () => {
if (!account || !hub) return EMPTY_LIST
Expand All @@ -48,6 +41,7 @@ export function useFungibleAssets<S extends 'all' | void = void, T extends Netwo
return hub.getFungibleAssets(account, {
indicator,
size: 50,
...options,
})
})
const assets = await asyncIteratorToArray(iterator)
Expand Down Expand Up @@ -101,5 +95,5 @@ export function useFungibleAssets<S extends 'all' | void = void, T extends Netwo

return 0
})
}, [account, chainId, hub, trustedTokens, blockedTokens, Others])
}, [account, chainId, hub, trustedTokens, blockedTokens, Others, JSON.stringify(options)])
}
13 changes: 5 additions & 8 deletions packages/plugin-infra/src/web3/useNonFungibleAssets.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { useAsyncRetry } from 'react-use'
import { asyncIteratorToArray, EMPTY_LIST } from '@masknet/shared-base'
import { pageableToIterator, HubIndicator, NetworkPluginID } from '@masknet/web3-shared-base'
import { pageableToIterator, NetworkPluginID } from '@masknet/web3-shared-base'
import type { Web3Helper } from '../web3-helpers'
import { useAccount } from './useAccount'
import { useWeb3Hub } from './useWeb3Hub'

export function useNonFungibleAssets<
S extends 'all' | void = void,
T extends NetworkPluginID = NetworkPluginID,
Indicator = HubIndicator,
>(
export function useNonFungibleAssets<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>(
pluginID?: T,
schemaType?: Web3Helper.SchemaTypeScope<S, T>,
options?: Web3Helper.Web3HubOptionsScope<S, T, Indicator>,
options?: Web3Helper.Web3HubOptionsScope<S, T>,
) {
const account = useAccount(pluginID, options?.account)
const hub = useWeb3Hub(pluginID, options)
Expand All @@ -25,9 +21,10 @@ export function useNonFungibleAssets<
return hub.getNonFungibleAssets(account, {
indicator,
size: 50,
...options,
})
})
const assets = await asyncIteratorToArray(iterator)
return assets.length && schemaType ? assets.filter((x) => x.schema === schemaType) : assets
}, [account, schemaType, hub])
}, [account, schemaType, hub, JSON.stringify(options)])
}
13 changes: 6 additions & 7 deletions packages/plugin-infra/src/web3/useWeb3Hub.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useAsyncRetry } from 'react-use'
import type { HubIndicator, NetworkPluginID } from '@masknet/web3-shared-base'
import type { NetworkPluginID } from '@masknet/web3-shared-base'
import { useChainId, useAccount } from '../entry-web3'
import type { Web3Helper } from '../web3-helpers'
import { useWeb3State } from './useWeb3State'

export function useWeb3Hub<
S extends 'all' | void = void,
T extends NetworkPluginID = NetworkPluginID,
Indicator = HubIndicator,
>(pluginID?: T, options?: Web3Helper.Web3HubOptionsScope<S, T, Indicator>) {
export function useWeb3Hub<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>(
pluginID?: T,
options?: Web3Helper.Web3HubOptionsScope<S, T>,
) {
const { Hub } = useWeb3State(pluginID)
const account = useAccount(pluginID)
const chainId = useChainId(pluginID)
Expand All @@ -18,7 +17,7 @@ export function useWeb3Hub<
account,
chainId,
...options,
} as Web3Helper.Web3HubOptionsScope<S, T>)
})
}, [account, chainId, Hub, JSON.stringify(options)])

return hub as Web3Helper.Web3HubScope<S, T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import { BaseInjectedProvider } from './BaseInjected'

export class PhantomProvider extends BaseInjectedProvider implements SolanaProvider {
constructor() {
injectedPhantomProvider.untilAvailable().then(() => {
injectedPhantomProvider.connect({
onlyIfTrusted: true,
})
})

super(ProviderType.Phantom, injectedPhantomProvider)
}

Expand Down
11 changes: 6 additions & 5 deletions packages/web3-providers/src/alchemy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Alchemy_EVM_API implements NonFungibleTokenAPI.Provider<ChainId_EVM
const res = await fetchJSON<AlchemyResponse_EVM>(
urlcat(`${chainInfo?.baseURL}${chainInfo?.API_KEY}/getNFTs/`, {
owner: from,
pageKey: indicator?.id && indicator.id !== '' ? indicator.id : undefined,
pageKey: typeof indicator?.index !== 'undefined' && indicator?.index !== 0 ? indicator.id : undefined,
}),
)

Expand Down Expand Up @@ -73,17 +73,18 @@ export class Alchemy_EVM_API implements NonFungibleTokenAPI.Provider<ChainId_EVM
}

export class Alchemy_FLOW_API implements NonFungibleTokenAPI.Provider<ChainId_FLOW, SchemaType_FLOW> {
getTokens = async (from: string, opts?: HubOptions<ChainId_FLOW>) => {
const chainInfo = Alchemy_FLOW_NetworkMap?.chains?.find((chain) => chain.chainId === opts?.chainId)
getTokens = async (from: string, { chainId, indicator }: HubOptions<ChainId_FLOW> = {}) => {
const chainInfo = Alchemy_FLOW_NetworkMap?.chains?.find((chain) => chain.chainId === chainId)
const res = await fetchJSON<AlchemyResponse_FLOW>(
urlcat(`${chainInfo?.baseURL}${chainInfo?.API_KEY}/getNFTs/`, {
owner: from,
pageKey: typeof indicator?.index !== 'undefined' && indicator?.index !== 0 ? indicator.id : undefined,
}),
)
const assets = res?.nfts?.map((nft) =>
createNftToken_FLOW((opts?.chainId as ChainId_FLOW | undefined) ?? ChainId_FLOW.Mainnet, nft),
createNftToken_FLOW((chainId as ChainId_FLOW | undefined) ?? ChainId_FLOW.Mainnet, nft),
)
return createPageable(assets, createIndicator(opts?.indicator))
return createPageable(assets, createIndicator(indicator))
}
getAsset = async (
address: string,
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-shared/base/src/utils/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { HubIndicator, Pageable } from '../specs'
export function createIndicator(indicator?: HubIndicator, id?: string) {
const index = indicator?.index ?? 0
return {
id: id ?? index.toString(),
id: id ?? indicator?.id ?? index.toString(),
index,
}
}
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function* pageableToIterator<T>(
const pageable = await getPageable(indicator)
if (!pageable) return
yield* pageable.data
if (!pageable.indicator) return
if (!pageable.nextIndicator) return
indicator = pageable.nextIndicator as HubIndicator
}
}

0 comments on commit ef4e8ee

Please sign in to comment.