Class: EthereumContractABI::Encoders::IntEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/ethereum-contract-abi/encoders/int_encoder.rb

Class Method Summary collapse

Class Method Details

.encode(int) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/ethereum-contract-abi/encoders/int_encoder.rb', line 6

def self.encode(int)
  hex = int < 0 ? (int & 0xffff).to_s(16).rjust(2, "0") : int.to_s(16).rjust(2, "0")
  # converting to byte string requires each byte is represented by exactly 2 hex characters

  adjusted_hex = hex.length % 2 != 0 ? "0" + hex : hex
  int_hex_bytes = Util.toHexByteString(adjusted_hex)
  pad_byte = int < 0 ? Constants::BYTE_ONE : Constants::BYTE_ZERO
  pad_byte * (32 - int_hex_bytes.bytesize) + int_hex_bytes
end