import type { SignedTransaction , Transaction , TransactionConfig , TransactionReceipt , PastLogsOptions , Log , } from 'web3-core' ; import { toHex } from 'web3-utils' ; import { ChainId , EthereumChainDetailed , EthereumMethodType , SendOverrides } from '@masknet/web3-shared-evm' ; import { request } from './request' ; export async function getChainId ( overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_CHAIN_ID , } , overrides ) ; } export async function getAccounts ( overrides ? ) { return request < string [ ] > ( { method : EthereumMethodType . ETH_ACCOUNTS , } , overrides ) ; } export async function getCode ( address , overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_GET_CODE , params : [ address , 'latest' ] , } , overrides ) ; } export async function getGasPrice ( overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_GAS_PRICE , } , overrides ) ; } export async function getBlockNumber ( overrides ? ) { const blockNumber = await request < string > ( { method : EthereumMethodType . ETH_BLOCK_NUMBER , } , overrides ) ; return Number . parseInt ( blockNumber , 16 ) || 0 ; } export async function getBalance ( address , overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_GET_BALANCE , params : [ address , 'latest' ] , } , overrides ) ; } export async function getTransactionByHash ( hash , overrides ? ) { return request < Transaction > ( { method : EthereumMethodType . ETH_GET_TRANSACTION_BY_HASH , params : [ hash ] , } , overrides ) ; } export async function getTransactionReceipt ( hash , overrides ? ) { return request < TransactionReceipt | null > ( { method : EthereumMethodType . MASK_GET_TRANSACTION_RECEIPT , params : [ hash ] , } , overrides ) ; } export async function getTransactionCount ( address , overrides ? ) { const count = await request < string > ( { method : EthereumMethodType . ETH_GET_TRANSACTION_COUNT , params : [ address , 'latest' ] , } , overrides ) ; return Number . parseInt ( count , 16 ) || 0 ; } export async function getPendingTransactions ( address , overrides ? ) { const filterId = await request < string > ( { method : EthereumMethodType . ETH_NEW_PENDING_TRANSACTION_FILTER , params : [ ] , } , overrides ) ; const transactions = await request < string [ ] > ( { method : EthereumMethodType . ETH_GET_FILTER_CHANGES , params : [ filterId ] , } , overrides ) ; return transactions ; } export async function call ( config , overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_CALL , params : [ config , 'latest' ] , } , overrides ) ; } export async function estimateGas ( config , overrides ? ) { const gas = await request < string > ( { method : EthereumMethodType . ETH_ESTIMATE_GAS , params : [ config ] , } , overrides ) ; return Number . parseInt ( gas , 16 ) || 0 ; } export async function sign ( dataToSign , address , overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_SIGN , params : [ dataToSign , address ] , } , overrides ) ; } export async function personalSign ( dataToSign , address , password ? , overrides ? ) { return request < string > ( { method : EthereumMethodType . PERSONAL_SIGN , params : [ dataToSign , address , password ] . filter ( ( x ) => typeof x !== 'undefined' ) , } , overrides ) ; } export async function addEthereumChain ( chainDetailed , address ? , overrides ? ) { return request < boolean > ( { method : EthereumMethodType . WALLET_ADD_ETHEREUM_CHAIN , params : [ chainDetailed , address ] . filter ( Boolean ) , } , overrides ) ; } export async function switchEthereumChain ( chainId , overrides ? ) { return request < boolean > ( { method : EthereumMethodType . WALLET_SWITCH_ETHEREUM_CHAIN , params : [ { chainId : toHex ( chainId ) , } , ] , } , overrides ) ; } export async function signTransaction ( config , overrides ? ) { return request < SignedTransaction > ( { method : EthereumMethodType . ETH_SIGN_TRANSACTION , params : [ config ] , } , overrides ) ; } export async function sendTransaction ( config , overrides ? ) { return request < string > ( { method : EthereumMethodType . ETH_SEND_TRANSACTION , params : [ config ] , } , overrides ) ; } export async function getPastLogs ( config , overrides ? ) { return new Promise < Log [ ] > ( ( resolve , reject ) => request < Log [ ] > ( { method : EthereumMethodType . ETH_GET_LOGS , params : [ config ] , } , overrides ) . then ( ( result ) => resolve ( result ) ) . catch ( ( ) => resolve ( [ ] ) ) ) ; }