Skip to content
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `chainId` parameter to `fetchLiveness` method, allowing direct chain ID specification without requiring a network client ID

### Deprecated

- Deprecate `networkClientId` parameter in `fetchLiveness` in favor of `chainId`

## [21.0.0]

### Changed
Expand Down
52 changes: 51 additions & 1 deletion src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
type TransactionParams,
TransactionStatus,
} from '@metamask/transaction-controller';
import type { Hex } from '@metamask/utils';
import nock from 'nock';
import * as sinon from 'sinon';

Expand All @@ -41,7 +42,7 @@ import {
getDefaultSmartTransactionsControllerState,
type SmartTransactionsControllerMessenger,
} from './SmartTransactionsController';
import type { SmartTransaction, UnsignedTransaction, Hex } from './types';
import type { SmartTransaction, UnsignedTransaction } from './types';
import { SmartTransactionStatuses, ClientId } from './types';
import * as utils from './utils';

Expand Down Expand Up @@ -1148,6 +1149,55 @@ describe('SmartTransactionsController', () => {
});
});
});

it('fetches liveness using chainId directly when provided', async () => {
await withController(async ({ controller }) => {
nock(SENTINEL_API_BASE_URL_MAP[sepoliaChainIdDec])
.get(`/network`)
.reply(200, createSuccessLivenessApiResponse());

expect(
controller.state.smartTransactionsState.livenessByChainId,
).toStrictEqual({
[ChainId.mainnet]: true,
[ChainId.sepolia]: true,
});

const liveness = await controller.fetchLiveness({
chainId: ChainId.sepolia,
});

expect(liveness).toBe(true);
expect(
controller.state.smartTransactionsState.livenessByChainId,
).toStrictEqual({
[ChainId.mainnet]: true,
[ChainId.sepolia]: true,
});
});
});

it('prioritizes chainId over networkClientId when both are provided', async () => {
await withController(async ({ controller }) => {
// Mock sepolia (the chainId we're passing directly)
nock(SENTINEL_API_BASE_URL_MAP[sepoliaChainIdDec])
.get(`/network`)
.reply(200, createSuccessLivenessApiResponse());

const liveness = await controller.fetchLiveness({
chainId: ChainId.sepolia,
networkClientId: NetworkType.mainnet, // This should be ignored
});

expect(liveness).toBe(true);
// Verify it used sepolia (from chainId), not mainnet (from networkClientId)
expect(
controller.state.smartTransactionsState.livenessByChainId[
ChainId.sepolia
],
).toBe(true);
});
});
});

describe('updateSmartTransaction', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
approvalTxFees: null,
tradeTxFees: null,
},

// TODO: set this to false once the clients are all refreshing the liveness state.
liveness: true,
livenessByChainId: {
[ChainId.mainnet]: true,
Expand Down Expand Up @@ -335,9 +337,9 @@
isSmartTransactionPending,
);
if (!this.timeoutHandle && pendingTransactions?.length > 0) {
this.poll();

Check warning on line 340 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 340 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
} else if (this.timeoutHandle && pendingTransactions?.length === 0) {
this.stop();

Check warning on line 342 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 342 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
}

Expand All @@ -362,7 +364,7 @@
}

this.timeoutHandle = setInterval(() => {
safelyExecute(async () => this.updateSmartTransactions());

Check warning on line 367 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 367 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}, this.#interval);
await safelyExecute(async () => this.updateSmartTransactions());
}
Expand Down Expand Up @@ -429,7 +431,7 @@
ethQuery = new EthQuery(provider);
}

this.#createOrUpdateSmartTransaction(smartTransaction, {

Check warning on line 434 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check warning on line 434 in src/SmartTransactionsController.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
chainId,
ethQuery,
});
Expand Down Expand Up @@ -1023,12 +1025,24 @@
);
}

/**
* Fetches the liveness status of Smart Transactions for a given chain.
*
* @param options - The options object.
* @param options.chainId - The chain ID to fetch liveness for. Preferred over networkClientId.
* @param options.networkClientId - The network client ID to derive chain ID from.
* @returns A promise that resolves to the liveness status.
*/
async fetchLiveness({
networkClientId,
chainId: chainIdArg,
}: {
/** @deprecated Use `chainId` instead. */
networkClientId?: NetworkClientId;
chainId?: Hex;
} = {}): Promise<boolean> {
const chainId = this.#getChainId({ networkClientId });
// Use chainId directly if provided, otherwise derive from networkClientId
const chainId = chainIdArg ?? this.#getChainId({ networkClientId });
let liveness = false;
try {
const response = await this.#trace(
Expand Down
Loading