From fb49474ac96163f819f3bc647b599eb7ccdd60b6 Mon Sep 17 00:00:00 2001 From: Daniel Rocha Date: Fri, 7 Jun 2024 17:44:50 +0200 Subject: [PATCH 1/2] feat: add `getAccountBalances` to `KeyringClient` (#340) --- src/KeyringClient.test.ts | 58 +++++++++++++++++++++++++++++++++++++++ src/KeyringClient.ts | 16 +++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/KeyringClient.test.ts b/src/KeyringClient.test.ts index 62ba2621b..c5d237b74 100644 --- a/src/KeyringClient.test.ts +++ b/src/KeyringClient.test.ts @@ -3,6 +3,7 @@ import { type KeyringRequest, type KeyringResponse, KeyringClient, + KeyringRpcMethod, } from '.'; // Import from `index.ts` to test the public API describe('KeyringClient', () => { @@ -84,6 +85,63 @@ describe('KeyringClient', () => { }); }); + describe('getAccountBalances', () => { + it('returns a valid response', async () => { + const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0']; + const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7'; + const expectedResponse = { + [assets[0] as string]: { + amount: '1234', + unit: 'sat', + }, + }; + + mockSender.send.mockResolvedValue(expectedResponse); + const balances = await keyring.getAccountBalances(id, assets); + + expect(mockSender.send).toHaveBeenCalledWith({ + jsonrpc: '2.0', + id: expect.any(String), + method: `${KeyringRpcMethod.GetAccountBalances}`, + params: { id, assets }, + }); + + expect(balances).toStrictEqual(expectedResponse); + }); + + it('throws an error because the amount has the wrong type', async () => { + const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0']; + const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7'; + const expectedResponse = { + [assets[0] as string]: { + amount: 1234, // Should be a `StringNumber` + unit: 'sat', + }, + }; + + mockSender.send.mockResolvedValue(expectedResponse); + await expect(keyring.getAccountBalances(id, assets)).rejects.toThrow( + 'At path: bip122:000000000019d6689c085ae165831e93/slip44:0.amount -- Expected a value of type `StringNumber`, but received: `1234`', + ); + }); + + it("throws an error because the amount isn't a StringNumber", async () => { + const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0']; + const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7'; + const expectedResponse = { + [assets[0] as string]: { + amount: 'not-a-string-number', // Should be a `StringNumber` + unit: 'sat', + }, + }; + + mockSender.send.mockResolvedValue(expectedResponse); + await expect(keyring.getAccountBalances(id, assets)).rejects.toThrow( + 'At path: bip122:000000000019d6689c085ae165831e93/slip44:0.amount -- Expected a value of type `StringNumber`, but received: `"not-a-string-number"`', + ); + }); + }); + describe('filterAccountChains', () => { it('should send a request to filter the chains supported by an account and return the response', async () => { const id = '49116980-0712-4fa5-b045-e4294f1d440e'; diff --git a/src/KeyringClient.ts b/src/KeyringClient.ts index 0f4f1923f..9b6b16c5d 100644 --- a/src/KeyringClient.ts +++ b/src/KeyringClient.ts @@ -8,6 +8,8 @@ import type { KeyringRequest, KeyringAccountData, KeyringResponse, + CaipAssetType, + Balance, } from './api'; import { ApproveRequestResponseStruct, @@ -15,6 +17,7 @@ import { DeleteAccountResponseStruct, ExportAccountResponseStruct, FilterAccountChainsResponseStruct, + GetAccountBalancesResponseStruct, GetAccountResponseStruct, GetRequestResponseStruct, ListAccountsResponseStruct, @@ -76,6 +79,19 @@ export class KeyringClient implements Keyring { ); } + async getAccountBalances( + id: string, + assets: CaipAssetType[], + ): Promise> { + return strictMask( + await this.#send({ + method: KeyringRpcMethod.GetAccountBalances, + params: { id, assets }, + }), + GetAccountBalancesResponseStruct, + ); + } + async createAccount( options: Record = {}, ): Promise { From f718c41697f588c73d4bab68a8524fd5ff66d984 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 7 Jun 2024 17:49:28 +0200 Subject: [PATCH 2/2] chore(deps): bump @metamask/snaps-sdk from 4.4.1 to 4.4.2 (#339) Bumps [@metamask/snaps-sdk](https://github.com/MetaMask/snaps) from 4.4.1 to 4.4.2. - [Release notes](https://github.com/MetaMask/snaps/releases) - [Commits](https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@4.4.1...@metamask/snaps-sdk@4.4.2) --- updated-dependencies: - dependency-name: "@metamask/snaps-sdk" dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 649a23dc7..787d5e2a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1155,8 +1155,8 @@ __metadata: linkType: hard "@metamask/snaps-sdk@npm:^4.2.0": - version: 4.4.1 - resolution: "@metamask/snaps-sdk@npm:4.4.1" + version: 4.4.2 + resolution: "@metamask/snaps-sdk@npm:4.4.2" dependencies: "@metamask/key-tree": ^9.1.1 "@metamask/providers": ^17.0.0 @@ -1164,7 +1164,7 @@ __metadata: "@metamask/utils": ^8.3.0 fast-xml-parser: ^4.3.4 superstruct: ^1.0.3 - checksum: 29dfc36821e77d033ddc1b8f1b8924b4880aca41a25e1767741b50659990a79d3026f3975613090342e98d0cf8d876a0e003edb23ff39d2927dc6473d5c441f9 + checksum: 2ff3949cee3b6c5a580304a02191f3ec7fb049460c2ff89b1731f24b215baf5f9c08834a0b2b703ff43e3b74ede387386e22a96810b50be106bb029b180c44ce languageName: node linkType: hard