From 1beac3a405831a61763b50a1a448457a6af42681 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 16 Oct 2024 09:47:12 +0200 Subject: [PATCH] ensure wallet id is in sync before saving the config --- components/use-vault.js | 2 +- wallets/index.js | 36 +++++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/components/use-vault.js b/components/use-vault.js index 6eb254b9..7efe861b 100644 --- a/components/use-vault.js +++ b/components/use-vault.js @@ -117,7 +117,7 @@ export function useVaultMigration () { migratedCount++ console.log('migrated to vault:', entryName) } else { - throw new Error('could not set vault entry') + console.log('could not set vault entry:', entryName) } } catch (e) { console.error('failed migrate to vault:', entryName, e) diff --git a/wallets/index.js b/wallets/index.js index dfde7a00..9cd45a8c 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -185,6 +185,15 @@ function useConfig (walletDef) { const canSend = !!walletDef?.sendPayment const canReceive = !walletDef?.clientOnly + const queryServerWallet = useCallback(async () => { + const wallet = await client.query({ + query: WALLET_BY_TYPE, + variables: { type: walletDef.walletType }, + fetchPolicy: 'network-only' + }) + return wallet?.data?.walletByType + }, [walletDef, client]) + const refreshConfig = useCallback(async () => { if (walletDef) { let newConfig = {} @@ -193,32 +202,28 @@ function useConfig (walletDef) { } // fetch server config - const serverConfig = await client.query({ - query: WALLET_BY_TYPE, - variables: { type: walletDef.walletType }, - fetchPolicy: 'network-only' - }) + const serverConfig = await queryServerWallet() - if (serverConfig?.data?.walletByType) { + if (serverConfig) { newConfig = { ...newConfig, - id: serverConfig.data.walletByType.id, - priority: serverConfig.data.walletByType.priority, - enabled: serverConfig.data.walletByType.enabled + id: serverConfig.id, + priority: serverConfig.priority, + enabled: serverConfig.enabled } - if (serverConfig.data.walletByType.wallet) { + if (serverConfig.wallet) { newConfig = { ...newConfig, - ...serverConfig.data.walletByType.wallet + ...serverConfig.wallet } } } // fetch client config let clientConfig = {} - if (serverConfig?.data?.walletByType) { + if (serverConfig) { if (clientVault.current) clientVault.current.close() - const newClientVault = openVault(client, me, serverConfig.data.walletByType) + const newClientVault = openVault(client, me, serverConfig) clientVault.current = newClientVault clientConfig = await newClientVault.get(walletDef.name, {}) if (clientConfig) { @@ -248,7 +253,7 @@ function useConfig (walletDef) { innerSetConfig(newConfig) // set wallet ref - innerSetCurrentWallet(serverConfig.data.walletByType) + innerSetCurrentWallet(serverConfig) } }, [walletDef, me]) @@ -257,6 +262,7 @@ function useConfig (walletDef) { }, [walletDef, me]) const saveConfig = useCallback(async (newConfig, { logger, skipTests }) => { + const serverConfig = await queryServerWallet() const priorityOnly = skipTests try { // gather configs @@ -308,7 +314,7 @@ function useConfig (walletDef) { const mutation = generateMutation(walletDef) const variables = { ...newServerConfig, - id: currentWallet?.id, + id: serverConfig?.id, settings: { autoWithdrawThreshold: Number(autoWithdrawThreshold == null ? autowithdrawSettings.autoWithdrawThreshold : autoWithdrawThreshold), autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent == null ? autowithdrawSettings.autoWithdrawMaxFeePercent : autoWithdrawMaxFeePercent),