Skip to content

Commit

Permalink
Merge pull request #17 from Odrin/feat/networks
Browse files Browse the repository at this point in the history
feat: networks api
  • Loading branch information
Rexagon authored Jun 5, 2024
2 parents c2023ba + cfa94b0 commit 1937c05
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
FunctionCall,
TokensObject,
DelayedMessage,
Network,
AddNetwork,
} from './models';

import { UniqueArray, Address } from './utils';
Expand Down Expand Up @@ -1632,6 +1634,45 @@ export type ProviderApi<Addr = Address> = {
message: DelayedMessage<Addr>;
};
};

/**
* Request user to add a new network.
* Shows an approval window to the user.
*
* ---
* Required permissions: `basic`
*/
addNetwork: {
input: {
/**
* Network info
*/
network: AddNetwork;
/**
* Whether to switch to the added network (false by default)
*/
switchNetwork?: boolean;
};
output: {
network: Network | null
};
};

/**
* Request user to change selected network.
* Shows an approval window to the user.
*
* ---
* Required permissions: `basic`
*/
changeNetwork: {
input: {
networkId: number;
};
output: {
network: Network | null;
};
};
};

/**
Expand Down
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
parsePartialTokensObject,
parseTransaction,
serializeTokensObject,
Network,
} from './models';
import { Address, DelayedTransactions, getUniqueId } from './utils';
import * as subscriber from './stream';
Expand Down Expand Up @@ -1040,6 +1041,32 @@ export class ProviderRpcClient {
};
}

/**
* Request user to add a new network.
* Shows an approval window to the user.
*
* ---
* Required permissions: `basic`
*/
public async addNetwork(args: ProviderApiRequestParams<'addNetwork'>): Promise<ProviderApiResponse<'addNetwork'>> {
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<ProviderApiResponse<'changeNetwork'>> {
await this.ensureInitialized();
return await this._api.changeNetwork(args);
}

private _registerEventHandlers(provider: Provider) {
const knownEvents: { [K in ProviderEvent]: (data: RawProviderEventData<K>) => ProviderEventData<K> } = {
connected: data => data,
Expand Down
81 changes: 81 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,87 @@ 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 & {};

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;
description: NetworkDescription;
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 */

/**
Expand Down

0 comments on commit 1937c05

Please sign in to comment.