storeMessage function

void Function(Builder builder) storeMessage(
  1. Message message, {
  2. bool forceRef = false,
})

Implementation

void Function(Builder builder) storeMessage(
  Message message, {
  bool forceRef = false,
}) {
  return (Builder builder) {
    builder.store(storeCommonMessageInfo(message.info));

    if (message.init == null) {
      builder.storeBit(0);
    } else {
      builder.storeBit(1);
      var initCell = beginCell().store(storeStateInit(message.init!));

      var needRef = false;
      if (forceRef) {
        needRef = true;
      } else if (builder.availableBits - 2 >= initCell.bits) {
        // very least for ref flag
        needRef = false;
      } else {
        needRef = true;
      }

      if (needRef) {
        builder.storeBit(1);
        builder.storeRef(initCell);
      } else {
        builder.storeBit(0);
        builder.storeBuilder(initCell);
      }
    }

    var needRef = false;
    if (forceRef) {
      needRef = true;
    } else {
      if (builder.availableBits - 1 >= message.body.bits.length &&
          builder.refs + message.body.refs.length <= 4) {
        // very least for ref flag
        needRef = false;
      } else {
        needRef = true;
      }
    }

    if (needRef) {
      builder.storeBit(1);
      builder.storeRef(message.body);
    } else {
      builder.storeBit(0);
      builder.storeBuilder(message.body.asBuilder());
    }
  };
}