addSpendingLimit

suspend fun addSpendingLimit(contextRuleId: UInt, policyAddress: String, spendingLimit: String, periodLedgers: UInt, decimals: Int = 7, selectedSigners: List<SelectedSigner> = emptyList(), forceMethod: SubmissionMethod? = null): TransactionResult

Adds a spending limit policy to a context rule.

A spending limit policy restricts the total amount that can be spent within a rolling time window. The period is specified in ledgers (approximately 5 seconds per ledger, 720 per hour, 17,280 per day).

Converts the amount to the token's base units using decimals and delegates to addPolicy.

IMPORTANT: This operation requires the connected wallet to have authorization on the smart account. The user will be prompted for biometric authentication to sign the transaction.

Contract limits:

  • Maximum OZConstants.MAX_POLICIES policies per context rule

  • Policy address must be a valid C-address

  • Spending limit must be greater than zero

  • Period ledgers must be greater than zero

Return

TransactionResult indicating success or failure

Parameters

contextRuleId

The context rule ID to add the policy to (0 for Default rule)

policyAddress

The policy contract address (C-address)

spendingLimit

Maximum amount per period as a positive decimal string (e.g., "100" or "10.5") with up to decimals fractional digits. Converted to the token's base units internally using decimals.

periodLedgers

Period duration in ledgers (17,280 = approximately 1 day)

decimals

The token's decimal scale used to convert spendingLimit to base units. Defaults to 7 (XLM and SAC-wrapped classic assets). Pass the token's decimals() value for tokens with a different scale (see OZTransactionOperations.fetchTokenDecimals).

selectedSigners

Optional list of signers for multi-signer authorization. When empty (default), uses single-signer auth with the connected passkey. When non-empty, coordinates signatures from all listed signers.

forceMethod

Optional submission method override. When null (default), uses the configured submission method (relayer if available, RPC otherwise).

Throws

if validation fails

if transaction submission fails

Example:

// Create a spending limit (1000 XLM per day)
val result = policyManager.addSpendingLimit(
contextRuleId = 0u,
policyAddress = "CBCD1234...",
spendingLimit = "1000",
periodLedgers = Util.LEDGERS_PER_DAY.toUInt()
)

if (result.success) {
println("Spending limit added: ${result.hash ?: ""}")
} else {
println("Failed to add policy: ${result.error ?: ""}")
}