UNPKG

@sky-mavis/tanto-connect

Version:
84 lines 3.68 kB
import {DEFAULT_CONNECTORS_CONFIG}from'../../common/connectors.mjs';import {ReconnectStorage}from'../../common/storage.mjs';import'../../common/constant.mjs';import'../../types/eip6963.mjs';import {requestRoninProvider}from'../../providers/ronin.mjs';import'@walletconnect/ethereum-provider';import'@safe-global/safe-apps-provider';import'@safe-global/safe-apps-sdk';import {ConnectorError,ConnectorErrorType}from'../../types/connector-error.mjs';import'@sky-mavis/waypoint';import'../../common/chain.mjs';import {numberToHex}from'../../utils/index.mjs';import {EIP1193Event}from'../../types/eip1193.mjs';import {BaseConnector}from'../base/BaseConnector.mjs';class RoninWalletConnector extends BaseConnector { isRonin; constructor(configs, provider) { super({ ...DEFAULT_CONNECTORS_CONFIG.RONIN_WALLET, ...configs }, provider); this.isRonin = true; } async connect(chainId) { const provider = await this.getProvider(); const accounts = await this.requestAccounts(); const currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { await this.switchChain(chainId); } const connectResults = { provider, chainId: chainId || currentChainId, account: accounts[0], }; this.setupProviderListeners(); this.onConnect(connectResults); ReconnectStorage.add(this.id); return connectResults; } async disconnect() { ReconnectStorage.remove(this.id); const provider = await this.getProvider(); await provider?.request({ method: 'wallet_disconnectSession', }); this.removeProviderListeners(); this.onDisconnect(); } async isAuthorized() { const accounts = await this.getAccounts(); return accounts.length > 0; } async getAccounts() { const provider = await this.getProvider(); return provider.request({ method: 'eth_accounts', }); } async switchChain(chain) { const provider = await this.getProvider(); return provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: numberToHex(chain) }], }); } async addChain() { throw new ConnectorError(ConnectorErrorType.ADD_CHAIN_NOT_SUPPORTED); } async getChainId() { const provider = await this.getProvider(); const chainId = await provider?.request({ method: 'eth_chainId', }); return Number(chainId); } async requestAccounts() { const provider = await this.getProvider(); return provider?.request({ method: 'eth_requestAccounts', }); } async requestProvider() { return requestRoninProvider(); } setupProviderListeners() { this.removeProviderListeners(); if (this.provider) { this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.on(EIP1193Event.CHAIN_CHANGED, this.onChainChanged); } } removeProviderListeners() { if (this.provider) { this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect); this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged); this.provider.removeListener(EIP1193Event.CHAIN_CHANGED, this.onChainChanged); } } }export{RoninWalletConnector};