Skip to content

Commit

Permalink
Merge pull request #1710 from novasamatech/hotfix/moonriver_staking
Browse files Browse the repository at this point in the history
Hotfix/moonriver staking
  • Loading branch information
valentunn authored Nov 6, 2024
2 parents 455ad1c + b7d8417 commit 83cef1c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 47 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
buildscript {
ext {
// App version
versionName = '8.7.2'
versionCode = 156
versionName = '8.7.3'
versionCode = 157

applicationId = "io.novafoundation.nova"
releaseApplicationSuffix = "market"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings

import io.novafoundation.nova.common.data.network.runtime.binding.bindAccountId
import io.novafoundation.nova.common.data.network.runtime.binding.bindList
import io.novafoundation.nova.common.data.network.runtime.binding.cast
import io.novafoundation.nova.common.data.network.runtime.binding.castToStruct
import io.novafoundation.nova.common.utils.percentageToFraction
import io.novasama.substrate_sdk_android.runtime.AccountId
import java.math.BigInteger

typealias Percent = BigInteger

@JvmInline
value class InflationDistributionConfig(val accounts: List<InflationDistributionAccount>)

class InflationDistributionAccount(
// Account which receives funds intended for parachain bond
val account: AccountId,

// Percent of inflation set aside for parachain bond account
// Will be integer number (30%)
val percent: Percent
)

fun InflationDistributionConfig.totalPercentAsFraction(): Double {
return accounts.sumOf { it.percent }.toDouble().percentageToFraction()
}

fun bindParachainBondConfig(decoded: Any?): InflationDistributionConfig {
val distributionAccount = bindInflationDistributionAccount(decoded)
return InflationDistributionConfig(listOf(distributionAccount))
}

fun bindInflationDistributionConfig(decoded: Any?): InflationDistributionConfig {
return InflationDistributionConfig(bindList(decoded, ::bindInflationDistributionAccount))
}

private fun bindInflationDistributionAccount(decoded: Any?): InflationDistributionAccount = decoded.castToStruct().let {
InflationDistributionAccount(
account = bindAccountId(it["account"]),
percent = bindPercent(it["percent"])
)
}

private fun bindPercent(dynamicInstance: Any?): Percent = dynamicInstance.cast()

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import io.novafoundation.nova.runtime.network.updaters.SingleStorageKeyUpdater
import io.novasama.substrate_sdk_android.runtime.RuntimeSnapshot
import io.novasama.substrate_sdk_android.runtime.metadata.storage
import io.novasama.substrate_sdk_android.runtime.metadata.storageKey
import io.novasama.substrate_sdk_android.runtime.metadata.storageOrNull

class ParachainBondInfoUpdater(
class InflationDistributionConfigUpdater(
stakingSharedState: StakingSharedState,
chainRegistry: ChainRegistry,
storageCache: StorageCache
) : SingleStorageKeyUpdater<Unit>(GlobalScope, stakingSharedState, chainRegistry, storageCache), ParachainStakingUpdater<Unit> {

override suspend fun storageKey(runtime: RuntimeSnapshot, scopeValue: Unit): String {
return runtime.metadata.parachainStaking().storage("ParachainBondInfo").storageKey()
val parachainStaking = runtime.metadata.parachainStaking()

return parachainStaking.storageOrNull("InflationDistributionInfo")?.storageKey()
?: parachainStaking.storage("ParachainBondInfo").storageKey()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package io.novafoundation.nova.feature_staking_impl.data.parachainStaking.reposi

import io.novafoundation.nova.common.data.network.runtime.binding.Perbill
import io.novafoundation.nova.common.data.network.runtime.binding.bindPerbill
import io.novafoundation.nova.common.utils.hasStorage
import io.novafoundation.nova.common.utils.parachainStaking
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.InflationDistributionConfig
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.InflationInfo
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.ParachainBondConfig
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.bindInflationDistributionConfig
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.bindInflationInfo
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.bindParachainBondConfig
import io.novafoundation.nova.runtime.multiNetwork.chain.model.ChainId
Expand All @@ -15,7 +17,7 @@ interface RewardsRepository {

suspend fun getInflationInfo(chainId: ChainId): InflationInfo

suspend fun getParachainBondConfig(chainId: ChainId): ParachainBondConfig
suspend fun getInflationDistributionConfig(chainId: ChainId): InflationDistributionConfig

suspend fun getCollatorCommission(chainId: ChainId): Perbill
}
Expand All @@ -29,9 +31,16 @@ class RealRewardsRepository(
}
}

override suspend fun getParachainBondConfig(chainId: ChainId): ParachainBondConfig {
override suspend fun getInflationDistributionConfig(chainId: ChainId): InflationDistributionConfig {
return storageDataSource.query(chainId) {
runtime.metadata.parachainStaking().storage("ParachainBondInfo").query(binding = ::bindParachainBondConfig)
val parachainStaking = runtime.metadata.parachainStaking()
val usesMultipleDistributionAccounts = parachainStaking.hasStorage("InflationDistributionInfo")

if (usesMultipleDistributionAccounts) {
parachainStaking.storage("InflationDistributionInfo").query(binding = ::bindInflationDistributionConfig)
} else {
parachainStaking.storage("ParachainBondInfo").query(binding = ::bindParachainBondConfig)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.CurrentRoundUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.DelegatorStateUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.InflationConfigUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.ParachainBondInfoUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.InflationDistributionConfigUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.ScheduledDelegationRequestsUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.blockhain.updaters.TotalDelegatedUpdater
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.repository.CurrentRoundRepository
Expand Down Expand Up @@ -98,7 +98,7 @@ class ParachainStakingUpdatersModule {
storageCache: StorageCache,
stakingSharedState: StakingSharedState,
chainRegistry: ChainRegistry,
) = ParachainBondInfoUpdater(
) = InflationDistributionConfigUpdater(
storageCache = storageCache,
stakingSharedState = stakingSharedState,
chainRegistry = chainRegistry
Expand Down Expand Up @@ -141,7 +141,7 @@ class ParachainStakingUpdatersModule {
currentRoundCollatorsUpdater: CurrentRoundCollatorsUpdater,
totalDelegatedUpdater: TotalDelegatedUpdater,
inflationConfigUpdater: InflationConfigUpdater,
parachainBondInfoUpdater: ParachainBondInfoUpdater,
inflationDistributionConfigUpdater: InflationDistributionConfigUpdater,
collatorCommissionUpdater: CollatorCommissionUpdater,
scheduledDelegationRequestsUpdater: ScheduledDelegationRequestsUpdater,
) = StakingUpdaters.Group(
Expand All @@ -150,7 +150,7 @@ class ParachainStakingUpdatersModule {
currentRoundCollatorsUpdater,
totalDelegatedUpdater,
inflationConfigUpdater,
parachainBondInfoUpdater,
inflationDistributionConfigUpdater,
collatorCommissionUpdater,
scheduledDelegationRequestsUpdater
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.novafoundation.nova.feature_staking_impl.domain.parachainStaking.rewards

import io.novafoundation.nova.common.data.network.runtime.binding.Perbill
import io.novafoundation.nova.common.utils.percentageToFraction
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.InflationDistributionConfig
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.InflationInfo
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.ParachainBondConfig
import io.novafoundation.nova.feature_staking_impl.data.parachainStaking.network.bindings.totalPercentAsFraction
import io.novafoundation.nova.feature_staking_impl.domain.rewards.PeriodReturns
import io.novasama.substrate_sdk_android.extensions.toHexString
import io.novasama.substrate_sdk_android.runtime.AccountId
Expand Down Expand Up @@ -31,7 +31,7 @@ interface ParachainStakingRewardCalculator {
private const val DAYS_IN_YEAR = 365

class RealParachainStakingRewardCalculator(
private val bondConfig: ParachainBondConfig,
private val inflationDistributionConfig: InflationDistributionConfig,
inflationInfo: InflationInfo,
totalIssuance: BigInteger,
totalStaked: BigInteger,
Expand Down Expand Up @@ -95,10 +95,8 @@ class RealParachainStakingRewardCalculator(
}

private fun calculatorApr(collatorStake: Double): Double {
return annualReturn * (1 - bondConfig.percentageAsFraction() - collatorCommission.toDouble()) * (averageStake / collatorStake)
return annualReturn * (1 - inflationDistributionConfig.totalPercentAsFraction() - collatorCommission.toDouble()) * (averageStake / collatorStake)
}

private fun ParachainBondConfig.percentageAsFraction() = percent.toDouble().percentageToFraction()
}

fun ParachainStakingRewardCalculator.maximumAnnualApr() = maximumGain(DAYS_IN_YEAR)
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ParachainStakingRewardCalculatorFactory(
val circulating = additionalIssuance + totalIssuance

return RealParachainStakingRewardCalculator(
bondConfig = rewardsRepository.getParachainBondConfig(chainId),
inflationDistributionConfig = rewardsRepository.getInflationDistributionConfig(chainId),
inflationInfo = rewardsRepository.getInflationInfo(chainId),
totalIssuance = circulating,
totalStaked = currentRoundRepository.totalStaked(chainId),
Expand All @@ -61,7 +61,7 @@ class ParachainStakingRewardCalculatorFactory(
chainId: ChainId,
snapshots: AccountIdMap<CollatorSnapshot>
) = RealParachainStakingRewardCalculator(
bondConfig = rewardsRepository.getParachainBondConfig(chainId),
inflationDistributionConfig = rewardsRepository.getInflationDistributionConfig(chainId),
inflationInfo = rewardsRepository.getInflationInfo(chainId),
totalIssuance = commonStakingRepository.getTotalIssuance(chainId),
totalStaked = currentRoundRepository.totalStaked(chainId),
Expand Down

0 comments on commit 83cef1c

Please sign in to comment.