@moonwall/cli
Version:
Testing framework for the Moon family of projects
109 lines (106 loc) • 3.56 kB
JavaScript
// src/internal/providerFactories.ts
import { ALITH_PRIVATE_KEY, deriveViemChain } from "@moonwall/util";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { Wallet, ethers } from "ethers";
import { createWalletClient, http, publicActions } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { Web3 } from "web3";
import { WebSocketProvider } from "web3-providers-ws";
import { createClient } from "polkadot-api";
import { getWsProvider, WsEvent } from "polkadot-api/ws-provider/web";
import { createLogger } from "@moonwall/util";
var logger = createLogger({ name: "providers" });
var debug = logger.debug.bind(logger);
var vitestAutoUrl = () => `ws://127.0.0.1:${process.env.MOONWALL_RPC_PORT}`;
// src/lib/configReader.ts
import "@moonbeam-network/api-augment";
import { readFile, access } from "fs/promises";
import { readFileSync, existsSync, constants } from "fs";
import JSONC from "jsonc-parser";
import path, { extname } from "path";
var cachedConfig;
function parseConfigSync(filePath) {
let result;
const file = readFileSync(filePath, "utf8");
switch (extname(filePath)) {
case ".json":
result = JSON.parse(file);
break;
case ".config":
result = JSONC.parse(file);
break;
default:
result = void 0;
break;
}
return result;
}
function getEnvironmentFromConfig() {
const globalConfig = importJsonConfig();
const config = globalConfig.environments.find(({ name }) => name === process.env.MOON_TEST_ENV);
if (!config) {
throw new Error(`Environment ${process.env.MOON_TEST_ENV} not found in config`);
}
return config;
}
function importJsonConfig() {
if (cachedConfig) {
return cachedConfig;
}
const configPath = process.env.MOON_CONFIG_PATH;
if (!configPath) {
throw new Error("No moonwall config path set. This is a defect, please raise it.");
}
const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
try {
const config = parseConfigSync(filePath);
const replacedConfig = replaceEnvVars(config);
cachedConfig = replacedConfig;
return cachedConfig;
} catch (e) {
console.error(e);
throw new Error(`Error import config at ${filePath}`);
}
}
function replaceEnvVars(value) {
if (typeof value === "string") {
return value.replace(/\$\{([^}]+)\}/g, (match, group) => {
const envVarValue = process.env[group];
return envVarValue || match;
});
}
if (Array.isArray(value)) {
return value.map(replaceEnvVars);
}
if (typeof value === "object" && value !== null) {
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, replaceEnvVars(v)]));
}
return value;
}
// src/lib/rpcFunctions.ts
async function customDevRpcRequest(method, params = []) {
const env = getEnvironmentFromConfig();
const endpoint = env.connections ? env.connections[0].endpoints[0].replaceAll("ws://", "http://") : vitestAutoUrl().replaceAll("ws://", "http://").replaceAll("wss://", "https://");
const data = {
jsonrpc: "2.0",
id: 1,
method,
params
};
if (endpoint.startsWith("ws")) {
console.log("you've passed a websocket to fetch, is this intended?");
}
const response = await fetch(endpoint, {
method: "POST",
body: JSON.stringify(data),
headers: { "Content-Type": "application/json" }
});
const responseData = await response.json();
if (responseData.error) {
throw new Error(responseData.error.message);
}
return responseData.result;
}
export {
customDevRpcRequest
};