Skip to content

Commit

Permalink
chore(web connector): rename network getter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Nov 4, 2024
1 parent fdb1786 commit 5842fa3
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 60 deletions.
16 changes: 8 additions & 8 deletions apps/docs/src/components/test-zkapp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const TestZkApp = () => {
});
const [results, setResults] = useObjectState({
mina_accounts: "",
mina_chainId: "",
mina_networkId: "",
mina_getBalance: "",
mina_sign: "",
mina_signFields: "",
Expand All @@ -45,12 +45,12 @@ export const TestZkApp = () => {
});
setResults(() => ({ mina_accounts: JSON.stringify(result) }));
};
const fetchChainId = async () => {
const fetchNetworkId = async () => {
if (!provider) return;
const { result } = await provider.request({
method: "mina_chainId",
method: "mina_networkId",
});
setResults(() => ({ mina_chainId: result }));
setResults(() => ({ mina_networkId: result }));
};
const fetchBalance = async () => {
if (!provider) return;
Expand Down Expand Up @@ -201,18 +201,18 @@ export const TestZkApp = () => {
Request Accounts
</button>
</div>
<label>mina_chainId</label>
<label>mina_networkId</label>
<div className="flex justify-between items-center gap-4">
<input
value={results.mina_chainId}
value={results.mina_networkId}
className="input input-bordered flex-1"
/>
<button
type="button"
className="btn btn-primary"
onClick={fetchChainId}
onClick={fetchNetworkId}
>
Get Chain ID
Get Network ID
</button>
</div>
<label>mina_getBalance</label>
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/src/pages/connect/wallet-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ const client = createWalletClient({ account, network: "devnet" });
const transactionCount = await client.getTransactionCount();
```

### Get chain ID
### Get network ID

Query the chain ID of the network.
Query the network ID of the network.

```ts twoslash
import { toAccount } from "@mina-js/accounts";
Expand All @@ -121,7 +121,7 @@ import { createWalletClient } from '@mina-js/connect'
const account = toAccount('B62qmWKtvNQTtUqo1LxfEEDLyWMg59cp6U7c4uDC7aqgaCEijSc3Hx5');
const client = createWalletClient({ account, network: "devnet" });

const chainId = await client.getChainId();
const networkId = await client.getNetworkId();
```

## Wallet commands
Expand Down
12 changes: 6 additions & 6 deletions apps/docs/src/pages/connect/wallet-interface.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_requestAccounts'>({ method: 'mina_requestAccounts' })
```

### mina_chainId
### mina_networkId

Get the chain ID. It's a string that represents the current network. Values are `mina:mainnet` or `mina:testnet`.
Get the network ID. It's a string that represents the current network. Values are `mina:mainnet` or `mina:testnet`.

```ts twoslash
import { createStore } from '@mina-js/connect'

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_chainId'>({ method: 'mina_chainId' })
const { result } = await provider.request<'mina_networkId'>({ method: 'mina_networkId' })
```

### mina_getBalance
Expand All @@ -52,16 +52,16 @@ const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_getBalance'>({ method: 'mina_getBalance' })
```

### mina_chainInformation
### mina_requestNetwork

Get chain information. Similar to `mina_chainId`, but more detailed. It returns current network's RPC url, name, and slug (chain ID).
Get network information. Similar to `mina_networkId`, but more detailed. It returns current network's RPC url, name, and slug (network ID).

```ts twoslash
import { createStore } from '@mina-js/connect'

const store = createStore()
const { provider } = store.getProviders()[0]
const { result } = await provider.request<'mina_chainInformation'>({ method: 'mina_chainInformation' })
const { result } = await provider.request<'mina_requestNetwork'>({ method: 'mina_requestNetwork' })
```

### mina_getState
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/pages/klesia/rpc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ Not supported on Zeko.

---

### mina_chainId
### mina_networkId

Returns the currently configured chain ID.
Returns the currently configured network ID.

---

Expand Down
4 changes: 2 additions & 2 deletions apps/klesia/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ describe("Mina Devnet RPC", () => {
expect(result.length).toBeGreaterThan(0);
});

it("returns result for mina_chainId", async () => {
it("returns result for mina_networkId", async () => {
const response = await request({
json: { method: "mina_chainId" },
json: { method: "mina_networkId" },
});
const { result } = (await response.json()) as { result: string };
expect(result.length).toBeGreaterThan(0);
Expand Down
4 changes: 2 additions & 2 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
const result = await mina.blockHash();
return json(buildResponse({ result }), 200);
})
.with({ method: KlesiaRpcMethod.enum.mina_chainId }, async () => {
const result = await mina.chainId();
.with({ method: KlesiaRpcMethod.enum.mina_networkId }, async () => {
const result = await mina.networkId();
return json(buildResponse({ result }), 200);
})
.with(
Expand Down
4 changes: 2 additions & 2 deletions apps/klesia/src/methods/mina.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ it("should return block hash", async () => {
expect(result.length).toBeGreaterThan(0);
});

it("should return chain id", async () => {
const result = await mina.chainId();
it("should return network id", async () => {
const result = await mina.networkId();
expect(result.length).toBeGreaterThan(0);
});

Expand Down
10 changes: 4 additions & 6 deletions apps/klesia/src/methods/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,17 @@ const blockHash = async () => {
return data.daemonStatus.stateHash;
};

const chainId = async () => {
const networkId = async () => {
const client = getNodeClient();
const { data } = await client.query(
gql`
query {
daemonStatus {
chainId
}
networkID
}
`,
{},
);
return data.daemonStatus.chainId;
return data.networkID;
};

const sendTransaction = async ({
Expand Down Expand Up @@ -177,7 +175,7 @@ export const mina = {
getTransactionCount,
getBalance,
blockHash,
chainId,
networkId,
sendTransaction,
getAccount,
};
8 changes: 4 additions & 4 deletions packages/connect/src/__snapshots__/client.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`matches snapshot of local source 1`] = `
"createNullifier": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getNetworkId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
Expand All @@ -19,7 +19,7 @@ exports[`matches snapshot of json-rpc source 1`] = `
"createNullifier": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getNetworkId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
Expand All @@ -33,7 +33,7 @@ exports[`matches snapshot of local source 2`] = `
"createNullifier": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getNetworkId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
Expand All @@ -47,7 +47,7 @@ exports[`matches snapshot of json-rpc source 2`] = `
"createNullifier": [Function: AsyncFunction],
"getAccounts": [Function: AsyncFunction],
"getBalance": [Function: AsyncFunction],
"getChainId": [Function: AsyncFunction],
"getNetworkId": [Function: AsyncFunction],
"getTransactionCount": [Function: AsyncFunction],
"prepareTransactionRequest": [Function: AsyncFunction],
"signFields": [Function: AsyncFunction],
Expand Down
6 changes: 3 additions & 3 deletions packages/connect/src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe("json-rpc source", () => {
expect(BigInt(transactionCount)).toBeGreaterThanOrEqual(0);
});

it("returns chain id", async () => {
it("returns network id", async () => {
const account = toAccount(PUBLIC_KEY);
const client = createWalletClient({
account,
network: "devnet",
providerSource: "klesia",
});
const chainId = await client.getChainId();
expect(chainId.length).toBeGreaterThan(0);
const networkId = await client.getNetworkId();
expect(networkId.length).toBeGreaterThan(0);
});
});

Expand Down
12 changes: 6 additions & 6 deletions packages/connect/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ export const createWalletClient = ({
});
return result;
};
const getChainId = async () => {
const getNetworkId = async () => {
return match(providerSource)
.with("klesia", async () => {
const result = await klesiaClient.request<"mina_chainId">({
method: "mina_chainId",
const result = await klesiaClient.request<"mina_networkId">({
method: "mina_networkId",
});
return result;
})
.otherwise(async () => {
const provider = getWalletProvider(providerSource);
const { result } = await provider.request<"mina_chainId">({
method: "mina_chainId",
const { result } = await provider.request<"mina_networkId">({
method: "mina_networkId",
});
return result;
});
Expand Down Expand Up @@ -136,7 +136,7 @@ export const createWalletClient = ({
getAccounts,
getBalance,
getTransactionCount,
getChainId,
getNetworkId,
signTransaction,
signMessage,
signFields,
Expand Down
26 changes: 13 additions & 13 deletions packages/providers/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { z } from "zod";

export const SwitchChainRequestParams = z
.object({
chainId: z.string(),
networkId: z.string(),
})
.strict();

Expand All @@ -40,11 +40,11 @@ export const AccountsRequestParamsSchema = RequestWithContext.extend({
export const RequestAccountsRequestParamsSchema = RequestWithContext.extend({
method: z.literal("mina_requestAccounts"),
}).strict();
export const ChainIdRequestParamsSchema = RequestWithContext.extend({
method: z.literal("mina_chainId"),
export const NetworkIdRequestParamsSchema = RequestWithContext.extend({
method: z.literal("mina_networkId"),
}).strict();
export const ChainInformationRequestParamsSchema = RequestWithContext.extend({
method: z.literal("mina_chainInformation"),
method: z.literal("mina_requestNetwork"),
}).strict();
export const GetBalanceRequestParamsSchema = RequestWithContext.extend({
method: z.literal("mina_getBalance"),
Expand Down Expand Up @@ -99,15 +99,15 @@ export const RequestAccountsRequestReturnSchema = z
result: z.array(PublicKeySchema),
})
.strict();
export const ChainIdRequestReturnSchema = z
export const NetworkIdRequestReturnSchema = z
.object({
method: z.literal("mina_chainId"),
method: z.literal("mina_networkId"),
result: NetworkId,
})
.strict();
export const ChainInformationRequestReturnSchema = z
.object({
method: z.literal("mina_chainInformation"),
method: z.literal("mina_requestNetwork"),
result: AddChainRequestParams,
})
.strict();
Expand Down Expand Up @@ -175,7 +175,7 @@ export const GetStateRequestReturnSchema = z
export const RpcReturnTypesUnion = z.discriminatedUnion("method", [
AccountsRequestReturnSchema,
RequestAccountsRequestReturnSchema,
ChainIdRequestReturnSchema,
NetworkIdRequestReturnSchema,
ChainInformationRequestReturnSchema,
GetBalanceRequestReturnSchema,
SignRequestReturnSchema,
Expand All @@ -192,7 +192,7 @@ export const RpcReturnTypesUnion = z.discriminatedUnion("method", [
export const ProviderRequestParamsUnion = z.discriminatedUnion("method", [
AccountsRequestParamsSchema,
RequestAccountsRequestParamsSchema,
ChainIdRequestParamsSchema,
NetworkIdRequestParamsSchema,
ChainInformationRequestParamsSchema,
GetBalanceRequestParamsSchema,
SignRequestParamsSchema,
Expand All @@ -211,23 +211,23 @@ export type ResultType<M extends string> = {
result: Extract<RpcReturnTypesUnionType, { method: M }>["result"];
};

export const ChainIdCallbackSchema = z
export const NetworkIdCallbackSchema = z
.function()
.args(z.object({ chainId: z.string() }))
.args(z.object({ networkId: z.string() }))
.returns(z.void());

// TODO: Add missing deconstruction types to listeners
export const ConnectedListenerSchema = z
.function()
.args(z.literal("connected"), ChainIdCallbackSchema)
.args(z.literal("connected"), NetworkIdCallbackSchema)
.returns(z.void());
export const DisconnectedListenerSchema = z
.function()
.args(z.literal("disconnected"), z.function())
.returns(z.void());
export const ChainChangedListenerSchema = z
.function()
.args(z.literal("chainChanged"), ChainIdCallbackSchema)
.args(z.literal("chainChanged"), NetworkIdCallbackSchema)
.returns(z.void());
export const AccountsChangedListenerSchema = z
.function()
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const KlesiaRpcMethod = z.enum([
"mina_getTransactionCount",
"mina_getBalance",
"mina_blockHash",
"mina_chainId",
"mina_networkId",
"mina_sendTransaction",
"mina_getAccount",
]);
Expand All @@ -180,7 +180,7 @@ export const KlesiaRpcMethodSchema = z.discriminatedUnion("method", [
params: EmptyParamsSchema,
}),
z.object({
method: z.literal(KlesiaRpcMethod.enum.mina_chainId),
method: z.literal(KlesiaRpcMethod.enum.mina_networkId),
params: EmptyParamsSchema,
}),
z.object({
Expand Down Expand Up @@ -223,7 +223,7 @@ export const KlesiaRpcResponseSchema = z.union([
result: z.string(),
}),
JsonRpcResponse.extend({
method: z.literal(KlesiaRpcMethod.enum.mina_chainId),
method: z.literal(KlesiaRpcMethod.enum.mina_networkId),
result: z.string(),
}),
JsonRpcResponse.extend({
Expand Down

0 comments on commit 5842fa3

Please sign in to comment.