fundWallet

suspend fun fundWallet(nativeTokenContract: String, forceMethod: SubmissionMethod? = null): String

Funds the smart account wallet using Friendbot (testnet only).

Creates a temporary keypair, funds it via Friendbot, then transfers the balance (minus reserve) to the smart account contract. Supports relayer fee sponsoring by converting source_account auth entries to Address credentials.

Flow:

  1. Generate random temporary keypair

  2. Fund temp account via Friendbot HTTP GET

  3. Wait briefly for funding to confirm

  4. Query temp account balance via native token contract simulation

  5. Calculate transfer amount (balance - reserve)

  6. Build transfer from temp to smart account

  7. Simulate to get auth entries

  8. Convert source_account auth entries to Address credentials (for relayer)

  9. Sign auth entries with temp keypair

  10. Re-simulate with signed auth entries

  11. Decide fee sponsoring mode and submit

  12. Return funded amount in XLM

Fee Sponsoring

When a relayer URL is configured via config.relayerUrl:

  • Mode 1: Used when no source_account auth exists after conversion. Sends host function + signed auth entries without signing the transaction envelope.

  • Mode 2: Used when source_account auth still exists (not converted). Sends fully signed transaction XDR with temp keypair signature.

When no relayer is configured, submits directly via RPC with temp keypair signature.

Source Account Auth Conversion

The funding flow converts source_account (Void) credentials to Address credentials with a generated nonce. This allows the relayer to substitute its own channel accounts for fee sponsoring, enabling zero-balance smart accounts to receive their first funds.

IMPORTANT: Only works on testnet. Do not use on mainnet.

Return

The amount funded in XLM

Parameters

nativeTokenContract

The native token (XLM) contract address (C-address)

forceMethod

Optional override to force relayer or RPC submission (default: auto-detect)

See also

OZTransactionOperations.convertAndSignAuthEntries

for source_account to Address credential conversion

Throws

if no wallet is connected

if the contract address is invalid

if any step of the funding flow fails

Example:

// Fund wallet (uses relayer if configured)
val fundedAmount = txOps.fundWallet(
nativeTokenContract = "CBCD1234..."
)
println("Funded $fundedAmount XLM")

// Fund wallet with direct RPC submission (no relayer)
val kit = OZSmartAccountKit.create(
config = config.copy(relayerUrl = null)
)
val amount = kit.transactionOperations.fundWallet(nativeTokenContract)
println("Funded directly: $amount XLM")