LogoLogo
SupportGithubSign InGet Access
  • Introduction
  • GETTING STARTED
    • SDK Quick Start
    • API Quick Start
  • Guides
    • Web
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Web authentication methods
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Configure a custom subdomain
      • Eject a wallet
      • Using the EIP-1193 Provider
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • iOS
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Manage ERC20 tokens
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
          • Passkey + Enclave Storage
        • Recover a wallet
      • Troubleshooting Tips
      • Feature Flags
    • Android
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • React Native
      • Create a wallet
      • Send tokens
      • Sign a transaction
      • Simulate a transaction
      • Back up a wallet
      • Recover a wallet
      • Cross-device sessions
      • Manage wallet lifecycle states
      • Connect with WalletConnect
      • Build a WebView
      • Perform swaps
      • Add custom signature hooks
      • MPC progress callbacks
      • Portal API methods
      • Eject a wallet
      • Legacy documentation
        • Back up a wallet
          • Backup Options
        • Recover a wallet
    • Enclave MPC API
      • Create a client
      • Create a wallet
      • Send tokens
      • Sign Ethereum transactions
      • Sign Solana transactions
      • Sign Tron transactions
      • Sign Stellar Transaction
      • Concurrent Transactions
      • Back up a wallet
      • Eject a wallet
  • Reference
    • iOS
      • createWallet
      • backupWallet
      • recoverWallet
      • ejectPrivateKeys
      • registerBackupMethod
      • setGDriveConfiguration
      • setPasskeyConfiguration
      • setPasskeyAuthenticationAnchor
      • setPassword
      • availableRecoveryMethods
      • doesWalletExist
      • isWalletBackedUp
      • isWalletOnDevice
      • isWalletRecoverable
      • getBalances
      • getAssets
      • getNftAssets
      • getTransactions
      • sendSol
      • evaluateTransaction
      • buildEip155Transaction
      • buildSolanaTransaction
      • getWalletCapabilities
    • Android
      • Reference Documentation
    • React Native
      • @portal-hq/core
      • Storage adapters
        • Cloud storage
          • @portal-hq/gdrive-storage
          • @portal-hq/icloud-storage
        • Mobile storage
          • @portal-hq/keychain
          • @portal-hq/mobile-key-values
    • Enclave MPC API
      • V1 endpoints
    • Client API
      • V3 endpoints
      • V1 endpoints
    • Custodian API
      • V3 endpoints
      • V1 endpoints
    • Swaps API
      • V3 endpoints
      • V1 endpoints
  • Resources
    • Flutter
      • iOS
      • Android
    • Error codes
      • Overview
      • Legacy Documentation
        • MPC errors
        • Network errors
        • General errors
        • Encryption errors
        • Portal Connect errors
    • Portal's MPC architecture
    • Authentication and API Keys
    • Self-Managed Backups
    • Alert Webhooks
    • Wallet lifecycle
    • Backup options
      • Password/PIN
      • GDrive
      • iCloud
      • Passkey + Enclave
    • WalletConnect metadata
    • Advanced security scanning
    • Account abstraction
    • Security firewall
    • Eject
    • Security
    • Blockchain support
    • Chain ID formatting
    • Testnet faucets
    • Going to Production
    • Rate Limits
    • Multi-backup migration guide
    • Multi-wallet migration guides
      • Migrating from Android SDK v3.x.x to v4.x.x
      • Migrating from iOS SDK v3.0.x to v3.2.x
  • Support
    • Changelog
      • Android
      • iOS
      • React Native
      • Web
      • Past Releases
        • 2024 Releases
        • 2023 Releases
    • Celo Hackathon Hub
    • Glossary
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Reference
  2. iOS

buildSolanaTransaction

Builds a Solana transaction for token transfers, handling both native SOL and SPL tokens with required program instructions.

Function Signature

public func buildSolanaTransaction(
    chainId: String,
    params: BuildTransactionParam
) async throws -> BuildSolanaTransactionResponse

Parameters

  • chainId: The chain identifier in CAIP-2 format (e.g., "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" for Solana mainnet)

  • params: A BuildTransactionParam object containing:

    • to: Recipient's Solana address

    • token: Token identifier ("SOL" for native transfers, mint address for SPL tokens)

    • amount: Amount to transfer as a string (in lamports for SOL, raw amount for SPL tokens)

Returns

A BuildSolanaTransactionResponse containing:

  • transaction: The serialized transaction string ready for signing

  • metadata: A BuildTransactionMetaData object containing:

    • amount: Formatted transaction amount

    • fromAddress: Sender's address

    • toAddress: Recipient's address

    • tokenAddress: Optional token mint address for SPL tokens

    • tokenDecimals: Number of decimal places for the token

    • tokenSymbol: Optional token symbol

    • rawAmount: Raw transaction amount value

  • error: Optional error message if the build process encounters issues

Example Usage

  1. Native SOL Transfer:

do {
    let params = BuildTransactionParam(
        to: "GxvUWHWwMpep8e6tXvVdaRmwEzKcUjhLYLhpXhS6wQtY",
        token: "SOL",
        amount: "1000000000" // 1 SOL = 1e9 lamports
    )
    
    let response = try await portal.buildSolanaTransaction(
        chainId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
        params: params
    )
    
    print("Transaction details:")
    print("Amount: \(response.metadata.amount) SOL")
    print("From: \(response.metadata.fromAddress)")
    print("To: \(response.metadata.toAddress)")
    print("Raw amount: \(response.metadata.rawAmount)")
    
    // Store transaction for signing
    let serializedTx = response.transaction
} catch {
    print("Failed to build SOL transfer: \(error)")
}
  1. SPL Token Transfer:

do {
    let params = BuildTransactionParam(
        to: "GxvUWHWwMpep8e6tXvVdaRmwEzKcUjhLYLhpXhS6wQtY",
        token: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC mint address
        amount: "100000000" // 100 USDC (6 decimals)
    )
    
    let response = try await portal.buildSolanaTransaction(
        chainId: "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
        params: params
    )
    
    print("SPL Token Transfer Details:")
    print("Token: \(response.metadata.tokenSymbol ?? "Unknown")")
    print("Token Address: \(response.metadata.tokenAddress ?? "N/A")")
    print("Amount: \(response.metadata.amount)")
    print("Decimals: \(response.metadata.tokenDecimals)")
} catch {
    print("Failed to build token transfer: \(error)")
}

Implementation Notes

  1. Amount Formatting

    • For native SOL:

      // SOL has 9 decimal places
      "1000000000"  // 1 SOL
      "500000000"   // 0.5 SOL
    • For SPL tokens:

      // USDC has 6 decimal places
      "1000000"     // 1 USDC
      "500000"      // 0.5 USDC
  2. Token Identifiers

    // Native SOL
    token: "SOL"
    
    // Common SPL Tokens
    token: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"  // USDC
    token: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"  // USDT
  3. Address Validation

    • Solana addresses must be Base58 encoded

    • Must be between 32 and 44 characters in length

    • Example validation:

      func isValidSolanaAddress(_ address: String) -> Bool {
          return address.count >= 32 && address.count <= 44
      }
  4. Error Handling

    if let error = response.error {
        // Build-specific error
        throw TransactionError.buildFailed(error)
    }
    
    // Validate address format
    guard isValidSolanaAddress(response.metadata.toAddress) else {
        throw TransactionError.invalidAddress
    }

Related Documentation

For more information about Solana transaction building, see:

PreviousbuildEip155TransactionNextgetWalletCapabilities

Last updated 4 months ago

Was this helpful?

Submitting a Solana Transaction