Beacon is a Tezos toolkit for wallet pairing. It allows wallet developers to integrate with dApps and dApps developers to give more choices to the user.
In order to implement the connect wallet button, and basic blockchain connectivity, we will start our first provider: WalletProvider
Add a providers folder in ./components, and create ./components/providers/WalletProvider.tsx
In all our providers, we will define context props, that will represent the properties that will be made available to children using our context. We will then export a useContext shorthand, and the provider itself.
In React component lifecycle, to update the state when the component loads or when a dependency is updated, we use an effect:
useEffect(() => {if (!Tezos) {// create Taquito's Tezos toolkit instance and connect it to our RPCconstTezos=newTezosToolkit(process.env.NEXT_PUBLIC_TEZOS_RPC??"http://localhost:20000" )// instantiate the BeaconWallet objectconstbeacon=newBeaconWallet({ name:"TZombies", preferredNetwork: (process.env.NEXT_PUBLIC_NETWORK||"ghostnet") asNetworkType, colorMode:ColorMode.DARK, })// link the Tezos toolkit with BeaconTezos.setWalletProvider(beacon)// link the Completium SDK to our toolkitset_binder_tezos_toolkit(Tezos)// a returning user may already have a connected walletbeacon.client.getActiveAccount().then(setAccount)// set the statesetTezos(Tezos)setWallet(beacon) }}, [Tezos])
Let's stop here for a moment and analyse what is going on. This is an important part of the Tezos specifics.
First, we create a TezosToolkit instance (from taquito). It will connect to the given node RPC
Then we create a BeaconWallet instance and we connect them, as per their documentation
We also need to connect the Completium SDK to our TezosToolkit instance
If there is already an account connected (a returning user) then the connection will be restored
Finally, we set the state of our provider with the account, the toolkit and Beacon
Note that this is only done once, after the page has loaded.
Connect/disconnect
We can now implement the connect and disconnect callbacks:
For the remainder of the tutorial, imports for providers and components such as these may be ommitted and it will be left to the reader to include them where necessary.