# Transfer Operations

Relevant source files

- [LinkSDK/Models/Payload.swift](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/Models/Payload.swift)
- [LinkSDKExample/LaunchScreen.storyboard](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDKExample/LaunchScreen.storyboard)
- [LinkSDKExample/Main.storyboard](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDKExample/Main.storyboard)
- [README.md](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/README.md?plain=1)

This document covers the cryptocurrency transfer functionality within the LinkSDK, specifically how the SDK handles the execution, monitoring, and completion of financial transfers between accounts and external wallets. Transfer operations represent the final step in the user's financial workflow after establishing connections to institutions.

For information about establishing initial connections to financial institutions, see [Integration Connection](/content/FrontFin/mesh-ios-sdk/4.1-integration-connection/index.html). For details about detecting and launching external wallet applications, see [External App Integration](/content/FrontFin/mesh-ios-sdk/4.3-external-app-integration/index.html).

## Transfer Workflow Architecture

The transfer operations in LinkSDK follow a web-to-native communication pattern where transfers are initiated and monitored through the web interface, with completion results communicated back to the iOS application through JavaScript message handlers.

```
"Host Application""onTransferFinished""WKScriptMessageHandler""LinkWebViewViewController""Web Interface""Host Application""onTransferFinished""WKScriptMessageHandler""LinkWebViewViewController""Web Interface"alt[Transfer Success][Transfer Error]"User initiates transfer""Display transfer interface""Process transfer execution""transferFinished message""Parse TransferFinishedPayload""TransferFinishedSuccessPayload""TransferFinishedErrorPayload""Transfer completion notification"
```

Sources: [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift381-466](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L381-L466)

## Transfer Message Types

The SDK defines specific JavaScript message types to handle different stages of transfer operations. The core transfer-related message type is processed through the `JSMessageType` enum.

```

Web JavaScript

jsMessageHandler

JSMessageType.transferFinished

TransferFinishedStatus

transferFinishedSuccess

transferFinishedError

TransferFinishedSuccessPayload

TransferFinishedErrorPayload

onTransferFinished(.success)

onTransferFinished(.error)
```

Sources: [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift50-61](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L50-L61) [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift63-66](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L63-L66)

## Transfer Data Models

The SDK provides structured data models to represent transfer completion states and associated metadata. These models ensure type-safe handling of transfer results.

| Data Structure | Purpose | Key Fields |
| --- | --- | --- |
| `TransferFinishedPayload` | Root payload enum | `.success()`, `.error()` |
| `TransferFinishedSuccessPayload` | Successful transfer details | `txId`, `txHash`, `symbol`, `amount` |
| `TransferFinishedErrorPayload` | Transfer failure information | `errorMessage` |

### TransferFinishedSuccessPayload Structure

The success payload contains comprehensive transaction metadata:

```
TransferFinishedSuccessPayload

+String? txId

+String? transferId

+String? txHash

+String? fromAddress

+String? toAddress

+String symbol

+Double amount

+Double? amountInFiat

+Double? totalAmountInFiat

+String? networkId

+String? networkName
```

The `symbol` and `amount` fields are required, representing the cryptocurrency symbol and transfer amount respectively. Optional fields provide additional context including blockchain transaction hashes, wallet addresses, fiat conversion amounts, and network identifiers.

Sources: [LinkSDK/Models/Payload.swift65-77](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/Models/Payload.swift#L65-L77)

## Transfer Message Processing

The `LinkWebViewViewController` implements `WKScriptMessageHandler` to process transfer completion messages from the web interface. The message processing flow handles both successful and failed transfer scenarios.

```
userContentController didReceive message

Extract message type

JSMessageType.transferFinished

Extract status from payload

TransferFinishedStatus.transferFinishedSuccess

TransferFinishedStatus.transferFinishedError

JSONDecoder decode TransferFinishedSuccessPayload

JSONDecoder decode TransferFinishedErrorPayload

configuration.onTransferFinished?(.success)

configuration.onTransferFinished?(.error)
```

The message handler extracts the `payload` dictionary from the JavaScript message, determines the transfer status, and decodes the appropriate payload structure using `JSONDecoder` before invoking the configured callback.

Sources: [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift403-418](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L403-L418)

## Configuration Integration

Transfer operations integrate with the SDK's configuration system through the `LinkConfiguration.onTransferFinished` callback property. This callback allows the host application to receive and process transfer completion events.

The callback signature accepts a `TransferFinishedPayload` enum value:

```
onTransferFinished: ((TransferFinishedPayload) -> Void)?
```

The host application can pattern match on the payload to handle success and error cases appropriately:

```
switch transferPayload {
case .success(let successPayload):
    // Handle successful transfer
case .error(let errorPayload):
    // Handle transfer error
}
```

Sources: [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift412](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L412-L412) [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift417](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L417-L417) [LinkSDK/Models/Payload.swift60-63](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/Models/Payload.swift#L60-L63)

## Error Handling

Transfer errors are communicated through the `TransferFinishedErrorPayload` structure, which provides a simple error message string. The error handling follows the same JavaScript-to-native message pattern as successful transfers.

```
Transfer Error in Web

transferFinished with error status

TransferFinishedErrorPayload

onTransferFinished(.error)

Application Error Handling
```

The error payload contains a single `errorMessage` field that provides human-readable error information to the host application.

Sources: [LinkSDK/Models/Payload.swift79-81](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/Models/Payload.swift#L79-L81) [LinkSDK/LinkWebViewController/LinkWebViewViewController.swift413-417](https://github.com/FrontFin/mesh-ios-sdk/blob/02fd63b0/LinkSDK/LinkWebViewController/LinkWebViewViewController.swift#L413-L417)
