Flutter package that simplifies integration with web3 DAPPS
This widget is the root provider that exposes blocs, initializes their interactions, and monitors external updates.
final BuildContext appContext;
BuildContext from MaterialApp, you can get him from NavigatorKey. Used to access blocs and display a modal in the application context.
final List<ChainModel> chains;
List of networks supported by the application.
final Widget Function(BuildContext appContext) updateDialogBuilder;
A function that should return a widget used as a modal window, which is called when the user has changed the account or network in his wallet
final Widget child;
Any widget, usually a MaterialApp
Wrap you MaterialApp(or other) widget to Web3Context
Web3Context(
appContext: appContext,
chains: [
ChainModel(
id: 1,
name: 'Mainnet',
rpcUrl: 'https://rpc.ankr.com/eth',
nativeCurrencySymbol: 'ETH',
nativeCurrencyDecimals: 18,
blockExplorerUrl: 'https://etherscan.io',
),
ChainModel(
id: 137,
name: 'Polygon Mainnet',
rpcUrl: 'https://polygon-rpc.com/',
nativeCurrencySymbol: 'MATIC',
nativeCurrencyDecimals: 18,
blockExplorerUrl: 'https://polygonscan.com/',
),
],
updateDialogBuilder: (appContext) => WalletUpdateDialog(
appContext: appContext,
),
child: MaterialApp(...),
)
Block that monitors the user's wallet connection
bool hasWalletConnection;
Public field. Is the user's wallet connected
void Function() logout;
Public event. Disconnects from the connection to the wallet
void Function() walletConnected;
Private event. Sets hasWalletConnection to true when one of the providers connects
context.read<WalletConnectionBloc>().add(WalletConnectionEvent.logout());
BlocListener<WalletConnectionBloc, WalletConnectionState>(
listenWhen: (p, n) => p.hasWalletConnection != n.hasWalletConnection,
listener: (context, state) {
...
},
)
Responsible for managing the selected network
final List<ChainModel> chains;
Public field. List of networks supported by the application.
ChainModel currentChain;
Public field. The current network on which the application is running
SwitchChainStrategy? switchChainStrategy;
Private field. Strategy defining behavior during network switching
void Function(ChainModel chain) switchChain;
Public event. Switch to network.
void Function(SwitchChainStrategy? switchChainStrategy) setSwitchChainStrategy;
Private event. Setting a strategy for switch network
void Function(int chainId) setChainById;
Private event. Set network by id
final List<ChainModel> supportedChains = context.read<ChainBloc>().chains;
final ChainModel currentChain = context.read<ChainBloc>().state.currentChain;
context.read<ChainBloc>().add(ChainEvent.switchChain(chain));
BlocListener<ChainBloc, ChainState>(
listenWhen: (p, n) => p.currentChain != n.currentChain,
listener: (context, state) {
...
},
)
Is the provider of the RpcService object
final String rpcUrl;
initial rpc service url
RpcService rpcService;
Public field. RpcService connected to the current network
void Function(RpcService rpcService) set;
Private event. Set new rpcService
void Function(String url) createFromUrl;
Private event. Create rpcService from url, and set this to state.
final RpcService rpcService = context.read<RpcBloc>().state.rpcService;
BlocListener<RpcService, RpcState>(
listenWhen: (p, n) => p.rpcService != n.rpcService,
listener: (context, state) {
...
},
)
Is the provider of the Web3Client object
final RpcService rpcService;
initial rpc service
Web3Client client;
Public field. Web3Client object connected to the current RpcService
void Function(Web3Client client) set;
Private event. Set new Web3Client
void Function(RpcService rpc) createFromRpc;
Private event. Create Web3Client from RpcService, and set this to state.
final Web3Client web3Client = context.read<Web3ClientBloc>().state.client;
BlocListener<Web3ClientBloc, Web3ClientState>(
listenWhen: (p, n) => p.client != n.client,
listener: (context, state) {
...
},
)
Is the provider of the CredentialsWithKnownAddress object
CredentialsWithKnownAddress? credentials;
Public field. If there is a connection to the user's wallet, it stores an object with which you can sign transactions
void Function(CredentialsWithKnownAddress? credentials) set;
Private event. Set new CredentialsWithKnownAddress or dispose it
final CredentialsWithKnownAddress credentials = context.read<CredentialsBloc>().state.credentials;
BlocListener<CredentialsBloc, CredentialsState>(
listenWhen: (p, n) => p.credentials != n.credentials,
listener: (context, state) {
...
},
)
An bloc that listens for external changes (change of address or network) in the user's wallet
String? newAccount;
Public field. Address set in the wallet
int? newChainId;
Public field. Chain id set in the wallet
String? acceptedAccount;
Private field. User accepted address to update application data
int? acceptedChainId;
Private field. User accepted chain id to update application data
void Function() acceptAccount;
Public event. Accept new account
void Function() acceptChain;
Public event. Accept new chain
void Function(String? account) account;
Private event. Set new account pending confirmation
void Function(int? chainId) chain;
Private event. Set new chain id pending confirmation
void Function() reset;
Private event. Reset bloc state
final String? newAccount = context.read<WalletExternalUpdatesBloc>().state.newAccount;
final int? newChainId = context.read<WalletExternalUpdatesBloc>().state.newChainId;
context.read<WalletExternalUpdatesBloc>().add(WalletExternalUpdatesEvent.acceptAccount());
context.read<WalletExternalUpdatesBloc>().add(WalletExternalUpdatesEvent.acceptChain());
BlocListener<WalletExternalUpdatesBloc, WalletExternalUpdatesState>(
listenWhen: (p, n) => p.newAccount != n.newAccount,
listener: (context, state) {
...
},
)
BlocListener<WalletExternalUpdatesBloc, WalletExternalUpdatesState>(
listenWhen: (p, n) => p.newChainId != n.newChainId,
listener: (context, state) {
...
},
)
Used to connect to browser wallets, web only
bool isSupported;
Public field. Is this type of connection supported?
bool isInstalled;
Public field. Is the wallet extension installed in the browser?
bool isConnected;
Private field. Is the connection connected. Use WalletConnectionBloc.state.isConnected instead
RpcService? rpcService;
Private field. RpcService connected to the user's wallet. Use RpcBloc.state.rpcService instead
CredentialsWithKnownAddress? credentials;
Private field. CredentialsWithKnownAddress connected to the user's wallet. Use CredentialsBloc.state.credentials instead
void Function() connect;
Public event. Initiates a connection to the browser wallet
void Function(Map<String, dynamic> json) restore;
Private event. Restores bloc state from serialized json
void Function() reset;
Private event. Reset bloc state to initial values
void Function(String account) updateCredentials;
Private event. Updates the credentials object
final walletConnectionBloc = context.read<WalletConnectionBloc>();
if(!walletConnectionBloc.state.isConnected) {
final browserExtensionProviderBloc = context.read<BrowserExtensionProviderBloc>();
final isSupported = browserExtensionProviderBloc.state.isSupported;
final isInstalled = browserExtensionProviderBloc.state.isInstalled;
if(isSupported && isInstalled) {
browserExtensionProviderBloc.add(BrowserExtensionProviderEvent.connect());
}
}
Used to connect to external wallets
String? displayUri;
Public field. If the session is initialized, here is the url string to connect to. Can be used as a deep link or qr code
bool isConnected;
Private field. Is the connection connected. Use WalletConnectionBloc.state.isConnected instead
RpcService? rpcService;
Private field. RpcService connected to the user's wallet. Use RpcBloc.state.rpcService instead
CredentialsWithKnownAddress? credentials;
Private field. CredentialsWithKnownAddress connected to the user's wallet. Use CredentialsBloc.state.credentials instead
WCSessionUpdateResponse? sessionUpdate;
Private field. Session update object
void Function() connect;
Public event. Initiates a connection to the external wallet
void Function(Map<String, dynamic> json) restore;
Private event. Restores bloc state from serialized json
void Function() reset;
Private event. Reset bloc state to initial values
void Function(String account) updateCredentials;
Private event. Updates the credentials object
void Function(String? displayUri) setDisplayUri;
Private event. Sets the string to connect to the wallet
void Function(WCSessionUpdateResponse sessionUpdate) updateSession;
Private event. Fires when the data in the wallet is updated
final walletConnectProviderBloc = context.read<WalletConnectProviderBloc>();
walletConnectProviderBloc.add(WalletConnectProviderEvent.connect());