MCPcopy Create free account
hub / github.com/api3dao/airnode / createAndMockGasTarget

Function createAndMockGasTarget

packages/airnode-node/test/mock-utils.ts:63–113  ·  view source on GitHub ↗
(txType: 'legacy' | 'eip1559')

Source from the content-addressed store, hash-verified

61 * Creates and mocks gas pricing-related resources based on txType.
62 */
63export const createAndMockGasTarget = (txType: 'legacy' | 'eip1559'): GasTargetMock => {
64 const gasPriceSpy = jest.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice');
65 const blockSpy = jest.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getBlock');
66 // Ethers does not export BlockWithTransactions so using a custom type definition to include the transactions array
67 const blockWithTransactionsSpy = jest.spyOn(
68 ethers.providers.StaticJsonRpcProvider.prototype,
69 'getBlockWithTransactions'
70 ) as unknown as jest.SpyInstance<
71 Promise<ethers.providers.Block & { transactions: ethers.providers.TransactionResponse[] }>
72 >;
73
74 const gasLimit = ethers.BigNumber.from(500_000);
75 const gasPrice = ethers.BigNumber.from(1_000);
76
77 if (txType === 'legacy') {
78 gasPriceSpy.mockResolvedValue(gasPrice);
79 blockWithTransactionsSpy
80 .mockResolvedValue({
81 number: 23,
82 transactions: Array(20).fill({ gasPrice }),
83 } as any)
84 .mockResolvedValue({
85 number: 3,
86 transactions: Array(20).fill({ gasPrice }),
87 } as any);
88 return { gasTarget: { type: 0, gasPrice, gasLimit }, blockSpy, gasPriceSpy, blockWithTransactionsSpy };
89 }
90
91 const baseFeePerGas = ethers.BigNumber.from(999);
92 blockSpy.mockResolvedValue({ baseFeePerGas } as ethers.providers.Block);
93 const maxPriorityFeePerGas = BigNumber.from(1);
94 const maxFeePerGas = baseFeePerGas.mul(BASE_FEE_MULTIPLIER).add(maxPriorityFeePerGas);
95 blockWithTransactionsSpy
96 .mockResolvedValue({
97 number: 23,
98 baseFeePerGas,
99 transactions: Array(20).fill({ maxPriorityFeePerGas, maxFeePerGas }),
100 } as any)
101 .mockResolvedValue({
102 number: 3,
103 baseFeePerGas,
104 transactions: Array(20).fill({ maxPriorityFeePerGas, maxFeePerGas }),
105 } as any);
106
107 return {
108 gasTarget: { type: 2, maxFeePerGas: gasPrice, maxPriorityFeePerGas: gasPrice, gasLimit },
109 blockSpy,
110 gasPriceSpy,
111 blockWithTransactionsSpy,
112 };
113};
114
115// Declare originalFs outside of mockReadFileSync to prevent infinite recursion errors in mockReadFileSync.
116const originalFs = fs.readFileSync;

Calls

no outgoing calls

Tested by

no test coverage detected