Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add updateEthereumChain, switchEthereumChain, getOwnedAssets, watchAsset #4

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

krzysu
Copy link
Contributor

@krzysu krzysu commented Oct 22, 2024

No description provided.

@krzysu krzysu force-pushed the feat-example-app branch 2 times, most recently from 0e0fa84 to 338a5d4 Compare October 22, 2024 13:34
@krzysu krzysu force-pushed the feat-more-methods-1 branch 3 times, most recently from 3456746 to 2764ecf Compare October 22, 2024 13:55
@krzysu krzysu marked this pull request as ready for review October 22, 2024 13:56
@krzysu krzysu requested a review from jdevcs October 22, 2024 13:56
src/WalletRpcPlugin.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@jdevcs jdevcs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krzysu
Copy link
Contributor Author

krzysu commented Oct 23, 2024

also changes required in readme and package.json like:

https://github.com/web3/web3-wallet-rpc-utils/blob/a342b7bc3fd16a85509369c2b30f5a984c1e663d/package.json#L2

Thanks

that is already covered in my very first PR https://github.com/web3/web3-wallet-rpc-utils/pull/2/files#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519

There are 3 PRs so far, each building on top of the other, as I try to move fast but also to keep the review scope small.

src/utils.ts Outdated
export function parseToGetOwnedAssetsResult(
result: unknown[]
): GetOwnedAssetsResult {
return result as GetOwnedAssetsResult;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a reminder for now to parse data returned by wallet to the expected format, even if not returned by wallet in the expected format.

* @param param - See {@link AddEthereumChainRequest}
* @returns a Promise that resolves if the request is successful
*/
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about removing Ethereum from the function names?
The usage would look like:

web3.walletRpc.addChain();
web3.walletRpc.switchChain();
web3.walletRpc.updateChain();

etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see arguments for both approaches, but I think it's best to maintain consistency with the RPC method names.

Copy link
Contributor

@danforbes danforbes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several comments/questions, but nothing blocking

README.md Outdated
Comment on lines 9 to 11
- [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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before. It would be nice to mention these aren't yet implemented.

});
```

#### watchAsset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the PR title to mention that this method is implemented?

* @param param - See {@link AddEthereumChainRequest}
* @returns a Promise that resolves if the request is successful
*/
public async addEthereumChain(param: AddEthereumChainRequest): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see arguments for both approaches, but I think it's best to maintain consistency with the RPC method names.

Comment on lines 12 to 18
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;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minimal doc comments on these functions would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually still don't understand what is this for, I copied this approach from some rpc plugin example. It is used as Web3PluginBase<WalletRpcApi> but if I remove it, or change the names/types it doesn't affect anything in my plugin code. maybe @avkos @luu-alex @jdevcs could share some more. Anyway, it's internal use only.

src/WalletRpcPlugin.ts Outdated Show resolved Hide resolved
src/WalletRpcPlugin.ts Outdated Show resolved Hide resolved
* @param param - See {@link UpdateEthereumChainRequest}
* @returns a Promise that resolves if the request is successful
*/
public async updateEthereumChain(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EIP for this method is stagnant and the method is not implemented by MetaMask. Are we sure we want to add this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will test all wallet rpc methods with a few different wallets and see what is actually implemented.

Comment on lines +64 to +67
/**
* Array of asset interface identifiers such as ['ERC20', 'ERC721'].
*/
types?: string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some of these less straightforward optional parameters, it may be nice to document what the behavior is when the parameter is omitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, right now I don't know what happens and it's up to each wallet to decide how they handle that. I want to test it with a few popular wallets and see if there is any patter there. We can also introduce our own error types.

src/types.ts Outdated
chainId: Numbers;
/**
* Asset interface ERC identifier, e.g., ERC20.
* Optional - EIP-1820 could be used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean, exactly, that "EIP-1820 could be used"? If this property is omitted, how should the user interpret it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I copied it from the EIP spec, I have no idea what does it mean :) I will remove it.

Comment on lines +25 to +29
it("should return correct result", async () => {
const result = await web3.walletRpc.addEthereumChain({ chainId: 5000 });

expect(result).toBeUndefined();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know yet how to test actual wallet responses as each wallet can return whatever they want. I'm thinking to add some response parsers to always return the same type, despite wallet response, but I need to test it on real examples first (with real browser extension wallets). This test is not testing much yet, I agree.

Base automatically changed from feat-example-app to main October 24, 2024 12:55
@krzysu krzysu changed the title feat: add updateEthereumChain, switchEthereumChain and getOwnedAssets feat: add updateEthereumChain, switchEthereumChain, getOwnedAssets, watchAsset Oct 24, 2024
@krzysu krzysu merged commit 44b6672 into main Oct 24, 2024
3 checks passed
@krzysu krzysu deleted the feat-more-methods-1 branch October 24, 2024 13:42
krzysu added a commit that referenced this pull request Oct 24, 2024
This was referenced Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants