Class: Solace::Composers::SquadsSmartAccountsUseSpendingLimitComposer

Inherits:
Base
  • Object
show all
Defined in:
lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb

Overview

Composes a ‘useSpendingLimit` instruction for the Squads Smart Account program.

Transfers from a vault to a destination within a pre-authorized spending limit — single signature from an allowed signer, no consensus. Supports both SOL limits and SPL Token / Token-2022 limits.

For SOL limits (no :mint), the four SPL-only optional accounts (mint, both token accounts, token program) are filled with the Squads program ID, Anchor’s convention for an absent optional account. For token limits, those four slots carry the real accounts; the program transfers via ‘transfer_checked`, so :decimals must match the mint.

Required params:

:settings       [#to_s]          Base58 address of the settings account.
:signer         [#to_s, Keypair] An allowed signer of the spending limit (must sign).
:spending_limit [#to_s]          The SpendingLimit PDA to spend against.
:smart_account  [#to_s]          The vault to transfer from.
:destination    [#to_s]          The destination owner (receives SOL, or owns the destination ATA).
:amount         [Integer]        Amount to transfer (mint decimals).

Optional params (SOL):

:decimals [Integer] Mint decimals, 9 for SOL (default: 9).
:memo     [String]  Indexing memo (default: nil).

Optional params (token limits — all four required together):

:mint                        [#to_s] The token mint (omit for SOL).
:token_program               [#to_s] The program owning the mint.
:smart_account_token_account [#to_s] The vault's ATA for the mint.
:destination_token_account   [#to_s] The destination owner's ATA (must already exist).

Instance Method Summary collapse

Instance Method Details

#amountInteger

Extracts the transfer amount from the params

Returns:

  • (Integer)

    Lamports to transfer



73
74
75
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 73

def amount
  params[:amount]
end

#build_instruction(context) ⇒ Solace::Instruction

Builds the instruction with resolved account indices.

For SOL limits the SPL-only optional slots (mint, token accounts, token program) carry the program account’s index — Anchor’s absent-optional convention. For token limits they carry the real account indices.

rubocop:disable Metrics/AbcSize – straight index resolution of up to 11 accounts

Parameters:

  • context (Solace::Utils::AccountContext)

    Merged context from TransactionComposer.

Returns:

  • (Solace::Instruction)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 176

def build_instruction(context)
  program_index = context.index_of(program_id)

  # Anchor signals an absent optional account by passing the program's own
  # account in that slot; for SOL limits all four token slots are absent.
  absent = program_index

  SquadsSmartAccounts::Instructions::UseSpendingLimitInstruction.build(
    amount:,
    decimals:,
    memo:,
    settings_index:                    context.index_of(settings),
    signer_index:                      context.index_of(signer),
    spending_limit_index:              context.index_of(spending_limit),
    smart_account_index:               context.index_of(),
    destination_index:                 context.index_of(destination),
    system_program_index:              context.index_of(system_program),
    mint_index:                        sol? ? absent : context.index_of(mint),
    smart_account_token_account_index: sol? ? absent : context.index_of(),
    destination_token_account_index:   sol? ? absent : context.index_of(),
    token_program_index:               sol? ? absent : context.index_of(token_program),
    program_index:
  )
end

#decimalsInteger

Extracts the mint decimals from the params

Returns:

  • (Integer)

    The mint decimals (defaults to 9 for SOL)



80
81
82
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 80

def decimals
  params[:decimals] || 9
end

#destinationString

Extracts the destination address from the params

Returns:

  • (String)

    The destination address



66
67
68
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 66

def destination
  params[:destination].to_s
end

#destination_token_accountString?

Extracts the destination’s token account from the params

Returns:

  • (String, nil)

    The destination ATA address (token limits only)



115
116
117
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 115

def 
  params[:destination_token_account]&.to_s
end

#memoString?

Extracts the memo from the params

Returns:

  • (String, nil)

    The memo



87
88
89
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 87

def memo
  params[:memo]
end

#mintString?

Extracts the token mint from the params

Returns:

  • (String, nil)

    The mint address, or nil for a SOL limit



94
95
96
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 94

def mint
  params[:mint]&.to_s
end

#program_idString

Returns the Squads Smart Account program id from the constants

Returns:

  • (String)

    The Squads Smart Account program id



130
131
132
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 130

def program_id
  SquadsSmartAccounts::PROGRAM_ID
end

#settingsString

Extracts the settings address from the params

Returns:

  • (String)

    The settings address



38
39
40
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 38

def settings
  params[:settings].to_s
end

#setup_accountsObject

Declares all accounts required by this instruction.

For SOL limits the four SPL-only optional slots resolve to the Squads program account (already declared readonly), so no extra declarations are needed. For token limits the real token accounts are declared.

rubocop:disable Metrics/AbcSize – straight enumeration of up to 11 accounts



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 148

def setup_accounts
  .add_readonly_nonsigner(settings)
  .add_readonly_signer(signer)
  .add_writable_nonsigner(spending_limit)
  .add_writable_nonsigner()
  .add_writable_nonsigner(destination)
  .add_readonly_nonsigner(system_program)
  .add_readonly_nonsigner(program_id)

  return if sol?

  .add_readonly_nonsigner(mint)
  .add_writable_nonsigner()
  .add_writable_nonsigner()
  .add_readonly_nonsigner(token_program)
end

#signerString

Extracts the allowed signer address from the params

Returns:

  • (String)

    The signer address



45
46
47
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 45

def signer
  params[:signer].to_s
end

#smart_accountString

Extracts the vault address from the params

Returns:

  • (String)

    The vault (smart account) address



59
60
61
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 59

def 
  params[:smart_account].to_s
end

#smart_account_token_accountString?

Extracts the vault’s token account from the params

Returns:

  • (String, nil)

    The vault ATA address (token limits only)



108
109
110
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 108

def 
  params[:smart_account_token_account]&.to_s
end

#sol?Boolean

Whether this is a SOL spending limit. True when no mint is given or the mint is the default pubkey — the same marker the program uses for SOL.

Returns:

  • (Boolean)


123
124
125
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 123

def sol?
  mint.nil? || mint.to_s == SquadsSmartAccounts::DEFAULT_PUBKEY
end

#spending_limitString

Extracts the spending limit PDA address from the params

Returns:

  • (String)

    The spending limit address



52
53
54
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 52

def spending_limit
  params[:spending_limit].to_s
end

#system_programString

Returns the system program id from the constants

Returns:

  • (String)

    The system program id



137
138
139
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 137

def system_program
  Solace::Constants::SYSTEM_PROGRAM_ID
end

#token_programString?

Extracts the token program from the params

Returns:

  • (String, nil)

    The token program address (token limits only)



101
102
103
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 101

def token_program
  params[:token_program]&.to_s
end