Implementing DeFi Protocol Connection in Mobile Wallet
DeFi from a mobile wallet isn't opening a WebView with a dApp. It's direct interaction with smart contracts: calling Uniswap Router methods, Aave LendingPool, Compound cToken. The interface is built on JSON ABI; transactions are signed locally; data is read via RPC without intermediaries.
Architecture: ABI-First Approach
Each DeFi protocol is a set of smart contracts with public ABI. The app's job: encode the call per ABI, sign the transaction, send it. For EVM, the web3swift (iOS) or web3j (Android) library provides EthereumContract for dynamic ABI interaction.
// iOS — call Uniswap V3 QuoterV2 for a quote
let quoterABI = try! EthereumContract(json: quoterV2ABI)
let result = try await quoterABI.read(
"quoteExactInputSingle",
parameters: [tokenIn, tokenOut, fee, amountIn, sqrtPriceLimitX96] as [AnyObject],
transactionOptions: nil
)
For Solana and its protocols (Raydium, Orca, Jupiter), Anchor SDK generates IDL (Interface Definition Language), similar to ABI. SolanaSwift + Anchor allow typed program instruction calls.
Uniswap V3: Swap via Router
Uniswap V3 is the most complex of popular protocols to integrate on mobile. SwapRouter02 (0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45) accepts ExactInputSingleParams:
// Android — encode exactInputSingle call
val function = Function(
"exactInputSingle",
listOf(
DynamicStruct(
Address(tokenIn),
Address(tokenOut),
Uint24(fee), // 500, 3000 or 10000
Address(recipient),
Uint256(amountIn),
Uint256(amountOutMinimum), // accounting for slippage
Uint256(BigInteger.ZERO) // sqrtPriceLimitX96
)
),
listOf(Uint256())
)
val encodedData = FunctionEncoder.encode(function)
Before swapping, you need approve to SwapRouter: ERC-20 approve(routerAddress, amountIn). Without it, the swap transaction fails with TransferHelper: TRANSFER_FROM_FAILED.
For quotes: QuoterV2.quoteExactInputSingle (read-only, off-chain). Never use onchain Quoter for realtime UI—it's eth_call, which hammers the node.
Aave V3: Supply and Borrow
Aave Pool (0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2 on Mainnet) accepts supply(asset, amount, onBehalfOf, referralCode). Logic:
- Approve Pool on
amountof token. - Call
supply—get aToken in return. - For borrowing—check
getUserAccountData, ensure healthFactor > 1.5 before recommending a borrow amount.
HealthFactor below 1.0 means liquidation. A mobile app must display HealthFactor in real time and warn as it approaches 1.1.
WalletConnect for dApp Browser
If the wallet wants external dApp connection via mobile browser—WalletConnect v2 SDK (com.walletconnect:walletkit for Android, WalletConnectSwift for iOS). The protocol establishes an encrypted channel between dApp and wallet. Wallet receives sign requests, shows transaction details to the user, signs locally, returns the signed hex.
State Management and Protocol Data Cache
DeFi data updates every block. Uniswap pool rate, Aave interest rates—read via Multicall3 every 12–15 seconds. Store in reactive state (Combine on iOS, Flow/StateFlow on Android). On RPC error, graceful degradation: show last known data with timestamp.
Typical Problems
Don't hardcode slippage tolerance. 0.5% for stablecoin pairs, 1–3% for volatile. Too low: transaction always reverts on volatile market. Too high: sandwich attack eats the difference.
Gas estimation for DeFi can miss: Uniswap V3 at some routes consumes 200k–500k gas. Multiply estimation by 1.3, not 1.1.
Timeline: 1–3 months depending on protocol set. One protocol (e.g., Uniswap V3 swap only): 1–2 weeks with testnet testing. Full DeFi hub with Uniswap + Aave + Compound: 2–3 months.







