How to use @truffle/codec - 10 common examples

To help you get started, we’ve selected a few @truffle/codec 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 / debug-utils / index.js View on Github external
formatValue: function(value, indent = 0, nativized = false) {
    let inspectOptions = {
      colors: true,
      depth: null,
      maxArrayLength: null,
      breakLength: 30
    };
    let valueToInspect = nativized
      ? value
      : new Codec.Format.Utils.Inspect.ResultInspector(value);
    return util
      .inspect(valueToInspect, inspectOptions)
      .split(/\r?\n/g)
      .map((line, i) => {
        // don't indent first line
        const padding = i > 0 ? Array(indent).join(" ") : "";
        return padding + line;
      })
      .join(OS.EOL);
  },
github trufflesuite / truffle / packages / codec / lib / read / bytes.ts View on Github external
export function readBytes(memory: Uint8Array, offset: number, length: number) {

  if(!Number.isSafeInteger(offset + length)) {
    throw new DecodingError({
      kind: "ReadErrorBytes" as const,
      start: offset,
      length
    });
  }

  // grab `length` bytes no matter what, here fill this array
  var bytes = new Uint8Array(length);
  bytes.fill(0); //fill it wil zeroes to start

  //if the start is beyond the end of memory, just return those 0s
  if (offset >= memory.length) {
    return bytes;
  }

  // if we're reading past the end of memory, truncate the length to read
github trufflesuite / truffle / packages / debugger / lib / evm / selectors / index.js View on Github external
createValue: createLeaf(["./isCreate", state], (isCreate, { stack }) => {
        if (!isCreate) {
          return null;
        }

        //creates have the value as the first argument
        let value = stack[stack.length - 1];
        return Codec.Conversion.toBN(value);
      }),
github trufflesuite / truffle / packages / debugger / lib / data / sagas / index.js View on Github external
function* decodeMappingKeySaga(indexDefinition, keyDefinition) {
  //something of a HACK -- cleans any out-of-range booleans
  //resulting from the main mapping key decoding loop
  let indexValue = yield* decodeMappingKeyCore(indexDefinition, keyDefinition);
  return indexValue ? Codec.Conversion.cleanBool(indexValue) : indexValue;
}
github trufflesuite / truffle / packages / debugger / lib / data / reducers.js View on Github external
//table here are supposed to preserve path information, we'll be
      //replacing a fairly bare-bones Slot object with one with a full path.

      //we do NOT want to distinguish between types with and without "_ptr" on
      //the end here! (or _slice)
      debug("typeIdentifier %s", typeIdentifier);
      typeIdentifier = Codec.Ast.Utils.regularizeTypeIdentifier(typeIdentifier);
      parentType = Codec.Ast.Utils.regularizeTypeIdentifier(parentType);

      debug("slot %o", slot);
      let hexSlotAddress = Codec.Conversion.toHexString(
        Codec.Storage.Utils.slotAddress(slot),
        Codec.Evm.Utils.WORD_SIZE
      );
      let parentAddress = slot.path
        ? Codec.Conversion.toHexString(
            Codec.Storage.Utils.slotAddress(slot.path),
            Codec.Evm.Utils.WORD_SIZE
          )
        : undefined;

      //this is going to be messy and procedural, sorry.  but let's start with
      //the easy stuff: create the new address if needed, clone if not
      let newState = {
        ...state,
        byAddress: {
          ...state.byAddress,
          [address]: {
            byType: {
              ...(state.byAddress[address] || { byType: {} }).byType
            }
          }
github trufflesuite / truffle / packages / debugger / lib / data / sagas / index.js View on Github external
baseNode,
  mappedPaths,
  currentAssignments,
  currentDepth
) {
  let fullId = stableKeccak256({
    astId: baseNode.id,
    stackframe: currentDepth
  });
  debug("astId: %d", baseNode.id);
  debug("stackframe: %d", currentDepth);
  debug("fullId: %s", fullId);
  debug("currentAssignments: %O", currentAssignments);
  //base expression is an expression, and so has a literal assigned to
  //it
  let offset = Codec.Conversion.toBN(
    currentAssignments.byId[fullId].ref.literal
  );
  return { offset };
}
github trufflesuite / truffle / packages / debugger / lib / data / reducers.js View on Github external
//found as the slotAddress of the slot's path object, if it exists -- if
      //it doesn't then we conclude that no the parent does not have a spot in
      //the table).  If the parent has a slot in the table already, then we
      //alter the child slot by replacing its path with the parent slot.  This
      //will keep the slotAddress the same, but since the versions kept in the
      //table here are supposed to preserve path information, we'll be
      //replacing a fairly bare-bones Slot object with one with a full path.

      //we do NOT want to distinguish between types with and without "_ptr" on
      //the end here! (or _slice)
      debug("typeIdentifier %s", typeIdentifier);
      typeIdentifier = Codec.Ast.Utils.regularizeTypeIdentifier(typeIdentifier);
      parentType = Codec.Ast.Utils.regularizeTypeIdentifier(parentType);

      debug("slot %o", slot);
      let hexSlotAddress = Codec.Conversion.toHexString(
        Codec.Storage.Utils.slotAddress(slot),
        Codec.Evm.Utils.WORD_SIZE
      );
      let parentAddress = slot.path
        ? Codec.Conversion.toHexString(
            Codec.Storage.Utils.slotAddress(slot.path),
            Codec.Evm.Utils.WORD_SIZE
          )
        : undefined;

      //this is going to be messy and procedural, sorry.  but let's start with
      //the easy stuff: create the new address if needed, clone if not
      let newState = {
        ...state,
        byAddress: {
          ...state.byAddress,
github trufflesuite / truffle / packages / debugger / lib / evm / selectors / index.js View on Github external
(isHalting, { stack }, remaining, finalStatus) => {
          if (!isHalting) {
            return null; //not clear this'll do much good since this may get
            //read as false, but, oh well, may as well
          }
          if (remaining <= 1) {
            return finalStatus;
          } else {
            const ZERO_WORD = "00".repeat(Codec.Evm.Utils.WORD_SIZE);
            return stack[stack.length - 1] !== ZERO_WORD;
          }
        }
      ),
github trufflesuite / truffle / packages / debugger / lib / evm / selectors / index.js View on Github external
(isCall, { stack }) => {
          if (!isCall) {
            return null;
          }

          let address = stack[stack.length - 2];
          return Codec.Evm.Utils.toAddress(address);
        }
      ),
github trufflesuite / truffle / packages / debugger / lib / data / sagas / index.js View on Github external
export function* decode(definition, ref) {
  let userDefinedTypes = yield select(data.views.userDefinedTypes);
  let state = yield select(data.current.state);
  let mappingKeys = yield select(data.views.mappingKeys);
  let allocations = yield select(data.info.allocations);
  let instances = yield select(data.views.instances);
  let contexts = yield select(data.views.contexts);
  let currentContext = yield select(data.current.context);
  let internalFunctionsTable = yield select(
    data.current.functionsByProgramCounter
  );
  let blockNumber = yield select(data.views.blockNumber);

  let ZERO_WORD = new Uint8Array(Codec.Evm.Utils.WORD_SIZE); //automatically filled with zeroes
  let NO_CODE = new Uint8Array(); //empty array

  let decoder = Codec.decodeVariable(definition, ref, {
    userDefinedTypes,
    state,
    mappingKeys,
    allocations,
    contexts,
    currentContext,
    internalFunctionsTable
  });

  debug("beginning decoding");
  let result = decoder.next();
  while (!result.done) {
    debug("request received");