How to use @truffle/code-utils - 3 common examples

To help you get started, we’ve selected a few @truffle/code-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github trufflesuite / truffle / packages / debugger / lib / solidity / selectors / index.js View on Github external
if (!binary) {
          return [];
        }

        let numInstructions;
        if (sourceMap) {
          numInstructions = sourceMap.split(";").length;
        } else {
          //HACK
          numInstructions = (binary.length - 2) / 2;
          //this is actually an overestimate, but that's OK
        }

        //because we might be dealing with a constructor with arguments, we do
        //*not* remove metadata manually
        let instructions = CodeUtils.parseCode(binary, numInstructions);

        if (!sourceMap) {
          // HACK
          // Let's create a source map to use since none exists. This source
          // map maps just as many ranges as there are instructions (or
          // possibly more), and marks them all as being Solidity-internal and
          // not jumps.
          sourceMap =
            binary !== "0x"
              ? "0:0:-1:-".concat(";".repeat(instructions.length - 1))
              : "";
        }

        var lineAndColumnMappings = Object.assign(
          {},
          ...Object.entries(sources).map(([id, { source }]) => ({
github trufflesuite / truffle / packages / truffle-core / lib / commands / opcode.js View on Github external
return done(
          new TruffleError(
            'Cannot find compiled contract with name "' + contractName + '"'
          )
        );
      }

      var bytecode = Contract.deployedBytecode;
      var numInstructions = Contract.deployedSourceMap.split(";").length;

      if (options.creation) {
        bytecode = Contract.bytecode;
        numInstructions = Contract.sourceMap.split(";").length;
      }

      var opcodes = CodeUtils.parseCode(bytecode, numInstructions);

      var indexLength = (opcodes.length + "").length;

      opcodes.forEach(function(opcode, index) {
        var strIndex = index + ":";

        while (strIndex.length < indexLength + 1) {
          strIndex += " ";
        }

        console.log(
          strIndex + " " + opcode.name + " " + (opcode.pushData || "")
        );
      });
    });
  }
github trufflesuite / truffle / packages / truffle-db / src / data / bytecode.ts View on Github external
export function readInstructions(
  bytecode: string,
  sourceMap: string | undefined
):
  DataModel.IInstruction[]
{
  const instructions = parseCode(bytecode);

  const sourceRanges = (sourceMap) ? readSourceMapRanges(sourceMap) : null;

  return instructions.map(
    (op: Instruction, index: number) => Object.assign(
      {
        programCounter: op.pc,
        opcode: op.name,
        meta: {
          cost: op.fee,
          pops: op.in,
          pushes: op.out,
          dynamic: op.dynamic
        }
      },

@truffle/code-utils

Utilities for parsing and managing EVM-compatible bytecode

MIT
Latest version published 8 months ago

Package Health Score

61 / 100
Full package analysis

Popular @truffle/code-utils functions

Similar packages