Skip to content

Commit

Permalink
✨ Add support for wallet disconnection in dApp
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Aug 7, 2024
1 parent 5a22fb3 commit 2e8375e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/Page/Dapp.elm
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type
| ConnectWalletWithProvider Uuid
| WalletConnected WalletConnection
| WalletNotConnected Uuid
| WalletDisconnected Uuid
-- notification
| ToastMsg Toast.Msg
| AddToast Notification
Expand Down Expand Up @@ -189,6 +190,23 @@ update msg (Model model) =
, Cmd.none
)

WalletDisconnected uuid ->
( { model
| wallets =
Dict.update uuid
(Maybe.map
(\w ->
{ w
| state =
NotConnected
}
)
)
model.wallets
}
, Cmd.none
)

AddToast notification ->
let
( tray, tmsg ) =
Expand Down Expand Up @@ -235,6 +253,7 @@ subscriptions _ =
[ receiveProviderAnnouncement mapProviderAnnounced
, receiveWalletConnected mapWalletConnected
, receiveWalletNotConnected mapWalletNotConnected
, receiveWalletDisconnected mapWalletDisconnected
, receiveNotification mapNotification
]

Expand All @@ -261,6 +280,9 @@ port receiveWalletConnected : (Json.Decode.Value -> msg) -> Sub msg
port receiveWalletNotConnected : (Uuid -> msg) -> Sub msg


port receiveWalletDisconnected : (Uuid -> msg) -> Sub msg


port receiveNotification : (Json.Decode.Value -> msg) -> Sub msg


Expand Down Expand Up @@ -342,6 +364,11 @@ mapWalletNotConnected uuid =
WalletNotConnected uuid


mapWalletDisconnected : Uuid -> Msg
mapWalletDisconnected uuid =
WalletDisconnected uuid


mapNotification : Json.Decode.Value -> Msg
mapNotification json =
let
Expand Down
14 changes: 11 additions & 3 deletions src/Page/dapp.port.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ const registerPorts = app => {
app.ports.connectWalletWithProvider.subscribe(async uuid => {
if (uuid in providers) {
try {
const provider = providers[uuid].provider;
const address = await provider.request({ method: 'eth_requestAccounts' });
const chainID = await provider.request({
const provider = providers[uuid];
const proxy = providers[uuid].provider;
const address = await proxy.request({ method: 'eth_requestAccounts' });
const chainID = await proxy.request({
method: 'eth_chainId'
});
const chainName = await chainIDtoName(chainID);
Expand All @@ -56,6 +57,13 @@ const registerPorts = app => {
chainID,
chainName
};
proxy.on('disconnect', error => {
app.ports.receiveWalletDisconnected.send(uuid);
app.ports.receiveNotification.send({
message: `Wallet ${provider.info.name} disconnected` + (error ? `: ${error.message}` : ''),
type: 'error'
});
});

app.ports.receiveWalletConnected.send(wallet);
} catch (error) {
Expand Down

0 comments on commit 2e8375e

Please sign in to comment.