From d05fde03da7950ca7db1bafa5ac2d32aaaf82afe Mon Sep 17 00:00:00 2001 From: Egor Komarov Date: Wed, 3 Apr 2024 14:59:49 +0200 Subject: [PATCH 1/3] feat: networks api --- src/api.ts | 46 +++++++++++++++++++++++++++++++++ src/index.ts | 36 ++++++++++++++++++++++++++ src/models.ts | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/src/api.ts b/src/api.ts index 3d4db725..ea561345 100644 --- a/src/api.ts +++ b/src/api.ts @@ -17,6 +17,7 @@ import { FunctionCall, TokensObject, DelayedMessage, + Network, } from './models'; import { UniqueArray, Address } from './utils'; @@ -1632,6 +1633,51 @@ export type ProviderApi = { message: DelayedMessage; }; }; + + /** + * Get a list of available networks. + * + * --- + * Required permissions: `basic` + */ + getAvailableNetworks: { + output: Network[]; + }; + + /** + * Request user to add a new network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + addNetwork: { + input: { + /** + * Network info + */ + network: Omit; + /** + * Whether to switch to the added network (false by default) + */ + switchNetwork?: boolean; + }; + output: Network | null; + }; + + /** + * Request user to change selected network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + changeNetwork: { + input: { + networkId: number; + }; + output: Network | null; + }; }; /** diff --git a/src/index.ts b/src/index.ts index 491982d3..253549e8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ import { parsePartialTokensObject, parseTransaction, serializeTokensObject, + Network, } from './models'; import { Address, DelayedTransactions, getUniqueId } from './utils'; import * as subscriber from './stream'; @@ -1040,6 +1041,41 @@ export class ProviderRpcClient { }; } + /** + * Get a list of available networks. + * + * --- + * Required permissions: `basic` + */ + public async getAvailableNetworks(): Promise { + await this.ensureInitialized(); + return await this._api.getAvailableNetworks(); + } + + /** + * Request user to add a new network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + public async addNetwork(args: ProviderApiRequestParams<'addNetwork'>): Promise { + await this.ensureInitialized(); + return await this._api.addNetwork(args); + } + + /** + * Request user to change selected network. + * Shows an approval window to the user. + * + * --- + * Required permissions: `basic` + */ + public async changeNetwork(args: ProviderApiRequestParams<'changeNetwork'>): Promise { + await this.ensureInitialized(); + return await this._api.changeNetwork(args); + } + private _registerEventHandlers(provider: Provider) { const knownEvents: { [K in ProviderEvent]: (data: RawProviderEventData) => ProviderEventData } = { connected: data => data, diff --git a/src/models.ts b/src/models.ts index 0fa3cb50..656e3a46 100644 --- a/src/models.ts +++ b/src/models.ts @@ -312,6 +312,76 @@ export type EncryptedData = { nonce: string; }; +/* Network stuff */ + +/** + * @category Models + */ +export type NetworkDescription = { + globalId: number; + capabilities: string; + signatureId: number | undefined; +}; + +/** + * @category Models + */ +export type NetworkConfig = { + symbol?: string; + explorerBaseUrl?: string; + tokensManifestUrl?: string; +}; + +/** + * @category Models + */ +export type GqlSocketParams = { + /** + * Path to graphql api endpoints + */ + endpoints: string[] + /** + * Frequency of sync latency detection + */ + latencyDetectionInterval: number + /** + * Maximum value for the endpoint's blockchain data sync latency + */ + maxLatency: number + /** + * Gql node type + */ + local: boolean +}; + +/** + * @category Models + */ +export type JrpcSocketParams = { + /** + * Path to jrpc api endpoint + */ + endpoint: string +}; + +/** + * @category Models + */ +export type ProtoSocketParams = JrpcSocketParams & {}; + +/** + * @category Models + */ +export type Network = { + name: string; + config: NetworkConfig; + description: NetworkDescription; +} & ( + | { type: 'graphql', data: GqlSocketParams } + | { type: 'jrpc', data: JrpcSocketParams } + | { type: 'proto', data: ProtoSocketParams } +); + /* ABI stuff */ /** From a5a45b8eb4669cf0f9d71ccb1b6bf6a3f1632019 Mon Sep 17 00:00:00 2001 From: Egor Komarov Date: Mon, 8 Apr 2024 12:36:36 +0200 Subject: [PATCH 2/3] fix: review --- src/api.ts | 11 ++++++++--- src/index.ts | 6 ++++-- src/models.ts | 23 +++++++++++++++++------ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/api.ts b/src/api.ts index ea561345..3cefe1bb 100644 --- a/src/api.ts +++ b/src/api.ts @@ -18,6 +18,7 @@ import { TokensObject, DelayedMessage, Network, + AddNetwork, } from './models'; import { UniqueArray, Address } from './utils'; @@ -1656,13 +1657,15 @@ export type ProviderApi = { /** * Network info */ - network: Omit; + network: AddNetwork; /** * Whether to switch to the added network (false by default) */ switchNetwork?: boolean; }; - output: Network | null; + output: { + network: Network | null + }; }; /** @@ -1676,7 +1679,9 @@ export type ProviderApi = { input: { networkId: number; }; - output: Network | null; + output: { + network: Network | null; + }; }; }; diff --git a/src/index.ts b/src/index.ts index 253549e8..3eb1a850 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1059,7 +1059,7 @@ export class ProviderRpcClient { * --- * Required permissions: `basic` */ - public async addNetwork(args: ProviderApiRequestParams<'addNetwork'>): Promise { + public async addNetwork(args: ProviderApiRequestParams<'addNetwork'>): Promise> { await this.ensureInitialized(); return await this._api.addNetwork(args); } @@ -1071,7 +1071,9 @@ export class ProviderRpcClient { * --- * Required permissions: `basic` */ - public async changeNetwork(args: ProviderApiRequestParams<'changeNetwork'>): Promise { + public async changeNetwork( + args: ProviderApiRequestParams<'changeNetwork'>, + ): Promise> { await this.ensureInitialized(); return await this._api.changeNetwork(args); } diff --git a/src/models.ts b/src/models.ts index 656e3a46..a32df03d 100644 --- a/src/models.ts +++ b/src/models.ts @@ -369,18 +369,29 @@ export type JrpcSocketParams = { */ export type ProtoSocketParams = JrpcSocketParams & {}; +export type GqlConnection = { type: 'graphql', data: GqlSocketParams }; +export type JrpcConnection = { type: 'jrpc', data: JrpcSocketParams }; +export type ProtoConnection = { type: 'proto', data: ProtoSocketParams }; + /** * @category Models */ export type Network = { name: string; - config: NetworkConfig; description: NetworkDescription; -} & ( - | { type: 'graphql', data: GqlSocketParams } - | { type: 'jrpc', data: JrpcSocketParams } - | { type: 'proto', data: ProtoSocketParams } -); + connection: GqlConnection | JrpcConnection | ProtoConnection | any; + config: NetworkConfig; +}; + +/** + * @category Models + */ +export type AddNetwork = { + name: string; + networkId: number; + connection: GqlConnection | JrpcConnection | ProtoConnection | any; + config?: NetworkConfig; +}; /* ABI stuff */ From cfa94b0b8292599ff253f0a0ff7150bae1b2b8fa Mon Sep 17 00:00:00 2001 From: Egor Komarov Date: Tue, 9 Apr 2024 17:44:50 +0200 Subject: [PATCH 3/3] fix: remove `getAvailableNetworks` method --- src/api.ts | 10 ---------- src/index.ts | 11 ----------- 2 files changed, 21 deletions(-) diff --git a/src/api.ts b/src/api.ts index 3cefe1bb..e32b461d 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1635,16 +1635,6 @@ export type ProviderApi = { }; }; - /** - * Get a list of available networks. - * - * --- - * Required permissions: `basic` - */ - getAvailableNetworks: { - output: Network[]; - }; - /** * Request user to add a new network. * Shows an approval window to the user. diff --git a/src/index.ts b/src/index.ts index 3eb1a850..8661a4ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1041,17 +1041,6 @@ export class ProviderRpcClient { }; } - /** - * Get a list of available networks. - * - * --- - * Required permissions: `basic` - */ - public async getAvailableNetworks(): Promise { - await this.ensureInitialized(); - return await this._api.getAvailableNetworks(); - } - /** * Request user to add a new network. * Shows an approval window to the user.