From dc8d71fdf8ae4d22042fa8f73b9d1648b35b4ab0 Mon Sep 17 00:00:00 2001 From: Michael Bisgaard Olesen Date: Tue, 31 Oct 2023 13:40:36 +0100 Subject: [PATCH] wallet-connectors: Remove deprecated JSON-RPC client integration (#47) ## Purpose Remove support for the depreated JSON-RPC client as per [this strategy](https://concordium.atlassian.net/wiki/spaces/EN/pages/1185349645/Remove+V1+GRPC+API). ## Changes Remove all symbols related to `WalletConnection.getJsonRpcClient`. --------- Co-authored-by: Doris Benda --- packages/react-components/CHANGELOG.md | 4 ++ packages/react-components/README.md | 13 +--- .../src/useContractSelector.ts | 2 +- packages/wallet-connectors/CHANGELOG.md | 24 +++++++ .../wallet-connectors/src/BrowserWallet.ts | 6 +- .../wallet-connectors/src/WalletConnect.ts | 24 +------ .../wallet-connectors/src/WalletConnection.ts | 69 +------------------ packages/wallet-connectors/src/index.ts | 2 - 8 files changed, 37 insertions(+), 107 deletions(-) diff --git a/packages/react-components/CHANGELOG.md b/packages/react-components/CHANGELOG.md index f0eb96d..91935c6 100644 --- a/packages/react-components/CHANGELOG.md +++ b/packages/react-components/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Build as ES module to facilitate tree-shaking. +### Fixed + +- `useContractSelector`: Fix docstring of `rpc` parameter. + ## [0.3.0] - 2023-06-04 ### Added diff --git a/packages/react-components/README.md b/packages/react-components/README.md index dcb847e..b89f2e4 100644 --- a/packages/react-components/README.md +++ b/packages/react-components/README.md @@ -24,17 +24,10 @@ Initialize the network configuration and wrap the component `MyAppComponent` tha in `WithWalletConnector`: ```typescript jsx -import { Network, WalletConnectionProps, WithWalletConnector } from '@concordium/react-components'; - -const testnet: Network = { - name: 'testnet', - genesisHash: '4221332d34e1694168c2a0c0b3fd0f273809612cb13d000d5c2e00e85f50f796', - jsonRpcUrl: 'https://json-rpc.testnet.concordium.com', - ccdScanBaseUrl: 'https://testnet.ccdscan.io', -}; +import { Network, TESTNET, WalletConnectionProps, WithWalletConnector } from '@concordium/react-components'; function MyRootComponent() { - return {(props) => }; + return {(props) => }; } function MyAppComponent(props: WalletConnectionProps) { @@ -139,7 +132,7 @@ export function ContractStuff({ rpc }: Props) { ``` Use the hook [`useGrpcClient`](#usegrpcclient) below to obtain a `ConcordiumGRPCClient` instance. -See [the sample dApp](../../samples/contractupdate/src/App.tsx) for a complete example. +See [the sample dApp](../../samples/contractupdate/src/Root.tsx) for a complete example. ### [`useGrpcClient`](./src/useGrpcClient.ts) diff --git a/packages/react-components/src/useContractSelector.ts b/packages/react-components/src/useContractSelector.ts index 62493a4..ba2c765 100644 --- a/packages/react-components/src/useContractSelector.ts +++ b/packages/react-components/src/useContractSelector.ts @@ -101,7 +101,7 @@ export interface ContractSelector { /** * React hook to look up a smart contract's data and state from its index. - * @param rpc JSON-RPC proxy client through which to perform the lookup. + * @param rpc gRPC client through which to perform the lookup. The JSON-RPC client is supported for backwards compatibility only. * @param input The index of the contract to look up. * @return The resolved contract and related state. */ diff --git a/packages/wallet-connectors/CHANGELOG.md b/packages/wallet-connectors/CHANGELOG.md index 7c03a11..8892685 100644 --- a/packages/wallet-connectors/CHANGELOG.md +++ b/packages/wallet-connectors/CHANGELOG.md @@ -22,6 +22,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `WalletConnect`: Fix schema object format conversion in `signAndSendTransaction` in request payloads. - `WalletConnect`: Use standard string identifiers for transaction type in request payload. +### Removed + +- `WalletConnection`: The deprecated method `getJsonRpcClient()` and related symbols have been removed + in favor of the new gRPC client (`ConcordiumGRPCClient`) for the Node API v2. + Existing usage is migrated by simply constructing and using this client directly + instead of using the one previously fetched with `connection.getJsonRpcClient`. + Usage of `withJsonRpcClient`, i.e., + ```typescript + if (connection) { + const res = await withJsonRpcClient(connection, func); + ... + } + ``` + may be directly replaced by + ```typescript + if (grpcClient) { + const res = await func(grpcClient); + ... + } + ``` + The `Network` type has a field `grpcOpts` which may be used to construct the client (see its docstring for details). + The field is optional, but present on the predefined constants `TESTNET` and `MAINNET`. + For users of `@concordium/react-components`, this is all wrapped into the hook `useGrpcClient`. + ## [0.3.1] - 2023-06-04 ### Added diff --git a/packages/wallet-connectors/src/BrowserWallet.ts b/packages/wallet-connectors/src/BrowserWallet.ts index 6eed174..1167a64 100644 --- a/packages/wallet-connectors/src/BrowserWallet.ts +++ b/packages/wallet-connectors/src/BrowserWallet.ts @@ -86,10 +86,6 @@ export class BrowserWalletConnector implements WalletConnector, WalletConnection return this.client.getMostRecentlySelectedAccount(); } - getJsonRpcClient() { - return this.client.getJsonRpcClient(); - } - /** * Returns a gRPC client that is ready to perform requests against some Concordium Node connected to network/chain * that the connected account lives on. @@ -101,7 +97,7 @@ export class BrowserWalletConnector implements WalletConnector, WalletConnection * that is independent of any connection. * See {@link Network.grpcOpts} for more details. * - * Implementation detail: The method cannot be moved to {@link WalletConnector} + * Implementation detail: The method cannot be moved to {@link BrowserWalletConnector} * as the Browser Wallet's RPC client doesn't work until a connection has been established. * * @return The Browser Wallet's internal gRPC client. diff --git a/packages/wallet-connectors/src/WalletConnect.ts b/packages/wallet-connectors/src/WalletConnect.ts index ed1d28a..5fd4950 100644 --- a/packages/wallet-connectors/src/WalletConnect.ts +++ b/packages/wallet-connectors/src/WalletConnect.ts @@ -2,9 +2,7 @@ import { AccountTransactionPayload, AccountTransactionSignature, AccountTransactionType, - HttpProvider, InitContractPayload, - JsonRpcClient, UpdateContractPayload, getTransactionKindString, serializeInitContractParameters, @@ -206,18 +204,10 @@ export class WalletConnectConnection implements WalletConnection { session: SessionTypes.Struct; - readonly jsonRpcClient: JsonRpcClient | undefined; - - constructor( - connector: WalletConnectConnector, - chainId: string, - session: SessionTypes.Struct, - jsonRpcClient: JsonRpcClient | undefined - ) { + constructor(connector: WalletConnectConnector, chainId: string, session: SessionTypes.Struct) { this.connector = connector; this.chainId = chainId; this.session = session; - this.jsonRpcClient = jsonRpcClient; } getConnector() { @@ -238,13 +228,6 @@ export class WalletConnectConnection implements WalletConnection { return fullAddress.substring(fullAddress.lastIndexOf(':') + 1); } - getJsonRpcClient() { - if (!this.jsonRpcClient) { - throw new Error('no JSON RPC URL provided in network configuration'); - } - return this.jsonRpcClient; - } - async signAndSendTransaction( accountAddress: string, type: AccountTransactionType, @@ -393,7 +376,7 @@ export class WalletConnectConnector implements WalletConnector { } async connect() { - const { name, jsonRpcUrl } = this.network; + const { name } = this.network; const chainId = `${WALLET_CONNECT_SESSION_NAMESPACE}:${name}`; const session = await new Promise((resolve) => { @@ -403,8 +386,7 @@ export class WalletConnectConnector implements WalletConnector { // Connect was cancelled. return undefined; } - const jsonRpcClient = jsonRpcUrl ? new JsonRpcClient(new HttpProvider(jsonRpcUrl)) : undefined; - const connection = new WalletConnectConnection(this, chainId, session, jsonRpcClient); + const connection = new WalletConnectConnection(this, chainId, session); this.connections.set(session.topic, connection); this.delegate.onConnected(connection, connection.getConnectedAccount()); return connection; diff --git a/packages/wallet-connectors/src/WalletConnection.ts b/packages/wallet-connectors/src/WalletConnection.ts index 50b8c03..a0d9beb 100644 --- a/packages/wallet-connectors/src/WalletConnection.ts +++ b/packages/wallet-connectors/src/WalletConnection.ts @@ -1,12 +1,6 @@ import { Buffer } from 'buffer/'; import { SendTransactionPayload, SmartContractParameters } from '@concordium/browser-wallet-api-helpers'; -import { - AccountTransactionSignature, - AccountTransactionType, - JsonRpcClient, - SchemaVersion, - toBuffer, -} from '@concordium/web-sdk'; +import { AccountTransactionSignature, AccountTransactionType, SchemaVersion, toBuffer } from '@concordium/web-sdk'; import { GrpcWebOptions } from '@protobuf-ts/grpcweb-transport'; export type ModuleSchema = { @@ -150,32 +144,6 @@ export interface WalletConnection { */ ping(): Promise; - /** - * Returns a JSON-RPC client that is ready to perform requests against some Concordium Node connected to network/chain - * that the connected account lives on. - * - * This method is included because it's part of the Browser Wallet API. - * It should be used with care as it's hard to guarantee that it actually connects to the expected network. - * As explained in {@link Network.jsonRpcUrl}, the application may easily instantiate its own client and use that instead. - * - * Implementation detail: The method cannot be moved to {@link WalletConnector} - * as the Browser Wallet's internal client doesn't work until a connection has been established. - * - * The client implements version 1 of the Node's API which is deprecated in favor of - * ConcordiumGRPCClient from {@link https://www.npmjs.com/package/@concordium/web-sdk @concordium/web-sdk}. - * - * The method {@link BrowserWalletConnector.getGrpcClient} exists for exposing the wallet's internal gRPC Web client for API version 2, - * but it's not recommended for use in most cases as there's no need for the client to be associated with a particular connection. - * - * Instead, instantiate the client manually or using a hook (for React) as described in {@link Network.grpcOpts}. - * - * @return A JSON-RPC client for performing requests against a Concordium Node connected to the appropriate network. - * @throws If the connection uses {@link Network.jsonRpcUrl} and that value is undefined. - * @throws If the connection is to the Browser Wallet and the installed version doesn't support the method. - * @deprecated Use ConcordiumGRPCClient instead. - */ - getJsonRpcClient(): JsonRpcClient; - /** * Assembles a transaction and sends it off to the wallet for approval and submission. * @@ -255,30 +223,6 @@ export interface Network { */ grpcOpts: GrpcWebOptions | undefined; - /** - * The URL of a {@link https://github.com/Concordium/concordium-json-rpc Concordium JSON-RPC proxy} instance - * for performing API (v1) queries against a Concordium Node instance connected to this network. - * - * The value is currently used only for {@link WalletConnectConnection WalletConnect connections} - * as {@link BrowserWalletConnector Browser Wallet connections} use the Browser Wallet's internal client. - * - * Setting the URL to the empty string disables the automatic initialization of the client returned by - * {@link WalletConnection.getJsonRpcClient} for WalletConnect connections. - * The concrete implementation {@link WalletConnectConnection.getJsonRpcClient} will then throw an exception - * as there is no client instance to return. - * - * There is no fundamental difference between using a client belonging to a connection compared to one that is initialized directly. - * Keeping it separate from connections gives the dApp more control and allows the client to be used even if no connections have been established. - * - * Initializing a separate client is straightforward: - *
-     *   import { HttpProvider, JsonRpcClient } from '@concordium/web-sdk';
-     *   ...
-     *   const client = new JsonRpcClient(new HttpProvider(network.jsonRpcUrl));
-     * 
- */ - jsonRpcUrl: string; - /** * The base URL of a {@link https://github.com/Concordium/concordium-scan CCDScan} instance * connected to this network. @@ -359,14 +303,3 @@ export interface WalletConnector { */ disconnect(): Promise; } - -/** - * Convenience function for invoking an async function with the JSON-RPC proxy client of the given {@link WalletConnection}. - * - * @param connection The connected used to resolve the RPC client. - * @param f The async function to invoke. - * @return The promise returned by {@link f}. - */ -export async function withJsonRpcClient(connection: WalletConnection, f: (c: JsonRpcClient) => Promise) { - return f(connection.getJsonRpcClient()); -} diff --git a/packages/wallet-connectors/src/index.ts b/packages/wallet-connectors/src/index.ts index c93dd98..afbaf31 100644 --- a/packages/wallet-connectors/src/index.ts +++ b/packages/wallet-connectors/src/index.ts @@ -23,7 +23,6 @@ export const TESTNET: Network = { grpcOpts: { baseUrl: 'https://grpc.testnet.concordium.com:20000', }, - jsonRpcUrl: 'https://json-rpc.testnet.concordium.com', ccdScanBaseUrl: 'https://testnet.ccdscan.io', }; @@ -36,6 +35,5 @@ export const MAINNET: Network = { grpcOpts: { baseUrl: 'https://grpc.mainnet.concordium.software:20000', }, - jsonRpcUrl: 'https://json-rpc.mainnet.concordium.software', ccdScanBaseUrl: 'https://ccdscan.io', };