Skip to content

Commit d2c3e3b

Browse files
committed
integrate shield gateway in transaction controller
1 parent 9fda9ea commit d2c3e3b

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

packages/transaction-controller/src/TransactionController.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import type {
122122
BeforeSignHook,
123123
TransactionContainerType,
124124
NestedTransactionMetadata,
125+
GetSimulationConfig,
125126
} from './types';
126127
import {
127128
GasFeeEstimateLevel,
@@ -450,6 +451,11 @@ export type TransactionControllerOptions = {
450451
) => Promise<{ transactionHash: string }>;
451452
publishBatch?: PublishBatchHook;
452453
};
454+
455+
/**
456+
* Optional parameters for the transaction simulation.
457+
*/
458+
getSimulationConfig?: GetSimulationConfig;
453459
};
454460

455461
/**
@@ -798,6 +804,8 @@ export class TransactionController extends BaseController<
798804

799805
readonly #securityProviderRequest?: SecurityProviderRequest;
800806

807+
readonly #getSimulationConfig?: GetSimulationConfig;
808+
801809
readonly #sign?: (
802810
transaction: TypedTransaction,
803811
from: string,
@@ -843,6 +851,7 @@ export class TransactionController extends BaseController<
843851
publicKeyEIP7702,
844852
securityProviderRequest,
845853
sign,
854+
getSimulationConfig,
846855
state,
847856
testGasFeeFlows,
848857
trace,
@@ -899,6 +908,7 @@ export class TransactionController extends BaseController<
899908
hooks?.publish ?? (() => Promise.resolve({ transactionHash: undefined }));
900909
this.#publishBatchHook = hooks?.publishBatch;
901910
this.#securityProviderRequest = securityProviderRequest;
911+
this.#getSimulationConfig = getSimulationConfig;
902912
this.#sign = sign;
903913
this.#testGasFeeFlows = testGasFeeFlows === true;
904914
this.#trace = trace ?? (((_request, fn) => fn?.()) as TraceCallback);
@@ -4171,6 +4181,7 @@ export class TransactionController extends BaseController<
41714181
ethQuery: this.#getEthQuery({ networkClientId }),
41724182
nestedTransactions,
41734183
txParams,
4184+
getSimulationConfig: this.#getSimulationConfig,
41744185
}),
41754186
);
41764187

@@ -4191,6 +4202,7 @@ export class TransactionController extends BaseController<
41914202
messenger: this.messagingSystem,
41924203
publicKeyEIP7702: this.#publicKeyEIP7702,
41934204
transactionMeta,
4205+
getSimulationConfig: this.#getSimulationConfig,
41944206
});
41954207
gasFeeTokens = gasFeeTokensResponse?.gasFeeTokens ?? [];
41964208
isGasFeeSponsored = gasFeeTokensResponse?.isGasFeeSponsored ?? false;

packages/transaction-controller/src/api/simulation-api.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../constants';
99
import { SimulationChainNotSupportedError, SimulationError } from '../errors';
1010
import { projectLogger } from '../logger';
11+
import type { GetSimulationConfig } from '../types';
1112

1213
const log = createModuleLogger(projectLogger, 'simulation-api');
1314

@@ -111,6 +112,11 @@ export type SimulationRequest = {
111112
* Defaults to false.
112113
*/
113114
withLogs?: boolean;
115+
116+
/**
117+
* Optional function to modify the simulation request.
118+
*/
119+
getSimulationConfig?: GetSimulationConfig;
114120
};
115121

116122
/** Raw event log emitted by a simulated transaction. */
@@ -274,7 +280,13 @@ export async function simulateTransactions(
274280
chainId: Hex,
275281
request: SimulationRequest,
276282
): Promise<SimulationResponse> {
277-
const url = await getSimulationUrl(chainId);
283+
let url = await getSimulationUrl(chainId);
284+
285+
const { newUrl, authorization } =
286+
(await request.getSimulationConfig?.(url)) || {};
287+
if (newUrl) {
288+
url = newUrl;
289+
}
278290

279291
const requestId = requestIdCounter;
280292
requestIdCounter += 1;
@@ -283,8 +295,18 @@ export async function simulateTransactions(
283295

284296
log('Sending request', url, request);
285297

298+
const headers: Record<string, string> = {
299+
'Content-Type': 'application/json',
300+
};
301+
302+
// Add optional authorization header, if provided.
303+
if (authorization) {
304+
headers.Authorization = authorization;
305+
}
306+
286307
const response = await fetch(url, {
287308
method: 'POST',
309+
headers,
288310
body: JSON.stringify({
289311
id: String(requestId),
290312
jsonrpc: '2.0',

packages/transaction-controller/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,3 +1910,11 @@ export type AssetsFiatValues = {
19101910
*/
19111911
sending?: string;
19121912
};
1913+
1914+
/**
1915+
* Parameters for the transaction simulation API.
1916+
*/
1917+
export type GetSimulationConfig = (url: string) => Promise<{
1918+
newUrl?: string;
1919+
authorization?: string;
1920+
}>;

packages/transaction-controller/src/utils/balance-changes.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type {
3232
SimulationToken,
3333
TransactionParams,
3434
NestedTransactionMetadata,
35+
GetSimulationConfig,
3536
} from '../types';
3637
import { SimulationTokenStandard } from '../types';
3738

@@ -51,6 +52,7 @@ export type GetBalanceChangesRequest = {
5152
ethQuery: EthQuery;
5253
nestedTransactions?: NestedTransactionMetadata[];
5354
txParams: TransactionParams;
55+
getSimulationConfig?: GetSimulationConfig;
5456
};
5557

5658
type ParsedEvent = {
@@ -107,6 +109,7 @@ type BalanceTransactionMap = Map<SimulationToken, SimulationRequestTransaction>;
107109
* @param request.to - The recipient of the transaction.
108110
* @param request.value - The value of the transaction.
109111
* @param request.data - The data of the transaction.
112+
* @param request.getSimulationConfig - Optional transaction simulation parameters.
110113
* @returns The simulation data.
111114
*/
112115
export async function getBalanceChanges(
@@ -729,6 +732,8 @@ async function baseRequest({
729732

730733
const isInsufficientBalance = currentBalanceBN.lt(requiredBalanceBN);
731734

735+
const { getSimulationConfig } = request;
736+
732737
return await simulateTransactions(chainId, {
733738
...params,
734739
transactions,
@@ -749,6 +754,7 @@ async function baseRequest({
749754
},
750755
},
751756
}),
757+
getSimulationConfig,
752758
});
753759
}
754760

packages/transaction-controller/src/utils/gas-fee-tokens.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type SimulationResponseTransaction,
1818
} from '../api/simulation-api';
1919
import { projectLogger } from '../logger';
20+
import type { GetSimulationConfig } from '../types';
2021

2122
const log = createModuleLogger(projectLogger, 'gas-fee-tokens');
2223

@@ -28,6 +29,7 @@ export type GetGasFeeTokensRequest = {
2829
messenger: TransactionControllerMessenger;
2930
publicKeyEIP7702?: Hex;
3031
transactionMeta: TransactionMeta;
32+
getSimulationConfig?: GetSimulationConfig;
3133
};
3234

3335
/**
@@ -39,6 +41,7 @@ export type GetGasFeeTokensRequest = {
3941
* @param request.messenger - The messenger instance.
4042
* @param request.publicKeyEIP7702 - Public key to validate EIP-7702 contract signatures.
4143
* @param request.transactionMeta - The transaction metadata.
44+
* @param request.getSimulationConfig - Optional transaction simulation parameters.
4245
* @returns An array of gas fee tokens.
4346
*/
4447
export async function getGasFeeTokens({
@@ -47,6 +50,7 @@ export async function getGasFeeTokens({
4750
messenger,
4851
publicKeyEIP7702,
4952
transactionMeta,
53+
getSimulationConfig,
5054
}: GetGasFeeTokensRequest) {
5155
const { delegationAddress, txParams } = transactionMeta;
5256
const { authorizationList: authorizationListRequest } = txParams;
@@ -95,6 +99,7 @@ export async function getGasFeeTokens({
9599
withFeeTransfer: true,
96100
with7702,
97101
},
102+
getSimulationConfig,
98103
});
99104

100105
log('Response', response);

0 commit comments

Comments
 (0)