Class: Solrengine::Programs::Instruction
- Inherits:
-
Object
- Object
- Solrengine::Programs::Instruction
- Defined in:
- lib/solrengine/programs/instruction.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
- .account(name, signer: false, writable: false, address: nil, pda: nil) ⇒ Object
- .accounts_list ⇒ Object
- .anchor_instruction_name ⇒ Object
- .argument(name, type) ⇒ Object
- .arguments_list ⇒ Object
- .discriminator ⇒ Object
-
.instruction_name(name_override = nil) ⇒ Object
Set the Anchor instruction name used for discriminator computation.
- .program_id(id = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Instruction
constructor
A new instance of Instruction.
-
#instruction_data ⇒ Object
Build the instruction data: 8-byte discriminator + Borsh-encoded arguments.
-
#to_instruction ⇒ Object
Build the instruction hash suitable for TransactionBuilder.
- #valid? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Instruction
Returns a new instance of Instruction.
61 62 63 64 65 66 |
# File 'lib/solrengine/programs/instruction.rb', line 61 def initialize(attributes = {}) @errors = [] attributes.each do |key, value| send(:"#{key}=", value) if respond_to?(:"#{key}=") end end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/solrengine/programs/instruction.rb', line 4 def errors @errors end |
Class Method Details
.account(name, signer: false, writable: false, address: nil, pda: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/solrengine/programs/instruction.rb', line 20 def account(name, signer: false, writable: false, address: nil, pda: nil) accounts_list << { name: name.to_sym, signer: signer, writable: writable, address: address, pda: pda } attr_accessor name unless address || pda end |
.accounts_list ⇒ Object
35 36 37 |
# File 'lib/solrengine/programs/instruction.rb', line 35 def accounts_list @accounts_list ||= [] end |
.anchor_instruction_name ⇒ Object
53 54 55 56 57 58 |
# File 'lib/solrengine/programs/instruction.rb', line 53 def anchor_instruction_name @instruction_name || begin short_name = name&.split("::")&.last || "" short_name.sub(/Instruction$/, "").gsub(/([a-z])([A-Z])/, '\1_\2').downcase end end |
.argument(name, type) ⇒ Object
15 16 17 18 |
# File 'lib/solrengine/programs/instruction.rb', line 15 def argument(name, type) arguments_list << { name: name.to_sym, type: type } attr_accessor name end |
.arguments_list ⇒ Object
31 32 33 |
# File 'lib/solrengine/programs/instruction.rb', line 31 def arguments_list @arguments_list ||= [] end |
.discriminator ⇒ Object
39 40 41 |
# File 'lib/solrengine/programs/instruction.rb', line 39 def discriminator BorshTypes::Discriminator.for_instruction(anchor_instruction_name) end |
.instruction_name(name_override = nil) ⇒ Object
Set the Anchor instruction name used for discriminator computation. If not set, derives from class name: “LockInstruction” -> “lock”
45 46 47 48 49 50 51 |
# File 'lib/solrengine/programs/instruction.rb', line 45 def instruction_name(name_override = nil) if name_override @instruction_name = name_override else @instruction_name end end |
.program_id(id = nil) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/solrengine/programs/instruction.rb', line 7 def program_id(id = nil) if id @program_id = id else @program_id end end |
Instance Method Details
#instruction_data ⇒ Object
Build the instruction data: 8-byte discriminator + Borsh-encoded arguments
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/solrengine/programs/instruction.rb', line 76 def instruction_data data = Borsh::Buffer.open do |buf| # Write discriminator buf.write(self.class.discriminator) # Write each argument self.class.arguments_list.each do |arg| value = send(arg[:name]) BorshTypes.write_field(buf, arg[:type], value) end end data end |
#to_instruction ⇒ Object
Build the instruction hash suitable for TransactionBuilder
92 93 94 95 96 97 98 |
# File 'lib/solrengine/programs/instruction.rb', line 92 def to_instruction { program_id: self.class.program_id, accounts: , data: instruction_data } end |
#valid? ⇒ Boolean
68 69 70 71 72 73 |
# File 'lib/solrengine/programs/instruction.rb', line 68 def valid? @errors = [] validate_arguments validate_accounts @errors.empty? end |