Skip to content

Commit

Permalink
feat: add auto update wallet (#1243)
Browse files Browse the repository at this point in the history
With this PR, the wallet will now check if it is running an old version
and will auto-update once a new version is available on the background,
respecting the user flow, if the users had any Wallet popup / tab open
it will not update the wallet and delay it for more 15 minutes.
  • Loading branch information
luizstacio authored Apr 24, 2024
1 parent 21a7d80 commit 06e2bf1
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-beds-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": minor
---

feat: add auto update wallet in background
1 change: 1 addition & 0 deletions packages/app/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_CRX_NAME="Fuel Wallet Development"
VITE_CRX_VERSION_API="https://fuellabs.github.io/fuels-wallet/latest.json"
VITE_FUEL_PROVIDER_URL=http://localhost:4000/graphql
VITE_FUEL_FAUCET_URL=http://localhost:4040
VITE_MNEMONIC_WORDS=12
Expand Down
1 change: 1 addition & 0 deletions packages/app/.env.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_CRX_NAME="Fuel Wallet"
VITE_CRX_VERSION_API="https://fuellabs.github.io/fuels-wallet/latest.json"
VITE_CRX_RELEASE=true
VITE_FUEL_PROVIDER_URL=https://beta-5.fuel.network/graphql
VITE_FUEL_FAUCET_URL=https://faucet-beta-5.fuel.network/
Expand Down
1 change: 1 addition & 0 deletions packages/app/.env.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_CRX_NAME="Fuel Wallet"
VITE_CRX_VERSION_API="https://fuellabs.github.io/fuels-wallet/latest.json"
VITE_FUEL_PROVIDER_URL=http://localhost:4001/graphql
VITE_FUEL_FAUCET_URL=http://localhost:4041
VITE_EXPLORER_URL=https://app.fuel.network/
Expand Down
1 change: 1 addition & 0 deletions packages/app/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare namespace NodeJS {
readonly VITE_MNEMONIC_WORDS: string;
readonly VITE_ADDR_OWNER: string;
readonly VITE_CRX_NAME: string;
readonly VITE_CRX_VERSION_API: string;
readonly VITE_CRX_RELEASE: string;
readonly VITE_AUTO_LOCK_IN_MINUTES: string;
}
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const {
VITE_DATABASE_VERSION,
VITE_CRX_NAME,
VITE_CRX,
VITE_CRX_VERSION_API,
VITE_AUTO_LOCK_IN_MINUTES,
VITE_SENTRY_DSN,
VITE_EXPLORER_URL,
Expand Down
38 changes: 38 additions & 0 deletions packages/app/src/systems/CRX/background/actions/autoUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { compareVersions } from 'compare-versions';
import { APP_VERSION, VITE_CRX_VERSION_API, WALLET_NAME } from '~/config';

// Check if user has any open tab if not return false
async function isOpen() {
// biome-ignore lint/suspicious/noExplicitAny: getContexts is not available on current types
const contexts = await (chrome.runtime as any).getContexts({});
const isOpen = !!contexts.find(({ contextType }: { contextType: string }) =>
['TAB', 'POPUP'].includes(contextType)
);
return isOpen;
}

async function runVersionCheck() {
const latestVersion = await fetch(VITE_CRX_VERSION_API)
.then((res) => res.json())
// If fails to fetch the version return a empty object
.catch(() => ({}));
const version = latestVersion[WALLET_NAME] || APP_VERSION;
// If app version is greater than the one on the release API ignores the check
if (compareVersions(APP_VERSION, version) > -1) return;
if (await isOpen()) return;
// Request update check and reload if available
console.log('[FUEL WALLET] Checking for updates...');
chrome.runtime.requestUpdateCheck((details) => {
if (details === 'update_available') {
console.log('[FUEL WALLET] Update available reload application...');
chrome.runtime.reload();
}
});
}

chrome.alarms.create('autoUpdate', { periodInMinutes: 5 });
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name === 'autoUpdate') {
runVersionCheck();
}
});
1 change: 1 addition & 0 deletions packages/app/src/systems/CRX/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './actions/autoUpdate';
import './actions/keepAwake';
import './actions/onInstall';
import './communication';
1 change: 1 addition & 0 deletions packages/app/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface ImportMetaEnv {
readonly VITE_MNEMONIC_WORDS: number;
readonly VITE_APP_VERSION: string;
readonly VITE_CRX: string;
readonly VITE_CRX_VERSION_API: string;
readonly VITE_ADDR_OWNER: string;
readonly VITE_CRX_NAME: string;
readonly VITE_AUTO_LOCK_IN_MINUTES: number;
Expand Down

0 comments on commit 06e2bf1

Please sign in to comment.