Skip to content

Commit

Permalink
fix: balance (#10391)
Browse files Browse the repository at this point in the history
* fix: balance

* fix: typeof ProviderResolverAPI_Base

* fix: typeof ChainResolverAPI_Base

* refactor: isSameURL
  • Loading branch information
guanbinrui committed Aug 14, 2023
1 parent d8feec5 commit 7401967
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
35 changes: 21 additions & 14 deletions packages/web3-providers/src/Web3/Base/apis/ChainResolverAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export class ChainResolverAPI_Base<ChainId, SchemaType, NetworkType> {
constructor(private getDescriptors: () => Array<ChainDescriptor<ChainId, SchemaType, NetworkType>>) {}

private getDescriptor(chainId: ChainId) {
return this.getDescriptors().find((x) => x.chainId === chainId)!
return this.getDescriptors().find((x) => x.chainId === chainId)
}

private getDescriptorRequired(chainId: ChainId) {
const descriptor = this.getDescriptor(chainId)
if (!descriptor) throw new Error(`Unknown chainId: ${chainId}. It might too early to access network state.`)
return descriptor
}

chainId = (name: string) =>
Expand All @@ -15,32 +21,33 @@ export class ChainResolverAPI_Base<ChainId, SchemaType, NetworkType> {
.includes(name?.toLowerCase()),
)?.chainId

coinMarketCapChainId = (chainId: ChainId) => this.getDescriptor(chainId).coinMarketCapChainId
coinMarketCapChainId = (chainId: ChainId) => this.getDescriptor(chainId)?.coinMarketCapChainId ?? ''

coinGeckoChainId = (chainId: ChainId) => this.getDescriptor(chainId).coinGeckoChainId
coinGeckoChainId = (chainId: ChainId) => this.getDescriptor(chainId)?.coinGeckoChainId ?? ''

coinGeckoPlatformId = (chainId: ChainId) => this.getDescriptor(chainId).coinGeckoPlatformId
coinGeckoPlatformId = (chainId: ChainId) => this.getDescriptor(chainId)?.coinGeckoPlatformId ?? ''

chainName = (chainId: ChainId) => this.getDescriptor(chainId).name
chainName = (chainId: ChainId) => this.getDescriptor(chainId)?.name ?? 'Custom Network'

chainFullName = (chainId: ChainId) => this.getDescriptor(chainId).fullName
chainFullName = (chainId: ChainId) => this.getDescriptor(chainId)?.fullName ?? 'Custom Network'

chainShortName = (chainId: ChainId) => this.getDescriptor(chainId).shortName
chainShortName = (chainId: ChainId) => this.getDescriptor(chainId)?.shortName ?? 'CUSTOM'

chainColor = (chainId: ChainId) => this.getDescriptor(chainId).color
chainColor = (chainId: ChainId) => this.getDescriptor(chainId)?.color ?? 'rgb(138, 138, 138)'

chainPrefix = (chainId: ChainId) => ''

networkType = (chainId: ChainId) => this.getDescriptor(chainId).type
networkType = (chainId: ChainId) => this.getDescriptorRequired(chainId)?.type

explorerUrl = (chainId: ChainId) => this.getDescriptor(chainId).explorerUrl
explorerUrl = (chainId: ChainId) => this.getDescriptorRequired(chainId)?.explorerUrl

nativeCurrency = (chainId: ChainId) => this.getDescriptor(chainId).nativeCurrency
nativeCurrency = (chainId: ChainId) => this.getDescriptorRequired(chainId)?.nativeCurrency

isValidChainId = (chainId: ChainId, testnet = false) => this.getDescriptor(chainId).network === 'mainnet' || testnet
isValidChainId = (chainId: ChainId, testnet = false) =>
this.getDescriptor(chainId)?.network === 'mainnet' || testnet

isMainnet = (chainId: ChainId) => this.getDescriptor(chainId).network === 'mainnet'
isMainnet = (chainId: ChainId) => this.getDescriptor(chainId)?.network === 'mainnet'

isFeatureSupported = (chainId: ChainId, feature: string) =>
!!this.getDescriptor(chainId).features?.includes(feature)
!!this.getDescriptor(chainId)?.features?.includes(feature)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export class ProviderResolverAPI_Base<ChainId, ProviderType> {
constructor(private getDescriptors: () => Array<ProviderDescriptor<ChainId, ProviderType>>) {}

private getDescriptor(providerType: ProviderType) {
return this.getDescriptors().find((x) => x.type === providerType)!
return this.getDescriptors().find((x) => x.type === providerType)
}

providerName = (providerType: ProviderType) => this.getDescriptor(providerType).name
providerName = (providerType: ProviderType) => this.getDescriptor(providerType)?.name

providerHomeLink = (providerType: ProviderType) => this.getDescriptor(providerType).homeLink
providerHomeLink = (providerType: ProviderType) => this.getDescriptor(providerType)?.homeLink

providerShortenLink = (providerType: ProviderType) => this.getDescriptor(providerType).shortenLink
providerShortenLink = (providerType: ProviderType) => this.getDescriptor(providerType)?.shortenLink

providerDownloadLink = (providerType: ProviderType) => this.getDescriptor(providerType).downloadLink
providerDownloadLink = (providerType: ProviderType) => this.getDescriptor(providerType)?.downloadLink
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { noop } from 'lodash-es'
import { isSameURL } from '@masknet/web3-shared-base'
import { ErrorEditor, isMaskOnlyMethodType, type Middleware } from '@masknet/web3-shared-evm'
import type { ConnectionContext } from '../libs/ConnectionContext.js'
import { Web3StateRef } from '../apis/Web3StateAPI.js'
Expand All @@ -7,14 +8,14 @@ import { ConnectionReadonlyAPI } from '../apis/ConnectionReadonlyAPI.js'
export class CustomNetwork implements Middleware<ConnectionContext> {
private Web3 = new ConnectionReadonlyAPI()

private get customNetwork() {
private get networks() {
if (!Web3StateRef.value?.Network) throw new Error('The web3 state does not load yet.')
const network = Web3StateRef.value.Network.network?.getCurrentValue()
return network?.isCustomized ? network : undefined
return Web3StateRef.value.Network.networks?.getCurrentValue()
}

async fn(context: ConnectionContext, next: () => Promise<void>) {
if (!this.customNetwork || context.risky || !context.writeable || isMaskOnlyMethodType(context.method)) {
const customNetwork = this.networks?.find((x) => x.isCustomized && isSameURL(x.rpcUrl, context.providerURL))
if (!customNetwork || context.risky || !context.writeable || isMaskOnlyMethodType(context.method)) {
await next()
return
}
Expand All @@ -23,7 +24,7 @@ export class CustomNetwork implements Middleware<ConnectionContext> {
const response = await this.Web3.getWeb3Provider({
chainId: context.chainId,
account: context.account,
providerURL: context.providerURL ?? this.customNetwork?.rpcUrl,
providerURL: context.providerURL,
}).sendAsync(context.request, noop)

const editor = ErrorEditor.from(null, response)
Expand Down
14 changes: 4 additions & 10 deletions packages/web3-providers/src/Web3/EVM/interceptors/Popups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ export class Popups implements Middleware<ConnectionContext> {
return Web3StateRef.value.Network.networks?.getCurrentValue()
}

private get customNetwork() {
if (!Web3StateRef.value?.Network) throw new Error('The web3 state does not load yet.')
const network = Web3StateRef.value.Network.network?.getCurrentValue()
return network?.isCustomized ? network : undefined
}

private async getPaymentToken(context: ConnectionContext) {
const maskAddress = getMaskTokenAddress(context.chainId)
try {
Expand Down Expand Up @@ -127,9 +121,9 @@ export class Popups implements Middleware<ConnectionContext> {
await MaskProvider.switchChain(context.chainId)

// if send risky requests to a custom network, the providerURL must be provided.
const matchNetworkByProviderURL = context.providerURL
? this.networks?.find((x) => x.isCustomized && isSameURL(x.rpcUrl, context.providerURL!))
: undefined
const matchNetworkByProviderURL = this.networks?.find(
(x) => x.isCustomized && isSameURL(x.rpcUrl, context.providerURL),
)

// a built-in network will be matched by chainId
const matchNetworkByChainId = this.networks?.find(
Expand All @@ -154,7 +148,7 @@ export class Popups implements Middleware<ConnectionContext> {
silent: context.silent,
owner: context.owner,
identifier: context.identifier?.toText(),
providerURL: context.providerURL ?? this.customNetwork?.rpcUrl,
providerURL: context.providerURL,
gasOptionType: context.gasOptionType,
},
},
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-shared/base/src/helpers/isSameURL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function isSameURL(a: string | URL, b: string | URL): boolean {
export function isSameURL(a: string | URL | undefined, b: string | URL | undefined): boolean {
if (!a || !b) return false

// Parse the input URLs if they are strings
let urlA: URL
let urlB: URL
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-shared/evm/src/constants/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export const CHAIN_DESCRIPTORS: Array<ChainDescriptor<ChainId, SchemaType, Netwo
coinGeckoChainId: '',
coinGeckoPlatformId: '',
type: (x.type as NetworkType | undefined) || NetworkType.Ethereum,
color: network?.iconColor || x.color || 'rgb(24, 163, 138)',
color: network?.iconColor || x.color || 'rgb(138, 138, 138)',
nativeCurrency: {
id: getTokenConstant(x.chainId, 'NATIVE_TOKEN_ADDRESS', ZERO_ADDRESS)!,
address: getTokenConstant(x.chainId, 'NATIVE_TOKEN_ADDRESS', ZERO_ADDRESS)!,
Expand Down

0 comments on commit 7401967

Please sign in to comment.