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

Commit

Permalink
Merge branch 'main' into replace-superstruct-with-fork
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc committed Jun 7, 2024
2 parents 438ef9b + f718c41 commit 09c011a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/KeyringClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type KeyringRequest,
type KeyringResponse,
KeyringClient,
KeyringRpcMethod,
} from '.'; // Import from `index.ts` to test the public API

describe('KeyringClient', () => {
Expand Down Expand Up @@ -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';
Expand Down
16 changes: 16 additions & 0 deletions src/KeyringClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import type {
KeyringRequest,
KeyringAccountData,
KeyringResponse,
CaipAssetType,
Balance,
} from './api';
import {
ApproveRequestResponseStruct,
CreateAccountResponseStruct,
DeleteAccountResponseStruct,
ExportAccountResponseStruct,
FilterAccountChainsResponseStruct,
GetAccountBalancesResponseStruct,
GetAccountResponseStruct,
GetRequestResponseStruct,
ListAccountsResponseStruct,
Expand Down Expand Up @@ -76,6 +79,19 @@ export class KeyringClient implements Keyring {
);
}

async getAccountBalances(
id: string,
assets: CaipAssetType[],
): Promise<Record<CaipAssetType, Balance>> {
return strictMask(
await this.#send({
method: KeyringRpcMethod.GetAccountBalances,
params: { id, assets },
}),
GetAccountBalancesResponseStruct,
);
}

async createAccount(
options: Record<string, Json> = {},
): Promise<KeyringAccount> {
Expand Down

0 comments on commit 09c011a

Please sign in to comment.