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 interface:

interface SubstrateConstants {
    fun find(moduleName: String, constantName: String): Flow<RuntimeModuleConstant?>
    fun <T: Any> fetch(moduleName: String, constantName: String, type: KClass<T>): Flow<T?>
    fun <T: Any> fetch(constant: RuntimeModuleConstant, type: KClass<T>): 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:

val client: SubstrateClient

// this one should be not null
val constant: RuntimeModuleConstant? = client.constants.find("System", "ss58prefix")

val prefixByLookup: UInt16? = client.constants.fetch("System", "ss58prefix", UInt16::class)
val prefixDirectly: UInt16? = constant?.let { client.constants.fetch(it, UInt16::class) }
// prefixByLookup == prefixDirectly

Last updated