From 323f603cb293eac593adf8bf4b8b7861c88fc8e2 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 19 Nov 2023 08:32:33 +0200 Subject: [PATCH] fix: code style --- package.json | 2 +- src/web3/adapters/GelatoAdapter.ts | 30 ++++---- src/web3/store/transactionsSlice.ts | 51 +++++-------- src/web3/store/walletSlice.ts | 111 +++++++++++++++------------- 4 files changed, 93 insertions(+), 101 deletions(-) diff --git a/package.json b/package.json index 2740e11..3b983e3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@bgd-labs/frontend-web3-utils", "description": "Frontend utilities common to multiple Web3 projects", - "version": "0.4.87", + "version": "0.4.88", "author": "BGD labs", "license": "MIT", "private": false, diff --git a/src/web3/adapters/GelatoAdapter.ts b/src/web3/adapters/GelatoAdapter.ts index 33cda93..ee32cee 100644 --- a/src/web3/adapters/GelatoAdapter.ts +++ b/src/web3/adapters/GelatoAdapter.ts @@ -62,6 +62,21 @@ export class GelatoAdapter implements AdapterInterface { this.set = set; } + checkIsGelatoAvailable = async (chainId: number) => { + try { + const response = await fetch(`https://relay.gelato.digital/relays/v2`); + if (!response.ok) { + return false; + } else { + const listOfRelays = (await response.json()) as { relays: string[] }; + return !!listOfRelays.relays.find((id) => +id === chainId); + } + } catch (e) { + console.error('Check gelato available error', e); + return false; + } + }; + startTxTracking = async (tx: PoolTx) => { if (isGelatoBaseTx(tx)) { const isPending = isGelatoTXPending(tx.gelatoStatus); @@ -89,21 +104,6 @@ export class GelatoAdapter implements AdapterInterface { } }; - checkIsGelatoAvailable = async (chainId: number) => { - try { - const response = await fetch(`https://relay.gelato.digital/relays/v2`); - if (!response.ok) { - return false; - } else { - const listOfRelays = (await response.json()) as { relays: string[] }; - return !!listOfRelays.relays.find((id) => +id === chainId); - } - } catch (e) { - console.error('Check gelato available error', e); - return false; - } - }; - private fetchGelatoTXStatus = async (taskId: string) => { const response = await fetch( `https://api.gelato.digital/tasks/status/${taskId}/`, diff --git a/src/web3/store/transactionsSlice.ts b/src/web3/store/transactionsSlice.ts index 0652bd1..9a1c3db 100644 --- a/src/web3/store/transactionsSlice.ts +++ b/src/web3/store/transactionsSlice.ts @@ -25,16 +25,15 @@ import { WalletType } from '../connectors'; import { IWalletSlice } from './walletSlice'; export type PoolTxParams = { - status?: TransactionStatus; pending: boolean; walletType: WalletType; + status?: TransactionStatus; replacedTxHash?: Hex; }; - +export type PoolTx = T & PoolTxParams; export type EthPoolTx = EthBaseTx & PoolTxParams; export type GelatoPoolTx = GelatoBaseTx & PoolTxParams; - -export type PoolTx = T & PoolTxParams; +export type TransactionPool = Record; export type TransactionsSliceBaseType = { clients: ClientsRecord; @@ -42,14 +41,7 @@ export type TransactionsSliceBaseType = { initTxPool: () => void; }; -export type TransactionPool = Record; - export interface ITransactionsState { - transactionsPool: TransactionPool>; - transactionsIntervalsMap: Record; -} - -export interface ITransactionsActions { adapters: { [TxAdapter.Ethereum]: EthereumAdapter; [TxAdapter.Safe]?: SafeAdapter; @@ -57,6 +49,14 @@ export interface ITransactionsActions { }; setAdapter: (adapter: TxAdapter) => void; + transactionsPool: TransactionPool>; + transactionsIntervalsMap: Record; + + isGelatoAvailable: boolean; + checkIsGelatoAvailable: (chainId: number) => Promise; +} + +export interface ITransactionsActions { txStatusChangedCallback: ( data: T & { status?: TransactionStatus; @@ -73,9 +73,6 @@ export interface ITransactionsActions { }) => Promise[string] | undefined>; addTXToPool: (tx: InitialTxParams) => TransactionPool>; removeTXFromPool: (txKey: string) => void; - - isGelatoAvailable: boolean; - checkIsGelatoAvailable: (chainId: number) => Promise; } export type ITransactionsSlice = ITransactionsActions & @@ -97,6 +94,7 @@ export function createTransactionsSlice({ Pick > { return (set, get) => ({ + txStatusChangedCallback, clients: defaultClients, setClient: (chainId, client) => { set((state) => @@ -106,7 +104,8 @@ export function createTransactionsSlice({ ); }, - txStatusChangedCallback, + transactionsPool: {}, + transactionsIntervalsMap: {}, adapters: { [TxAdapter.Ethereum]: new EthereumAdapter(get, set), @@ -126,9 +125,6 @@ export function createTransactionsSlice({ } }, - transactionsPool: {}, - transactionsIntervalsMap: {}, - initTxPool: () => { const localStorageTXPool = getLocalStorageTxPool(); @@ -168,7 +164,6 @@ export function createTransactionsSlice({ } const chainId = Number(desiredChainID); - let adapterType = TxAdapter.Ethereum; let newTxKey: Hex | string | undefined = isHex(txKey) ? txKey : undefined; @@ -257,26 +252,16 @@ export function createTransactionsSlice({ delete draft.transactionsPool[txKey]; }), ); - - const txPool = get().transactionsPool; - setLocalStorageTxPool(txPool); + setLocalStorageTxPool(get().transactionsPool); }, // need for gelato only isGelatoAvailable: true, checkIsGelatoAvailable: async (chainId) => { + get().setAdapter(TxAdapter.Gelato); const adapter = get().adapters[TxAdapter.Gelato]; - if (adapter) { - const isAvailable = await adapter.checkIsGelatoAvailable(chainId); - set({ isGelatoAvailable: isAvailable }); - } else { - get().setAdapter(TxAdapter.Gelato); - const isAvailable = - await get().adapters[TxAdapter.Gelato]?.checkIsGelatoAvailable( - chainId, - ); - set({ isGelatoAvailable: isAvailable }); - } + const isAvailable = await adapter?.checkIsGelatoAvailable(chainId); + set({ isGelatoAvailable: isAvailable }); }, }); } diff --git a/src/web3/store/walletSlice.ts b/src/web3/store/walletSlice.ts index 9bc6c88..188a8ff 100644 --- a/src/web3/store/walletSlice.ts +++ b/src/web3/store/walletSlice.ts @@ -35,42 +35,48 @@ export interface Wallet { walletClient: WalletClient; // isActive is added, because Wallet can be connected but not active, i.e. wrong network isActive: boolean; - // isContractAddress is added, to check if wallet address is contract + // isContractAddress is added, to check if wallet address is contract (mostly fo safe) isContractAddress: boolean; } export type IWalletSlice = { - isContractWalletRecord: Record; + connectors: ConnectorType[]; + setConnectors: (connectors: ConnectorType[]) => void; + + defaultChainId: number; + setDefaultChainId: (chainId: number) => void; + + initDefaultWallet: () => Promise; + + isActiveWalletSetting: boolean; activeWallet?: Wallet; setActiveWallet: ( wallet: Omit, ) => Promise; - isActiveWalletSetting: boolean; - connectWallet: (walletType: WalletType, chainId?: number) => Promise; - disconnectActiveWallet: () => Promise; + walletActivating: boolean; walletConnectionError: string; + connectWallet: (walletType: WalletType, chainId?: number) => Promise; + disconnectActiveWallet: () => Promise; resetWalletConnectionError: () => void; - initDefaultWallet: () => Promise; - changeActiveWalletAccount: (account?: GetAccountResult) => Promise; + checkAndSwitchNetwork: (chainId?: number) => Promise; + isActiveWalletAccountChanging: boolean; - changeActiveWalletChain: (chain?: Chain) => Promise; + changeActiveWalletAccount: (account?: GetAccountResult) => Promise; isActiveWalletChainChanging: boolean; - checkAndSwitchNetwork: (chainId?: number) => Promise; - connectors: ConnectorType[]; - setConnectors: (connectors: ConnectorType[]) => void; + changeActiveWalletChain: (chain?: Chain) => Promise; + impersonated?: { account?: Account; address?: Hex; isViewOnly?: boolean; }; setImpersonated: (privateKeyOrAddress: string) => void; + + isContractWalletRecord: Record; checkIsContractWallet: ( wallet: Omit, ) => Promise; - - defaultChainId: number; - setDefaultChainId: (chainId: number) => void; }; export function createWalletSlice({ @@ -79,9 +85,6 @@ export function createWalletSlice({ walletConnected: (wallet: Wallet) => void; }): StoreSlice { return (set, get) => ({ - isContractWalletRecord: {}, - walletActivating: false, - walletConnectionError: '', connectors: [], setConnectors: async (connectors) => { if (get().connectors.length !== connectors.length) { @@ -90,6 +93,12 @@ export function createWalletSlice({ get().initTxPool(); } }, + + defaultChainId: mainnet.id, + setDefaultChainId: (chainId) => { + set({ defaultChainId: chainId }); + }, + initDefaultWallet: async () => { const lastConnectedWallet = localStorage.getItem( LocalStorageKeys.LastConnectedWallet, @@ -100,6 +109,7 @@ export function createWalletSlice({ } }, + isActiveWalletSetting: false, setActiveWallet: async (wallet) => { if (wallet.isActive) { if (wallet.chain) { @@ -128,8 +138,9 @@ export function createWalletSlice({ } } }, - isActiveWalletSetting: false, + walletActivating: false, + walletConnectionError: '', connectWallet: async (walletType, chainId) => { clearWalletLinkLocalStorage(); clearWalletConnectV2LocalStorage(); @@ -178,7 +189,7 @@ export function createWalletSlice({ } } catch (e) { if (e instanceof Error) { - const errorMessage = e.message ? e.message.toString() : e.toString(); + const errorMessage = e.message ? String(e.message) : String(e); set({ walletConnectionError: errorMessage, }); @@ -187,6 +198,16 @@ export function createWalletSlice({ } set({ walletActivating: false }); }, + disconnectActiveWallet: async () => { + await disconnect(); + set({ activeWallet: undefined }); + deleteLocalStorageWallet(); + clearWalletLinkLocalStorage(); + clearWalletConnectV2LocalStorage(); + }, + resetWalletConnectionError: () => { + set({ walletConnectionError: '' }); + }, checkAndSwitchNetwork: async (chainId) => { const activeWallet = get().activeWallet; if (chainId && activeWallet && activeWallet?.chain?.id !== chainId) { @@ -207,32 +228,8 @@ export function createWalletSlice({ }); } }, - disconnectActiveWallet: async () => { - await disconnect(); - set({ activeWallet: undefined }); - deleteLocalStorageWallet(); - clearWalletLinkLocalStorage(); - clearWalletConnectV2LocalStorage(); - }, - - checkIsContractWallet: async (wallet) => { - const address = wallet.address; - const walletRecord = get().isContractWalletRecord[address]; - if (walletRecord !== undefined) { - return walletRecord; - } - const codeOfWalletAddress = await wallet.client.getBytecode({ - address: wallet.address, - }); - const isContractWallet = !!codeOfWalletAddress; - set((state) => - produce(state, (draft) => { - draft.isContractWalletRecord[address] = isContractWallet; - }), - ); - return isContractWallet; - }, + isActiveWalletAccountChanging: false, changeActiveWalletAccount: async (account) => { const activeWallet = get().activeWallet; if ( @@ -252,7 +249,7 @@ export function createWalletSlice({ set({ isActiveWalletAccountChanging: false }); } }, - isActiveWalletAccountChanging: false, + isActiveWalletChainChanging: false, changeActiveWalletChain: async (chain) => { const activeWallet = get().activeWallet; if ( @@ -273,7 +270,6 @@ export function createWalletSlice({ set({ isActiveWalletChainChanging: false }); } }, - isActiveWalletChainChanging: false, setImpersonated: (privateKeyOrAddress) => { if (isAddress(privateKeyOrAddress)) { @@ -292,13 +288,24 @@ export function createWalletSlice({ }); } }, - resetWalletConnectionError: () => { - set({ walletConnectionError: '' }); - }, - defaultChainId: mainnet.id, - setDefaultChainId: (chainId) => { - set({ defaultChainId: chainId }); + isContractWalletRecord: {}, + checkIsContractWallet: async (wallet) => { + const address = wallet.address; + const walletRecord = get().isContractWalletRecord[address]; + if (walletRecord !== undefined) { + return walletRecord; + } + const codeOfWalletAddress = await wallet.client.getBytecode({ + address: wallet.address, + }); + const isContractWallet = !!codeOfWalletAddress; + set((state) => + produce(state, (draft) => { + draft.isContractWalletRecord[address] = isContractWallet; + }), + ); + return isContractWallet; }, }); }