Skip to content

Commit 37d9615

Browse files
committed
feat(liveness): Add chainId parameter to fetchLiveness
1 parent a45a447 commit 37d9615

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add `chainId` parameter to `fetchLiveness` method, allowing direct chain ID specification without requiring a network client ID
13+
14+
### Deprecated
15+
16+
- Deprecate `networkClientId` parameter in `fetchLiveness` in favor of `chainId`
17+
1018
## [21.0.0]
1119

1220
### Changed

src/SmartTransactionsController.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
type TransactionParams,
2626
TransactionStatus,
2727
} from '@metamask/transaction-controller';
28+
import type { Hex } from '@metamask/utils';
2829
import nock from 'nock';
2930
import * as sinon from 'sinon';
3031

@@ -41,7 +42,7 @@ import {
4142
getDefaultSmartTransactionsControllerState,
4243
type SmartTransactionsControllerMessenger,
4344
} from './SmartTransactionsController';
44-
import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
45+
import type { SmartTransaction, UnsignedTransaction } from './types';
4546
import { SmartTransactionStatuses, ClientId } from './types';
4647
import * as utils from './utils';
4748

@@ -1148,6 +1149,55 @@ describe('SmartTransactionsController', () => {
11481149
});
11491150
});
11501151
});
1152+
1153+
it('fetches liveness using chainId directly when provided', async () => {
1154+
await withController(async ({ controller }) => {
1155+
nock(SENTINEL_API_BASE_URL_MAP[sepoliaChainIdDec])
1156+
.get(`/network`)
1157+
.reply(200, createSuccessLivenessApiResponse());
1158+
1159+
expect(
1160+
controller.state.smartTransactionsState.livenessByChainId,
1161+
).toStrictEqual({
1162+
[ChainId.mainnet]: true,
1163+
[ChainId.sepolia]: true,
1164+
});
1165+
1166+
const liveness = await controller.fetchLiveness({
1167+
chainId: ChainId.sepolia,
1168+
});
1169+
1170+
expect(liveness).toBe(true);
1171+
expect(
1172+
controller.state.smartTransactionsState.livenessByChainId,
1173+
).toStrictEqual({
1174+
[ChainId.mainnet]: true,
1175+
[ChainId.sepolia]: true,
1176+
});
1177+
});
1178+
});
1179+
1180+
it('prioritizes chainId over networkClientId when both are provided', async () => {
1181+
await withController(async ({ controller }) => {
1182+
// Mock sepolia (the chainId we're passing directly)
1183+
nock(SENTINEL_API_BASE_URL_MAP[sepoliaChainIdDec])
1184+
.get(`/network`)
1185+
.reply(200, createSuccessLivenessApiResponse());
1186+
1187+
const liveness = await controller.fetchLiveness({
1188+
chainId: ChainId.sepolia,
1189+
networkClientId: NetworkType.mainnet, // This should be ignored
1190+
});
1191+
1192+
expect(liveness).toBe(true);
1193+
// Verify it used sepolia (from chainId), not mainnet (from networkClientId)
1194+
expect(
1195+
controller.state.smartTransactionsState.livenessByChainId[
1196+
ChainId.sepolia
1197+
],
1198+
).toBe(true);
1199+
});
1200+
});
11511201
});
11521202

11531203
describe('updateSmartTransaction', () => {

src/SmartTransactionsController.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,12 +1023,24 @@ export class SmartTransactionsController extends StaticIntervalPollingController
10231023
);
10241024
}
10251025

1026+
/**
1027+
* Fetches the liveness status of Smart Transactions for a given chain.
1028+
*
1029+
* @param options - The options object.
1030+
* @param options.chainId - The chain ID to fetch liveness for. Preferred over networkClientId.
1031+
* @param options.networkClientId - The network client ID to derive chain ID from.
1032+
* @returns A promise that resolves to the liveness status.
1033+
*/
10261034
async fetchLiveness({
10271035
networkClientId,
1036+
chainId: chainIdArg,
10281037
}: {
1038+
/** @deprecated Use `chainId` instead. */
10291039
networkClientId?: NetworkClientId;
1040+
chainId?: Hex;
10301041
} = {}): Promise<boolean> {
1031-
const chainId = this.#getChainId({ networkClientId });
1042+
// Use chainId directly if provided, otherwise derive from networkClientId
1043+
const chainId = chainIdArg ?? this.#getChainId({ networkClientId });
10321044
let liveness = false;
10331045
try {
10341046
const response = await this.#trace(

0 commit comments

Comments
 (0)