Constants Service

Getting constants is one of the tasks you would need to do if you want to ensure that you're working on a proper network, or to get some constant values to build something like crowdloan client around this.

Here is the service protocol:

public protocol SubstrateConstants: AnyObject {
    func find(
        moduleName: String,
        constantName: String
    ) async throws -> RuntimeModuleConstant?
    func fetch<T: Decodable>(
        moduleName: String,
        constantName: String
    ) async throws -> T?
    func fetch<T: Decodable>(_ type: T.Type, constant: RuntimeModuleConstant) throws -> T?

Basically, the first one is used when you want to actually find some runtime metadata constant reference, which is a proxy method to lookup in fact.

Second one is to fetch its value by telling client what type of data do you expect.

And the last one is to fetch constant value if you're aware of its reference, which you could obtain using first function.

So here are some examples:

let client: SubstrateClient

let constant: RuntimeModuleConstant? = try await client.constants.find(moduleName: "System", constantName: "ss58prefix")
let prefixByLookup: UInt16? = try await client.constants.fetch(moduleName: "System", constantName: "ss58prefix")
if let constant = constant {
    let prefixDirectly: UInt16? = try await client.constants.fetch(UInt16.self, constant: constant)

Last updated