Skip to content

Commit

Permalink
V28 runtime migration (#150)
Browse files Browse the repository at this point in the history
* V28 migration

* Add not indexed changes

* Fix module name
  • Loading branch information
valentunn authored Feb 9, 2021
1 parent aaf845b commit cb4ede5
Show file tree
Hide file tree
Showing 41 changed files with 794 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import jp.co.soramitsu.common.data.network.rpc.ConnectionManager
import jp.co.soramitsu.common.mixin.api.NetworkStateMixin
import jp.co.soramitsu.common.resources.ResourceManager
import jp.co.soramitsu.feature_account_api.domain.interfaces.AccountRepository
import jp.co.soramitsu.feature_wallet_api.di.WalletUpdaters
import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletRepository
import jp.co.soramitsu.feature_wallet_api.domain.model.BuyTokenRegistry

Expand All @@ -22,4 +23,6 @@ interface RootDependencies {
fun buyTokenRegistry(): BuyTokenRegistry

fun resourceManager(): ResourceManager

fun walletUpdaters(): WalletUpdaters
}
16 changes: 13 additions & 3 deletions app/src/main/java/jp/co/soramitsu/app/root/di/RootFeatureModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ package jp.co.soramitsu.app.root.di

import dagger.Module
import dagger.Provides
import jp.co.soramitsu.app.root.domain.CompositeUpdater
import jp.co.soramitsu.app.root.domain.RootInteractor
import jp.co.soramitsu.common.di.scope.FeatureScope
import jp.co.soramitsu.core_api.data.network.Updater
import jp.co.soramitsu.feature_account_api.domain.interfaces.AccountRepository
import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletRepository
import jp.co.soramitsu.feature_wallet_api.di.WalletUpdaters
import jp.co.soramitsu.feature_wallet_api.domain.model.BuyTokenRegistry

@Module
class RootFeatureModule {

@Provides
@FeatureScope
fun provideRootUpdater(
walletUpdaters: WalletUpdaters
): Updater {
return CompositeUpdater(walletUpdaters.updaters)
}

@Provides
@FeatureScope
fun provideRootInteractor(
accountRepository: AccountRepository,
walletRepository: WalletRepository,
rootUpdater: Updater,
buyTokenRegistry: BuyTokenRegistry
): RootInteractor {
return RootInteractor(accountRepository, buyTokenRegistry, walletRepository)
return RootInteractor(accountRepository, rootUpdater, buyTokenRegistry)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jp.co.soramitsu.app.root.domain

import jp.co.soramitsu.core_api.data.network.Updater
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext

class CompositeUpdater(
private val updaters: List<Updater>
) : Updater {

constructor(vararg updaters: Updater) : this(updaters.toList())

override suspend fun listenForUpdates(): Unit = withContext(Dispatchers.IO) {
val coroutines = updaters.map {
async { it.listenForUpdates() }
}

coroutines.awaitAll()
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
package jp.co.soramitsu.app.root.domain

import jp.co.soramitsu.core_api.data.network.Updater
import jp.co.soramitsu.feature_account_api.domain.interfaces.AccountRepository
import jp.co.soramitsu.feature_account_api.domain.model.Node
import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletRepository
import jp.co.soramitsu.feature_wallet_api.domain.model.BuyTokenRegistry
import jp.co.soramitsu.feature_wallet_impl.data.buyToken.ExternalProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flowOn

class RootInteractor(
private val accountRepository: AccountRepository,
private val buyTokenRegistry: BuyTokenRegistry,
private val walletRepository: WalletRepository
private val rootUpdater: Updater,
private val buyTokenRegistry: BuyTokenRegistry
) {

fun selectedNodeFlow() = accountRepository.selectedNodeFlow()

suspend fun listenForAccountUpdates(networkType: Node.NetworkType) = accountRepository.selectedAccountFlow()
.filter { it.network.type == networkType }
.distinctUntilChanged { old, new -> old.address == new.address }
.flowOn(Dispatchers.IO)
.collectLatest { walletRepository.listenForUpdates(it) }
suspend fun listenForUpdates() {
rootUpdater.listenForUpdates()
}

fun isBuyProviderRedirectLink(link: String) = buyTokenRegistry.availableProviders
.filterIsInstance<ExternalProvider>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RootViewModel(
}
}.flowOn(Dispatchers.IO)
.collectLatest {
interactor.listenForAccountUpdates(it.networkType)
interactor.listenForUpdates()
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ buildscript {

bouncyCastleVersion = '1.60'

fearlessLibVersion = '1.0.35'
fearlessLibVersion = '1.0.47'

gifVersion = '1.2.19'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
package jp.co.soramitsu.common.utils

import io.emeraldpay.polkaj.scale.ScaleCodecReader
import io.emeraldpay.polkaj.scale.ScaleCodecWriter
import jp.co.soramitsu.fearless_utils.extensions.fromHex
import jp.co.soramitsu.fearless_utils.extensions.toHexString
import jp.co.soramitsu.fearless_utils.hash.Hasher.blake2b256
import jp.co.soramitsu.fearless_utils.scale.EncodableStruct
import jp.co.soramitsu.fearless_utils.scale.Schema
import jp.co.soramitsu.fearless_utils.scale.dataType.DataType
import jp.co.soramitsu.fearless_utils.ss58.SS58Encoder
import jp.co.soramitsu.feature_account_api.domain.model.Node
import java.io.ByteArrayOutputStream

fun SS58Encoder.encode(publicKey: ByteArray, networkType: Node.NetworkType): String {
return encode(publicKey, networkType.runtimeConfiguration.addressByte)
}

fun <T> DataType<T>.fromHex(hex: String): T {
val codecReader = ScaleCodecReader(hex.fromHex())

return read(codecReader)
}

fun <T> DataType<T>.toByteArray(value: T): ByteArray {
val stream = ByteArrayOutputStream()
val writer = ScaleCodecWriter(stream)

write(writer, value)

return stream.toByteArray()
}

fun <S : Schema<S>> EncodableStruct<S>.hash(): String {
return schema.toByteArray(this).blake2b256().toHexString(withPrefix = true)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jp.co.soramitsu.common.utils

import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first

class SuspendableProperty<T> {
private val value = MutableSharedFlow<T>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)

fun invalidate() {
value.resetReplayCache()
}

fun set(new: T) {
value.tryEmit(new) // always successful, since BufferOverflow.DROP_OLDEST is used
}

suspend fun get(): T = value.first()
}
1 change: 1 addition & 0 deletions core-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
12 changes: 12 additions & 0 deletions core-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: 'java-library'
apply plugin: 'kotlin'

compileKotlin {
kotlinOptions {
freeCompilerArgs = ["-Xallow-result-return-type"]
}
}

dependencies {
implementation coroutinesDep
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package jp.co.soramitsu.core_api.data.network

interface Updater {
/**
* Implementations should be aware of cancellation
*/
suspend fun listenForUpdates()
}
2 changes: 2 additions & 0 deletions feature-account-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ compileKotlin {

dependencies {
implementation coroutinesDep

implementation project(':core-api')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jp.co.soramitsu.feature_account_impl.data.network.blockchain

import jp.co.soramitsu.common.data.network.rpc.SocketSingleRequestExecutor
import jp.co.soramitsu.fearless_utils.wsrpc.mappers.nonNull
import jp.co.soramitsu.fearless_utils.wsrpc.mappers.string
import jp.co.soramitsu.fearless_utils.wsrpc.mappers.pojo
import jp.co.soramitsu.fearless_utils.wsrpc.request.runtime.system.NodeNetworkTypeRequest

class AccountSubstrateSourceImpl(
Expand All @@ -12,6 +12,6 @@ class AccountSubstrateSourceImpl(
override suspend fun getNodeNetworkType(nodeHost: String): String {
val request = NodeNetworkTypeRequest()

return socketRequestExecutor.executeRequest(request, nodeHost, string().nonNull())
return socketRequestExecutor.executeRequest(request, nodeHost, pojo<String>().nonNull())
}
}
1 change: 1 addition & 0 deletions feature-wallet-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dependencies {
implementation coroutinesDep

implementation project(':feature-account-api')
api project(':core-api')
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import jp.co.soramitsu.feature_wallet_api.domain.interfaces.WalletRepository
import jp.co.soramitsu.feature_wallet_api.domain.model.BuyTokenRegistry

interface WalletFeatureApi {

fun provideUpdaters(): WalletUpdaters

fun provideWalletRepository(): WalletRepository

fun provideTokenRegistry(): BuyTokenRegistry
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jp.co.soramitsu.feature_wallet_api.di

import jp.co.soramitsu.core_api.data.network.Updater

class WalletUpdaters(val updaters: List<Updater>)
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ interface WalletRepository {

suspend fun checkTransferValidity(transfer: Transfer): TransferValidityStatus

suspend fun listenForUpdates(account: Account)
suspend fun listenForAccountInfoUpdates(account: Account)

suspend fun listenForStakingLedgerUpdates(account: Account)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import jp.co.soramitsu.feature_account_api.domain.model.Node
import jp.co.soramitsu.feature_wallet_api.domain.model.Transfer
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.response.BalanceChange
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.response.FeeResponse
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.AccountInfo
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.ActiveEraInfo
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.StakingLedger
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.SubmittableExtrinsic
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.account.AccountInfoSchema
import jp.co.soramitsu.feature_wallet_impl.data.network.blockchain.struct.extrinsic.TransferExtrinsic
import kotlinx.coroutines.flow.Flow

interface SubstrateRemoteSource {
suspend fun fetchAccountInfo(address: String, networkType: Node.NetworkType): EncodableStruct<AccountInfo>
suspend fun fetchAccountInfo(address: String, networkType: Node.NetworkType): EncodableStruct<AccountInfoSchema>

suspend fun getTransferFee(
account: Account,
Expand All @@ -37,8 +37,8 @@ interface SubstrateRemoteSource {

suspend fun getActiveEra(): EncodableStruct<ActiveEraInfo>

suspend fun fetchAccountTransactionInBlock(
suspend fun fetchAccountTransfersInBlock(
blockHash: String,
account: Account
): List<EncodableStruct<SubmittableExtrinsic>>
): List<TransferExtrinsic>
}
Loading

0 comments on commit cb4ede5

Please sign in to comment.