Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 09c011a

Browse files
committed
Merge branch 'main' into replace-superstruct-with-fork
2 parents 438ef9b + f718c41 commit 09c011a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/KeyringClient.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type KeyringRequest,
44
type KeyringResponse,
55
KeyringClient,
6+
KeyringRpcMethod,
67
} from '.'; // Import from `index.ts` to test the public API
78

89
describe('KeyringClient', () => {
@@ -84,6 +85,63 @@ describe('KeyringClient', () => {
8485
});
8586
});
8687

88+
describe('getAccountBalances', () => {
89+
it('returns a valid response', async () => {
90+
const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0'];
91+
const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7';
92+
const expectedResponse = {
93+
[assets[0] as string]: {
94+
amount: '1234',
95+
unit: 'sat',
96+
},
97+
};
98+
99+
mockSender.send.mockResolvedValue(expectedResponse);
100+
const balances = await keyring.getAccountBalances(id, assets);
101+
102+
expect(mockSender.send).toHaveBeenCalledWith({
103+
jsonrpc: '2.0',
104+
id: expect.any(String),
105+
method: `${KeyringRpcMethod.GetAccountBalances}`,
106+
params: { id, assets },
107+
});
108+
109+
expect(balances).toStrictEqual(expectedResponse);
110+
});
111+
112+
it('throws an error because the amount has the wrong type', async () => {
113+
const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0'];
114+
const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7';
115+
const expectedResponse = {
116+
[assets[0] as string]: {
117+
amount: 1234, // Should be a `StringNumber`
118+
unit: 'sat',
119+
},
120+
};
121+
122+
mockSender.send.mockResolvedValue(expectedResponse);
123+
await expect(keyring.getAccountBalances(id, assets)).rejects.toThrow(
124+
'At path: bip122:000000000019d6689c085ae165831e93/slip44:0.amount -- Expected a value of type `StringNumber`, but received: `1234`',
125+
);
126+
});
127+
128+
it("throws an error because the amount isn't a StringNumber", async () => {
129+
const assets = ['bip122:000000000019d6689c085ae165831e93/slip44:0'];
130+
const id = '1617ea08-d4b6-48bf-ba83-901ef1e45ed7';
131+
const expectedResponse = {
132+
[assets[0] as string]: {
133+
amount: 'not-a-string-number', // Should be a `StringNumber`
134+
unit: 'sat',
135+
},
136+
};
137+
138+
mockSender.send.mockResolvedValue(expectedResponse);
139+
await expect(keyring.getAccountBalances(id, assets)).rejects.toThrow(
140+
'At path: bip122:000000000019d6689c085ae165831e93/slip44:0.amount -- Expected a value of type `StringNumber`, but received: `"not-a-string-number"`',
141+
);
142+
});
143+
});
144+
87145
describe('filterAccountChains', () => {
88146
it('should send a request to filter the chains supported by an account and return the response', async () => {
89147
const id = '49116980-0712-4fa5-b045-e4294f1d440e';

src/KeyringClient.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import type {
88
KeyringRequest,
99
KeyringAccountData,
1010
KeyringResponse,
11+
CaipAssetType,
12+
Balance,
1113
} from './api';
1214
import {
1315
ApproveRequestResponseStruct,
1416
CreateAccountResponseStruct,
1517
DeleteAccountResponseStruct,
1618
ExportAccountResponseStruct,
1719
FilterAccountChainsResponseStruct,
20+
GetAccountBalancesResponseStruct,
1821
GetAccountResponseStruct,
1922
GetRequestResponseStruct,
2023
ListAccountsResponseStruct,
@@ -76,6 +79,19 @@ export class KeyringClient implements Keyring {
7679
);
7780
}
7881

82+
async getAccountBalances(
83+
id: string,
84+
assets: CaipAssetType[],
85+
): Promise<Record<CaipAssetType, Balance>> {
86+
return strictMask(
87+
await this.#send({
88+
method: KeyringRpcMethod.GetAccountBalances,
89+
params: { id, assets },
90+
}),
91+
GetAccountBalancesResponseStruct,
92+
);
93+
}
94+
7995
async createAccount(
8096
options: Record<string, Json> = {},
8197
): Promise<KeyringAccount> {

0 commit comments

Comments
 (0)