import * as SourceMap from '@tevm/voltaire/SourceMap';
// Map revert at PC to source location
function getSourceLocation(pc: number, sourceMap: SourceMap.SourceMap) {
const entry = SourceMap.getEntryAt(sourceMap, pc);
if (!entry) return null;
return {
file: sourceFiles[entry.fileIndex],
start: entry.start,
end: entry.start + entry.length,
code: sourceFiles[entry.fileIndex]?.slice(entry.start, entry.start + entry.length),
};
}
// Use in error handling
try {
// Execute contract call
} catch (error) {
if (error.data?.revertPc) {
const location = getSourceLocation(error.data.revertPc, sourceMap);
console.log(`Reverted at: ${location?.file}:${location?.start}`);
console.log(`Code: ${location?.code}`);
}
}