Skip to content

feat: update active networks to fetch on transaction confirmation #6017

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions packages/multichain-network-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add automatic network activity refresh when transactions are confirmed ([#6017](https://github.com/MetaMask/core/pull/6017))
- Listen to `TransactionController:transactionConfirmed` events
- Automatically call `getNetworksWithTransactionActivityByAccounts()` with a 30-second delay to allow blockchain indexing services to process new transactions
- Add error handling for failed network activity refresh attempts

## [0.9.0]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ function setupController({
'NetworkController:findNetworkClientIdByChainId',
'AccountsController:listMultichainAccounts',
],
allowedEvents: ['AccountsController:selectedAccountChange'],
allowedEvents: [
'AccountsController:selectedAccountChange',
'TransactionController:transactionConfirmed',
],
});

const defaultNetworkService = createMockNetworkService();
Expand Down Expand Up @@ -640,4 +643,102 @@ describe('MultichainNetworkController', () => {
});
});
});

describe('handle TransactionController:transactionConfirmed event', () => {
const MOCK_EVM_ADDRESS = '0x1234567890123456789012345678901234567890';

it('calls getNetworksWithTransactionActivityByAccounts when transaction is confirmed', async () => {
jest
.spyOn(global, 'setTimeout')
.mockImplementation((callback: () => void) => {
callback();
return 0 as unknown as NodeJS.Timeout;
});

const mockNetworkService = createMockNetworkService();
const { controller, messenger } = setupController({
mockNetworkService,
});

messenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => [
createMockInternalAccount({
type: EthAccountType.Eoa,
address: MOCK_EVM_ADDRESS,
scopes: [EthScope.Eoa],
}),
],
);

const getNetworksWithTransactionActivitySpy = jest.spyOn(
controller,
'getNetworksWithTransactionActivityByAccounts',
);

messenger.publish('TransactionController:transactionConfirmed', {
id: 'test-transaction-id',
status: 'confirmed',
} as Record<string, unknown>);

await Promise.resolve();

expect(getNetworksWithTransactionActivitySpy).toHaveBeenCalled();
expect(mockNetworkService.fetchNetworkActivity).toHaveBeenCalled();
});

it('handles errors gracefully when getNetworksWithTransactionActivityByAccounts fails', async () => {
const mockNetworkService = createMockNetworkService();
const { controller, messenger } = setupController({
mockNetworkService,
});

messenger.registerActionHandler(
'AccountsController:listMultichainAccounts',
() => [
createMockInternalAccount({
type: EthAccountType.Eoa,
address: MOCK_EVM_ADDRESS,
scopes: [EthScope.Eoa],
}),
],
);

const getNetworksWithTransactionActivitySpy = jest
.spyOn(controller, 'getNetworksWithTransactionActivityByAccounts')
.mockRejectedValue(new Error('Network error'));

const consoleErrorSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {
// no op
});

const setTimeoutSpy = jest
.spyOn(global, 'setTimeout')
.mockImplementation((callback: () => void) => {
// Execute callback immediately but return to event loop
setImmediate(callback);
return 0 as unknown as NodeJS.Timeout;
});

messenger.publish('TransactionController:transactionConfirmed', {
id: 'test-transaction-id',
status: 'confirmed',
} as Record<string, unknown>);

await new Promise(setImmediate);
await new Promise(setImmediate);

expect(getNetworksWithTransactionActivitySpy).toHaveBeenCalled();
expect(consoleErrorSpy).toHaveBeenCalledWith(
'Failed to refresh network activity after transaction confirmed:',
expect.any(Error),
);

setTimeoutSpy.mockRestore();
consoleErrorSpy.mockRestore();
getNetworksWithTransactionActivitySpy.mockRestore();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export class MultichainNetworkController extends BaseController<
> {
readonly #networkService: AbstractMultichainNetworkService;

/**
* Delay in milliseconds to wait for blockchain indexing services to process
* new transactions before refreshing network activity data
*/
static readonly #TRANSACTION_INDEXING_DELAY_MS = 30000;

constructor({
messenger,
state,
Expand Down Expand Up @@ -292,6 +298,27 @@ export class MultichainNetworkController extends BaseController<
// DO NOT publish MultichainNetworkController:networkDidChange to prevent circular listener loops
}

/**
* Handles when a transaction is confirmed to refresh network activity data
* Includes a delay to allow blockchain indexing services to process new transactions
*/
async #handleOnTransactionConfirmed() {
try {
await new Promise((resolve) =>
setTimeout(
resolve,
MultichainNetworkController.#TRANSACTION_INDEXING_DELAY_MS,
),
);
await this.getNetworksWithTransactionActivityByAccounts();
} catch (error) {
console.error(
'Failed to refresh network activity after transaction confirmed:',
error,
);
}
}

/**
* Subscribes to message events.
*/
Expand All @@ -301,6 +328,14 @@ export class MultichainNetworkController extends BaseController<
'AccountsController:selectedAccountChange',
(account) => this.#handleOnSelectedAccountChange(account),
);

// Handle transaction confirmation to refresh network activity
this.messagingSystem.subscribe(
'TransactionController:transactionConfirmed',
async () => {
await this.#handleOnTransactionConfirmed();
},
);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/multichain-network-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,17 @@ export type AccountsControllerSelectedAccountChangeEvent = {
payload: [InternalAccount];
};

export type TransactionControllerTransactionConfirmedEvent = {
type: `TransactionController:transactionConfirmed`;
payload: [transactionMeta: Record<string, unknown>];
};

/**
* Events that this controller is allowed to subscribe.
*/
export type AllowedEvents = AccountsControllerSelectedAccountChangeEvent;
export type AllowedEvents =
| AccountsControllerSelectedAccountChangeEvent
| TransactionControllerTransactionConfirmedEvent;

export type MultichainNetworkControllerAllowedActions =
| MultichainNetworkControllerActions
Expand Down
Loading