Module: Solrengine::Programs::BorshTypes
- Defined in:
- lib/solrengine/programs/borsh_types.rb
Defined Under Namespace
Modules: Discriminator, PublicKey
Constant Summary collapse
- TYPE_REGISTRY =
Maps IDL type strings to borsh gem read/write operations
{ "bool" => { read: :read_bool, write: :write_bool, size: 1 }, "u8" => { read: :read_u8, write: :write_u8, size: 1 }, "u16" => { read: :read_u16, write: :write_u16, size: 2 }, "u32" => { read: :read_u32, write: :write_u32, size: 4 }, "u64" => { read: :read_u64, write: :write_u64, size: 8 }, "u128" => { read: :read_u128, write: :write_u128, size: 16 }, "i8" => { read: :read_i8, write: :write_i8, size: 1 }, "i16" => { read: :read_i16, write: :write_i16, size: 2 }, "i32" => { read: :read_i32, write: :write_i32, size: 4 }, "i64" => { read: :read_i64, write: :write_i64, size: 8 }, "i128" => { read: :read_i128, write: :write_i128, size: 16 }, "f32" => { read: :read_f32, write: :write_f32, size: 4 }, "f64" => { read: :read_f64, write: :write_f64, size: 8 }, "string" => { read: :read_string, write: :write_string, size: nil }, "pubkey" => { read: :read_pubkey, write: :write_pubkey, size: 32 } }.freeze
Class Method Summary collapse
-
.encode_compact_u16(value) ⇒ Object
Encode compact-u16 (Solana’s variable-length encoding for array lengths in transactions).
-
.field_size(type) ⇒ Object
Calculate the fixed byte size for a type (nil if variable-length).
-
.read_field(buffer, type) ⇒ Object
Read a value from a Borsh::Buffer based on IDL type.
-
.write_field(buffer, type, value) ⇒ Object
Write a value to a Borsh::Buffer based on IDL type.
Class Method Details
.encode_compact_u16(value) ⇒ Object
Encode compact-u16 (Solana’s variable-length encoding for array lengths in transactions)
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/solrengine/programs/borsh_types.rb', line 146 def self.encode_compact_u16(value) bytes = [] val = value loop do byte = val & 0x7F val >>= 7 byte |= 0x80 if val > 0 bytes << byte break if val == 0 end bytes.pack("C*") end |
.field_size(type) ⇒ Object
Calculate the fixed byte size for a type (nil if variable-length)
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/solrengine/programs/borsh_types.rb', line 92 def self.field_size(type) case type when String TYPE_REGISTRY.dig(type, :size) when Hash nil # complex types are variable-length else nil end end |
.read_field(buffer, type) ⇒ Object
Read a value from a Borsh::Buffer based on IDL type
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/solrengine/programs/borsh_types.rb', line 56 def self.read_field(buffer, type) case type when String if type == "pubkey" PublicKey.decode(buffer) elsif TYPE_REGISTRY.key?(type) buffer.send(TYPE_REGISTRY[type][:read]) else raise DeserializationError, "Unknown type: #{type}" end when Hash read_complex_type(buffer, type) else raise DeserializationError, "Unsupported type spec: #{type.inspect}" end end |
.write_field(buffer, type, value) ⇒ Object
Write a value to a Borsh::Buffer based on IDL type
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/solrengine/programs/borsh_types.rb', line 74 def self.write_field(buffer, type, value) case type when String if type == "pubkey" PublicKey.encode(buffer, value) elsif TYPE_REGISTRY.key?(type) buffer.send(TYPE_REGISTRY[type][:write], value) else raise DeserializationError, "Unknown type: #{type}" end when Hash write_complex_type(buffer, type, value) else raise DeserializationError, "Unsupported type spec: #{type.inspect}" end end |