Skip to content

Commit

Permalink
feat: add more methods and types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Urbas authored and krzysu committed Oct 24, 2024
1 parent 338a5d4 commit b19b187
Show file tree
Hide file tree
Showing 12 changed files with 590 additions and 58 deletions.
83 changes: 74 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

This Web3.js plugin adds support for the following wallet-related RPC methods:

- wallet_addEthereumChain (EIP-3085)
- wallet_updateEthereumChain (EIP-2015)
- wallet_switchEthereumChain (EIP-3326)
- wallet_getOwnedAssets (EIP-2256)
- wallet_watchAsset (EIP-747)
- wallet_requestPermissions (EIP-2255)
- wallet_getPermissions (EIP-2255)
- [wallet_addEthereumChain (EIP-3085)](https://eips.ethereum.org/EIPS/eip-3085)
- [wallet_updateEthereumChain (EIP-2015)](https://eips.ethereum.org/EIPS/eip-2015)
- [wallet_switchEthereumChain (EIP-3326)](https://eips.ethereum.org/EIPS/eip-3326)
- [wallet_getOwnedAssets (EIP-2256)](https://eips.ethereum.org/EIPS/eip-2256)
- [wallet_watchAsset (EIP-747)](https://eips.ethereum.org/EIPS/eip-747)
- [wallet_requestPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255)
- [wallet_getPermissions (EIP-2255)](https://eips.ethereum.org/EIPS/eip-2255)

## Installation

Expand All @@ -31,17 +31,82 @@ pnpm add web3-plugin-wallet-rpc
### Register plugin

```typescript
import { Web3 } from "web3";
import { WalletRpcPlugin } from "web3-plugin-wallet-rpc";
web3 = new Web3(/* provider here */);

const web3 = new Web3("https://eth.llamarpc.com");
web3.registerPlugin(new WalletRpcPlugin());
```

### Methods

#### addEthereumChain

Invokes the `wallet_addEthereumChain` method as defined in [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085).

```typescript
await web3.walletRpc.addEthereumChain({
chainId: 5000,
blockExplorerUrls: ["https://mantlescan.xyz"],
chainName: "Mantle",
iconUrls: ["https://icons.llamao.fi/icons/chains/rsz_mantle.jpg"],
nativeCurrency: {
name: "Mantle",
symbol: "MNT",
decimals: 18,
},
rpcUrls: ["https://rpc.mantle.xyz"],
});
```

#### updateEthereumChain

Invokes the `wallet_updateEthereumChain` method as defined in [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015).

```typescript
await web3.walletRpc.updateEthereumChain({
chainId: 5000,
blockExplorerUrls: ["https://mantlescan.xyz"],
chainName: "Mantle",
nativeCurrency: {
name: "Mantle",
symbol: "MNT",
decimals: 18,
},
rpcUrls: ["https://rpc.mantle.xyz"],
});
```

#### switchEthereumChain

Invokes the `wallet_switchEthereumChain` method as defined in [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326).

```typescript
await web3.walletRpc.switchEthereumChain({ chainId: 5000 });
```

#### getOwnedAssets

Invokes the `wallet_getOwnedAssets` method as defined in [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256).

```typescript
const ownedAssets = await web3.walletRpc.getOwnedAssets({
address: "0xa5653e88D9c352387deDdC79bcf99f0ada62e9c6",
});
```

#### watchAsset

Invokes the `wallet_watchAsset` method as defined in [EIP-747](https://eips.ethereum.org/EIPS/eip-747).

```typescript
await web3.walletRpc.addEthereumChain({ chainId: "0x1388" }); // chainId 5000 is Mantle Mainnet
await web3.walletRpc.watchAsset({
type: "ERC20",
options: {
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
symbol: "USDC",
},
});
```

## Contributing
Expand Down
152 changes: 152 additions & 0 deletions src/WalletRpcPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { Web3PluginBase, utils, validator } from "web3";
import {
AddEthereumChainRequest,
GetOwnedAssetsRequest,
GetOwnedAssetsResult,
SwitchEthereumChainRequest,
UpdateEthereumChainRequest,
WatchAssetRequest,
} from "./types";
import { parseToGetOwnedAssetsResult } from "./utils";

type WalletRpcApi = {
wallet_addEthereumChain: (param: AddEthereumChainRequest) => void;
wallet_updateEthereumChain: (param: UpdateEthereumChainRequest) => void;
wallet_switchEthereumChain: (param: SwitchEthereumChainRequest) => void;
wallet_getOwnedAssets: (param: GetOwnedAssetsRequest) => GetOwnedAssetsResult;
wallet_watchAsset: (param: WatchAssetRequest) => boolean;
};

/**
* This Web3.js plugin adds support for various wallet-related RPC methods.
*
* @example
* Initialize the plugin
*
* ```typescript
* import { Web3 } from "web3";
* import { WalletRpcPlugin } from "web3-plugin-wallet-rpc";
*
* const web3 = new Web3("https://eth.llamarpc.com");
* web3.registerPlugin(new WalletRpcPlugin());
* ```
*/
export class WalletRpcPlugin extends Web3PluginBase<WalletRpcApi> {
public pluginNamespace = "walletRpc";

public constructor() {
super();
}

/**
* Request to add a new chain to the user's wallet.
*
* See [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085) for more details.
*
* @param param - Details of the chain to add
* @returns a Promise that resolves if the request is successful
*/
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> {
return this.requestManager.send({
method: "wallet_addEthereumChain",
params: [
{
...param,
chainId: utils.toHex(param.chainId),
},
],
});
}

/**
* Switch to a new chain and register it with the user’s wallet if it isn’t already recognized.
*
* See [EIP-2015](https://eips.ethereum.org/EIPS/eip-2015) for more details.
*
* @param param - Details of the chain to switch to and possibly add
* @returns a Promise that resolves if the request is successful
*/
public async updateEthereumChain(
param: UpdateEthereumChainRequest
): Promise<void> {
return this.requestManager.send({
method: "wallet_updateEthereumChain",
params: [
{
...param,
chainId: utils.toHex(param.chainId),
},
],
});
}

/**
* Switch the wallet’s currently active chain.
*
* See [EIP-3326](https://eips.ethereum.org/EIPS/eip-3326) for more details.
*
* @param param - See {@link SwitchEthereumChainRequest}
* @returns a Promise that resolves if the request is successful
*/
public async switchEthereumChain(
param: SwitchEthereumChainRequest
): Promise<void> {
return this.requestManager.send({
method: "wallet_switchEthereumChain",
params: [
{
...param,
chainId: utils.toHex(param.chainId),
},
],
});
}

/**
* Return a list of owned assets for the given address.
*
* See [EIP-2256](https://eips.ethereum.org/EIPS/eip-2256) for more details.
*
* @param param - Details of the request for owned assets
* @returns a Promise that resolves to a list of owned assets, see {@link GetOwnedAssetsResult}
*/
public async getOwnedAssets(
param: GetOwnedAssetsRequest
): Promise<GetOwnedAssetsResult> {
validator.validator.validate(["address"], [param.address]);

const trueParam = { ...param };
if (trueParam.options?.chainId) {
trueParam.options.chainId = utils.toHex(trueParam.options.chainId);
}

const result = await this.requestManager.send({
method: "wallet_getOwnedAssets",
params: [trueParam],
});

return parseToGetOwnedAssetsResult(result);
}

/**
* Add an asset to the user's wallet.
*
* See [EIP-747](https://eips.ethereum.org/EIPS/eip-747) for more details.
*
* @param param - Details of the asset to watch
* @returns a Promise that resolves to `true` if the request is successful
*/
public async watchAsset(param: WatchAssetRequest): Promise<boolean> {
return this.requestManager.send({
method: "wallet_watchAsset",
params: [param],
});
}
}

// Module Augmentation
declare module "web3" {
interface Web3Context {
walletRpc: WalletRpcPlugin;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./plugin";
export * from "./WalletRpcPlugin";
export * from "./types";
33 changes: 0 additions & 33 deletions src/plugin.ts

This file was deleted.

Loading

0 comments on commit b19b187

Please sign in to comment.