Skip to content

Commit

Permalink
feat: fire event to dapps when editing current connection (#1684)
Browse files Browse the repository at this point in the history
- Closes #1561

---

| 📷  Demo |
| --- |
| <video
src="https://github.com/user-attachments/assets/8a445fa8-f508-41f3-95eb-6ed671ace618"
/> |
  • Loading branch information
helciofranco authored Dec 5, 2024
1 parent 21eb5cd commit 882f3ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-nails-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Fire event to dApps when editing connections on the Fuel Wallet.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
MessageTypes,
} from '@fuel-wallet/types';
import type { CommunicationEventArg, Connection } from '@fuel-wallet/types';
import {} from '@fuel-wallet/types';
import { Address, type Network } from 'fuels';
import type {
JSONRPCParams,
Expand Down
78 changes: 55 additions & 23 deletions packages/app/src/systems/CRX/background/services/DatabaseEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import type {
import { CONTENT_SCRIPT_NAME, MessageTypes } from '@fuel-wallet/types';
import { ConnectionService } from '~/systems/DApp/services';

import {
type AccountEvent,
type AccountsEvent,
type ConnectionEvent,
FuelConnectorEventTypes,
type NetworkEvent,
} from 'fuels';
import type { CommunicationProtocol } from './CommunicationProtocol';
import { DatabaseObservable } from './DatabaseObservable';

Expand Down Expand Up @@ -50,16 +57,17 @@ export class DatabaseEvents {
const connections = await ConnectionService.getConnections();
const origins = connections.map((connection) => connection.origin);

const result: NetworkEvent['data'] = {
chainId: updateEvent.obj.chainId,
url: updateEvent.obj.url,
};

this.communicationProtocol.broadcast(
origins,
this.createEvents([
{
event: 'currentNetwork',
params: [
{
url: updateEvent.obj.url,
},
],
event: FuelConnectorEventTypes.currentNetwork,
params: [result],
},
])
);
Expand Down Expand Up @@ -87,44 +95,68 @@ export class DatabaseEvents {

// Notify all connections that the current account is connected
// by sending the current account address
const result: AccountEvent['data'] = updateEvent.obj.address;
this.communicationProtocol.broadcast(
addressConnectedOrigins,
this.createEvents([
{
event: 'currentAccount',
params: [updateEvent.obj.address],
event: FuelConnectorEventTypes.currentAccount,
params: [result],
},
])
);
// Nofity all connections that the current account is not connected
// by sending a null value
if (hasUnconnectedOrigins) {
const result: AccountEvent['data'] = null;
this.communicationProtocol.broadcast(
addressNotConnectedOrigins,
this.createEvents([
{
event: 'currentAccount',
params: [null],
event: FuelConnectorEventTypes.currentAccount,
params: [result],
},
])
);
}
}
);

this.databaseObservable.on('connections:delete', async (updateEvent) => {
const deletedConnection = updateEvent.oldObj as Connection;

this.communicationProtocol.broadcast(
deletedConnection.origin,
this.createEvents([
{
event: 'connection',
params: [false],
},
])
);
});
this.databaseObservable.on<'connections:update', Connection>(
'connections:update',
async (updateEvent) => {
const updatedConnection = updateEvent.obj;
const result: AccountsEvent['data'] = updatedConnection.accounts;

this.communicationProtocol.broadcast(
updatedConnection.origin,
this.createEvents([
{
event: FuelConnectorEventTypes.accounts,
params: [result],
},
])
);
}
);

this.databaseObservable.on<'connections:delete', Connection>(
'connections:delete',
async (deleteEvent) => {
const deletedConnection = deleteEvent.oldObj;
const result: ConnectionEvent['data'] = false;

this.communicationProtocol.broadcast(
deletedConnection.origin,
this.createEvents([
{
event: FuelConnectorEventTypes.connection,
params: [result],
},
])
);
}
);
}

stop() {
Expand Down

0 comments on commit 882f3ea

Please sign in to comment.