Usage
This section provides instructions on how to use WalletConnectModal in your project.
Implementation
Start by importing WalletConnectModal
and initializing it.
Trigger the modal
Once you have obtained your connection uri, you can open or close the modal.
From here on, use provider
as you normally would, WalletConnectModal will be shown and hidden automatically i.e.
Start by importing WalletConnectModal
and initializing it.
Trigger the modal
Once you have obtained your connection uri, you can open or close the modal.
From here on, use provider
as you normally would, WalletConnectModal will be shown and hidden automatically i.e.
Configure Networking and Pair clients
Make sure that you properly configure Networking and Pair Clients first.
Initialize WalletConnectModal Client
In order to initialize a client just call a configure
method from the WalletKit instance wrapper
This example will default to using following namespaces.
If you want to change that you can call configure and define your own session parameters like this.
or you can change them later by calling WalletConnectModal.set(sessionParams: SessionParams(...))
WalletConnectModal
is a singleton that interacts with the WalletConnectModal SDK.
Initialize
SessionParams
This example will default to using following namespaces. You can define your own session parameters like this.
IMPORTANT: SessionParams
must be set before opening the modal.
Create your WalletConnectModalService
which is your primary class for opening, closing, disconnecting, etc.
Be sure to update the project ID and metadata with your own.
The service must be initialized before it can be used.
With the WalletConnectModalService
created and ready, you can call _service.open()
to open the modal.
To make things easy, you can use the WalletConnectModalConnect widget to open the modal. This is a button that changes its state based on the modal and connection. This widget requires the WalletConnectModalService to be passed in.
iOS Setup
For each app you would like to be able to deep link to, you must add that app’s link into the ios/Runner/Info.plist
file like so:
To handle deep linking to your app, you will also need to add the following to the plist file:
Android Setup
On android 11+ you must specify that use can use the internet, along with the different packages you would like to be able to deep link to in the android/app/src/main/AndroidManifest.xml
file like so:
For other packages, see the example project
For some reason, multiple wallets have the metamask
intent, and will launch metamask as a result.
This is a bug in the wallets, not this package.
Start by importing @walletconnect/react-native-compat
at the top of your app. Then import the WalletConnect Modal package, replace YOUR_PROJECT_ID
with your Reown Cloud Project ID and add your Project’s info in providerMetadata
- Fill in the Project ID and Metadata fields in the
Assets/WalletConnectUnity/Resources/WalletConnectProjectConfig
asset.- If you don’t have a Project ID, you can create one at Reown Cloud.
- The
Redirect
fields are optional. They are used to redirect the user back to your app after they approve or reject the session.
- Add
WalletConnectModal
prefab fromWalletConnectUnity Modal
package to the first scene in your game.
Usage
openModal
Action to open the modal. Returns promise that resolves once modal is visible.
Example
Reference
closeModal
Action to close the modal.
Example
Reference
subscribeModal
Action to subscribe to modal state changes.
Example
Reference
openModal
Action to open the modal. Returns promise that resolves once modal is visible.
Example
Reference
closeModal
Action to close the modal.
Example
Reference
subscribeModal
Action to subscribe to modal state changes.
Example
Reference
To actually present the modal you can simply call.
It will traverse the view hierarchy and try to present from top most controller. This is meant more towards SwiftUI.
Otherwise you can specify the viewController to present from.
Subscribe for WalletConnectModal Publishers
The following publishers are available to subscribe:
Sign methods
WalletConnectModal is internally using Sign SDK and most of its method are being exposed through WalletConnectModal interface.
Where to go from here
Check the WalletConnectModal usage in our Example Showcase app that is part of WalletConnectSwiftV2 repository.
Build API documentation in Xcode by going to Product -> Build Documentation
Android Compose
IMPORTANT: WalletConnectModal uses accompanist navigation material inside. ModalBottomSheetLayout
should be imported from Accompanist Navigation Material
Android View
Navigation Component
Kotlin DSL
WalletConnectModal.ModalDelegate
The WalletConnectModal needs a WalletConnectModal.ModalDelegate
passed to it for it to be able to expose asynchronously updates sent from the Wallet. It can only be called after successful WalletConnectModal
initialization
Connect
More about optional and required namespaces can be found here
Disconnect
Request
Get List of Active Sessions
To get a list of active sessions, call WalletConnectModal.getListOfActiveSessions()
which will return a list of type Modal.Model.Session
.
Get list of pending session requests for a topic
To get an active session for a topic, call WalletConnectModal.getActiveSessionByTopic()
and pass a topic which will return
a Modal.Model.Session
object containing requestId, method, chainIs and params for pending request.
You can launch the currently connected wallet by calling service.launchCurrentWallet()
.
useWalletConnectModal
Hook to programmatically control the modal. Useful when you want to use your own UI elements and subscribe to modals state.
*Note: A new session is created automatically when the modal is opened, so avoid calling provider.connect
by yourself.
Example
Connection and Events
- WalletConnect Modal is a singleton that can be accessed from any scene.
- By default Modal will initialize itself asynchronously on Awake. During initialization it will also try to connect to the last session.
- After initialization, Modal invokes
WalletConnectModal.Ready
static event. - If
Ready
argument’sSessionResumed
istrue
, it means that Modal has successfully connected to the last session. In this case you don’t need to open the modal. Otherwise, open the modal withWalletConnectModal.Open()
static method.
Subscribe to ActiveSessionChanged
and SessionDeleted
events. It’s recommended to do it in Ready
event handler.
Disconnection
To disconnect from the current session, call WalletConnectModal.Disconnect()
static method.
Interaction with RPC
The WalletConnect Modal is responsible for facilitating communication between the game and the wallet.
Some methods do not require the user to interact with the wallet. For example, eth_getBalance
is used to get the address balance,
and eth_call
is used to read data from a smart contract without modifying its state, hence no signature is required.
To call these methods, you can use the Nethereum.Web3 package.
Interaction with Smart Contracts
To query smart contracts, you can use Nethereum.Web3 package
to make eth_call
requests directly to the RPC endpoint.
However, to call methods that modify the state of the smart contract, you need user to sign the transaction in the wallet.
There are two ways to interact with smart contracts:
- With Interceptor: using Nethereum’s
RequestInterceptor
(recommended) - Manual: using Nethereum’s tools to encode data and make requests directly with WalletConnect
WalletConnect provides a Nethereum interceptor utility that will route requests that require user signature to the wallet. With this approach, you don’t need to manually encode data and make requests. Use convenient Nethereum’s methods to interact with smart contracts, and interceptor will automatically route requests to the wallet.
WalletConnectUnity Nethereum source code
Install interceptor
Use interceptor
This example shows how to call transfer
method of ERC20 smart contract using interceptor.
Nethereum allows to deploy and interact with custom smart contracts as well. Refer to the Nethereum documentation for more information.
WalletConnect provides a Nethereum interceptor utility that will route requests that require user signature to the wallet. With this approach, you don’t need to manually encode data and make requests. Use convenient Nethereum’s methods to interact with smart contracts, and interceptor will automatically route requests to the wallet.
WalletConnectUnity Nethereum source code
Install interceptor
Use interceptor
This example shows how to call transfer
method of ERC20 smart contract using interceptor.
Nethereum allows to deploy and interact with custom smart contracts as well. Refer to the Nethereum documentation for more information.
The example below shows how to call approve
method of WETH9 (Wrapped Ether) smart contract. It encodes data with Nethereum and makes request with WalletConnect.
Please refer to Nethereum documentation for more details. Nethereum provides tools to simplify encoding and decoding. These tools hadn’t been used in the example above to better illustrate the process.