🟦
Mobile SDK Quick Start

What is Mobile SDK?

Symbiosis Mobile SDK is a Kotlin MultiPlatform library designed to create applications that work with the Symbiosis protocol and Web3.
In the first place, the SDK and the documentation presented in this space are for mobile application developers. However, thanks to Kotlin versatility, it also can be used for desktop and back-end applications.
Symbiosis Mobile SDK currently provides support for Android OS. Soon we will add support for iOS.
Please refer to Symbiosis documentation for how the Symbiosis protocol works and to Glossary for definitions of the terms used in this space.

Quick Start

There are two use cases:
  • On-chain swap (the exchanging tokens are on the same blockchain),
  • Cross-chain swap (the exchanging tokens are on different blockchains).

On-chain (Uni-Like) Swap

There is an example of an on-chain swap using a Uni-like DEX.
1
val sdk = SymbiosisSdk(...)
2
val credentials = Credentials.createFromKeyPhrase("A mnemonic phrase goes here")
3
?: error("Invalid mnemonic phrase")
4
​
5
// CAKE β†’ BUSD trade
6
val token1 = sdk.bscTestnet.token.CAKE
7
val token2 = sdk.bscTestnet.token.BUSD
8
​
9
suspend fun main() = with(sdk.bscTestnetClient) { // своп Π½Π° сСти bsc
10
val (trade) = swap.findBestTradeExactIn(
11
// NetworkTokenPair checks if the tokens are actually from one network
12
networkTokenPair = NetworkTokenPair(token1, token2),
13
// a number of CAKE tokens to spend
14
amountIn = 1_000_000_000_000_000_000.bi
15
) ?: error("Trade was not found")
16
// Calculated amount of BUSD tokens to receive
17
println("Estimated amountOut: ${trade.amountOut}")
18
​
19
// Sending the swap transaction
20
val hash = swap.execute(credentials, trade)
21
​
22
// Waiting for the transaction processing on the origin blockchain
23
println("""
24
Transaction hash: $hash
25
Transaction status: ${waitForTransactionReceipt(hash).status}
26
""".trimIndent())
27
}
Copied!

Cross-chain Swap

Let's change the code above a bit to get an example of a cross-chain swap using Symbiosis DEXes.
1
val sdk = SymbiosisSdk()
2
​
3
// using BSC Testnet β†’ ETH Rinkeby for this example
4
val crossChain = sdk.bscTestnetEthRinkebyClient
5
​
6
val credentials = Credentials.createFromKeyPhrase("A mnemonic phrase goes here")
7
?: error("Invalid mnemonic phrase")
8
​
9
// CAKE β†’ UNI trade
10
val token1 = sdk.bscTestnet.token.CAKE
11
val token2 = sdk.ethRinkeby.token.UNI
12
​
13
suspend fun main() {
14
val trade = crossChain.findBestTradeExactIn(
15
fromToken = token1,
16
targetToken = token2,
17
// a number of CAKE tokens to spend
18
amountIn = token2.convertRealToInteger(1.bn)
19
) ?: error("Trade was not found")
20
// Calculated amount of UNI tokens to receive
21
val amountOutNum = trade.convertIntegerToReal(trade.finalAmountOut)
22
println("Estimated amountOut: $amountOutNum}")
23
​
24
// Sending the cross-chain swap transaction
25
val transaction = crossChain.execute(credentials, trade)
26
val log = transaction.waitForComplete()
27
​
28
val success = when (log) {
29
null -> false
30
else -> true
31
}
32
​
33
// Waiting for the transaction processing on the origin blockchain
34
// and an event on the destination blockchain
35
println("""
36
Transaction hash on BSCT: ${transaction.hash}
37
Transaction hash on Rinkeby: ${log.transactionHash}
38
Transaction status: ${if (success) "Success" else "Failure" }
39
""".trimIndent())
40
}
Copied!

SDK Components

Symbiosis Mobile SDK consists of the following components:
  • ​Broken link component implements interactions with blockchains.
  • ​Broken link component works with wallets, transaction signing.
  • ​Broken link component helps to work with tokens: native cryptocurrencies, ERC20 tokens, wrapped tokens, sTokens.
  • ​Broken link component implements swaps: on-chain swaps, cross-chain swaps.
  • ​Broken link component deals with emergencies: reverting stuck transactions.
  • Utils:
    • ​Broken link util implements several requests to one blockchain in one request.
    • ​Broken link implements calculations with floating-point numbers.
    • ​Broken link util helps to retrieve information about transactions, events, blocks.

Dependencies

Symbiosis Mobile SDK uses the following third-party libraries:
  • ​kotlinx.coroutines for asynchronous callbacks,
  • ​Ktor client for interacting with Web3 by HTTPS protocol,
  • ​moko-web3 for interacting with blockchains by Web3,
  • ​Web3j for signing transactions in a jvm target.

GitHub Repository