function encode(buffer, num, offset) {
  const size = encodingLength(num);
  // ~6 bit
  if (size === 1) {
    tools.writeUInt8(buffer, offset, num);
    // 8 bit
  } else if (size === 2) {
    tools.writeUInt8(buffer, offset, OPS.OP_PUSHDATA1);
    tools.writeUInt8(buffer, offset + 1, num);
    // 16 bit
  } else if (size === 3) {
    tools.writeUInt8(buffer, offset, OPS.OP_PUSHDATA2);
    tools.writeUInt16(buffer, offset + 1, num, 'LE');
    // 32 bit
  } else {
    tools.writeUInt8(buffer, offset, OPS.OP_PUSHDATA4);
    tools.writeUInt32(buffer, offset + 1, num, 'LE');
  }
  return size;
}
Encodes a number into a buffer using a variable-length encoding scheme.
The encoded buffer is written starting at the specified offset.
Returns the size of the encoded buffer.

@param buffer - The buffer to write the encoded data into.
@param num - The number to encode.
@param offset - The offset at which to start writing the encoded buffer.
@returns The size of the encoded buffer.