Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/dashboard/src/web3/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@masknet/web3-shared-evm'
import { getProxyWebsocketInstance } from '@masknet/web3-shared-base'
import { Services, Messages, PluginServices, PluginMessages } from '../API'
import { TokenListApi } from '@masknet/web3-providers'
import { TokenList } from '@masknet/web3-providers'

export const Web3Context: Web3ProviderType = {
allowTestnet: createSubscriptionFromAsync(Services.Settings.getWalletAllowTestChain, false, () => {
Expand Down Expand Up @@ -95,7 +95,7 @@ export const Web3Context: Web3ProviderType = {
getCollectionsNFT: PluginServices.Wallet.getCollectionsNFT,
getAddressNamesList: PluginServices.Wallet.getAddressNames,
getTransactionList: PluginServices.Wallet.getTransactionList,
fetchERC20TokensFromTokenLists: TokenListApi.fetchERC20TokensFromTokenLists,
fetchERC20TokensFromTokenLists: TokenList.fetchERC20TokensFromTokenLists,
providerSocket: getProxyWebsocketInstance((info) =>
PluginMessages.Wallet.events.socketMessageUpdated.sendToAll(info),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UserNFTContainerAtTwitter } from '@masknet/web3-providers'
import { Twitter } from '@masknet/web3-providers'
import { useAsyncRetry } from 'react-use'
import type { AsyncState } from 'react-use/lib/useAsyncFn'

export function useNFTContainerAtTwitter(screenName: string): AsyncState<{ address: string; token_id: string }> {
return useAsyncRetry(async () => {
return UserNFTContainerAtTwitter.getUserNftContainer(screenName)
}, [UserNFTContainerAtTwitter, screenName])
return Twitter.getUserNftContainer(screenName)
}, [Twitter, screenName])
}
10 changes: 3 additions & 7 deletions packages/mask/src/plugins/Wallet/services/tokenPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { startEffects } from '../../../../utils-pure'
import { UPDATE_CHAIN_STATE_DELAY } from '../constants'
import { currentTokenPricesSettings } from '../settings'
import { uniq } from 'lodash-unified'
import { TokenPriceApi } from '@masknet/web3-providers'
import { TokenPrice } from '@masknet/web3-providers'

const { run } = startEffects(import.meta.webpackHot)

Expand Down Expand Up @@ -46,11 +46,7 @@ export async function updateTokenPrices() {
const platforms = Object.keys(trackingContracts)
await Promise.allSettled(
platforms.map(async (platform) => {
const prices = await TokenPriceApi.getTokenPrices(
platform,
trackingContracts[platform],
CurrencyType.USD,
)
const prices = await TokenPrice.getTokenPrices(platform, trackingContracts[platform], CurrencyType.USD)
updateCurrentPrices(prices)
}),
)
Expand All @@ -76,7 +72,7 @@ export async function updateNativeTokenPrices() {

// update chain state
try {
const prices = await TokenPriceApi.getNativeTokenPrice(trackingNativeTokenIds, CurrencyType.USD)
const prices = await TokenPrice.getNativeTokenPrice(trackingNativeTokenIds, CurrencyType.USD)
updateCurrentPrices(prices)
} catch {
// do nothing
Expand Down
6 changes: 3 additions & 3 deletions packages/mask/src/web3/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type { InternalSettings } from '../settings/createSettings'
import { Flags } from '../../shared'
import Services from '../extension/service'
import { getProxyWebsocketInstance } from '@masknet/web3-shared-base'
import { TokenListApi, UserNFTContainerAtTwitter } from '@masknet/web3-providers'
import { TokenList, Twitter } from '@masknet/web3-providers'
import type { ERC721 } from '@masknet/web3-contracts/types/ERC721'
import type { AbiItem } from 'web3-utils'
import ERC721ABI from '@masknet/web3-contracts/abis/ERC721.json'
Expand Down Expand Up @@ -147,7 +147,7 @@ function createWeb3Context(disablePopup = false, isMask = false): Web3ProviderTy
getAddressNamesList: async (identity: Parameters<typeof WalletRPC.getAddressNames>[0]) => {
const addressNames = await WalletRPC.getAddressNames(identity)
if (identity.identifier.network === 'twitter.com') {
const result = await UserNFTContainerAtTwitter.getUserNftContainer(identity.identifier.userId ?? '')
const result = await Twitter.getUserNftContainer(identity.identifier.userId ?? '')
if (result?.type_name.toUpperCase() === 'ERC721') {
const contractAddress = await getTokenOwner(result.address, result.token_id)
if (contractAddress)
Expand All @@ -164,7 +164,7 @@ function createWeb3Context(disablePopup = false, isMask = false): Web3ProviderTy
return addressNames
},
getTransactionList: WalletRPC.getTransactionList,
fetchERC20TokensFromTokenLists: TokenListApi.fetchERC20TokensFromTokenLists,
fetchERC20TokensFromTokenLists: TokenList.fetchERC20TokensFromTokenLists,
providerSocket: getProxyWebsocketInstance((info) => WalletMessages.events.socketMessageUpdated.sendToAll(info)),
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/web3-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { NFTScanAPI } from './NFTScan'
import { NativeExplorerAPI } from './explorer'
import { RSS3API } from './rss3'
import { KeyValueAPI } from './kv'
import { UserNFTContainerAtTwitterAPI } from './UserNFTContainer'
import { TokenList } from './TokenList'
import { TokenPrice } from './TokenPrice'
import { TwitterAPI } from './twitter'
import { TokenListAPI } from './token-list'
import { TokenPriceAPI } from './token-price'

export * from './types'
export * from './opensea/utils'
Expand All @@ -19,10 +19,10 @@ export const CoinGecko = new CoinGeckoAPI()
export const Explorer = new NativeExplorerAPI()
export const RSS3 = new RSS3API()
export const KeyValue = new KeyValueAPI()
export const UserNFTContainerAtTwitter = new UserNFTContainerAtTwitterAPI()
export const Twitter = new TwitterAPI()

export const TokenListApi = new TokenList()
export const TokenPriceApi = new TokenPrice()
export const TokenList = new TokenListAPI()
export const TokenPrice = new TokenPriceAPI()

// Method for provider proxy
export { getOpenSeaNFTList, getOpenSeaCollectionList } from './opensea'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
getChainDetailed,
} from '@masknet/web3-shared-evm'
import { groupBy } from 'lodash-unified'
import type { TokenListAPI } from '../types'
import type { TokenListBaseAPI } from '../types'

const NATIVE_TOKEN_ADDRESS_IN_1INCH = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'

const fetchTokenList = memoizePromise(
async (url: string) => {
const response = await fetch(url, { cache: 'force-cache' })
return response.json() as Promise<TokenListAPI.TokenList | TokenListAPI.TokenObject>
return response.json() as Promise<TokenListBaseAPI.TokenList | TokenListBaseAPI.TokenObject>
},
(url) => url,
)
Expand All @@ -28,7 +28,7 @@ async function fetch1inchERC20TokensFromTokenList(
url: string,
chainId = ChainId.Mainnet,
): Promise<ERC20TokenDetailed[]> {
const tokens = ((await fetchTokenList(url)) as TokenListAPI.TokenObject).tokens
const tokens = ((await fetchTokenList(url)) as TokenListBaseAPI.TokenObject).tokens
const _tokens = Object.values(tokens)
return _tokens
.filter((x) => x.address.toLowerCase() !== NATIVE_TOKEN_ADDRESS_IN_1INCH)
Expand All @@ -49,7 +49,7 @@ async function fetchCommonERC20TokensFromTokenList(
url: string,
chainId = ChainId.Mainnet,
): Promise<ERC20TokenDetailed[]> {
return ((await fetchTokenList(url)) as TokenListAPI.TokenList).tokens
return ((await fetchTokenList(url)) as TokenListBaseAPI.TokenList).tokens
.filter(
(x) =>
x.chainId === chainId &&
Expand Down Expand Up @@ -90,7 +90,7 @@ async function fetchERC20TokensFromTokenList(urls: string[], chainId = ChainId.M
* @param chainId
*/

export class TokenList implements TokenListAPI.Provider {
export class TokenListAPI implements TokenListBaseAPI.Provider {
async fetchERC20TokensFromTokenLists(url: string[], chainId: ChainId) {
const result = memoizePromise(
async (urls: string[], chainId = ChainId.Mainnet): Promise<ERC20TokenDetailed[]> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { CurrencyType } from '@masknet/plugin-infra'
import type { TokenPriceAPI } from '../types'
import type { TokenPriceBaseAPI } from '../types'

const URL_BASE = 'https://api.coingecko.com/api/v3'

export class TokenPrice implements TokenPriceAPI.Provider {
export class TokenPriceAPI implements TokenPriceBaseAPI.Provider {
async getTokenPrices(platform: string, contractAddresses: string[], currency: CurrencyType) {
const addressList = contractAddresses.join(',')
const requestPath = `${URL_BASE}/simple/token_price/${platform}?contract_addresses=${addressList}&vs_currencies=${currency}`
const prices = await fetch(requestPath).then((r) => r.json() as Promise<TokenPriceAPI.CryptoPrice>)
const prices = await fetch(requestPath).then((r) => r.json() as Promise<TokenPriceBaseAPI.CryptoPrice>)
return prices
}
async getNativeTokenPrice(tokenIds: string[], currency: CurrencyType) {
const requestPath = `${URL_BASE}/simple/price?ids=${tokenIds.join(',')}&vs_currencies=${currency}`
const prices = await fetch(requestPath).then((r) => r.json() as Promise<TokenPriceAPI.CryptoPrice>)
const prices = await fetch(requestPath).then((r) => r.json() as Promise<TokenPriceBaseAPI.CryptoPrice>)
return prices
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { escapeRegExp } from 'lodash-unified'
import urlcat from 'urlcat'
import type { UserNFTContainerAPI } from '../types'
import type { TwitterBaseAPI } from '../types'

function getScriptURL(content: string, name: string) {
const matchURL = new RegExp(
Expand Down Expand Up @@ -38,7 +38,7 @@ async function getUserNftContainer(
): Promise<{
data: {
user: {
result: UserNFTContainerAPI.NFTContainer
result: TwitterBaseAPI.NFTContainer
}
}
}> {
Expand Down Expand Up @@ -83,7 +83,7 @@ async function getTokens() {
}
}

export class UserNFTContainerAtTwitterAPI implements UserNFTContainerAPI.Provider {
export class TwitterAPI implements TwitterBaseAPI.Provider {
async getUserNftContainer(
screenName: string,
): Promise<{ address: string; token_id: string; type_name: string } | undefined> {
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export namespace SecurityAPI {
}
}

export namespace UserNFTContainerAPI {
export namespace TwitterBaseAPI {
export interface NFTContainer {
has_nft_avatar: boolean
nft_avatar_metadata: AvatarMetadata
Expand Down Expand Up @@ -394,7 +394,7 @@ export namespace UserNFTContainerAPI {
}
}

export namespace TokenListAPI {
export namespace TokenListBaseAPI {
export interface Token {
address: string
chainId: number
Expand Down Expand Up @@ -426,7 +426,7 @@ export namespace TokenListAPI {
}
}

export namespace TokenPriceAPI {
export namespace TokenPriceBaseAPI {
export interface PriceRecord {
[currency: string]: number
}
Expand Down