Class: Solace::Composers::SquadsSmartAccountsUseSpendingLimitComposer
- Inherits:
-
Base
- Object
- Base
- Solace::Composers::SquadsSmartAccountsUseSpendingLimitComposer
- 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
-
#amount ⇒ Integer
Extracts the transfer amount from the params.
-
#build_instruction(context) ⇒ Solace::Instruction
Builds the instruction with resolved account indices.
-
#decimals ⇒ Integer
Extracts the mint decimals from the params.
-
#destination ⇒ String
Extracts the destination address from the params.
-
#destination_token_account ⇒ String?
Extracts the destination’s token account from the params.
-
#memo ⇒ String?
Extracts the memo from the params.
-
#mint ⇒ String?
Extracts the token mint from the params.
-
#program_id ⇒ String
Returns the Squads Smart Account program id from the constants.
-
#settings ⇒ String
Extracts the settings address from the params.
-
#setup_accounts ⇒ Object
Declares all accounts required by this instruction.
-
#signer ⇒ String
Extracts the allowed signer address from the params.
-
#smart_account ⇒ String
Extracts the vault address from the params.
-
#smart_account_token_account ⇒ String?
Extracts the vault’s token account from the params.
-
#sol? ⇒ Boolean
Whether this is a SOL spending limit.
-
#spending_limit ⇒ String
Extracts the spending limit PDA address from the params.
-
#system_program ⇒ String
Returns the system program id from the constants.
-
#token_program ⇒ String?
Extracts the token program from the params.
Instance Method Details
#amount ⇒ Integer
Extracts the transfer amount from the params
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
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(smart_account), 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(smart_account_token_account), destination_token_account_index: sol? ? absent : context.index_of(destination_token_account), token_program_index: sol? ? absent : context.index_of(token_program), program_index: ) end |
#decimals ⇒ Integer
Extracts the mint decimals from the params
80 81 82 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 80 def decimals params[:decimals] || 9 end |
#destination ⇒ String
Extracts the destination address from the params
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_account ⇒ String?
Extracts the destination’s token account from the params
115 116 117 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 115 def destination_token_account params[:destination_token_account]&.to_s end |
#memo ⇒ String?
Extracts the memo from the params
87 88 89 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 87 def memo params[:memo] end |
#mint ⇒ String?
Extracts the token mint from the params
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_id ⇒ String
Returns the Squads Smart Account program id from the constants
130 131 132 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 130 def program_id SquadsSmartAccounts::PROGRAM_ID end |
#settings ⇒ String
Extracts the settings address from the params
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_accounts ⇒ Object
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 account_context.add_readonly_nonsigner(settings) account_context.add_readonly_signer(signer) account_context.add_writable_nonsigner(spending_limit) account_context.add_writable_nonsigner(smart_account) account_context.add_writable_nonsigner(destination) account_context.add_readonly_nonsigner(system_program) account_context.add_readonly_nonsigner(program_id) return if sol? account_context.add_readonly_nonsigner(mint) account_context.add_writable_nonsigner(smart_account_token_account) account_context.add_writable_nonsigner(destination_token_account) account_context.add_readonly_nonsigner(token_program) end |
#signer ⇒ String
Extracts the allowed signer address from the params
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_account ⇒ String
Extracts the vault address from the params
59 60 61 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 59 def smart_account params[:smart_account].to_s end |
#smart_account_token_account ⇒ String?
Extracts the vault’s token account from the params
108 109 110 |
# File 'lib/solace/squads_smart_accounts/composers/use_spending_limit_composer.rb', line 108 def smart_account_token_account 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.
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_limit ⇒ String
Extracts the spending limit PDA address from the params
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_program ⇒ String
Returns the system program id from the constants
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_program ⇒ String?
Extracts the token program from the params
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 |