|
| 1 | +### SubstrateConstantsService example |
| 2 | + |
| 3 | +Bellow is provided an example of how `SubstrateConstantsService` can be used to get runtime module |
| 4 | +constant and/or it's value bytes decoded to a specified generic type `T` conforming to `Codable`. |
| 5 | + |
| 6 | +## Initialization |
| 7 | + |
| 8 | +First of all, we need to create the client. `SubstrateClient`'s initializer takes two parameters. |
| 9 | +The first one is the required `URL`, and the second one is settings. It helps to define how the |
| 10 | +data should be fetched and how it should be handled after that and on which dispatch queue |
| 11 | +the results should be returned. It has a static func `default()` |
| 12 | +which provides the settings with a default configuration. In this case the main |
| 13 | +thread is used. There is another method called `default(clientQueue:)` where a custom |
| 14 | +queue can be created. The settings parameter in`SubstrateClient`'s |
| 15 | +initializer is predefined and set to default. |
| 16 | + |
| 17 | +Here is how one could create a client: |
| 18 | + |
| 19 | +```Swift |
| 20 | +guard let url = URL(string: network.endpointInfo.url) else { return nil } |
| 21 | +let client = SubstrateClient(url: url) |
| 22 | +``` |
| 23 | +or |
| 24 | + |
| 25 | +```Swift |
| 26 | +let client = SubstrateClient(url: url, settings: SubstrateClientSettings.default(clientQueue: DispatchQueue(label: "Some queue")) |
| 27 | +``` |
| 28 | + |
| 29 | +The client has a property called `module` from where we can get access to an interface for getting |
| 30 | +`RuntimeMetadata` and fetching `StorageItems`. |
| 31 | + |
| 32 | +Next we need to get `SubstrateConstantsService`. To do that we can call |
| 33 | +`constantsService(completion:)` method on the client. It will return a ready service to be used: |
| 34 | + |
| 35 | +```Swift |
| 36 | +client.constantsService { constantService in |
| 37 | + // Do something with the service |
| 38 | +} |
| 39 | +``` |
| 40 | +Before taking a look how to use the service, it's worth mentioning that runtime |
| 41 | +module constant is defined by the following object: |
| 42 | +```Swift |
| 43 | +public class RuntimeModuleConstant: Codable { |
| 44 | + let name: String |
| 45 | + let type: BigUInt |
| 46 | + let valueBytes: [UInt8] |
| 47 | + let docs: [String] |
| 48 | + |
| 49 | + init(name: String, type: BigUInt, valueBytes: [UInt8], docs: [String]) { |
| 50 | + self.name = name |
| 51 | + self.type = type |
| 52 | + self.valueBytes = valueBytes |
| 53 | + self.docs = docs |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | +It's `valueBytes` property is decoded into a specified generic `T` type. |
| 58 | + |
| 59 | +## Usage |
| 60 | + |
| 61 | +Now, when we have all the required components, we can start getting the constants. To do that |
| 62 | +we should call `fetch(moduleName:constantName:completion:)` method on `SubstrateConstantsService`. |
| 63 | +This method finds a runtime module constant by the constant's name in a specified module |
| 64 | +and in its completion it returns the module's value bytes decoded into a specified type. |
| 65 | + |
| 66 | +```Swift |
| 67 | +try constantService.fetch( |
| 68 | + moduleName: constant.module, |
| 69 | + constantName: constant.constant |
| 70 | + ) { (storageItem: T?) in |
| 71 | + // Do something with the storage item |
| 72 | + } |
| 73 | +``` |
| 74 | + |
| 75 | +Also, it's possible to get `RuntimeModuleConstant` object itself, to access |
| 76 | +it's other properties. |
| 77 | + |
| 78 | +```Swift |
| 79 | +try constantService.find( |
| 80 | + moduleName: constant.module, |
| 81 | + constantName: constant.constant |
| 82 | + ) { runtimeModuleConstant in |
| 83 | + // Do something with the runtime module constant |
| 84 | + } |
| 85 | +``` |
| 86 | + |
| 87 | +After that, the module constant can be used to get a decoded object of a |
| 88 | +generic type `T` from it's `valueBytes` property. |
| 89 | + |
| 90 | +```Swift |
| 91 | +let fetchedValue = try service.fetch(T.self, constant: runtimeModuleConstant) |
| 92 | +``` |
0 commit comments