Skip to content

Adding Alchemy and Tenderly Simulation Support to Wizard for Debugging ⚡ #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/simulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
addressFrom32bytesTo20bytes,
decodeErrorMessage,
} from "../utils";
import { nodeRealFactory } from "./providers";
import { nodeRealFactory , alchemyFactory , tenderlyFactory } from "./providers";

const { ERC1155, ERC20, ERC721, NATIVE } = ASSET_TYPE;
export default class Simulator {
Expand Down Expand Up @@ -234,4 +234,6 @@ export default class Simulator {
};

static nodeRealFactory = nodeRealFactory;
static alchemyFactory = alchemyFactory;
static tenderlyFactory = tenderlyFactory;
}
105 changes: 105 additions & 0 deletions src/simulator/providers/alchemy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { DebugCallTracerWithLogs, DebugProvider } from "../../types/simulator";
import axios from "axios";
import { WizardError } from "../../utils";

type CallTracerParams = {
from: string;
to: string;
value: string;
data: string;
gas: string;
};

const baseURLs: { [chainId: number]: string } = {};

export const alchemyFactory = (apiKey: string, chainId: number): DebugProvider => {
if (chainId !== 1 && chainId !== 5) {
throw new Error(`Alchemy: unsupported chainId: ${chainId}`);
}

baseURLs[chainId] = getBaseURL(chainId, apiKey);

return {
chainId,
debugTrace: (input) =>
callTracer(
{
from: input.from,
to: input.to,
data: input.calldata,
value: input.value || "0x0",
gas: `0x${input.gas.toString(16)}`,
},
baseURLs[chainId]
),
};
};

/**
*
* @param {string} param.from sender of transaction, most oif the time this is an EOA (wallet)
* @param {string} param.to receiver of transaction, most of the time this is a protocol smart contract
* @param {hexString} param.value ETH value encoded as hexadecimal string
* @param {hexString} param.data transaction calldata encoded as hexadecimal string
* @returns {DebugCallTracerWithLogs} debugCallTracerWithLogs
* @see https://geth.ethereum.org/docs/developers/evm-tracing/built-in-tracers#config to understand better the tracers config
* @see
*/
async function callTracer(
{ from, to, value, data, gas }: CallTracerParams,
baseURL: string
): Promise<DebugCallTracerWithLogs> {

const res = await axios.post(baseURL, {
id: 1,
jsonrpc: "2.0",
method: "trace_call",
params: [
{
from,
to,
value,
data,
gas,
},
/**
* @dev ethereum commitment tags: latest | safe | finalized | earliest | pending
* earliest ≤ finalized ≤ safe ≤ latest ≤ pending
* @see https://docs.alchemy.com/reference/ethereum-developer-guide-to-the-merge#what-are-safe-and-finalized
*/
"latest",
{
/**
* @dev callTracer accepts two options
* - onlyTopCall: true instructs the tracer to only process the main (top-level) call and none of the sub-calls.
* This avoids extra processing for each call frame if only the top-level call info are required.
* - withLog: true instructs the tracer to also collect the logs emitted during each call.
*/
tracer: "callTracer",
tracerConfig: {
withLog: true,
},
},
],
});

if (!res.data.result) {
const errorCode = res.data?.error?.code;
const message = res.data?.error?.message;
throw new WizardError(
`NodeReal: simulation failed with code error: ${errorCode} and message: ${message}`
);
}

/// @dev returned as an array to keep same pattern with internal call facilitate recursive processing
return [res.data.result] as DebugCallTracerWithLogs;
}

function getBaseURL(chainId: 1 | 5, apiKey: string): string {
const baseURLs = {
1: `https://eth-mainnet.g.alchemy.com/v2//${apiKey}`,
5: `https://eth-goerli.g.alchemy.com/v2/${apiKey}`,
};

return baseURLs[chainId];
}
2 changes: 2 additions & 0 deletions src/simulator/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { nodeRealFactory } from "./nodeReal";
export { alchemyFactory } from "./alchemy";
export { tenderlyFactory } from "./tenderly";
101 changes: 101 additions & 0 deletions src/simulator/providers/tenderly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { DebugCallTracerWithLogs, DebugProvider } from "../../types/simulator";
import axios from "axios";
import { WizardError } from "../../utils";

type CallTracerParams = {
network_id: string;
block_number?: number;
input: string;
from: string;
to: string;
value: string;
data: string;
gas: string;
};


export const tenderlyFactory = (apiKey: string, chainId: number , accountID : string, projectID : string): DebugProvider => {


const baseURLs = getBaseURL(accountID, projectID);

return {
chainId,
debugTrace: (input) =>
callTracer(
{
network_id: `${chainId.toString()}`,
from: input.from,
block_number: input.blockNumber,
to: input.to,
data: input.calldata,
value: input.value || "0x0",
gas: `0x${input.gas.toString(16)}`,
input: input.calldata,
},
baseURLs,
apiKey
),
};

/**
*
* @param {string} param.from sender of transaction, most oif the time this is an EOA (wallet)
* @param {string} param.to receiver of transaction, most of the time this is a protocol smart contract
* @param {hexString} param.value ETH value encoded as hexadecimal string
* @param {hexString} param.data transaction calldata encoded as hexadecimal string
* @returns {DebugCallTracerWithLogs} debugCallTracerWithLogs
* @see https://geth.ethereum.org/docs/developers/evm-tracing/built-in-tracers#config to understand better the tracers config
* @see
*/

async function callTracer(
{ from, to, value, data, gas , block_number , network_id , input }: CallTracerParams,
baseURL: string,
apiKey: string
): Promise<DebugCallTracerWithLogs> {
const res = await axios.post(baseURL,
{
network_id,
block_number,
from,
input,
to,
gas,
value,
data,
},
{
headers: {
Authorization: `Bearer ${apiKey}`, // Replace <TOKEN> with your actual authorization token
"Content-Type": "application/json",
},
}
);

const transaction = res.data.transaction;

const callTrace = transaction.transaction_info.call_trace;

const logs = res.data.transaction.transaction_info.logs;

if (!res.data.result) {
const errorCode = res.data?.error?.code;
const message = res.data?.error?.message;
throw new WizardError(
`NodeReal: simulation failed with code error: ${errorCode} and message: ${message}`
);
}

/// @dev returned as an array to keep same pattern with internal call facilitate recursive processing
return [callTrace , logs] as DebugCallTracerWithLogs;
}

function getBaseURL(accountID : string , projectID : string): string {
const baseURLs = `https://api.tenderly.co/api/v1/account/${accountID}/project/${projectID}/simulate`;

return baseURLs;
}


};
1 change: 1 addition & 0 deletions src/types/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type SimulationParams = {
gas: number;
value?: string;
chainId: number;
blockNumber?: number;
};

type DebugCallTracerWithLogsCall = {
Expand Down