Skip to content

This repository is used as a library for loopr-wallet-android, the official Loopring Wallet for Android.

License

Notifications You must be signed in to change notification settings

coreycaplan3/loopr-wallet-android-network

Repository files navigation

loopr-wallet-android-network

This repository is used as a library for loopr-wallet-android, the official Loopring Wallet for Android.

It contains the necessary APIs and libraries to interact with all of the network-based features for the application. Developers are encouraged to add features, make changes, and use this library as a dependency for other Android Loopr Wallets.

This library uses Kotlin's Coroutines to make asynchronous requests. For more information on Kotlin's Coroutines, see here.

Adding the Library

To add this library as a dependency to your project via Gradle, add the following Maven URL to your project-level build.gradle file:

allprojects {
    repositories {
        maven {
            url  "https://jcenter.bintray.com"
        }
    }
}

Then, add the following dependency to your app-level build.gradle file:

implementation 'org.loopring.android:loopr-wallet-network:0.9.1'

Using the ETH Wrapper

Sending ETH is really simple. BigIntegers are used, so no decimals are ever needed. Instead the number should be formatted as follows:

  • amount = amountAsInteger * 10^(decimalPlaces)
  • Meaning, the amount is calculated using the decimal value (as seen in the UI) times the 10^(18; number of decimal places that ETH has)
fun sendEther(
        web3j: Web3j,
        amount: BigDecimal, 
        destAddress: String, 
        credentials: Credentials,
        gasPrice: BigInteger,
        gasLimit: BigInteger
) = runBlocking {
    EthService.getInstance(web3j)
        .sendEth(amount, destAddress, credentials, gasPrice, gasLimit)
        .await()
}

Using the ERC-20 Token Wrapper

Sending an ERC-20 token is also easy, with the help of the wrapper class. BigIntegers are used, so no decimals are ever needed. Instead the number should be formatted as follows:

  • tokenAmount = tokenAmountAsDecimal * 10^(decimalPlaces)
  • Meaning, the tokenAmount is calculated using the integer value (as seen in the UI) times the 10^(number of decimal places the token has)
fun sendToken(
        contractAddress: String,
        web3j: Web3j,
        credentials: Credentials,
        gasPrice: BigInteger,
        gasLimit: BigInteger,
        binary: String,
        receiverAddress: String,
        tokenAmount: BigInteger
) = runBlocking {
    Erc20Wrapper.getInstance(contractAddress, web3j, credentials, gasPrice, gasLimit, binary)
            .transfer(receiverAddress, tokenAmount)
            .await()
}

Setting up the Etherscan and Ethplorer APIs

To use the Etherscan API, you need to set the API key. To do so, set the apiKey variable, as seen below.

Notice that the Ethplorer API key is optional as of right now, since they have a free tier.

fun setupKey() {
    EtherscanService.apiKey = "your-api-key"
    EthplorerService.apiKey = "your-api-key" // Optional, since you can use a free key
}

Using the Etherscan and Ethplorer APIs

Using the API is as simple as using Retrofit, which has been orchestrated work to with the respective APIs.

// Assume that [apiKey] was already set!!
fun getAddressHistory(address: String) = runBlocking {
    EthplorerService.getService().getAddressHistory(address).await()
}
 // Assume that [apiKey] was already set!!
fun getTransactions(address: String) = runBlocking {
    EtherscanService.getService().getTransactions(address).await()
}

Thanks and Credits

About

This repository is used as a library for loopr-wallet-android, the official Loopring Wallet for Android.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published