Class: Hyperliquid::Cloid
- Inherits:
-
Object
- Object
- Hyperliquid::Cloid
- 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
-
.from_int(value) ⇒ Cloid
Create a Cloid from an integer.
-
.from_str(value) ⇒ Cloid
Create a Cloid from a hex string.
-
.from_uuid(uuid) ⇒ Cloid
Create a Cloid from a UUID string.
-
.random ⇒ Cloid
Generate a random Cloid.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Equality check.
-
#hash ⇒ Integer
Hash for use in Hash keys.
-
#initialize(raw_cloid) ⇒ Cloid
constructor
Initialize a new Cloid from a raw hex string.
-
#inspect ⇒ String
Inspect representation.
-
#to_raw ⇒ String
Get the raw hex string representation.
-
#to_s ⇒ String
String representation.
Constructor Details
#initialize(raw_cloid) ⇒ Cloid
Initialize a new Cloid from a raw hex string
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
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
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
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 |
.random ⇒ Cloid
Generate a random Cloid
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
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 |
#hash ⇒ Integer
Hash for use in Hash keys
53 54 55 |
# File 'lib/hyperliquid/cloid.rb', line 53 def hash @raw_cloid.hash end |
#inspect ⇒ String
Inspect representation
31 32 33 |
# File 'lib/hyperliquid/cloid.rb', line 31 def inspect @raw_cloid end |
#to_raw ⇒ String
Get the raw hex string representation
19 20 21 |
# File 'lib/hyperliquid/cloid.rb', line 19 def to_raw @raw_cloid end |
#to_s ⇒ String
String representation
25 26 27 |
# File 'lib/hyperliquid/cloid.rb', line 25 def to_s @raw_cloid end |