|
| 1 | +# Advanced |
| 2 | + |
| 3 | +## Code Structure |
| 4 | + |
| 5 | +To make user better understand the whole design structure of CosmosKit, here to briefly introduce some important classes from `@cosmos-kit/core`. |
| 6 | + |
| 7 | +There are four important classes. |
| 8 | + |
| 9 | +- WalletManager |
| 10 | +- MainWalletBase |
| 11 | +- ChainWalletBase |
| 12 | +- WalletRepo |
| 13 | + |
| 14 | +Before all, we need to clarify that there are two types of entities in CosmosKit as a wallet adapter: **Chain** and **Wallet**. Chain is identified by chain name i.e. `cosmoshub`, `osmosis` etc. And wallet is identified by wallet name i.e. `keplr-extension`, `keplr-mobile`, `cosmostation-extension` etc. |
| 15 | + |
| 16 | +> Note that we're taking a single wallet application as a wallet in CosmosKit rather than the wallet product name. Taking `Keplr` as an example, we differentiate `extension` and `mobile` in our code because they are connected via totally different codes. So for product `Keplr`, we have two wallets `keplr-extension` and `keplr-mobile` in CosmosKit. |
| 17 | +
|
| 18 | +### WalletManager |
| 19 | + |
| 20 | +`WalletManager` is the entrance of the whole code and it manages all `WalletRepo`, `MainWalletBase`, `ChainWalletBase` instances in it. It also corresponds to `ChainProvider` in `@cosmos-kit/react-lite` and `@cosmos-kit/react`. You can find that the properties of JSX element `ChainProvider` are almost the same with the constructor arguments of `WalletManager`. All necessary chain information and wallet information from `ChainProvider` will be passed to corresponding wallet classes via `WalletManager`. |
| 21 | + |
| 22 | +Three important properties/arguments in `ChainProvider`/`WalletManager` are `chains`, `assetLists` and `wallets`. `chains` and `assetLists` provide chain information, and `wallets` provides wallet information. Actually `wallets` is an array of `MainWalletBase` instances. Here leads to the second class `MainWalletBase`. |
| 23 | + |
| 24 | +### MainWalletBase |
| 25 | + |
| 26 | +`MainWalletBase` is meant to provide a base implementation and unified interface for all different wallets like `keplr-extension`, `keplr-mobile` and `cosmostation-extension`. Basically every wallet has it's own `MainWallet` class, which extends `MainWalletBase` in common, but with `WalletClient` implemented in different ways. In this way `WalletManager` can handle all different wallets no matter how different they're inside. |
| 27 | + |
| 28 | +> For practice you can take a look at [How to Integrate New Wallets into CosmosKit](/integrating-wallets/adding-new-wallets) |
| 29 | +
|
| 30 | +`MainWalletBase` is only about wallet and it's not about any specific chain. And it's responsible for initializing wallet client and managing all chain wallets. Here brings in the third class `ChainWalletBase`. |
| 31 | + |
| 32 | +> So far `MainWalletBase` is dealing with four different broadcast/synchronization events for chain wallets. |
| 33 | +> |
| 34 | +> - broadcast_client |
| 35 | +> - broadcast_env |
| 36 | +> - sync_connect |
| 37 | +> - sync_disconnect |
| 38 | +> |
| 39 | +> See details below. |
| 40 | +
|
| 41 | +### ChainWalletBase |
| 42 | + |
| 43 | +When you're trying to connect to a wallet, you always need to provide a target chain name so that the wallet knows what to response. So `ChainWalletBase` is the class actually being used for real connection. It's the finest grain of functionality that with chain specified and also wallet specified. |
| 44 | + |
| 45 | +We're separating `MainWalletBase` and `ChainWalletBase` because it's clearer to put some common properties like `wallet client` and `env` in the `MainWalletBase` to enable |
| 46 | +centralized management and distribution (events `broadcast_client` and `broadcast_env`), and put only chain specified functionalities in `ChainWalletBase`. |
| 47 | + |
| 48 | +Basically how many `chains` are provided in `ChainProvider` or `WalletManager`, how many `ChainWalletBase` instances will be constructed for a wallet. `ChainWalletBase` instances are independent with each other unless `sync` is set `true`. All the synchronization are also handled in `MainWalletBase` (events `sync_connect` and `sync_disconnect`). |
| 49 | + |
| 50 | +### WalletRepo |
| 51 | + |
| 52 | +We have a class `MainWalletBase` with wallet specified to manage all chain wallets. All these chain wallets are with the same wallet name but different chain name. Accordingly we also have another class `WalletRepo`, which with chain specified to manage all chain wallets that with the same chain name but different wallet name. |
| 53 | + |
| 54 | +### MainWalletBase vs. WalletRepo |
| 55 | + |
| 56 | +#### 1. **MainWalletBase** |
| 57 | +- **Purpose**: Manages a collection of chain wallets. |
| 58 | +- **Key Identifier**: Wallet name. |
| 59 | +- **Example**: It handles wallets like cosmoshub/keplr-extension, osmosis/keplr-extension, etc. These are wallets from different chains but with the same wallet name. |
| 60 | + |
| 61 | +#### 2. **WalletRepo** |
| 62 | +- **Purpose**: Manages chain wallets too, but with a different approach. |
| 63 | +- **Key Identifier**: Chain name. |
| 64 | +- **Example**: It manages wallets like cosmoshub/keplr-extension, cosmoshub/keplr-mobile, etc. These are wallets from the same chain but with different wallet names. |
| 65 | + |
| 66 | +#### Common Point |
| 67 | +- **Both MainWalletBase and WalletRepo** are involved in managing chain wallets, which are wallets associated with different blockchain networks. |
| 68 | + |
| 69 | +#### Key Differences |
| 70 | +- **MainWalletBase**: Focuses on managing wallets based on the wallet’s name. It doesn’t matter what chain the wallet is from; as long as they share the same wallet name, MainWalletBase manages them. |
| 71 | +- **WalletRepo**: Concentrates on managing wallets based on the chain’s name. Here, the specific wallet names don’t matter; WalletRepo groups and manages wallets that are on the same blockchain network. |
| 72 | + |
| 73 | +#### Practical Use |
| 74 | +- **In some decentralized applications (dapps)**, the focus might be more on the blockchain network (chain) rather than the wallet itself. In such cases, WalletRepo is particularly useful because it provides a perspective based on the chain, allowing different wallets on the same chain to be managed together. |
| 75 | + |
| 76 | +#### Summary |
| 77 | +- **MainWalletBase**: Manages wallets across different chains but with the same wallet name. |
| 78 | +- **WalletRepo**: Manages different wallets on the same chain. |
| 79 | + |
| 80 | +In essence, these two classes offer different ways of organizing and accessing chain wallets, based on what the primary point of interest is (wallet name or chain name). |
| 81 | + |
| 82 | +So far `WalletRepo` is only used in [`WalletModal`](https://docs.cosmology.zone/cosmos-kit/provider/chain-provider#walletmodal) properties. |
0 commit comments