Skip to content

Commit 3fffda9

Browse files
feat: EIP-5792 Wallet Call API support (#41)
Co-authored-by: Daniel Sinclair <d@niel.nyc>
1 parent fab58e9 commit 3fffda9

13 files changed

Lines changed: 1250 additions & 223 deletions

.changeset/batch-receipts.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@rainbow-me/provider": minor
3+
---
4+
5+
Add [EIP-5792 Wallet Call API](https://eips.ethereum.org/EIPS/eip-5792) support
6+
7+
- Add `wallet_getCapabilities`, `wallet_sendCalls`, `wallet_getCallsStatus`, and `wallet_showCallsStatus` RPC method handlers
8+
- Add `createProviderError` for typed pass-through errors from EIP-5792 callbacks
9+
- Export `BatchRecord`, `BatchRecordBase`, `PendingBatchRecord`, `FinalBatchRecord`, `SendCallsParams`, and `RequestCapability` types
10+
- **Breaking:** `handleProviderRequest` adds `getCapabilities`, `getBatchByKey`, `setBatch`, and `showCallsStatus` callback options and replaces `isSupportedChain` with `getSupportedChainIds`
11+
- Batch state and capability resolution are the caller's responsibility so the provider stays stateless

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Required for `yarn anvil` and `yarn test` (anvil forks mainnet)
2+
ETH_MAINNET_RPC=https://your-eth-mainnet-rpc-url

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@
3535
"jsdom": "24.0.0",
3636
"prettier": "3.2.4",
3737
"typescript": "5.3.3",
38+
"viem": "2.47.5",
3839
"vitest": "1.6.1"
3940
},
4041
"dependencies": {
4142
"@ethersproject/abstract-provider": "5.7.0",
4243
"@ethersproject/bignumber": "5.7.0",
4344
"@ethersproject/providers": "5.7.2",
4445
"@metamask/eth-sig-util": "7.0.1",
45-
"eventemitter3": "5.0.1",
46-
"viem": "1.21.4"
46+
"eventemitter3": "5.0.1"
4747
},
48-
"resolutions": {
49-
"vite": "5.1.7",
50-
"braces": "3.0.3"
48+
"peerDependencies": {
49+
"viem": "^2.40.3"
5150
},
5251
"packageManager": "yarn@4.2.2"
5352
}

src/error.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { RequestError } from './references/messengers';
2+
import { errorCodes } from './references/errorCodes';
3+
4+
export const buildError = ({
5+
id,
6+
message,
7+
errorCode,
8+
}: {
9+
id: number;
10+
errorCode: {
11+
code: number;
12+
name: string;
13+
};
14+
message?: string;
15+
}): { id: number; error: RequestError } => ({
16+
id,
17+
error: {
18+
name: errorCode.name,
19+
message,
20+
code: errorCode.code,
21+
},
22+
});
23+
24+
/** Error shape that callbacks can throw to pass through to the dapp (avoids INTERNAL_ERROR) */
25+
export const isPassThroughError = (
26+
err: unknown,
27+
): err is { code: number; name: string; message?: string } =>
28+
err !== null &&
29+
typeof err === 'object' &&
30+
typeof (err as { code?: unknown }).code === 'number' &&
31+
typeof (err as { name?: unknown }).name === 'string';
32+
33+
export const toPassThroughResponse = (
34+
id: number,
35+
err: { code: number; name: string; message?: string },
36+
) =>
37+
buildError({
38+
id,
39+
message: err.message,
40+
errorCode: { code: err.code, name: err.name },
41+
});
42+
43+
/** Creates an error that will be passed through when thrown from EIP-5792 callbacks. */
44+
export const createProviderError = (
45+
code: keyof typeof errorCodes,
46+
message?: string,
47+
) => {
48+
const errorCode = errorCodes[code];
49+
return Object.assign(new Error(message ?? errorCode.name), {
50+
code: errorCode.code,
51+
name: errorCode.name,
52+
});
53+
};

0 commit comments

Comments
 (0)