From 4ec30c8bab01d58e6c8d53ee3e0cc9ec0aeacd42 Mon Sep 17 00:00:00 2001 From: Daniel Rocha <68558152+danroc@users.noreply.github.com> Date: Mon, 27 May 2024 21:24:31 +0200 Subject: [PATCH] refactor: split `api.ts` --- src/api/balance.test-d.ts | 19 +++++++++++++++++++ src/api/balance.ts | 11 +++++++++++ src/api/index.ts | 1 + src/api/keyring.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/api/balance.test-d.ts create mode 100644 src/api/balance.ts diff --git a/src/api/balance.test-d.ts b/src/api/balance.test-d.ts new file mode 100644 index 000000000..ccb0cc662 --- /dev/null +++ b/src/api/balance.test-d.ts @@ -0,0 +1,19 @@ +import { expectAssignable, expectNotAssignable } from 'tsd'; + +import type { Balance } from './balance'; + +expectAssignable({ amount: '1.0', unit: 'ETH' }); +expectAssignable({ amount: '0.1', unit: 'BTC' }); +expectAssignable({ amount: '.1', unit: 'gwei' }); +expectAssignable({ amount: '.1', unit: 'wei' }); +expectAssignable({ amount: '1.', unit: 'sat' }); + +expectNotAssignable({ amount: 1, unit: 'ETH' }); +expectNotAssignable({ amount: true, unit: 'ETH' }); +expectNotAssignable({ amount: undefined, unit: 'ETH' }); +expectNotAssignable({ amount: null, unit: 'ETH' }); + +expectNotAssignable({ amount: '1.0', unit: 1 }); +expectNotAssignable({ amount: '1.0', unit: true }); +expectNotAssignable({ amount: '1.0', unit: undefined }); +expectNotAssignable({ amount: '1.0', unit: null }); diff --git a/src/api/balance.ts b/src/api/balance.ts new file mode 100644 index 000000000..8335bbc53 --- /dev/null +++ b/src/api/balance.ts @@ -0,0 +1,11 @@ +import type { Infer } from 'superstruct'; +import { string } from 'superstruct'; + +import { object } from '../superstruct'; + +export const BalanceStruct = object({ + amount: string(), + unit: string(), +}); + +export type Balance = Infer; diff --git a/src/api/index.ts b/src/api/index.ts index e1ca839a9..c0c60a360 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,5 @@ export * from './account'; +export * from './balance'; export * from './export'; export * from './keyring'; export * from './request'; diff --git a/src/api/keyring.ts b/src/api/keyring.ts index de3b0bd13..8d019552a 100644 --- a/src/api/keyring.ts +++ b/src/api/keyring.ts @@ -1,6 +1,7 @@ import type { Json } from '@metamask/utils'; import type { KeyringAccount } from './account'; +import type { Balance } from './balance'; import type { KeyringAccountData } from './export'; import type { KeyringRequest } from './request'; import type { KeyringResponse } from './response'; @@ -44,6 +45,32 @@ export type Keyring = { */ createAccount(options?: Record): Promise; + /** + * Retrieve the balances of given account. + * + * @example + * ```ts + * keyring.getAccountBalances( + * '43550276-c7d6-4fac-87c7-00390ad0ce90', + * ['bip122:000000000019d6689c085ae165831e93/slip44:0'] + * ); + * + * // Returns: + * // { + * // 'bip122:000000000019d6689c085ae165831e93/slip44:0': { + * // amount: '0.0001', + * // unit: 'BTC', + * // } + * // } + * ``` + * @param id - The ID of the account to the balance of. + * @param assets - List of asset IDs (CAIP-19) to retrieve the balances of. + */ + getAccountBalances?( + id: string, + assets: string[], + ): Promise>; + /** * Filter supported chains for a given account. *