7

I'm off-chain indexing some pallet events into my off-chain local storage using an OCW. Now, I want to be able to retrieve this data via RPC from an external service. Is this possible? Does it require the OCW to access the local-storage as part of the RPC query?

Do Custom RPC work for the off-chain runtime? https://docs.substrate.io/v3/runtime/custom-rpcs/

Any example will be appreciate it. TIA, Cheers!

NB: I'm not after an Oracle, I need the opposite case.

1 Answer 1

2

Yes, Custom RPC work for the off-chain runtime. Custom Rpc Doc For example, you can define custom rpc in the following way:

#[rpc]
public trait SomeOcsApi {
  
   #[rpc(name="some_method")]
   fn some_method(params...) {
      ...
   }
}

And, in the implementation, just read the OCS storage

struct SomeOcsApiImpl<T: OffchainStorage> {
   storage: Arc<<RwLock<T>>>,
};
impl SomeOcsApi for SomeOcsApiImpl {
   value = self
            .storage
            .read()
            .get(
                sp_offchain::STORAGE_PREFIX,
                &*derive_storage_key(key.as_bytes(), &did),
            )
            .map(from_utf8)
            .unwrap_or_default();

   Ok(value)
}

Surely, in your OC indexer should write event to this storage also, share this reference with OC indexer.

5
  • Hi @ironman, thanks for your reply. There is a missing step in you answer that I could not figure it out yet, which is: How to extend the RPC into the rpc.rs file. There is a lot of doc, about this when you are trying to reach out the runtime storage, but in this case I want the offchain storage, something like the following snip: // Add a silly RPC that returns constant values io.extend_with(SomeOcsApi::to_delegate(SomeOcsApiImpl<T> ?????? ))); TIA Mar 28, 2022 at 4:43
  • 2
    @PabloBuitrago as my answer says: 1. annotate your rpc trait with #[rpc] 2. annotate your rpc method with #[rpc] 3. impl the rpc trait, with the Off-Chain Storage as a field. 4. in your node/rpc.rs, config the delegate to your defined rpc. For step 1-3, you can take our rpc as reference; For step4, you cant take our node rpc as reference. Mar 29, 2022 at 10:36
  • Thanks @ironman. Congrats! or Parami. Looks promising. Mar 29, 2022 at 15:02
  • 1
    A common suggestion is when you encounter how-to-use problem, alway go to substrate/Polkadot/Acala/Parami's repository, search a example. Mar 30, 2022 at 2:48
  • Great suggestion! Once more, thank you @ironman. Mar 30, 2022 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.