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

Commit

Permalink
feat: simplify local substrate process management (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Jun 27, 2022
1 parent fdf9a56 commit afdb686
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 177 deletions.
16 changes: 9 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ const myChain = C.chain(myBeacon);
During development, connecting to a live network can slow down the feedback loop. It also may be infeasible, in the case that you are without an internet connection. In these situations, you can utilize Capi's test utilities.

```diff
+ const node = await C.test.node();
+
- const chain = C.chain(myBeacon);
+ const chain = C.test.chain();
+ const chain = C.test.chain(node);

//

+ node.close()
```

Under the hood, Capi will spin up a tiny, temporary chain. You can even access accounts (and their corresponding signers).
Here, we've spun up a tiny, temporary chain. You can even access accounts (and their corresponding signers).

```ts
const chain = C.test.chain();

const { alice } = chain.address;
```

Expand All @@ -126,8 +130,6 @@ For convenience, we'll be utilizing the test chain and addresses.
### Read a Balance

```ts
const chain = C.test.chain();

const alicePublicKey = chain.address.alice.asPublicKeyBytes();

const value = await chain
Expand Down Expand Up @@ -179,7 +181,7 @@ if (result instanceof Error) {
```ts
import * as C from "../mod.ts";

const chain = C.test.chain();
// ...

const { alice, bob } = chain.address;

Expand Down
5 changes: 3 additions & 2 deletions examples/balance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as C from "../mod.ts";

const chain = C.test.chain();
const node = await C.test.node();
const chain = C.test.chain(node);
const alice = await chain.address.alice.asPublicKeyBytes();
const result = await chain
.pallet("System")
.entry("Account", alice)
.read();

console.log({ result });
node.close();
31 changes: 0 additions & 31 deletions examples/playground.ts
Original file line number Diff line number Diff line change
@@ -1,31 +0,0 @@
import { assert } from "../_deps/asserts.ts";
import { Hashers, Sr25519 } from "../bindings/mod.ts";
import * as M from "../frame_metadata/mod.ts";
import * as C from "../mod.ts";
import * as U from "../util/mod.ts";

const client = await C.test.rpcClient();
assert(!(client instanceof Error));

const metadataRaw = await client.call("state_getMetadata", []);
assert(metadataRaw.result);

const metadata = M.fromPrefixedHex(metadataRaw.result);
const lookup = new M.Lookup(metadata);
const pallet = lookup.getPalletByName("System");
const storageEntry = lookup.getStorageEntryByPalletAndName(pallet, "Account");
const deriveCodec = M.DeriveCodec(metadata);
const [sr25519, hashers] = await Promise.all([Sr25519(), Hashers()]);
const $storageMapKey = M.$storageMapKey({
deriveCodec,
hashers,
pallet,
storageEntry,
});
const alice = sr25519.TestUser.fromName("alice");
const key = U.hex.encode($storageMapKey.encode(alice.publicKey)) as U.HexString;
const storageRaw = await client.call("state_getStorage", [key]);
console.log(deriveCodec(storageEntry.value).decode(U.hex.decode(storageRaw.result!)));

// // console.log({ result });
await client.close();
5 changes: 4 additions & 1 deletion examples/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as C from "../mod.ts";

const chain = C.test.chain();
const node = await C.test.node();
const chain = C.test.chain(node);

const { alice, bob } = chain.address;

Expand All @@ -17,3 +18,5 @@ const result = chain
for await (const event of result) {
console.log({ event });
}

node.close();
5 changes: 5 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export * from "./core/mod.ts";
export * from "./known/mod.ts";
export * from "./primitives/mod.ts";
export * as test from "./test-util/mod.ts";
export {
type SubstrateProcess,
substrateProcess,
type SubstrateProcessConfig,
} from "./util/mod.ts";
1 change: 0 additions & 1 deletion rpc/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./Base.ts";
export * from "./messages.ts";
export * from "./providers/detect.ts";
export * from "./providers/local.ts";
export * from "./providers/proxy.ts";
export * from "./providers/smoldot.ts";
9 changes: 2 additions & 7 deletions rpc/providers/detect.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { unreachable } from "../../_deps/asserts.ts";
import { AnyMethods } from "../../util/mod.ts";
import { LocalBeacon, localClient } from "./local.ts";
import { ProxyBeacon, ProxyClient, proxyClient } from "./proxy.ts";
import { SmoldotBeacon, SmoldotClient, smoldotClient } from "./smoldot.ts";

export type StdBeacon<M extends AnyMethods> = ProxyBeacon<M> | SmoldotBeacon<M> | LocalBeacon<M>;
export type StdBeacon<M extends AnyMethods> = ProxyBeacon<M> | SmoldotBeacon<M>;
export type StdClient<M extends AnyMethods> = ProxyClient<M> | SmoldotClient<M>;

export function detectClient<M extends AnyMethods>(
beacon: ProxyBeacon<M> | SmoldotBeacon<M> | LocalBeacon<M>,
) {
export function detectClient<M extends AnyMethods>(beacon: ProxyBeacon<M> | SmoldotBeacon<M>) {
if (beacon instanceof ProxyBeacon) {
return proxyClient(beacon);
} else if (beacon instanceof SmoldotBeacon) {
return smoldotClient(beacon);
} else if (beacon instanceof LocalBeacon) {
return localClient(beacon);
} else {
unreachable();
}
Expand Down
105 changes: 0 additions & 105 deletions rpc/providers/local.ts

This file was deleted.

16 changes: 5 additions & 11 deletions test-util/Chain.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import { Beacon } from "../Beacon.ts";
import { Chain } from "../core/Chain.ts";
import { KnownRpcMethods } from "../known/mod.ts";
import { LocalBeacon, LocalClientProps } from "../rpc/mod.ts";
import { ProxyBeacon } from "../rpc/mod.ts";
import { SubstrateProcess } from "../util/mod.ts";
import { TestAddresses } from "./Addresses.ts";

export class TestChain extends Chain<Beacon<LocalClientProps, KnownRpcMethods>> {
export class TestChain extends Chain<ProxyBeacon<KnownRpcMethods>> {
override address: TestAddresses<this> = new TestAddresses(this);
}

export function chain() {
return new TestChain(
new LocalBeacon({
path: "./node-template",
cwd: new URL(".", import.meta.url).pathname,
dev: true,
}),
);
export function chain(process: SubstrateProcess) {
return new TestChain(new ProxyBeacon(process.url));
}
2 changes: 1 addition & 1 deletion test-util/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./Chain.ts";
export * from "./rpcClient.ts";
export * from "./node.ts";
12 changes: 12 additions & 0 deletions test-util/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { assert } from "../_deps/asserts.ts";
import { SubstrateProcess, substrateProcess } from "../util/mod.ts";

export async function node(): Promise<SubstrateProcess> {
const process = await substrateProcess({
path: "./node-template",
cwd: new URL(".", import.meta.url).pathname,
dev: true,
});
assert(!(process instanceof Error));
return process;
}
11 changes: 0 additions & 11 deletions test-util/rpcClient.ts

This file was deleted.

1 change: 1 addition & 0 deletions util/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./discovery_value_validation.ts";
export * from "./env.ts";
export * from "./ErrorCtor.ts";
export * as hex from "./hex.ts";
export * from "./substrateProcess.ts";
export * from "./types.ts";
Loading

0 comments on commit afdb686

Please sign in to comment.