@tevm/memory-client
Version:
MemoryClient for tevm is an in memory devnet for JavaScript
132 lines (128 loc) • 4.29 kB
JavaScript
import { createTransport, createClient, publicActions, walletActions, testActions } from 'viem';
export { createClient, publicActions, testActions, walletActions } from 'viem';
import { createCommon, tevmDefault } from '@tevm/common';
import { requestEip1193, tevmSend, tevmActions } from '@tevm/decorators';
import { createTevmNode } from '@tevm/node';
import { callHandler, contractHandler, deployHandler, dumpStateHandler, getAccountHandler, loadStateHandler, mineHandler, setAccountHandler } from '@tevm/actions';
// src/createClient.js
var createTevmTransport = (options = {}) => {
const tevmMap = /* @__PURE__ */ new Map();
return ({ timeout = 2e4, retryCount = 3, chain }) => {
const dynamicChain = chain && "ethjsCommon" in chain ? (
/** @type {import('@tevm/common').Common} */
chain
) : chain !== void 0 ? (
// if user passed in chain instead of common create a common from it with cancun and default eips
createCommon({ ...chain, hardfork: "prague", loggingLevel: "warn" })
) : void 0;
const common = options.common ?? dynamicChain;
const id = common?.id ?? -1;
const tevm = tevmMap.get(id) ?? createTevmNode({ ...options, ...common !== void 0 ? { common } : {} }).extend(requestEip1193()).extend(tevmSend());
tevmMap.set(id, tevm);
return (
/** @type {any} */
createTransport(
{
request: (
/** @type any */
tevm.request
),
type: "tevm",
name: (
/** options?.name ?? */
"Tevm transport"
),
key: (
/* options?.key ?? */
"tevm"
),
timeout,
retryCount,
retryDelay: (
/* options?.retryDelay ?? */
150
)
},
{ tevm }
)
);
};
};
var tevmViemActions = () => {
const extension = (client) => {
const { call, contract, deploy, mine, loadState, dumpState, setAccount, getAccount, ready, deal } = client.transport.tevm.extend(tevmActions());
return {
tevmReady: ready,
tevmCall: call,
tevmContract: contract,
tevmDeploy: deploy,
tevmMine: mine,
tevmLoadState: loadState,
tevmDumpState: dumpState,
tevmSetAccount: setAccount,
tevmGetAccount: getAccount,
tevmDeal: deal
};
};
return extension;
};
// src/createMemoryClient.js
var createMemoryClient = (options) => {
const common = (() => {
if (options?.common !== void 0) {
return options.common;
}
if (options?.fork?.transport) {
return void 0;
}
return tevmDefault;
})();
const memoryClient = createClient({
...options,
cacheTime: 0,
transport: createTevmTransport({
...options,
...common !== void 0 ? { common } : {}
}),
type: "tevm",
...common !== void 0 ? { chain: common } : {}
}).extend(tevmViemActions()).extend(publicActions).extend(walletActions).extend(testActions({ mode: "anvil" }));
return (
/** @type {any} */
memoryClient
);
};
var tevmCall = async (client, params) => {
return callHandler(client.transport.tevm)(params);
};
var tevmContract = async (client, params) => {
return contractHandler(client.transport.tevm)(
/** @type {any}*/
params
);
};
var tevmDeploy = async (client, params) => {
return deployHandler(client.transport.tevm)(params);
};
var tevmDumpState = async (client) => {
return dumpStateHandler(client.transport.tevm)();
};
var tevmGetAccount = async (client, params) => {
return getAccountHandler(client.transport.tevm)(params);
};
var tevmLoadState = async (client, params) => {
return loadStateHandler(client.transport.tevm)(params);
};
var tevmMine = async (client, params) => {
return mineHandler(client.transport.tevm)(params);
};
// src/tevmReady.js
var tevmReady = async (client) => {
return client.transport.tevm.ready();
};
var tevmSetAccount = async (client, params) => {
return setAccountHandler(client.transport.tevm)(params);
};
export { createMemoryClient, createTevmTransport, tevmCall, tevmContract, tevmDeploy, tevmDumpState, tevmGetAccount, tevmLoadState, tevmMine, tevmReady, tevmSetAccount, tevmViemActions };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map