Class: Solace::Utils::AccountContext

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

Returns:

  • (Hash)

    The default account data with lowest level of permissions

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAccountContext

Initialize the account context



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

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

Instance Attribute Details

#accountsObject

The accounts in the transaction



28
29
30
# File 'lib/solace/utils/account_context.rb', line 28

def accounts
  @accounts
end

#DEFAULT_ACCOUNTObject

The default account data



12
13
14
15
16
# File 'lib/solace/utils/account_context.rb', line 12

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

#headerObject

The header for the transaction



22
23
24
# File 'lib/solace/utils/account_context.rb', line 22

def header
  @header
end

#pubkey_account_mapObject

The map of accounts



34
35
36
# File 'lib/solace/utils/account_context.rb', line 34

def 
  
end

Instance Method Details

#add_readonly_nonsigner(pubkey) ⇒ Object

Add a readonly account

Parameters:



75
76
77
# File 'lib/solace/utils/account_context.rb', line 75

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

#add_readonly_signer(pubkey) ⇒ Object

Add a readonly signer account

Parameters:



67
68
69
# File 'lib/solace/utils/account_context.rb', line 67

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

#add_writable_nonsigner(pubkey) ⇒ Object

Add a writable account

Parameters:



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

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

#add_writable_signer(pubkey) ⇒ Object

Add a signer account

Parameters:



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

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



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

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is a fee payer



83
84
85
# File 'lib/solace/utils/account_context.rb', line 83

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



167
168
169
# File 'lib/solace/utils/account_context.rb', line 167

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



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

Parameters:



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..each do |pubkey, data|
    (
      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

Parameters:

  • pubkey (String)

    The pubkey of the account

Returns:

  • (Boolean)

    Whether the account is readonly and not a signer



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

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



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

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

#set_fee_payer(pubkey) ⇒ Object

Set the fee payer account

Parameters:



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

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



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

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



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

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



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

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



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

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