Skip to content

Commit

Permalink
fix: provider event tweak (#659)
Browse files Browse the repository at this point in the history
1. send connect event on network change
2. send accountsChanged event when switch from/to cfx network

PORTAL-2548
  • Loading branch information
yqrashawn authored Jan 25, 2022
1 parent 5476b09 commit 06a966b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 69 deletions.
7 changes: 7 additions & 0 deletions .yarn/versions/b0b0a3a4.yml
Original file line number Diff line number Diff line change
@@ -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
107 changes: 57 additions & 50 deletions packages/db/src/main/cfxjs/db/queries.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions packages/rpcs/wallet_setAppCurrentNetwork/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
}
37 changes: 23 additions & 14 deletions packages/rpcs/wallet_setCurrentNetwork/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 06a966b

Please sign in to comment.