Class: Solace::Utils::AccountContext

Inherits:
Object
  • Object
show all
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.

Examples:

Usage

# Create a new account context
context = Solace::Utils::AccountContext.new

# Add accounts
context.add_writable_signer('pubkey1')
context.add_readonly_nonsigner('pubkey2')

# Merge accounts from another context
context = context.merge_from(other_context)

# Set fee payer
context.set_fee_payer('pubkey3')

# Compile the accounts
context.compile

See Also:

Since:

  • 0.0.3

Constant Summary collapse

DEFAULT_ACCOUNT =

Returns The default account data with lowest level of permissions.

Returns:

  • (Hash)

    The default account data with lowest level of permissions

Since:

  • 0.0.3

{
  signer: false,
  writable: false,
  fee_payer: false
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccountContext

Initialize the account context

Since:

  • 0.0.3



62
63
64
65
66
# File 'lib/solace/utils/account_context.rb', line 62

def initialize
  @header = []
  @accounts = []
   = {}
end

Instance Attribute Details

#accountsObject

The accounts in the transaction



53
54
55
# File 'lib/solace/utils/account_context.rb', line 53

def accounts
  @accounts
end

#DEFAULT_ACCOUNTObject

The default account data



37
38
39
40
41
# File 'lib/solace/utils/account_context.rb', line 37

 = {
  signer: false,
  writable: false,
  fee_payer: false
}.freeze

#headerObject

The header for the transaction



47
48
49
# File 'lib/solace/utils/account_context.rb', line 47

def header
  @header
end

#pubkey_account_mapObject

The map of accounts



59
60
61
# File 'lib/solace/utils/account_context.rb', line 59

def 
  
end

Instance Method Details

#add_readonly_nonsigner(pubkey) ⇒ Object

Add a readonly account

Parameters:

Since:

  • 0.0.3



99
100
101
# File 'lib/solace/utils/account_context.rb', line 99

def add_readonly_nonsigner(pubkey)
  (pubkey, signer: false, writable: false)
end

#add_readonly_signer(pubkey) ⇒ Object

Add a readonly signer account

Parameters:

Since:

  • 0.0.3



92
93
94
# File 'lib/solace/utils/account_context.rb', line 92

def add_readonly_signer(pubkey)
  (pubkey, signer: true, writable: false)
end

#add_writable_nonsigner(pubkey) ⇒ Object

Add a writable account

Parameters:

Since:

  • 0.0.3



85
86
87
# File 'lib/solace/utils/account_context.rb', line 85

def add_writable_nonsigner(pubkey)
  (pubkey, signer: false, writable: true)
end

#add_writable_signer(pubkey) ⇒ Object

Add a signer account

Parameters:

Since:

  • 0.0.3



78
79
80
# File 'lib/solace/utils/account_context.rb', line 78

def add_writable_signer(pubkey)
  (pubkey, signer: true, writable: true)
end

#compileHash

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

Returns:

  • (Hash)

    The compiled accounts and header

Since:

  • 0.0.3



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

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is a fee payer

Since:

  • 0.0.3



107
108
109
# File 'lib/solace/utils/account_context.rb', line 107

def fee_payer?(pubkey)
  [pubkey].try { |acc| acc[:fee_payer] }
end

#index_of(pubkey_str) ⇒ Integer

Index of a pubkey in the accounts array

Parameters:

  • pubkey_str (String)

    The public key of the account

Returns:

  • (Integer)

    The index of the pubkey in the accounts array or -1 if not found

Since:

  • 0.0.3



187
188
189
# File 'lib/solace/utils/account_context.rb', line 187

def index_of(pubkey_str)
  indices[pubkey_str] || -1
end

#indicesHash{String => Integer}

Get map of indicies for pubkeys in accounts array

Returns:

  • (Hash{String => Integer})

    The indices of the pubkeys in the accounts array

Since:

  • 0.0.3



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

Parameters:

Since:

  • 0.0.3



162
163
164
165
166
167
# File 'lib/solace/utils/account_context.rb', line 162

def merge_from(other_context)
  other_context..each do |pubkey, data|
    signer, writable, fee_payer = data.values_at(:signer, :writable, :fee_payer)
    (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

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is readonly and not a signer

Since:

  • 0.0.3



155
156
157
# File 'lib/solace/utils/account_context.rb', line 155

def readonly_nonsigner?(pubkey)
  [pubkey].try { |acc| !acc[:signer] && !acc[:writable] }
end

#readonly_signer?(pubkey) ⇒ Boolean

Predicate to check if an account is a readonly signer

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is a readonly signer

Since:

  • 0.0.3



147
148
149
# File 'lib/solace/utils/account_context.rb', line 147

def readonly_signer?(pubkey)
  [pubkey].try { |acc| acc[:signer] && !acc[:writable] }
end

#set_fee_payer(pubkey) ⇒ Object

Set the fee payer account

Parameters:

Since:

  • 0.0.3



71
72
73
# File 'lib/solace/utils/account_context.rb', line 71

def set_fee_payer(pubkey)
  (pubkey, signer: true, writable: true, fee_payer: true)
end

#signer?(pubkey) ⇒ Boolean

Predicate to check if an account is a signer

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is a signer

Since:

  • 0.0.3



115
116
117
# File 'lib/solace/utils/account_context.rb', line 115

def signer?(pubkey)
  [pubkey].try { |acc| acc[:signer] }
end

#writable?(pubkey) ⇒ Boolean

Predicate to check if an account is writable

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is writable

Since:

  • 0.0.3



123
124
125
# File 'lib/solace/utils/account_context.rb', line 123

def writable?(pubkey)
  [pubkey].try { |acc| acc[:writable] }
end

#writable_nonsigner?(pubkey) ⇒ Boolean

Predicate to check if an account is writable and not a signer

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is writable and not a signer

Since:

  • 0.0.3



139
140
141
# File 'lib/solace/utils/account_context.rb', line 139

def writable_nonsigner?(pubkey)
  [pubkey].try { |acc| !acc[:signer] && acc[:writable] }
end

#writable_signer?(pubkey) ⇒ Boolean

Predicate to check if an account is a writable signer

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is a writable signer

Since:

  • 0.0.3



131
132
133
# File 'lib/solace/utils/account_context.rb', line 131

def writable_signer?(pubkey)
  [pubkey].try { |acc| acc[:signer] && acc[:writable] }
end