Skip to content

Commit

Permalink
Merge branch 'dev' into 232-feature-decode-evm-transaction-call-data-…
Browse files Browse the repository at this point in the history
…for-user-readability
  • Loading branch information
zzggo committed Dec 17, 2024
2 parents fef0ee8 + 2731ca9 commit b3c2d93
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 179 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ jobs:
FB_APP_ID="${{ secrets.FB_APP_ID }}"
FB_MEASUREMENT_ID="${{ secrets.FB_MEASUREMENT_ID }}"
FB_FUNCTIONS="${{ secrets.FB_FUNCTIONS }}"
API_NEWS_PATH="${{ vars.API_NEWS_PATH }}"
API_CONFIG_PATH="${{ vars.API_CONFIG_PATH }}"
API_BASE_URL="${{ vars.API_BASE_URL }}"
# manifest
OAUTH2_CLIENT_ID="${{ secrets.OAUTH2_CLIENT_ID }}"
OAUTH2_SCOPES="${{ vars.OAUTH2_SCOPES }}"
Expand Down
57 changes: 0 additions & 57 deletions build/plugins/AssetReplacePlugin.js

This file was deleted.

58 changes: 0 additions & 58 deletions build/plugins/FirebaseFixPlugin.js

This file was deleted.

24 changes: 0 additions & 24 deletions build/webpack.common.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const ESLintWebpackPlugin = require('eslint-webpack-plugin');
// const tsImportPluginFactory = require('ts-import-plugin');
const AssetReplacePlugin = require('./plugins/AssetReplacePlugin');
const FirebaseFixPlugin = require('./plugins/FirebaseFixPlugin');
const { version } = require('../_raw/manifest.json');
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
Expand Down Expand Up @@ -105,19 +101,6 @@ const config = (env) => {
name: '[name].[ext]',
},
},
// {
// test: /\.wasm$/,
// include: path.resolve(__dirname, 'node_modules/@trustwallet/wallet-core/dist/lib'),
// use: [{
// loader: 'file-loader',
// options: {
// name: '[name].[ext]',
// outputPath: '/',
// },
// }],
// type: 'javascript/auto',

// },
{
test: /\.wasm$/,
type: 'webassembly/async',
Expand All @@ -142,10 +125,6 @@ const config = (env) => {
],
},
plugins: [
new FirebaseFixPlugin(),
// new ESLintWebpackPlugin({
// files: '**/*.{ts,tsx,js,jsx}',
// }),
new CopyPlugin({
patterns: [
{
Expand Down Expand Up @@ -187,9 +166,6 @@ const config = (env) => {
process: 'process',
dayjs: 'dayjs',
}),
// new AssetReplacePlugin({
// '#PAGEPROVIDER#': 'pageProvider',
// }),
new webpack.DefinePlugin({
'process.env.version': JSON.stringify(`version: ${version}`),
'process.env.release': JSON.stringify(version),
Expand Down
15 changes: 7 additions & 8 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as bip39 from 'bip39';
import { ethErrors } from 'eth-rpc-errors';
import * as ethUtil from 'ethereumjs-util';
import { getApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getAuth } from 'firebase/auth/web-extension';
import web3, { TransactionError } from 'web3';

import eventBus from '@/eventBus';
Expand Down Expand Up @@ -3475,19 +3475,18 @@ export class WalletController extends BaseController {
}
} catch (err: unknown) {
// An error has occurred while listening to the transaction
console.log(typeof err);
console.log({ err });
console.error('listenTransaction error ', err);
let errorMessage = 'unknown error';
let errorCode: number | undefined = undefined;

if (err instanceof TransactionError) {
errorCode = err.code;
errorMessage = err.message;
} else if (err instanceof Error) {
errorMessage = err.message;
} else if (typeof err === 'string') {
errorMessage = err;
} else {
if (err instanceof Error) {
errorMessage = err.message;
} else if (typeof err === 'string') {
errorMessage = err;
}
// From fcl-core transaction-error.ts
const ERROR_CODE_REGEX = /\[Error Code: (\d+)\]/;
const match = errorMessage.match(ERROR_CODE_REGEX);
Expand Down
2 changes: 1 addition & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
indexedDBLocalPersistence,
setPersistence,
onAuthStateChanged,
} from 'firebase/auth';
} from 'firebase/auth/web-extension';

import eventBus from '@/eventBus';
import { Message } from '@/shared/utils/messaging';
Expand Down
16 changes: 12 additions & 4 deletions src/background/service/conditions-evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { NewsConditionType } from '@/shared/types/news-types';

import packageJson from '../../../package.json';
import { userWalletService } from '../service';
import { StorageEvaluator } from '../service/storage-evaluator';

import openapi from './openapi';

const CURRENT_VERSION = chrome.runtime.getManifest().version;
const CURRENT_VERSION = packageJson.version;

class ConditionsEvaluator {
private async evaluateCondition(condition: NewsConditionType): Promise<boolean> {
Expand All @@ -19,7 +20,13 @@ class ConditionsEvaluator {

case 'canUpgrade':
const latestVersion = await openapi.getLatestVersion();
return this.compareVersions(CURRENT_VERSION, latestVersion) < 0;
try {
const canUpgrade = this.compareVersions(CURRENT_VERSION, latestVersion) < 0;
return canUpgrade;
} catch (err) {
console.error('Error evaluating canUpgrade', err);
return false;
}

case 'insufficientStorage': {
const currentAddress = userWalletService.getCurrentAddress();
Expand Down Expand Up @@ -53,14 +60,14 @@ class ConditionsEvaluator {
return 0;
}

async evaluateConditions(conditions?: NewsConditionType[]): Promise<boolean> {
async evaluateConditions(conditions?: { type: NewsConditionType }[]): Promise<boolean> {
if (!conditions || conditions.length === 0) {
return true; // No conditions means always show
}

// Evaluate all conditions (AND logic)
for (const condition of conditions) {
if (!(await this.evaluateCondition(condition))) {
if (!(await this.evaluateCondition(condition.type))) {
return false;
}
}
Expand All @@ -76,6 +83,7 @@ class ConditionsEvaluator {
async evaluateBalanceCondition(address: string): Promise<boolean> {
const storageEvaluator = new StorageEvaluator();
const { isBalanceSufficient } = await storageEvaluator.evaluateStorage(address);

return !isBalanceSufficient;
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/background/service/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ class NewsService {
if (!this.store) await this.init();

const news = await openapi.getNews();

// Remove dismissed news and evaluate conditions
const filteredNews = await Promise.all(
news
.filter((n) => !this.isDismissed(n.id))
.map(async (newsItem) => {
const shouldShow = await conditionsEvaluator.evaluateConditions(newsItem.conditions);
return shouldShow ? newsItem : null;
})
);

return filteredNews.filter((item): item is NewsItem => item !== null);

const filteredNewsPromises = news
.filter((n) => !this.isDismissed(n.id))
.map(async (newsItem) => {
const shouldShow = await conditionsEvaluator.evaluateConditions(newsItem.conditions);
return shouldShow ? newsItem : null;
});
const filteredNews = await Promise.all(filteredNewsPromises)
.catch((error) => {
console.error('Error evaluating conditions', error);
return [];
})
.then((news) => news.filter((item) => item !== null));

return filteredNews as NewsItem[];
};

isRead = (id: string): boolean => {
Expand Down
23 changes: 17 additions & 6 deletions src/background/service/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
onAuthStateChanged,
type Unsubscribe,
type User,
} from 'firebase/auth';
} from 'firebase/auth/web-extension';
import { getInstallations, getId } from 'firebase/installations';
import type { TokenInfo } from 'flow-native-token-registry';
import log from 'loglevel';
Expand Down Expand Up @@ -2342,15 +2342,20 @@ class OpenApiService {

getNews = async (): Promise<NewsItem[]> => {
// Get news from firebase function
const baseURL = getFirbaseFunctionUrl();

const cachedNews = await storage.getExpiry('news');

if (cachedNews) {
return cachedNews;
}

const data = await this.sendRequest('GET', '/news', {}, {}, baseURL);
const data = await this.sendRequest(
'GET',
process.env.API_NEWS_PATH,
{},
{},
process.env.API_BASE_URL
);

const timeNow = new Date(Date.now());

Expand Down Expand Up @@ -2458,9 +2463,15 @@ class OpenApiService {
}

try {
const config = this.store.config.get_version;
const data = await this.sendRequest(config.method, config.path);
const version = data.extensionVersion;
const result = await this.sendRequest(
'GET',
process.env.API_CONFIG_PATH,
{},
{},
process.env.API_BASE_URL
);

const version = result.version;

// Cache for 1 hour
await storage.setExpiry('latestVersion', version, 3600000);
Expand Down
2 changes: 1 addition & 1 deletion src/background/service/userWallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as secp from '@noble/secp256k1';
import * as fcl from '@onflow/fcl';
import { getApp } from 'firebase/app';
import { getAuth, signInAnonymously } from 'firebase/auth';
import { getAuth, signInAnonymously } from 'firebase/auth/web-extension';

import { type ActiveChildType } from '@/shared/types/wallet-types';
import { withPrefix } from '@/shared/utils/address';
Expand Down
18 changes: 13 additions & 5 deletions src/background/utils/remoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,23 @@ class fetchRemoteConfig {
const exp = 1000 * 60 * 60 * 1 + now.getTime();
if (expire < now.getTime()) {
try {
const result = await openapi.sendRequest('GET', '/config', {}, {}, BASE_FUNCTIONS_URL);
const result = await openapi.sendRequest(
'GET',
process.env.API_CONFIG_PATH,
{},
{},
process.env.API_BASE_URL
);
// fetch(`${baseURL}/config`);
// const result = await config.json();
this.configState.result = result;
const config = result.config;

this.configState.result = config;
this.configState.expireTime = exp;
await storage.set('freeGas', result.features.free_gas);
await storage.set('alchemyAPI', result.features.alchemy_api);
await storage.set('freeGas', config.features.free_gas);
await storage.set('alchemyAPI', config.features.alchemy_api);
// console.log('remoteConfig ->', result, result.features.free_gas)
return result;
return config;
} catch (err) {
console.error(err);
await storage.set('freeGas', defaultConfig.features.free_gas);
Expand Down
Loading

0 comments on commit b3c2d93

Please sign in to comment.