Skip to content

Commit 70aad60

Browse files
doc: add doc to extend RPC API and create custom transports (#64)
* doc: add doc to extend RPC API and create custom transports * doc: add error handling section
1 parent 9fae09d commit 70aad60

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,133 @@ const result = await client.invokeMethod({
3232
await client.revokeSession();
3333
```
3434

35+
## Extending RPC Types
36+
37+
The client's RPC requests are strongly typed, enforcing the RPC methods and params to be defined ahead of usage. The client supports extending
38+
the default RPC API with custom methods. This is useful when working with chains that have additional RPC methods beyond
39+
the standard ones.
40+
41+
### Define Custom RPC Types
42+
43+
```typescript
44+
import type { RpcMethod } from '@metamask/multichain-api-client';
45+
46+
// Define your custom RPC structure
47+
type MyCustomRpc = {
48+
mychain: {
49+
methods: {
50+
customMethod: RpcMethod<{ param1: string; param2: number }, { result: string }>;
51+
anotherMethod: RpcMethod<{ data: string }, boolean>;
52+
};
53+
events: ['customEvent'];
54+
};
55+
};
56+
```
57+
58+
### Use Extended RPC Types
59+
60+
```typescript
61+
import { getMultichainClient, getDefaultTransport } from '@metamask/multichain-api-client';
62+
63+
// Create a client with extended types
64+
const client = getMultichainClient({ transport: getDefaultTransport() })
65+
.extendsRpcApi<MyCustomRpc>();
66+
67+
// Now you can use your custom methods with full type safety
68+
const result = await client.invokeMethod({
69+
scope: 'mychain:123', // Your custom chain scope
70+
request: {
71+
method: 'customMethod',
72+
params: { param1: 'hello', param2: 42 }
73+
}
74+
});
75+
```
76+
77+
## Creating Custom Transports
78+
79+
Transports handle the communication layer between your application and the wallet. You can create custom transports for different environments or communication methods.
80+
81+
### Transport Interface
82+
83+
A transport must implement the following interface:
84+
85+
```typescript
86+
type Transport = {
87+
connect: () => Promise<void>;
88+
disconnect: () => Promise<void>;
89+
isConnected: () => boolean;
90+
request: <TRequest, TResponse>(request: TRequest) => Promise<TResponse>;
91+
onNotification: (callback: (data: unknown) => void) => () => void;
92+
};
93+
```
94+
95+
### Example: Custom Transport
96+
97+
```typescript
98+
import type { Transport, TransportRequest, TransportResponse } from '@metamask/multichain-api-client';
99+
100+
export function getCustomTransport(): Transport {
101+
return {
102+
connect: async () => { ... },
103+
disconnect: async () => { ... },
104+
isConnected: () => { ...},
105+
request: async <TRequest extends TransportRequest, TResponse extends TransportResponse>( request: TRequest ): Promise<TResponse> => { ... },
106+
onNotification: (callback: (data: unknown) => void) => { ... },
107+
};
108+
}
109+
110+
// Usage
111+
const transport = getCustomTransport();
112+
const client = getMultichainClient({ transport });
113+
```
114+
115+
## Error Handling
116+
117+
The client provides two main error types for handling different failure scenarios:
118+
119+
### TransportError
120+
121+
`TransportError` is thrown when there are issues with the transport layer communication, such as connection failures or the targeted browser extension not being installed.
122+
123+
```typescript
124+
import { TransportError } from '@metamask/multichain-api-client';
125+
126+
try {
127+
const client = getMultichainClient({ transport: getDefaultTransport() });
128+
await client.createSession({ optionalScopes: ['eip155:1'] });
129+
} catch (error) {
130+
if (error instanceof TransportError) {
131+
console.error('Transport error:', error.message);
132+
console.error('Original error:', error.cause);
133+
}
134+
}
135+
```
136+
137+
### MultichainApiError
138+
139+
`MultichainApiError` is thrown when the wallet returns an error response to API requests. This includes permission denials, invalid parameters, and other wallet-specific errors.
140+
141+
```typescript
142+
import { MultichainApiError } from '@metamask/multichain-api-client';
143+
144+
try {
145+
const result = await client.invokeMethod({
146+
scope: 'eip155:1',
147+
request: {
148+
method: 'eth_sendTransaction',
149+
params: { to: '0x1234...', value: '0x0' }
150+
}
151+
});
152+
} catch (error) {
153+
if (error instanceof MultichainApiError) {
154+
console.error('Multichain API error:', error.message);
155+
console.error('Error details:', error.cause);
156+
}
157+
}
158+
```
159+
160+
Both error types extend the standard `Error` class and may include the original error in the `cause` property for debugging purposes.
161+
35162
## API
36163

37164
See our documentation:

0 commit comments

Comments
 (0)