From 06a966bf9186b7e2635d309d0e125a176d1bbc98 Mon Sep 17 00:00:00 2001 From: yqrashawn Date: Tue, 25 Jan 2022 13:15:41 +0800 Subject: [PATCH] fix: provider event tweak (#659) 1. send connect event on network change 2. send accountsChanged event when switch from/to cfx network PORTAL-2548 --- .yarn/versions/b0b0a3a4.yml | 7 ++ packages/db/src/main/cfxjs/db/queries.cljs | 107 ++++++++++-------- .../rpcs/wallet_setAppCurrentNetwork/index.js | 30 ++++- .../rpcs/wallet_setCurrentNetwork/index.js | 37 +++--- 4 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 .yarn/versions/b0b0a3a4.yml diff --git a/.yarn/versions/b0b0a3a4.yml b/.yarn/versions/b0b0a3a4.yml new file mode 100644 index 000000000..b05588d99 --- /dev/null +++ b/.yarn/versions/b0b0a3a4.yml @@ -0,0 +1,7 @@ +releases: + "@fluent-wallet/db": patch + "@fluent-wallet/wallet_set-app-current-network": patch + "@fluent-wallet/wallet_set-current-network": patch + +declined: + - helios-background diff --git a/packages/db/src/main/cfxjs/db/queries.cljs b/packages/db/src/main/cfxjs/db/queries.cljs index 54aea559b..428c6cdec 100644 --- a/packages/db/src/main/cfxjs/db/queries.cljs +++ b/packages/db/src/main/cfxjs/db/queries.cljs @@ -411,21 +411,27 @@ (defn set-current-network "set wallet current network, change all apps' currentNetwork" - [net] + [nextnet] (let [selected-net (q '[:find ?net . :where [?net :network/selected true]]) ;; unselect selected net net-unselect [[:db.fn/retractAttribute selected-net :network/selected]] ;; select unselected net - net-select [[:db/add net :network/selected true]] - ;; get all app - apps (q '[:find [?app ...] - :where - [?app :app/site]]) - apps-reselect (mapv (fn [app] [:db/add app :app/currentNetwork net]) apps) - txns (concat net-unselect net-select apps-reselect)] + net-select [[:db/add nextnet :network/selected true]] + txns (concat net-unselect net-select)] (t txns) + true)) + +(defn get-apps-with-different-selected-network + "given the to-be-selected network, return all apps with different selected network" + [nextnet] + (let [apps (q '[:find [?app ...] + :in $ ?net + :where + [?app :app/site] + (not [?app :app/currentNetwork ?net])] + nextnet)] (map (partial e :app) apps))) (defn upsert-app-permissions @@ -1173,48 +1179,49 @@ rst (t txs)] (clj->js rst))) - :getPendingAuthReq get-pending-auth-req - :newAddressTx new-address-tx - :newtokenTx new-token-tx - :getGroupFirstAccountId get-group-first-account-id - :filterAccountGroupByNetworkType filter-account-group-by-network-type - :getExportAllData get-export-all-data - :setCurrentAccount set-current-account - :setCurrentNetwork set-current-network - :upsertAppPermissions upsert-app-permissions - :accountAddrByNetwork account-addr-by-network - :retract retract - :retractAttr retract-attr - :getCurrentAddr #(e :address (get-current-addr)) - :addTokenToAddr add-token-to-addr - :getSingleCallBalanceParams get-single-call-balance-params - :upsertBalances upsert-balances - :retractAddressToken retract-address-token - :getAddrTxByHash get-addr-tx-by-hash - :isTokenInAddr token-in-addr? - :getUnfinishedTx get-unfinished-tx - :getUnfinishedTxCount get-unfinished-tx-count - :setTxSkipped set-tx-skipped - :setTxFailed set-tx-failed - :setTxSending set-tx-sending - :setTxPending set-tx-pending - :setTxPackaged set-tx-packaged - :setTxExecuted set-tx-executed - :setTxConfirmed set-tx-confirmed - :setTxChainSwitched set-tx-chain-switched - :setTxUnsent set-tx-unsent - :getCfxTxsToEnrich get-cfx-txs-to-enrich - :cleanupTx cleanup-tx - :findApp get-apps - :findAddress get-address - :findNetwork get-network - :findAccount get-account - :findToken get-token - :findGroup get-group - :validateAddrInApp validate-addr-in-app - :upsertTokenList upsert-token-list - :retractNetwork retract-network - :retractGroup retract-group + :getPendingAuthReq get-pending-auth-req + :newAddressTx new-address-tx + :newtokenTx new-token-tx + :getGroupFirstAccountId get-group-first-account-id + :filterAccountGroupByNetworkType filter-account-group-by-network-type + :getExportAllData get-export-all-data + :setCurrentAccount set-current-account + :setCurrentNetwork set-current-network + :upsertAppPermissions upsert-app-permissions + :accountAddrByNetwork account-addr-by-network + :retract retract + :retractAttr retract-attr + :getCurrentAddr #(e :address (get-current-addr)) + :addTokenToAddr add-token-to-addr + :getSingleCallBalanceParams get-single-call-balance-params + :upsertBalances upsert-balances + :retractAddressToken retract-address-token + :getAddrTxByHash get-addr-tx-by-hash + :isTokenInAddr token-in-addr? + :getUnfinishedTx get-unfinished-tx + :getUnfinishedTxCount get-unfinished-tx-count + :setTxSkipped set-tx-skipped + :setTxFailed set-tx-failed + :setTxSending set-tx-sending + :setTxPending set-tx-pending + :setTxPackaged set-tx-packaged + :setTxExecuted set-tx-executed + :setTxConfirmed set-tx-confirmed + :setTxChainSwitched set-tx-chain-switched + :setTxUnsent set-tx-unsent + :getCfxTxsToEnrich get-cfx-txs-to-enrich + :cleanupTx cleanup-tx + :findApp get-apps + :findAddress get-address + :findNetwork get-network + :findAccount get-account + :findToken get-token + :findGroup get-group + :validateAddrInApp validate-addr-in-app + :upsertTokenList upsert-token-list + :retractNetwork retract-network + :retractGroup retract-group + :getAppsWithDifferentSelectedNetwork get-apps-with-different-selected-network :queryqueryApp get-apps :queryqueryAddress get-address diff --git a/packages/rpcs/wallet_setAppCurrentNetwork/index.js b/packages/rpcs/wallet_setAppCurrentNetwork/index.js index 94562cef4..33c099c89 100644 --- a/packages/rpcs/wallet_setAppCurrentNetwork/index.js +++ b/packages/rpcs/wallet_setAppCurrentNetwork/index.js @@ -8,21 +8,41 @@ export const schemas = { export const permissions = { external: ['popup'], - db: ['getNetworkById', 'getAppById', 't'], + db: ['getNetworkById', 'getAppById', 't', 'accountAddrByNetwork'], } export const main = ({ Err: {InvalidParams}, - db: {getNetworkById, getAppById, t}, + db: {getNetworkById, getAppById, t, accountAddrByNetwork}, params: {appId, networkId}, + network, }) => { - const network = getNetworkById(networkId) - if (!network) throw InvalidParams(`Invalid network ${networkId}`) + const nextNetwork = getNetworkById(networkId) + if (!nextNetwork) throw InvalidParams(`Invalid network ${networkId}`) const app = getAppById(appId) if (!app) throw InvalidParams(`Invalid app id ${appId}`) t([{eid: app.eid, app: {currentNetwork: networkId}}]) const {post} = app.site - if (post) post({event: 'chainChanged', params: network.chainId}) + + if (!post) return + + post({event: 'chainChanged', params: nextNetwork.chainId}) + post({ + event: 'connect', + params: {chainId: nextNetwork.chainId, networkId: nextNetwork.netId}, + }) + + // fire accountsChanged event when + // eth switch to cfx + // cfx switch to eth + // cfx to another cfx network + if (network.type === 'cfx' || nextNetwork.type === 'cfx') { + const addr = accountAddrByNetwork({ + account: app.currentAccount.eid, + network: nextNetwork.eid, + }) + post({event: 'accountsChanged', params: addr.value}) + } } diff --git a/packages/rpcs/wallet_setCurrentNetwork/index.js b/packages/rpcs/wallet_setCurrentNetwork/index.js index 6f1f2678a..b21ae85e0 100644 --- a/packages/rpcs/wallet_setCurrentNetwork/index.js +++ b/packages/rpcs/wallet_setCurrentNetwork/index.js @@ -9,27 +9,36 @@ export const schemas = { export const permissions = { external: ['popup'], - methods: [], - db: ['setCurrentNetwork', 'getNetworkById', 'getSite'], + methods: ['wallet_setAppCurrentNetwork'], + db: [ + 'setCurrentNetwork', + 'getNetworkById', + 'getAppsWithDifferentSelectedNetwork', + ], } -export const main = ({ +export const main = async ({ Err: {InvalidParams}, - db: {setCurrentNetwork, getNetworkById, getSite}, + db: {setCurrentNetwork, getNetworkById, getAppsWithDifferentSelectedNetwork}, + rpcs: {wallet_setAppCurrentNetwork}, params: networks, + network, }) => { const [networkId] = networks - const network = getNetworkById(networkId) - if (!network) throw InvalidParams(`Invalid networkId ${networkId}`) + const nextNetwork = getNetworkById(networkId) + if (!nextNetwork) throw InvalidParams(`Invalid networkId ${networkId}`) + const apps = getAppsWithDifferentSelectedNetwork(networkId) setCurrentNetwork(networkId) - getSite().forEach( - ({post}) => - post && - post({ - event: 'chainChanged', - params: network.chainId, - }), + + await Promise.all( + apps.map(async app => + wallet_setAppCurrentNetwork( + {network}, + {appId: app.eid, networkId: networkId}, + ), + ), ) - Sentry.setTag('current_network', network.name) + + Sentry.setTag('current_network', nextNetwork.name) }