Skip to content

Getting started

Aleksandr Ershov edited this page Dec 5, 2019 · 10 revisions

All source codes are available in example-module

This library is written in Kotlin and network part is based on RxJava and Retrofit, which become the modern Android development standard. If you use your own ways of working with a network, like Coroutines, you can implement them using network models, interfaces and crypto-primitives from this library. They allow to prepare, create and sign transactions for Waves blockchain. Feel free to report any bugs, issues or typos here.

Get started

Let's get started and go step-by-step.

  1. (A) Add Waves Android SDK and Rx-Java (if needed) libraries to your app-project in app/build.gradle file
dependencies {
    // ...
    // Check last version at https://search.maven.org/artifact/com.wavesplatform
    implementation 'com.wavesplatform:android-sdk:0.0.8'
    // If you will use Waves Rx service method
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    // ...
}

With last SDK version:

Maven Central

Add jitpack in root/build.gradle

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
  1. (B) Or you can add wavesplatform-release.aar-file to your app from here to app/libs
dependencies {
    // ...
    implementation(name:'wavesplatform-release', ext:'aar')
    // If you will use Waves Rx service method
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    // ...
}

And add libraries to your project in root/build.gradle

    //...
    allprojects {
        repositories {
            google()
            jcenter()
            flatDir {
                dirs 'libs'
            }
            //...
        }
    }
    //...
  1. Add uses-permissions about Internet in a manifest file (app/src/main/manifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mysite.mywavesapplication">
 
    <uses-permission android:name="android.permission.INTERNET"/>
 
    <application>
        <!-- ... -->
    </application>
 
</manifest>
  1. Add library initialization to onCreate() method in your Application-class extension
class App : Application() {
     
    override fun onCreate() {
        super.onCreate()
        // ...
        // Waves SDK initialization
        WavesSdk.init(this)
        
        // or use Environment.TEST_NET for switch to Test-Net
        // WavesSdk.init(this, Environment.TEST_NET)
        // ...
    }
}
  1. Now everything is ready to start using Waves. For example, we want to generate a new seed phrase and get an address for blockchain. It is available in WavesCrypto module.
fun seedPhraseGeneration() {
    // Generate or add your seed-phrase
    val newSeed: String = WavesCrypto.randomSeed()
    // Get address by seed-phrase
    val address: String = WavesCrypto.addressBySeed(newSeed)
}

You can find more details about other methods in WavesCrypto

  1. Now let's see how we can work with the blockchain.
class MainActivity : AppCompatActivity() {
 
    // For Activity or Fragment add Observables in CompositeDisposable from Rx
    private val compositeDisposable = CompositeDisposable()
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
    }
 
 
    // Now you can get Waves balance from Node
    private fun getWavesBalance(address: String) {
        compositeDisposable.add(
            WavesSdk.service()
                .getNode() // You can choose different Waves services: node, matcher and data service
                .wavesBalance(address) // Here methods of service
                .compose(RxUtil.applyObservableDefaultSchedulers()) // Rx Asynchron settings
                .subscribe({ wavesBalance ->
                    // Do something on success, now we have wavesBalance.balance in satoshi in Long
                    Toast.makeText(
                        this@MainActivity,
                        "Balance is : ${getScaledAmount(wavesBalance.balance, 8)} Waves",
                        Toast.LENGTH_SHORT)
                        .show()
                }, { error ->
                    // Do something on fail
                    val errorMessage = "Can't get wavesBalance! + ${error.message}"
                    Log.e("MainActivity", errorMessage)
                    error.printStackTrace()
                    Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_SHORT).show()
                })
            )
    }
 
 
    // Handle NetworkExceptions if it necessary
    private fun handleNetworkErrors() {
        WavesSdk.service().addOnErrorListener(object : OnErrorListener {
            override fun onError(exception: NetworkException) {
                // Handle NetworkException here
            }
        })
    }
 
 
    // And you must unsubscribe in onDestroy()
    override fun onDestroy() {
        super.onDestroy()
        compositeDisposable.clear()
    }
}

You can change service to Data, Matcher or Node

  1. Signing and sending a transaction:
private fun transactionsBroadcast() {
     
    // Creation Transfer transaction and fill it with parameters
    val transferTransaction = TransferTransaction(
            assetId = WavesConstants.WAVES_ASSET_ID_EMPTY,
            recipient = "someAddressOrAlias",
            amount = 100000000,
            fee = WavesConstants.WAVES_MIN_FEE,
            attachment = "Some comment to transaction",
            feeAssetId = WavesConstants.WAVES_ASSET_ID_EMPTY)
 
    // Sign transaction with seed
    transferTransaction.sign(seed = "sign transaction with your seed phrase")
 
    // Try to send transaction into Waves blockchain
    compositeDisposable.add(WavesSdk.service()
            .getNode()
            .transactionsBroadcast(transferTransaction)
            .compose(RxUtil.applyObservableDefaultSchedulers())
            .subscribe({ response ->
                // Do something on success, now we have wavesBalance.balance in satoshi in Long
            }, { error ->
                // Do something on fail
            }))
}

Other examples of transactions are available here in tests

  1. You can find your transaction in Explorer by transaction ID - txId.

  2. Also, you can use Explorer Testnet for testing your app with libraries. You can get some Testnet Waves to your balance in the faucet. And you can check any sent transactions in Wavesexplorer in the Mainnet or in the Testnet

  3. Basically, that's it 😎You can find other transactions, Node-Service methods, Matcher-Service methods and Data-Service methods for your great apps. Let your app start working with Waves 🤝