class ContractDefinitionPayloadConverter implements PayloadConverter<ContractDefinition> {

  @Override
  public BytesValue convertToPayload(final ContractDefinition contractDefinition) {
    try {
      logger.debug("Convert to payload from {}", contractDefinition);
      final byte[] rawContract = contractDefinition.getDecodedContract().getValue();
      final ByteArrayOutputStream rawStream = new ByteArrayOutputStream();
      final LittleEndianDataOutputStream dataOut = new LittleEndianDataOutputStream(rawStream);
      try {
        dataOut.writeInt(rawContract.length + 4);
        dataOut.write(rawContract);
        if (!contractDefinition.getConstructorArgs().isEmpty()) {
          final BytesValue constructorArgs =
              mapper.marshal(contractDefinition.getConstructorArgs());
          dataOut.write(constructorArgs.getValue());
        }
      } finally {
        dataOut.close();
      }
      return BytesValue.of(rawStream.toByteArray());
    } catch (HerajException e) {
      throw e;
    } catch (Exception e) {
      throw new HerajException(e);
    }
  }

}