Skip to content
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ dependencies {
implementation project(':uniswapkit')
implementation project(':oneinchkit')
implementation project(':nftkit')
implementation project(':merkleiokit')
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.horizontalsystems.ethereumkit.core.EthereumKit.SyncState
import io.horizontalsystems.ethereumkit.core.IApiStorage
import io.horizontalsystems.ethereumkit.core.IBlockchain
import io.horizontalsystems.ethereumkit.core.IBlockchainListener
import io.horizontalsystems.ethereumkit.core.INonceProvider
import io.horizontalsystems.ethereumkit.core.RpcApiProviderFactory
import io.horizontalsystems.ethereumkit.core.TransactionBuilder
import io.horizontalsystems.ethereumkit.models.Address
Expand All @@ -42,7 +43,7 @@ class RpcBlockchain(
private val storage: IApiStorage,
private val syncer: IRpcSyncer,
private val transactionBuilder: TransactionBuilder
) : IBlockchain, IRpcSyncerListener {
) : IBlockchain, IRpcSyncerListener, INonceProvider {

private val disposables = CompositeDisposable()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ abstract class JsonRpc<T>(
if (response.error != null) {
throw ResponseError.RpcError(response.error)
}
return parseResult(response.result, gson)
}

fun parseResult(result: JsonElement?, gson: Gson): T {
return try {
gson.fromJson(result, typeOfResult) as T
} catch (error: Throwable) {
val result = parseResult(response.result, gson)
if (result == null) {
throw ResponseError.InvalidResult(result.toString())
}

return result
}

private fun parseResult(result: JsonElement?, gson: Gson): T = try {
gson.fromJson(result, typeOfResult)
} catch (error: Throwable) {
throw ResponseError.InvalidResult(result.toString())
}

sealed class ResponseError : Throwable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ import java.util.logging.Logger

class EthereumKit(
private val blockchain: IBlockchain,
private val transactionManager: TransactionManager,
private val nonceProvider: NonceProvider,
val transactionManager: TransactionManager,
private val transactionSyncManager: TransactionSyncManager,
private val connectionManager: ConnectionManager,
private val address: Address,
Expand Down Expand Up @@ -153,7 +154,7 @@ class EthereumKit(
}

fun getNonce(defaultBlockParameter: DefaultBlockParameter): Single<Long> {
return blockchain.getNonce(defaultBlockParameter)
return nonceProvider.getNonce(defaultBlockParameter)
}

fun getFullTransactionsFlowable(tags: List<List<String>>): Flowable<List<FullTransaction>> {
Expand Down Expand Up @@ -224,7 +225,7 @@ class EthereumKit(
gasLimit: Long,
nonce: Long? = null
): Single<RawTransaction> {
val nonceSingle = nonce?.let { Single.just(it) } ?: blockchain.getNonce(DefaultBlockParameter.Pending)
val nonceSingle = nonce?.let { Single.just(it) } ?: nonceProvider.getNonce(DefaultBlockParameter.Pending)

return nonceSingle.flatMap { nonce ->
Single.just(RawTransaction(gasPrice, gasLimit, address, value, nonce, transactionInput))
Expand Down Expand Up @@ -311,6 +312,14 @@ class EthereumKit(
transactionSyncManager.add(transactionSyncer)
}

fun addNonceProvider(provider: INonceProvider) {
nonceProvider.addProvider(provider)
}

fun addExtraDecorator(decorator: IExtraDecorator) {
decorationManager.addExtraDecorator(decorator)
}

fun addMethodDecorator(decorator: IMethodDecorator) {
decorationManager.addMethodDecorator(decorator)
}
Expand Down Expand Up @@ -492,8 +501,12 @@ class EthereumKit(
transactionSyncManager.add(internalTransactionsSyncer)
transactionSyncManager.add(ethereumTransactionSyncer)

val nonceProvider = NonceProvider()
nonceProvider.addProvider(blockchain)

val ethereumKit = EthereumKit(
blockchain,
nonceProvider,
transactionManager,
transactionSyncManager,
connectionManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ import io.horizontalsystems.ethereumkit.api.models.AccountState
import io.horizontalsystems.ethereumkit.contracts.ContractEventInstance
import io.horizontalsystems.ethereumkit.contracts.ContractMethod
import io.horizontalsystems.ethereumkit.decorations.TransactionDecoration
import io.horizontalsystems.ethereumkit.models.*
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.DefaultBlockParameter
import io.horizontalsystems.ethereumkit.models.Eip20Event
import io.horizontalsystems.ethereumkit.models.GasPrice
import io.horizontalsystems.ethereumkit.models.InternalTransaction
import io.horizontalsystems.ethereumkit.models.ProviderEip1155Transaction
import io.horizontalsystems.ethereumkit.models.ProviderEip721Transaction
import io.horizontalsystems.ethereumkit.models.ProviderInternalTransaction
import io.horizontalsystems.ethereumkit.models.ProviderTokenTransaction
import io.horizontalsystems.ethereumkit.models.ProviderTransaction
import io.horizontalsystems.ethereumkit.models.RawTransaction
import io.horizontalsystems.ethereumkit.models.Signature
import io.horizontalsystems.ethereumkit.models.Transaction
import io.horizontalsystems.ethereumkit.models.TransactionLog
import io.horizontalsystems.ethereumkit.models.TransactionTag
import io.horizontalsystems.ethereumkit.spv.models.AccountStateSpv
import io.horizontalsystems.ethereumkit.spv.models.BlockHeader
import io.reactivex.Single
Expand Down Expand Up @@ -106,6 +120,10 @@ interface IEventDecorator {
fun contractEventInstances(logs: List<TransactionLog>): List<ContractEventInstance>
}

interface IExtraDecorator {
fun extra(hash: ByteArray) : Map<String, Any>
}

interface ITransactionDecorator {
fun decoration(
from: Address?,
Expand All @@ -125,3 +143,7 @@ interface ITransactionProvider {
fun getEip721Transactions(startBlock: Long): Single<List<ProviderEip721Transaction>>
fun getEip1155Transactions(startBlock: Long): Single<List<ProviderEip1155Transaction>>
}

interface INonceProvider {
fun getNonce(defaultBlockParameter: DefaultBlockParameter): Single<Long>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.horizontalsystems.ethereumkit.core

import io.horizontalsystems.ethereumkit.models.DefaultBlockParameter
import io.reactivex.Single

class NonceProvider : INonceProvider {
private val providers = mutableListOf<INonceProvider>()

fun addProvider(provider: INonceProvider) {
providers.add(provider)
}

override fun getNonce(defaultBlockParameter: DefaultBlockParameter): Single<Long> {
val singles = providers.map {
it.getNonce(defaultBlockParameter)
}

return Single
.zip(singles) { objects: Array<Any> ->
var maxNonce = -1L
for (obj in objects) {
if (obj is Long) {
maxNonce = maxOf(maxNonce, obj)
}
}
maxNonce
}
.flatMap { nonce: Long ->
if (nonce == -1L) {
Single.error(IllegalStateException("Could not fetch nonce. None of the providers returned a value."))
} else {
Single.just(nonce)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,34 @@ import io.horizontalsystems.ethereumkit.contracts.ContractEventInstance
import io.horizontalsystems.ethereumkit.contracts.ContractMethod
import io.horizontalsystems.ethereumkit.contracts.EmptyMethod
import io.horizontalsystems.ethereumkit.core.IEventDecorator
import io.horizontalsystems.ethereumkit.core.IExtraDecorator
import io.horizontalsystems.ethereumkit.core.IMethodDecorator
import io.horizontalsystems.ethereumkit.core.ITransactionDecorator
import io.horizontalsystems.ethereumkit.core.ITransactionStorage
import io.horizontalsystems.ethereumkit.models.*
import io.horizontalsystems.ethereumkit.models.Address
import io.horizontalsystems.ethereumkit.models.FullRpcTransaction
import io.horizontalsystems.ethereumkit.models.FullTransaction
import io.horizontalsystems.ethereumkit.models.InternalTransaction
import io.horizontalsystems.ethereumkit.models.Transaction
import io.horizontalsystems.ethereumkit.models.TransactionData
import io.horizontalsystems.ethereumkit.models.TransactionLog
import java.math.BigInteger

class DecorationManager(private val userAddress: Address, private val storage: ITransactionStorage) {
private val methodDecorators = mutableListOf<IMethodDecorator>()
private val eventDecorators = mutableListOf<IEventDecorator>()
private val transactionDecorators = mutableListOf<ITransactionDecorator>()
private val extraDecorators = mutableListOf<IExtraDecorator>()

private fun extra(hash: ByteArray) = buildMap {
extraDecorators.forEach { extraDecorator ->
putAll(extraDecorator.extra(hash))
}
}

fun addExtraDecorator(decorator: IExtraDecorator) {
extraDecorators.add(decorator)
}

fun addMethodDecorator(decorator: IMethodDecorator) {
methodDecorators.add(decorator)
Expand Down Expand Up @@ -58,7 +76,7 @@ class DecorationManager(private val userAddress: Address, private val storage: I
eventInstancesMap[transaction.hashString] ?: listOf()
)

return@map FullTransaction(transaction, decoration)
return@map FullTransaction(transaction, decoration, extra(transaction.hash))
}
}

Expand All @@ -81,7 +99,7 @@ class DecorationManager(private val userAddress: Address, private val storage: I
fullRpcTransaction.rpcReceipt?.logs?.let { eventInstances(it) } ?: listOf()
)

return FullTransaction(transaction, decoration)
return FullTransaction(transaction, decoration, extra(transaction.hash))
}

private fun getInternalTransactionsMap(transactions: List<Transaction>): Map<String, List<InternalTransaction>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import io.horizontalsystems.ethereumkit.decorations.TransactionDecoration

class FullTransaction(
val transaction: Transaction,
val decoration: TransactionDecoration
val decoration: TransactionDecoration,
val extra: Map<String, Any>
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.horizontalsystems.ethereumkit.core.toHexString
import java.math.BigInteger

@Entity
class Transaction(
data class Transaction(
@PrimaryKey
val hash: ByteArray,
val timestamp: Long,
Expand Down
1 change: 1 addition & 0 deletions merkleiokit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
62 changes: 62 additions & 0 deletions merkleiokit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
id 'maven-publish'
}

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
}
}
}
}

android {
compileSdkVersion 32

defaultConfig {
minSdkVersion 26
targetSdkVersion 32

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

kotlinOptions {
jvmTarget = '11'
}
}

dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.google.code.gson:gson:2.9.0'

implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

implementation 'androidx.core:core-ktx:1.8.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

// Room
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
kapt "androidx.room:room-compiler:$room_version"

implementation project(':ethereumkit')
}
Empty file added merkleiokit/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions merkleiokit/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.horizontalsystems.merkleiokit

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.horizontalsystems.merkleiokit.test", appContext.packageName)
}
}
1 change: 1 addition & 0 deletions merkleiokit/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="io.horizontalsystems.merkleiokit" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.horizontalsystems.merkleiokit

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(
entities = [
MerkleTransactionHash::class,
],
version = 1,
exportSchema = false
)
abstract class MerkleDatabase : RoomDatabase() {
abstract fun merkleTransactionDao(): MerkleTransactionDao

companion object Companion {
fun getInstance(context: Context, databaseName: String): MerkleDatabase {
return Room.databaseBuilder(context, MerkleDatabase::class.java, databaseName)
.fallbackToDestructiveMigration()
.allowMainThreadQueries()
.build()
}
}
}
Loading