Class: Hyperliquid::Cloid

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperliquid/cloid.rb

Overview

Client Order ID for tracking orders Must be a 16-byte hex string in format: 0x + 32 hex characters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_cloid) ⇒ Cloid

Initialize a new Cloid from a raw hex string

Parameters:

  • raw_cloid (String)

    Hex string in format 0x + 32 hex characters

Raises:

  • (ArgumentError)

    If format is invalid



12
13
14
15
# File 'lib/hyperliquid/cloid.rb', line 12

def initialize(raw_cloid)
  @raw_cloid = raw_cloid.downcase
  validate!
end

Class Method Details

.from_int(value) ⇒ Cloid

Create a Cloid from an integer

Parameters:

  • value (Integer)

    Integer value (0 to 2^128-1)

Returns:

  • (Cloid)

    New Cloid instance

Raises:

  • (ArgumentError)

    If value is out of range



62
63
64
65
66
67
# File 'lib/hyperliquid/cloid.rb', line 62

def from_int(value)
  raise ArgumentError, 'cloid integer must be non-negative' if value.negative?
  raise ArgumentError, 'cloid integer must be less than 2^128' if value >= 2**128

  new(format('0x%032x', value))
end

.from_str(value) ⇒ Cloid

Create a Cloid from a hex string

Parameters:

  • value (String)

    Hex string in format 0x + 32 hex characters

Returns:

  • (Cloid)

    New Cloid instance



72
73
74
# File 'lib/hyperliquid/cloid.rb', line 72

def from_str(value)
  new(value)
end

.from_uuid(uuid) ⇒ Cloid

Create a Cloid from a UUID string

Parameters:

  • uuid (String)

    UUID string (with or without dashes)

Returns:

  • (Cloid)

    New Cloid instance

Raises:

  • (ArgumentError)


85
86
87
88
89
90
# File 'lib/hyperliquid/cloid.rb', line 85

def from_uuid(uuid)
  hex = uuid.delete('-').downcase
  raise ArgumentError, 'UUID must be 32 hex characters' unless hex.match?(/\A[0-9a-f]{32}\z/)

  new("0x#{hex}")
end

.randomCloid

Generate a random Cloid

Returns:

  • (Cloid)

    New random Cloid instance



78
79
80
# File 'lib/hyperliquid/cloid.rb', line 78

def random
  from_int(SecureRandom.random_number(2**128))
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Equality check

Parameters:

  • other (Cloid, String)

    Another Cloid or string to compare

Returns:

  • (Boolean)

    True if equal



38
39
40
41
42
43
44
45
46
47
# File 'lib/hyperliquid/cloid.rb', line 38

def ==(other)
  case other
  when Cloid
    @raw_cloid == other.to_raw
  when String
    @raw_cloid == other.downcase
  else
    false
  end
end

#hashInteger

Hash for use in Hash keys

Returns:

  • (Integer)

    Hash value



53
54
55
# File 'lib/hyperliquid/cloid.rb', line 53

def hash
  @raw_cloid.hash
end

#inspectString

Inspect representation

Returns:

  • (String)

    The raw cloid string



31
32
33
# File 'lib/hyperliquid/cloid.rb', line 31

def inspect
  @raw_cloid
end

#to_rawString

Get the raw hex string representation

Returns:

  • (String)

    The raw cloid string



19
20
21
# File 'lib/hyperliquid/cloid.rb', line 19

def to_raw
  @raw_cloid
end

#to_sString

String representation

Returns:

  • (String)

    The raw cloid string



25
26
27
# File 'lib/hyperliquid/cloid.rb', line 25

def to_s
  @raw_cloid
end