UNPKG

@kodex-react/ctx-ethers

Version:

Provides a React context provider for the Ethers.js library, which allows you to interact with Ethereum smart contracts.

103 lines (102 loc) 2.81 kB
/// <reference types="react" /> import type { Result } from '@ethersproject/abi'; import type { TransactionResponse, Web3Provider, Block } from '@ethersproject/providers'; import type { FunctionFragment } from '@ethersproject/abi'; import type { BigNumber } from '@ethersproject/bignumber'; export type { TransactionResponse, Provider, TransactionReceipt } from '@ethersproject/providers'; export type { FunctionFragment, JsonFragment, Result } from '@ethersproject/abi'; export type FnParameters = { [paramIndex: number]: any; }; export interface EthersContextProps { autoEnable?: boolean; disableChainReload?: boolean; children?: React.ReactNode; } export type Operation = { index?: number; resultValue?: string | BigNumber | Result; sigHash?: string; signature?: string; value?: any; callData?: string; target: string; abi: FunctionFragment; parameters: FnParameters; }; export interface EthersProviderState { errors: Error[]; chainId?: number; blockNumber?: number; address?: string; balance?: BigNumber; nonce?: number; gasLimit?: BigNumber; blockTime?: number; noInjectedProvider?: boolean; block?: Block; isConnected?: boolean; } export type EthersProviderActions = { type: 'reset'; } | { type: 'set-all'; payload: EthersProviderState; } | { type: 'set-chain-id'; payload?: number; } | { type: 'set-block-number'; payload?: number; } | { type: 'set-address'; payload?: string; } | { type: 'set-balance'; payload?: BigNumber; } | { type: 'set-nonce'; payload?: number; } | { type: 'set-gas-limit'; payload?: BigNumber; } | { type: 'set-block-time'; payload?: number; } | { type: 'set-no-injected-provider'; payload?: boolean; } | { type: 'add-error'; payload: Error; } | { type: 'remove-error'; index: number; } | { type: 'set-errors'; payload: Error[]; } | { type: 'set-block'; payload?: Block; } | { type: 'set-is-connected'; payload?: boolean; }; export interface EthersContext { state: EthersProviderState; dispatch: React.Dispatch<EthersProviderActions>; watchAsset(address: string, symbol: string, decimals: number, image?: string): Promise<void>; provider?: Web3Provider; web3?: Web3Provider; sign(message: string): Promise<{ address: string; signature: string; } | undefined>; enable(): Promise<void>; sha3(input: string): string; switchChain(chainId: number): Promise<null>; addChain(options: AddEthereumChainParameter): Promise<null>; isUnlocked(): Promise<boolean>; estimateSecondsLeft(toBlock: number): number | undefined; getRevertReason(tx: TransactionResponse | Error | string): Promise<string | undefined>; }