Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fire event to dapps when editing current connection #1684

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading