Class: Solace::Utils::AccountContext
- Inherits:
-
Object
- Object
- Solace::Utils::AccountContext
- Defined in:
- lib/solace/utils/account_context.rb
Overview
Utility for managing account context for composers
This utility is used to manage the accounts in a transaction composer and instructions composer. It provides methods for managing the accounts and their permissions, as well as compiling the accounts into the final format required by the instruction builders. Concerns like deduplication and ordering are handled by this utility.
Constant Summary collapse
- DEFAULT_ACCOUNT =
Returns The default account data with lowest level of permissions.
{ signer: false, writable: false, fee_payer: false }.freeze
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
62 63 64 65 66 |
# File 'lib/solace/utils/account_context.rb', line 62 def initialize @header = [] @accounts = [] @pubkey_account_map = {} end |
Instance Attribute Details
#accounts ⇒ Object
The accounts in the transaction
53 54 55 |
# File 'lib/solace/utils/account_context.rb', line 53 def accounts @accounts end |
#DEFAULT_ACCOUNT ⇒ Object
The default account data
37 38 39 40 41 |
# File 'lib/solace/utils/account_context.rb', line 37 DEFAULT_ACCOUNT = { signer: false, writable: false, fee_payer: false }.freeze |
#header ⇒ Object
The header for the transaction
47 48 49 |
# File 'lib/solace/utils/account_context.rb', line 47 def header @header end |
#pubkey_account_map ⇒ Object
The map of accounts
59 60 61 |
# File 'lib/solace/utils/account_context.rb', line 59 def pubkey_account_map @pubkey_account_map end |
Instance Method Details
#add_readonly_nonsigner(pubkey) ⇒ Object
Add a readonly account
99 100 101 |
# File 'lib/solace/utils/account_context.rb', line 99 def add_readonly_nonsigner(pubkey) merge_account(pubkey, signer: false, writable: false) end |
#add_readonly_signer(pubkey) ⇒ Object
Add a readonly signer account
92 93 94 |
# File 'lib/solace/utils/account_context.rb', line 92 def add_readonly_signer(pubkey) merge_account(pubkey, signer: true, writable: false) end |
#add_writable_nonsigner(pubkey) ⇒ Object
Add a writable account
85 86 87 |
# File 'lib/solace/utils/account_context.rb', line 85 def add_writable_nonsigner(pubkey) merge_account(pubkey, signer: false, writable: true) end |
#add_writable_signer(pubkey) ⇒ Object
Add a signer account
78 79 80 |
# File 'lib/solace/utils/account_context.rb', line 78 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
177 178 179 180 181 |
# File 'lib/solace/utils/account_context.rb', line 177 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
107 108 109 |
# File 'lib/solace/utils/account_context.rb', line 107 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
187 188 189 |
# File 'lib/solace/utils/account_context.rb', line 187 def index_of(pubkey_str) indices[pubkey_str] || -1 end |
#indices ⇒ Hash{String => Integer}
Get map of indicies for pubkeys in accounts array
194 195 196 |
# File 'lib/solace/utils/account_context.rb', line 194 def indices accounts.each_with_index.to_h end |
#merge_from(other_context) ⇒ Object
Merge all accounts from another AccountContext into this one
162 163 164 165 166 167 |
# File 'lib/solace/utils/account_context.rb', line 162 def merge_from(other_context) other_context.pubkey_account_map.each do |pubkey, data| signer, writable, fee_payer = data.values_at(:signer, :writable, :fee_payer) merge_account(pubkey, signer: signer, writable: writable, fee_payer: fee_payer) end end |
#readonly_nonsigner?(pubkey) ⇒ Boolean
Predicate to check if an account is readonly and not a signer
155 156 157 |
# File 'lib/solace/utils/account_context.rb', line 155 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
147 148 149 |
# File 'lib/solace/utils/account_context.rb', line 147 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
71 72 73 |
# File 'lib/solace/utils/account_context.rb', line 71 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
115 116 117 |
# File 'lib/solace/utils/account_context.rb', line 115 def signer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:signer] } end |
#writable?(pubkey) ⇒ Boolean
Predicate to check if an account is writable
123 124 125 |
# File 'lib/solace/utils/account_context.rb', line 123 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
139 140 141 |
# File 'lib/solace/utils/account_context.rb', line 139 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
131 132 133 |
# File 'lib/solace/utils/account_context.rb', line 131 def writable_signer?(pubkey) @pubkey_account_map[pubkey].try { |acc| acc[:signer] && acc[:writable] } end |