diff --git a/packages/wallet-manager/src/test/WalletManagerController.test.ts b/packages/wallet-manager/src/test/WalletManagerController.test.ts new file mode 100644 index 00000000..dd07a32b --- /dev/null +++ b/packages/wallet-manager/src/test/WalletManagerController.test.ts @@ -0,0 +1,40 @@ +import { LitElement } from 'lit'; +import { ExternalProvider } from '@ethersproject/providers'; +import { ethers } from 'ethers' +import { describe, it, beforeEach, vi, expect } from 'vitest'; +import { WalletManagerController } from '..'; + +class WidgetTestFixture extends LitElement { + constructor() { + super(); + } +} + +customElements.define('widget-test-fixture', WidgetTestFixture); + +describe('WalletManagerController', () => { + let walletController: WalletManagerController; + const walletTextFixture = new WidgetTestFixture(); + + beforeEach(() => { + window.ethereum = { + request: () => Promise.resolve(true), + on: vi.fn() + } as ExternalProvider & { on: () => void }; + + walletController = new WalletManagerController(walletTextFixture); + }); + + it('should instantiate', () => { + expect(walletController).toBeInstanceOf(WalletManagerController); + }); + it('should initialize evm wallet from window', () => { + walletController.initFromWindow(); + expect(walletController.evmWallet).toBeDefined(); + }); + it('should initialize evm wallet from web3 provider', () => { + const provider = new ethers.providers.Web3Provider(window.ethereum); + walletController.initFromWeb3Provider(provider); + expect(walletController.evmWallet).toBeDefined(); + }); +}); diff --git a/packages/wallet-manager/src/wallets/Evm/Evm.ts b/packages/wallet-manager/src/wallets/Evm/Evm.ts index 99bda695..df33bb61 100644 --- a/packages/wallet-manager/src/wallets/Evm/Evm.ts +++ b/packages/wallet-manager/src/wallets/Evm/Evm.ts @@ -150,6 +150,15 @@ class EvmWallet extends events.EventEmitter implements IEvmWallet { } } + /** + * @name addChain + * @param chainId + * @param chainName + * @param rpcUrl + * @param nativeCurrency - { name, symbol, decimals } + * @returns void + * @description Adds a new chain to the wallet + */ public async addChain({ chainId, chainName, diff --git a/packages/wallet-manager/src/wallets/Substrate/Substrate.ts b/packages/wallet-manager/src/wallets/Substrate/Substrate.ts index f635079e..c95a202f 100644 --- a/packages/wallet-manager/src/wallets/Substrate/Substrate.ts +++ b/packages/wallet-manager/src/wallets/Substrate/Substrate.ts @@ -11,21 +11,46 @@ class SubstrateWallet implements ISusbtrateWallet { this.apiPromise = apiPromise; } - static async connectFromWssProvider(wssProvider: string) { + /** + * @name connectFromWssProvider + * @param wssProvider + * @returns SubstrateWallet + * @description Initializes the SubstrateWallet from a wssProvider + */ + static async connectFromWssProvider( + wssProvider: string + ): Promise { const wsProvider = await SubstrateWallet.conntectToApi(wssProvider); const apiPromise = await ApiPromise.create({ provider: wsProvider }); return new SubstrateWallet(apiPromise); } - static connectFromApiPromise(apiPromise: ApiPromise) { + /** + * @name connectFromApiPromise + * @param apiPromise + * @returns SubstrateWallet + * @description Initializes the SubstrateWallet from an ApiPromise + */ + static connectFromApiPromise(apiPromise: ApiPromise): SubstrateWallet { return new SubstrateWallet(apiPromise); } + /** + * @name conntectToApi + * @param wssProvider + * @returns WsProvider + * @description Initializes the WsProvider + */ static async conntectToApi(wssProvider: string): Promise { const wsProvider = new WsProvider(wssProvider); return wsProvider; } + /** + * @name connect + * @returns void + * @description Connects to the Substrate Wallet + */ public async connect() { const injectors = await web3Enable('Polkadot Wallet'); // using polkadot-js extension