-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1284 from novasamatech/fix/polkadot-inflation
Support runtime api for predicting inflation
- Loading branch information
Showing
8 changed files
with
223 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...et/Modules/Staking/Services/RewardCalculatorService/RelayChain/PolkadotRewardEngine.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
import BigInt | ||
import SubstrateSdk | ||
|
||
final class PolkadotRewardEngine: RewardCalculatorEngine { | ||
let inflationPrediction: RuntimeApiInflationPrediction | ||
|
||
init( | ||
chainId: ChainModel.Id, | ||
assetPrecision: Int16, | ||
inflationPrediction: RuntimeApiInflationPrediction, | ||
totalIssuance: BigUInt, | ||
validators: [EraValidatorInfo], | ||
eraDurationInSeconds: TimeInterval | ||
) { | ||
self.inflationPrediction = inflationPrediction | ||
|
||
super.init( | ||
chainId: chainId, | ||
assetPrecision: assetPrecision, | ||
totalIssuance: totalIssuance, | ||
validators: validators, | ||
eraDurationInSeconds: eraDurationInSeconds | ||
) | ||
} | ||
|
||
override func calculateAnnualInflation() -> Decimal { | ||
guard let validatorsMint = inflationPrediction.nextMint.items.first else { | ||
return 0 | ||
} | ||
|
||
let validatorsMintDecimal = Decimal.fromSubstrateAmount( | ||
validatorsMint, | ||
precision: assetPrecision | ||
) ?? 0 | ||
|
||
let daysInYear = TimeInterval(CalculationPeriod.year.inDays) | ||
let erasInYear = daysInYear * TimeInterval.secondsInDay / eraDurationInSeconds | ||
|
||
let inflationPerMint = validatorsMintDecimal / totalIssuance | ||
|
||
return inflationPerMint * Decimal(erasInYear) | ||
} | ||
|
||
override func calculateEraReturn(from annualReturn: Decimal) -> Decimal { | ||
guard eraDurationInSeconds > 0 else { | ||
return 0 | ||
} | ||
|
||
let daysInYear = TimeInterval(CalculationPeriod.year.inDays) | ||
let erasInYear = daysInYear * TimeInterval.secondsInDay / eraDurationInSeconds | ||
|
||
guard erasInYear > 0 else { | ||
return 0 | ||
} | ||
|
||
let rawAnnualReturn = (annualReturn as NSDecimalNumber).doubleValue | ||
let result = pow(rawAnnualReturn + 1.0, 1.0 / Double(erasInYear)) - 1.0 | ||
|
||
return Decimal(result) | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...les/Staking/Services/RewardCalculatorService/RelayChain/PolkadotRewardParamsService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import Foundation | ||
import SubstrateSdk | ||
import BigInt | ||
import Operation_iOS | ||
|
||
enum PolkadotRewardParamsServiceError: Error { | ||
case runtimeApiNotFound | ||
} | ||
|
||
struct RuntimeApiInflationPrediction: Decodable, Equatable { | ||
struct NextMint: Decodable, Equatable { | ||
let items: [BigUInt] | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
|
||
items = try container.decode([StringScaleMapper<BigUInt>].self).map(\.value) | ||
} | ||
} | ||
|
||
@StringCodable var inflation: BigUInt | ||
let nextMint: NextMint | ||
} | ||
|
||
final class PolkadotRewardParamsService: BaseSyncService { | ||
let connection: JSONRPCEngine | ||
let runtimeCodingService: RuntimeCodingServiceProtocol | ||
let stateCallFactory: StateCallRequestFactoryProtocol | ||
let operationQueue: OperationQueue | ||
|
||
private var cancellableStore = CancellableCallStore() | ||
|
||
private var stateObserver = Observable<RuntimeApiInflationPrediction?>(state: nil) | ||
|
||
init( | ||
connection: JSONRPCEngine, | ||
runtimeCodingService: RuntimeCodingServiceProtocol, | ||
stateCallFactory: StateCallRequestFactoryProtocol, | ||
operationQueue: OperationQueue | ||
) { | ||
self.connection = connection | ||
self.runtimeCodingService = runtimeCodingService | ||
self.stateCallFactory = stateCallFactory | ||
self.operationQueue = operationQueue | ||
} | ||
|
||
override func performSyncUp() { | ||
let codingFactoryOperation = runtimeCodingService.fetchCoderFactoryOperation() | ||
|
||
let fetchWrapper = OperationCombiningService<RuntimeApiInflationPrediction>.compoundNonOptionalWrapper( | ||
operationManager: OperationManager(operationQueue: operationQueue) | ||
) { | ||
let codingFactory = try codingFactoryOperation.extractNoCancellableResultData() | ||
|
||
guard let runtimeApi = codingFactory.metadata.getRuntimeApiMethod( | ||
for: "Inflation", | ||
methodName: "experimental_inflation_prediction_info" | ||
) else { | ||
throw PolkadotRewardParamsServiceError.runtimeApiNotFound | ||
} | ||
|
||
return self.stateCallFactory.createWrapper( | ||
for: runtimeApi.callName, | ||
paramsClosure: nil, | ||
codingFactoryClosure: { codingFactory }, | ||
connection: self.connection, | ||
queryType: String(runtimeApi.method.output) | ||
) | ||
} | ||
|
||
fetchWrapper.addDependency(operations: [codingFactoryOperation]) | ||
|
||
let totalWrapper = fetchWrapper.insertingHead(operations: [codingFactoryOperation]) | ||
|
||
executeCancellable( | ||
wrapper: totalWrapper, | ||
inOperationQueue: operationQueue, | ||
backingCallIn: cancellableStore, | ||
runningCallbackIn: nil, | ||
mutex: mutex | ||
) { [weak self] result in | ||
switch result { | ||
case let .success(model): | ||
self?.completeImmediate(nil) | ||
self?.stateObserver.state = model | ||
case let .failure(error): | ||
self?.completeImmediate(error) | ||
} | ||
} | ||
} | ||
|
||
override func stopSyncUp() { | ||
cancellableStore.cancel() | ||
} | ||
} | ||
|
||
extension PolkadotRewardParamsService: RewardCalculatorParamsServiceProtocol { | ||
func subcribe( | ||
using notificationQueue: DispatchQueue, | ||
notificationClosure: @escaping (Result<RewardCalculatorParams, Error>) -> Void | ||
) { | ||
mutex.lock() | ||
|
||
stateObserver.addObserver(with: self, queue: notificationQueue) { _, newPrediction in | ||
guard let newPrediction else { | ||
return | ||
} | ||
|
||
notificationClosure(.success(.polkadot(inflationPrediction: newPrediction))) | ||
} | ||
|
||
mutex.unlock() | ||
|
||
setup() | ||
} | ||
|
||
func unsubscribe() { | ||
mutex.lock() | ||
|
||
stateObserver.removeObserver(by: self) | ||
|
||
mutex.unlock() | ||
|
||
throttle() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters