Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

feat: encode narrow RPC methods in beacon, propagate to messages #126

Merged
merged 1 commit into from
Jun 19, 2022

Conversation

harrysolovay
Copy link
Contributor

@harrysolovay harrysolovay commented Jun 19, 2022

Closes #108. This PR changes the RPC interface.

We must now create a beacon, the type of which is encoded with supported methods.

The following is a beacon that only supports the state_getMetadata method.

import * as rpc from "capi/rpc";

const beacon = rpc.beacon<"state_getMetadata">([URL]);

We can allow all methods by simply omitting the type arg.

const beacon = rpc.beacon([URL]); // supports all methods

Now we can create the client:

const client = await rpc.client(beacon);

We'll get a type error if we attempt to utilize methods that are NOT encoded in the beacon as accessible.

// Allow only the `state_getStorage` method
const client = await rpc.client(rpc.beacon<"state_getStorage">(polkadotBeacon));

// Ensure that the client was successfully opened
assert(!(client instanceof Error));

// Attempt to call a different method
const result = await client.call("state_getMetadata", []);
//                                ~~~~~~~~~~~~~~~~~
//                                ^
//                                Argument of type '"state_getMetadata"' is not assignable to parameter of type '"state_getStorage"'.
                                  

Ultimately, we'll want to package chain-specific beacons in known in order to protect developers from utilizing methods that don't exist.

@harrysolovay harrysolovay merged commit 24a1538 into main Jun 19, 2022
@harrysolovay harrysolovay deleted the chain-specific-rpc-types branch June 19, 2022 18:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Approach to Narrow RPC Typings / Differentiation
1 participant