# Chain Abstraction (/wallets/android/chain-abstraction)

<HowItWorks  components={props.components} />

## Methods [#methods]

The following methods from Wallet SDK are used in implementing chain abstraction.

<Callout type="info">
💡 Chain abstraction is currently in the early access phase and requires the `@ChainAbstractionExperimentalApi` annotation.
</Callout>

### Prepare [#prepare] 

This method is used to check if chain abstraction is needed. If it is, it will return a `PrepareSuccess.Available` object with the necessary transactions and funding information. 
If it is not, it will return a `PrepareSuccess.NotRequired` object with the original transaction. 

<Callout type="info">
Accounts field is a list of CAIP-20 accounts you are sourcing from e.g. Solana account
</Callout>

```kotlin
@ChainAbstractionExperimentalApi
fun prepare(
  initialTransaction: Wallet.Model.InitialTransaction,
  accounts: List<String>,
  onSuccess: (Wallet.Model.PrepareSuccess) -> Unit,
  onError: (Wallet.Model.PrepareError) -> Unit
)
```

### Execute [#execute]

This method is used to execute the chain abstraction operation. It broadcasts the bridging and initial transactions and waits for them to be completed.
The method returns a `ExecuteSuccess` object with the transaction hash and receipt.

```kotlin
@ChainAbstractionExperimentalApi
fun execute(
  prepareAvailable: Wallet.Model.PrepareSuccess.Available,
  prepareSignedTxs: List<Wallet.Model.RouteSig>,
  initSignedTx: String,
  onSuccess: (Wallet.Model.ExecuteSuccess) -> Unit,
  onError: (Wallet.Model.Error) -> Unit
)
``` 

## Usage [#usage]

When sending a transaction, first check if chain abstraction is needed using the `prepare` method. If it is needed, you must sign all the fulfillment transactions and use the `execute` method. 

If the operation is successful, use `execute` method and await the transaction hash and receipt. 
If the operation is unsuccessful, send the JsonRpcError to the dapp and display the error to the user. 

```kotlin
    val initialTransaction = Wallet.Model.Transaction(...)
    WalletKit.ChainAbstraction.prepare(
      initialTransaction,
      caip10Accounts,
      onSuccess = { prepareSuccess ->
        when (prepareSuccess) {
          is Wallet.Model.PrepareSuccess.Available -> {
            // If the route is available, present a CA transaction flow

            //sign route transactions
            transactionsDetails?.route?.forEach { route ->
              route.transactionDetails.forEach { transactionDetails ->
                val signedTransaction = Signer.signHash(transactionDetails.transactionHashToSign, EthAccountDelegate.privateKey)
                eip155Signatures.add(signedTransaction)
              }
            }
          }

          //sign initial transaction
          val signedInitialTx = Signer.signHash(transactionsDetails?.initialDetails.transactionHashToSign, EthAccountDelegate.privateKey)
            
            //Call the execute
            WalletKit.ChainAbstraction.execute(prepareSuccess, eip155Signatures, signedInitialTx
                onSuccess = {
                    //The execution of the Chain Abstraction is successfull
                    //Send the response to the Dapp or show to the user
                },
                onError = {
                    //Execute error - wallet should send the JsonRpcError to a dapp for given request and display error to the user
                }
            )
          }

          is Wallet.Model.PrepareSuccess.NotRequired -> {
                  // user does not need to move funds from other chains, sign and broadcast original transaction
          }
        }
      },
      onError = { prepareError ->
            // One of the possible errors: NoRoutesAvailable, InsufficientFunds, InsufficientGasFunds - wallet should send the JsonRpcError to a dapp for given request and display error to the user
      }
   )
```

For example, check out implementation of chain abstraction in [sample wallet](https://github.com/WalletConnect/WalletConnectKotlinV2/tree/master/sample/wallet) with Kotlin. 

<ErrorHandling  components={props.components} />

## Testing [#testing] 

To test Chain Abstraction, you can use the [AppKit laboratory](https://lab.reown.com/appkit/?name=wagmi) and try sending any supported [tokens](/wallets/features/chain-abstraction#what-are-the-supported-tokens-and-networks) with any chain abstraction supported wallet. 
You can also use this [sample wallet](https://appdistribution.firebase.dev/i/076a3bc9669d3bee) for testing. 

## ProGuard rules [#proguard-rules]

If you encounter issues with minification, add the below rules to your application:

```
-keepattributes *Annotation*

-keep class com.sun.jna.** { *; }
-keepclassmembers class com.sun.jna.** {
    native <methods>;
    *;
}

-keep class uniffi.** { *; }

# Preserve all public and protected fields and methods
-keepclassmembers class ** {
    public *;
    protected *;
}

-dontwarn uniffi.**
-dontwarn com.sun.jna.**
```
