Class: Solace::Utils::AccountContext
- Inherits:
-
Object
- Object
- Solace::Utils::AccountContext
- Defined in:
- lib/solace/utils/account_context.rb
Overview
Internal utility for managing account context in transaction building with automatic deduplication and sorting
Constant Summary collapse
- DEFAULT_ACCOUNT =
Returns The default account data with lowest level of permissions.
{ signer: false, writable: false, fee_payer: false, }
Instance Attribute Summary collapse
-
#accounts ⇒ Object
The accounts in the transaction.
-
#DEFAULT_ACCOUNT ⇒ Object
The default account data.
-
#header ⇒ Object
The header for the transaction.
-
#pubkey_account_map ⇒ Object
The map of accounts.
Instance Method Summary collapse
-
#add_readonly_nonsigner(pubkey) ⇒ Object
Add a readonly account.
-
#add_readonly_signer(pubkey) ⇒ Object
Add a readonly signer account.
-
#add_writable_nonsigner(pubkey) ⇒ Object
Add a writable account.
-
#add_writable_signer(pubkey) ⇒ Object
Add a signer account.
-
#compile ⇒ Hash
Compile accounts into final format.
-
#fee_payer?(pubkey) ⇒ Boolean
Predicate to check if an account is a fee payer.
-
#index_of(pubkey_str) ⇒ Integer
Index of a pubkey in the accounts array.
-
#indices ⇒ Hash<String, Integer>
Get map of indicies for pubkeys in accounts array.
-
#initialize ⇒ AccountContext
constructor
Initialize the account context.
-
#merge_from(other_context) ⇒ Object
Merge all accounts from another AccountContext into this one.
-
#readonly_nonsigner?(pubkey) ⇒ Boolean
Predicate to check if an account is readonly and not a signer.
-
#readonly_signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a readonly signer.
-
#set_fee_payer(pubkey) ⇒ Object
Set the fee payer account.
-
#signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a signer.
-
#writable?(pubkey) ⇒ Boolean
Predicate to check if an account is writable.
-
#writable_nonsigner?(pubkey) ⇒ Boolean
Predicate to check if an account is writable and not a signer.
-
#writable_signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a writable signer.
Constructor Details
#initialize ⇒ AccountContext
Initialize the account context
37 38 39 40 41 |
# File 'lib/solace/utils/account_context.rb', line 37 def initialize @header = [] @accounts = [] @pubkey_account_map = {} end |
Instance Attribute Details
#accounts ⇒ Object
The accounts in the transaction
28 29 30 |
# File 'lib/solace/utils/account_context.rb', line 28 def accounts @accounts end |
#DEFAULT_ACCOUNT ⇒ Object
The default account data
12 13 14 15 16 |
# File 'lib/solace/utils/account_context.rb', line 12 DEFAULT_ACCOUNT = { signer: false, writable: false, fee_payer: false, } |
#header ⇒ Object
The header for the transaction
22 23 24 |
# File 'lib/solace/utils/account_context.rb', line 22 def header @header end |
#pubkey_account_map ⇒ Object
The map of accounts
34 35 36 |
# File 'lib/solace/utils/account_context.rb', line 34 def pubkey_account_map @pubkey_account_map end |
Instance Method Details
#add_readonly_nonsigner(pubkey) ⇒ Object
Add a readonly account
75 76 77 |
# File 'lib/solace/utils/account_context.rb', line 75 def add_readonly_nonsigner(pubkey) merge_account(pubkey, signer: false, writable: false) end |
#add_readonly_signer(pubkey) ⇒ Object
Add a readonly signer account
67 68 69 |
# File 'lib/solace/utils/account_context.rb', line 67 def add_readonly_signer(pubkey) merge_account(pubkey, signer: true, writable: false) end |
#add_writable_nonsigner(pubkey) ⇒ Object
Add a writable account
60 61 62 |
# File 'lib/solace/utils/account_context.rb', line 60 def add_writable_nonsigner(pubkey) merge_account(pubkey, signer: false, writable: true) end |
#add_writable_signer(pubkey) ⇒ Object
Add a signer account
53 54 55 |
# File 'lib/solace/utils/account_context.rb', line 53 def add_writable_signer(pubkey) merge_account(pubkey, signer: true, writable: true) end |
#compile ⇒ Hash
Compile accounts into final format
Gets unique accounts and sorts them in the following order:
- Signers first (Solana requirement)
- Then writable accounts
- Then readonly accounts
157 158 159 160 161 |
# File 'lib/solace/utils/account_context.rb', line 157 def compile self.header = calculate_header self.accounts = order_accounts self end |
#fee_payer?(pubkey) ⇒ Boolean
Predicate to check if an account is a fee payer
83 84 85 |
# File 'lib/solace/utils/account_context.rb', line 83 def fee_payer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:fee_payer] } end |
#index_of(pubkey_str) ⇒ Integer
Index of a pubkey in the accounts array
167 168 169 |
# File 'lib/solace/utils/account_context.rb', line 167 def index_of(pubkey_str) indices[pubkey_str] || -1 end |
#indices ⇒ Hash<String, Integer>
Get map of indicies for pubkeys in accounts array
174 175 176 |
# File 'lib/solace/utils/account_context.rb', line 174 def indices accounts.each_with_index.to_h end |
#merge_from(other_context) ⇒ Object
Merge all accounts from another AccountContext into this one
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/solace/utils/account_context.rb', line 138 def merge_from(other_context) other_context.pubkey_account_map.each do |pubkey, data| merge_account( pubkey, signer: data[:signer], writable: data[:writable], fee_payer: data[:fee_payer] ) end end |
#readonly_nonsigner?(pubkey) ⇒ Boolean
Predicate to check if an account is readonly and not a signer
131 132 133 |
# File 'lib/solace/utils/account_context.rb', line 131 def readonly_nonsigner?(pubkey) @pubkey_account_map[pubkey].try { |acc| !acc[:signer] && !acc[:writable] } end |
#readonly_signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a readonly signer
123 124 125 |
# File 'lib/solace/utils/account_context.rb', line 123 def readonly_signer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:signer] && !acc[:writable] } end |
#set_fee_payer(pubkey) ⇒ Object
Set the fee payer account
46 47 48 |
# File 'lib/solace/utils/account_context.rb', line 46 def set_fee_payer(pubkey) merge_account(pubkey, signer: true, writable: true, fee_payer: true) end |
#signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a signer
91 92 93 |
# File 'lib/solace/utils/account_context.rb', line 91 def signer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:signer] } end |
#writable?(pubkey) ⇒ Boolean
Predicate to check if an account is writable
99 100 101 |
# File 'lib/solace/utils/account_context.rb', line 99 def writable?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:writable] } end |
#writable_nonsigner?(pubkey) ⇒ Boolean
Predicate to check if an account is writable and not a signer
115 116 117 |
# File 'lib/solace/utils/account_context.rb', line 115 def writable_nonsigner?(pubkey) @pubkey_account_map[pubkey].try { |acc| !acc[:signer] && acc[:writable] } end |
#writable_signer?(pubkey) ⇒ Boolean
Predicate to check if an account is a writable signer
107 108 109 |
# File 'lib/solace/utils/account_context.rb', line 107 def writable_signer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:signer] && acc[:writable] } end |