-
Notifications
You must be signed in to change notification settings - Fork 20
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 #893 from novasamatech/fix/support-treasury-batch
Support batch call when extracting requested amount for referendum 86933jup3
- Loading branch information
Showing
11 changed files
with
313 additions
and
57 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
24 changes: 24 additions & 0 deletions
24
novawallet/Common/Substrate/Calls/UtilityPallet/Utility+Calls.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,24 @@ | ||
import Foundation | ||
import SubstrateSdk | ||
|
||
extension UtilityPallet { | ||
static var batchPath: CallCodingPath { | ||
CallCodingPath(moduleName: name, callName: "batch") | ||
} | ||
|
||
static var batchAllPath: CallCodingPath { | ||
CallCodingPath(moduleName: name, callName: "batch_all") | ||
} | ||
|
||
static var forceBatchPath: CallCodingPath { | ||
CallCodingPath(moduleName: name, callName: "force_batch") | ||
} | ||
|
||
static func isBatch(path: CallCodingPath) -> Bool { | ||
[batchPath, batchAllPath, forceBatchPath].contains(path) | ||
} | ||
|
||
struct Call: Codable { | ||
let calls: [RuntimeCall<JSON>] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
novawallet/Common/Substrate/Types/UtilityPallet/UtilityPallet.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,5 @@ | ||
import Foundation | ||
|
||
enum UtilityPallet { | ||
static let name = "Utility" | ||
} |
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
50 changes: 50 additions & 0 deletions
50
novawallet/Modules/Vote/Governance/Operation/Action/Parser/GovSpentAmountBatchHandler.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,50 @@ | ||
import Foundation | ||
import SubstrateSdk | ||
import RobinHood | ||
|
||
extension GovSpentAmount { | ||
final class BatchHandler { | ||
private func handleInternal( | ||
call: RuntimeCall<JSON>, | ||
handlers: [GovSpentAmountHandling], | ||
context: GovSpentAmount.Context | ||
) throws -> [CompoundOperationWrapper<ReferendumActionLocal.AmountSpendDetails?>]? { | ||
for handler in handlers { | ||
if | ||
let wrappers = try handler.handle( | ||
call: call, | ||
internalHandlers: handlers, | ||
context: context | ||
) { | ||
return wrappers | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
} | ||
|
||
extension GovSpentAmount.BatchHandler: GovSpentAmountHandling { | ||
func handle( | ||
call: RuntimeCall<JSON>, | ||
internalHandlers: [GovSpentAmountHandling], | ||
context: GovSpentAmount.Context | ||
) throws -> [CompoundOperationWrapper<ReferendumActionLocal.AmountSpendDetails?>]? { | ||
let path = CallCodingPath(moduleName: call.moduleName, callName: call.callName) | ||
|
||
guard UtilityPallet.isBatch(path: path) else { | ||
return nil | ||
} | ||
|
||
let runtimeContext = context.codingFactory.createRuntimeJsonContext() | ||
|
||
let calls = try call.args.map(to: UtilityPallet.Call.self, with: runtimeContext.toRawContext()).calls | ||
|
||
let wrappers = try calls.flatMap { call in | ||
try handleInternal(call: call, handlers: internalHandlers, context: context) ?? [] | ||
} | ||
|
||
return wrappers | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
novawallet/Modules/Vote/Governance/Operation/Action/Parser/GovSpentAmountExtractor.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,56 @@ | ||
import Foundation | ||
import SubstrateSdk | ||
import RobinHood | ||
|
||
protocol GovSpentAmountHandling { | ||
func handle( | ||
call: RuntimeCall<JSON>, | ||
internalHandlers: [GovSpentAmountHandling], | ||
context: GovSpentAmount.Context | ||
) throws -> [CompoundOperationWrapper<ReferendumActionLocal.AmountSpendDetails?>]? | ||
} | ||
|
||
enum GovSpentAmount { | ||
struct Context { | ||
let codingFactory: RuntimeCoderFactoryProtocol | ||
let connection: JSONRPCEngine | ||
let requestFactory: StorageRequestFactoryProtocol | ||
} | ||
|
||
final class Extractor { | ||
let handlers: [GovSpentAmountHandling] | ||
|
||
init(handlers: [GovSpentAmountHandling]) { | ||
self.handlers = handlers | ||
} | ||
|
||
func createExtractionWrappers( | ||
from call: RuntimeCall<JSON>, | ||
context: GovSpentAmount.Context | ||
) throws -> [CompoundOperationWrapper<ReferendumActionLocal.AmountSpendDetails?>]? { | ||
for handler in handlers { | ||
if let wrappers = try handler.handle( | ||
call: call, | ||
internalHandlers: handlers, | ||
context: context | ||
) { | ||
return wrappers | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
} | ||
|
||
extension GovSpentAmount.Extractor { | ||
static var defaultExtractor: GovSpentAmount.Extractor { | ||
.init( | ||
handlers: [ | ||
GovSpentAmount.BatchHandler(), | ||
GovSpentAmount.TreasurySpentHandler(), | ||
GovSpentAmount.TreasuryApproveHandler() | ||
] | ||
) | ||
} | ||
} |
Oops, something went wrong.