-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f3f30c
commit 8b7809d
Showing
24 changed files
with
439 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { first, memoize } from 'lodash-es' | ||
import Web3SDK from 'web3' | ||
import type { HttpProvider } from 'web3-core' | ||
import { | ||
AddressType, | ||
ChainId, | ||
createWeb3Provider, | ||
createWeb3Request, | ||
getRPCConstants, | ||
isValidAddress, | ||
Web3Provider, | ||
} from '@masknet/web3-shared-evm' | ||
import type { Web3BaseAPI } from '../../entry-types.js' | ||
|
||
const createWeb3SDK = memoize( | ||
(url: string) => new Web3SDK(url), | ||
(url) => url.toLowerCase(), | ||
) | ||
|
||
export class Web3API implements Web3BaseAPI.Provider<ChainId, AddressType, Web3Provider, Web3SDK> { | ||
async getAddressType(chainId: ChainId, address: string): Promise<AddressType | undefined> { | ||
if (!isValidAddress(address)) return | ||
const code = await this.createWeb3(chainId).eth.getCode(address) | ||
return code === '0x' ? AddressType.ExternalOwned : AddressType.Contract | ||
} | ||
|
||
createWeb3(chainId: ChainId) { | ||
const RPC_URL = first(getRPCConstants(chainId).RPC_URLS) | ||
if (!RPC_URL) throw new Error('Failed to create web3 provider.') | ||
return createWeb3SDK(RPC_URL) | ||
} | ||
|
||
createProvider(chainId: ChainId) { | ||
const web3 = this.createWeb3(chainId) | ||
const provider = web3.currentProvider as HttpProvider | ||
return createWeb3Provider(createWeb3Request(provider.send)) | ||
} | ||
} |
Oops, something went wrong.