/******************************************************************************* * (c) 2023 unipackage * * Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0 * (the "Apache License"). You may not use this file except in compliance with one of these * licenses. You may obtain a copy of the MIT License at * * https://opensource.org/licenses/MIT * * Or the Apache License, Version 2.0 at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the MIT License or the Apache License for the specific language governing permissions and * limitations under the respective licenses. ********************************************************************************/ import nock from "nock" import { IEVM } from "../../../src" export interface EvmMockHookInput { evm: IEVM method: string errorCode?: number reply?: string jsonrpcVersion?: string id?: string } export const defaultEvmMockHookInput: EvmMockHookInput = { evm: {} as IEVM, method: "", reply: "", errorCode: 200, jsonrpcVersion: "2.0", id: "d9038463-6ec7-45d2-a0cb-c3dda7958196", } export function mochaAddEvmHook(evmMockHookInput: EvmMockHookInput) { const mergeInput = { ...defaultEvmMockHookInput, evmMockHookInput } const url = mergeInput.evm.getProviderUrl() if (!url) { return } const urlObject = new URL(url) const functionSignature = mergeInput.evm.encodeFunctionSignatureByFuntionName(mergeInput.method) nock(urlObject.hostname) .post(urlObject.pathname, new RegExp("(?=" + functionSignature + ")")) .reply(mergeInput.errorCode, { jsonrpc: mergeInput.jsonrpcVersion, result: mergeInput.reply, id: mergeInput.id, }) console.log(`==== Add method [${mergeInput.method}] hook success!====`) }