Skip to content

Commit

Permalink
Merge pull request #597 from enkryptcom/develop
Browse files Browse the repository at this point in the history
Release 2.1.0
  • Loading branch information
kvhnuke authored Jan 16, 2025
2 parents 793d0f0 + 422eda8 commit 620d312
Show file tree
Hide file tree
Showing 97 changed files with 3,162 additions and 626 deletions.
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@enkryptcom/extension",
"version": "2.0.2",
"version": "2.1.0",
"private": true,
"type": "module",
"scripts": {
Expand Down
33 changes: 30 additions & 3 deletions packages/extension/src/libs/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
GenericEvents,
NFTEventType,
NetworkChangeEvents,
NetworkType,
SendEventType,
SettingEventType,
SwapEventType,
UpdatesEventType,
UpdatesOpenLocation
} from './types';

const metrics = new Metrics();
Expand All @@ -18,9 +21,24 @@ const trackGenericEvents = (event: GenericEvents) => {
metrics.track('generic', { event });
};

const trackNetworkSelected = (
const trackNetwork = (
event: NetworkChangeEvents,
options: { provider: ProviderName; network: NetworkNames },
options: {
provider?: ProviderName;
network?: NetworkNames,
networkTab?: string,
networkType?: NetworkType,
isPinned?: boolean,
sortOption?: string,
customRpcUrl?: string,
customNetworkName?: string,
customNetworkNameLong?: string,
customNetworkCurrency?: string,
customNetworkCurrencyLong?: string,
customChainId?: string,
customBlockExplorerUrlTx?: string
customBlockExplorerUrlAddr?: string
},
) => {
metrics.track('network', { event, ...options });
};
Expand Down Expand Up @@ -75,6 +93,14 @@ const trackDAppsEvents = (
metrics.track('dapps', { event, ...options });
};

const trackUpdatesEvents = (event: UpdatesEventType, options: {
network: NetworkNames;
location?: UpdatesOpenLocation;
duration?: number;
}): void => {
metrics.track('updatesClick', { event, ...options });

}
const optOutofMetrics = (optOut: boolean) => {
if (!__IS_FIREFOX__) {
metrics.setOptOut(false);
Expand All @@ -87,12 +113,13 @@ const optOutofMetrics = (optOut: boolean) => {
};

export {
trackNetworkSelected,
trackNetwork,
trackSwapEvents,
trackBuyEvents,
trackSendEvents,
trackNFTEvents,
trackDAppsEvents,
optOutofMetrics,
trackGenericEvents,
trackUpdatesEvents
};
24 changes: 24 additions & 0 deletions packages/extension/src/libs/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,23 @@ export enum GenericEvents {
login_error = 'login_error',
}

export enum NetworkType {
Regular = 'regular',
Custom = 'custom',
Testnet = 'testnet',
}

export enum NetworkChangeEvents {
NetworkChangePopup = 'network_change_popup',
NetworkChangeAPI = 'network_change_api',
NetworkTabsClicked = 'network_tabs_clicked',
NetworkPinnedStatusChanged = 'network_pinned_status_changed',
NetworkActiveChanged = 'network_active_changed',
NetworkSortOptionChanged = 'network_sort_option_changed',
NetworkAddCustomClicked = 'network_add_custom_clicked',
NetworkCustomNetworkAdded = 'network_custom_network_added',
NetworkDeleteCustomNetwork = 'network_delete_custom_network',
NetworkCustomBackButton = 'network_custom_back_button',
}

export enum BuyEventType {
Expand Down Expand Up @@ -47,3 +61,13 @@ export enum DAppsEventType {
export enum SettingEventType {
OptOut = 'opt_out',
}

export enum UpdatesEventType {
UpdatesOpen = 'updates_open',
UpdatesClosed = 'updates_closed',
}

export enum UpdatesOpenLocation {
settings = 'settings',
logo = "logo",
}
86 changes: 80 additions & 6 deletions packages/extension/src/libs/networks-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ class NetworksState {
const networks: NetworkStorageElement[] = POPULAR_NAMES.map(name => ({
name,
}));
await this.setState({ networks, newNetworksVersion: '' });
await this.setState({
networks,
newNetworksVersion: '',
enabledTestNetworks: [],
newUsedFeatures: { networks: [], swap: [] },
});
}

/**
* Pins or unpins a network on the UI.
* @param targetNetworkName - the name of the network to set the status of
* @param isActive - represents whether or not the network is pinned on the ui
*/
async setNetworkStatus(
targetNetworkName: string,
isActive: boolean,
Expand All @@ -43,6 +53,16 @@ class NetworksState {
await this.setState(state);
}

/**
* Inserts networks with new features.
*
* This method first retrieves the current state and checks if the networks
* have been updated to the latest version. If the state and networks are defined,
* it filters out the networks that are not in the predefined list of networks with new features.
* It then maps the filtered networks to a new network item and inserts them into the valid networks.
* The new networks are inserted at the 6th index, or at the end if there are fewer than 6 networks.
* The state is then updated with the new networks and the latest version.
*/
async insertNetworksWithNewFeatures(): Promise<void> {
const state: IState | undefined = await this.getState();
if (
Expand All @@ -69,11 +89,44 @@ class NetworksState {
.concat(fnetworkItem, validNetworks.slice(insertIdx));
state.networks = validNetworks;
state.newNetworksVersion = __PACKAGE_VERSION__ as string;
state.newUsedFeatures = { networks: [], swap: [] };
await this.setState(state);
}
}

async getActiveNetworkNames(): Promise<string[]> {
async setUsedFeature(feature: 'networks' | 'swap', networkName: string) {
const state: IState | undefined = await this.getState();
if (state) {
const newUsedFeatures = state.newUsedFeatures || {
networks: [],
swap: [],
};
newUsedFeatures[feature].push(networkName);
await this.setState({ ...state, newUsedFeatures });
}
}

async getUsedFeatures(): Promise<IState['newUsedFeatures']> {
const state: IState | undefined = await this.getState();
if (state && state.newUsedFeatures) {
return state.newUsedFeatures;
}
return { networks: [], swap: [] };
}

/**
* Retrieves the names of the pinned networks.
*
* This method first ensures that networks with new features are inserted.
* It then attempts to retrieve the current state. If the state and its networks
* are defined, it maps and returns the names of the valid networks.
* If the state or networks are not defined, it sets the initial active networks
* and returns a predefined list of popular network names.
*
* Previously, the method was named `getActiveNetworks`.
* @returns {Promise<string[]>} A promise that resolves to an array of active network names.
*/
async getPinnedNetworkNames(): Promise<string[]> {
await this.insertNetworksWithNewFeatures();
const state: IState | undefined = await this.getState();
if (state && state.networks) {
Expand All @@ -85,16 +138,37 @@ class NetworksState {
}
}

async getEnabledTestNetworks(): Promise<string[]> {
await this.insertNetworksWithNewFeatures();
const state: IState | undefined = await this.getState();
if (state && state.enabledTestNetworks) {
const validNetworks = state.enabledTestNetworks;
return validNetworks.map(({ name }) => name);
} else {
this.setState(Object.assign({}, state, { enabledTestNetworks: [] }));
return [];
}
}

async setTestnetStatus(
networkName: string,
isEnabled: boolean,
): Promise<void> {
const state: IState | undefined = await this.getState();
const enabledTestNetworks = (state.enabledTestNetworks || []).filter(
n => n.name !== networkName,
);
if (isEnabled) enabledTestNetworks.push({ name: networkName });
await this.setState({ ...state, enabledTestNetworks });
}

async reorderNetwork(networkNames: string[]): Promise<void> {
const state: IState | undefined = await this.getState();
const activeNetworks: NetworkStorageElement[] = networkNames.map(name => ({
name,
isActive: true,
}));
await this.setState({
networks: activeNetworks,
newNetworksVersion: state.newNetworksVersion,
});
await this.setState({ ...state, networks: activeNetworks });
}

async setState(state: IState): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions packages/extension/src/libs/networks-state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ export interface NetworkStorageElement {
export interface IState {
networks: NetworkStorageElement[];
newNetworksVersion: string;
enabledTestNetworks: NetworkStorageElement[];
newUsedFeatures: {
networks: string[];
swap: string[];
};
}
41 changes: 41 additions & 0 deletions packages/extension/src/libs/recently-sent-addresses/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { InternalStorageNamespace } from "@/types/provider";
import BrowserStorage from "../common/browser-storage";
import { IState, } from "./types";
import { NetworkNames } from "@enkryptcom/types";
import { BaseNetwork } from "@/types/base-network";

class RecentlySentAddressesState {
#storage: BrowserStorage

constructor() {
this.#storage = new BrowserStorage(
InternalStorageNamespace.recentlySentAddresses,
);
}

async addRecentlySentAddress(
network: Pick<BaseNetwork, 'name' | 'displayAddress'>,
address: string,
): Promise<void> {
const key = network.name
const state: IState | undefined = await this.#storage.get(key)
const newState: IState = {
...state,
addresses: Array.from(new Set([
network.displayAddress(address),
...(state?.addresses ?? []),
])).slice(0, 5),
}
await this.#storage.set(key, newState)
}

async getRecentlySentAddresses(networkName: NetworkNames): Promise<string[]> {
const key = networkName
const out: IState | undefined = await this.#storage.get(key)
if (!out) return []
return out.addresses
}
}

export default RecentlySentAddressesState

3 changes: 3 additions & 0 deletions packages/extension/src/libs/recently-sent-addresses/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type IState = {
addresses: string[]
}
62 changes: 62 additions & 0 deletions packages/extension/src/libs/updates-state/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import BrowserStorage from '../common/browser-storage';
import { InternalStorageNamespace } from '@/types/provider';
import { IState, StorageKeys } from './types';

class UpdatesState {
private storage: BrowserStorage;

constructor() {
this.storage = new BrowserStorage(InternalStorageNamespace.updatesState);
}

async setState(state: IState): Promise<void> {
return this.storage.set(StorageKeys.updatesInfo, state);
}

async getState(): Promise<IState> {
const state = this.storage.get(StorageKeys.updatesInfo);
if (!state) {
const newState: IState = {
lastVersionViewed: '',
currentRelease: '',
currentReleaseTimestamp: 0,
}
return newState
}
return state;
}

async getLastVersionViewed(): Promise<IState['lastVersionViewed']> {
const state: IState = await this.getState();
return state?.lastVersionViewed ?? '';
}
async setLastVersionViewed(lastVersionViewed: string): Promise<void> {
const state: IState = await this.getState();
const newState: IState = { ...state, lastVersionViewed }
await this.setState(newState);
}

async getCurrentRelease(): Promise<IState['currentRelease']> {
const state: IState = await this.getState();
return state?.currentRelease ?? '';
}

async setCurrentRelease(currentRelease: string): Promise<void> {
const state: IState = await this.getState();
const newState: IState = { ...state, currentRelease }
await this.setState(newState);
}

async getCurrentReleaseTimestamp(): Promise<IState['currentReleaseTimestamp']> {
const state: IState = await this.getState();
return state?.currentReleaseTimestamp ?? 0;
}

async setCurrentReleaseTimestamp(currentReleaseTimestamp: number): Promise<void> {
const state: IState = await this.getState();
const newState: IState = { ...state, currentReleaseTimestamp }
await this.setState(newState);
}
}

export default UpdatesState;
9 changes: 9 additions & 0 deletions packages/extension/src/libs/updates-state/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export enum StorageKeys {
updatesInfo = 'updates-info',
}

export interface IState {
lastVersionViewed: string;
currentRelease: string;
currentReleaseTimestamp: number;
}
Loading

0 comments on commit 620d312

Please sign in to comment.