Skip to content

Commit 4e2d46d

Browse files
authored
refactor(react, vue): dedicated spendable balances function (#793)
1 parent 275aaeb commit 4e2d46d

11 files changed

Lines changed: 60 additions & 14 deletions

File tree

.changeset/evil-pears-judge.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@reactive-dot/react": minor
3+
"@reactive-dot/vue": minor
4+
---
5+
6+
Added dedicated `useSpendableBalances` hook/composable.

apps/docs/react/guides/number-utility.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ const amountFromPlanck = useNativeTokenAmountFromPlanck();
8080

8181
## Spendable balance
8282

83-
The [`useSpendableBalance`](/api/react/function/useSpendableBalance) hook can be used to get the [spendable balance](https://wiki.polkadot.network/docs/learn-account-balances) of an account(s).
83+
The [`useSpendableBalance`](/api/react/function/useSpendableBalance) & [`useSpendableBalances`](/api/react/function/useSpendableBalances) hooks can be used to get the [spendable balance](https://wiki.polkadot.network/docs/learn-account-balances) of an account(s).
8484

8585
```ts
86-
import { useSpendableBalance } from "@reactive-dot/react";
86+
import { useSpendableBalance, useSpendableBalances } from "@reactive-dot/react";
8787

8888
const spendableBalance = useSpendableBalance(ACCOUNT_ADDRESS);
8989

9090
console.log(spendableBalance.toLocaleString("en-NZ")); // DOT 10.00
9191

92-
const spendableBalances = useSpendableBalance([
92+
const spendableBalances = useSpendableBalances([
9393
ACCOUNT_ADDRESS_1,
9494
ACCOUNT_ADDRESS_2,
9595
ACCOUNT_ADDRESS_3,

apps/docs/vue/guides/number-utility.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@ console.log(nativeToken.value.amountFromNumber(1).toLocaleString("en-NZ")); // D
6969

7070
## Spendable balance
7171

72-
The [`useSpendableBalance`](/api/vue/function/useSpendableBalance) composable can be used to get the [spendable balance](https://wiki.polkadot.network/docs/learn-account-balances) of an account(s).
72+
The [`useSpendableBalance`](/api/react/function/useSpendableBalance) & [`useSpendableBalances`](/api/react/function/useSpendableBalances) composable can be used to get the [spendable balance](https://wiki.polkadot.network/docs/learn-account-balances) of an account(s).
7373

7474
```vue
7575
<script setup lang="ts">
76-
import { useSpendableBalance } from "@reactive-dot/vue";
76+
import { useSpendableBalance, useSpendableBalances } from "@reactive-dot/vue";
7777
7878
const { data: spendableBalance } = await useSpendableBalance(ACCOUNT_ADDRESS);
7979
8080
console.log(spendableBalance.toLocaleString("en-NZ")); // DOT 10.00
8181
82-
const { data: spendableBalances } = await useSpendableBalance([
82+
const { data: spendableBalances } = await useSpendableBalances([
8383
ACCOUNT_ADDRESS_1,
8484
ACCOUNT_ADDRESS_2,
8585
ACCOUNT_ADDRESS_3,

packages/react/src/hooks/use-balance.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DenominatedNumber } from "../../../utils/build/denominated-number.js";
2-
import { useSpendableBalance } from "./use-balance.js";
2+
import { useSpendableBalance, useSpendableBalances } from "./use-balance.js";
33
import { useNativeTokenAmountFromPlanck } from "./use-native-token-amount.js";
44
import { useLazyLoadQuery } from "./use-query.js";
55
import { idle, Query } from "@reactive-dot/core";
@@ -77,7 +77,7 @@ it("should return spendable balance for single address", () => {
7777

7878
it("should return spendable balances array for multiple addresses", () => {
7979
const { result } = renderHook(() =>
80-
useSpendableBalance([
80+
useSpendableBalances([
8181
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
8282
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
8383
]),
@@ -93,7 +93,7 @@ it("should return spendable balances array for multiple addresses", () => {
9393

9494
it("should return spendable balances array for an array of one address", () => {
9595
const { result } = renderHook(() =>
96-
useSpendableBalance(["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"]),
96+
useSpendableBalances(["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"]),
9797
);
9898

9999
expect(result.current).toEqual(

packages/react/src/hooks/use-balance.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function useSpendableBalance(
3838
/**
3939
* Hook for getting accounts’ spendable balances.
4040
*
41+
* @deprecated Use {@link useSpendableBalances} instead.
4142
* @param addresses - The account-addresses
4243
* @param options - Additional options
4344
* @returns The accounts’ spendable balances
@@ -89,3 +90,17 @@ export function useSpendableBalance(
8990

9091
return Array.isArray(addressOrAddresses) ? balances : balances[0]!;
9192
}
93+
94+
/**
95+
* Hook for getting accounts’ spendable balances.
96+
*
97+
* @param addresses - The account-addresses
98+
* @param options - Additional options
99+
* @returns The accounts’ spendable balances
100+
*/
101+
export function useSpendableBalances(
102+
addresses: SS58String[],
103+
options?: Options,
104+
) {
105+
return useSpendableBalance(addresses, options);
106+
}

packages/react/src/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ it("should match inline snapshot", () =>
1111
"SignerProvider",
1212
"useAccounts",
1313
"useSpendableBalance",
14+
"useSpendableBalances",
1415
"useBlock",
1516
"useChainId",
1617
"useChainIds",

packages/react/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ export { ReactiveDotProvider } from "./contexts/provider.js";
66
export { QueryOptionsProvider } from "./contexts/query-options.js";
77
export { SignerProvider } from "./contexts/signer.js";
88
export { useAccounts } from "./hooks/use-accounts.js";
9-
export { useSpendableBalance } from "./hooks/use-balance.js";
9+
export {
10+
useSpendableBalance,
11+
useSpendableBalances,
12+
} from "./hooks/use-balance.js";
1013
export { useBlock } from "./hooks/use-block.js";
1114
export { useChainId, useChainIds } from "./hooks/use-chain-id.js";
1215
export { useChainSpecData } from "./hooks/use-chain-spec-data.js";

packages/vue/src/composables/use-balance.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DenominatedNumber } from "../../../utils/build/denominated-number.js";
22
import { chainIdKey } from "../keys.js";
33
import { withSetup } from "../test-utils.js";
4-
import { useSpendableBalance } from "./use-balance.js";
4+
import { useSpendableBalance, useSpendableBalances } from "./use-balance.js";
55
import { useNativeTokenPromise } from "./use-native-token.js";
66
import { useQueryObservable } from "./use-query.js";
77
import { Query } from "@reactive-dot/core";
@@ -82,7 +82,7 @@ it("should return spendable balance for single address", async () => {
8282
it("should return spendable balances array for multiple addresses", async () => {
8383
const { result } = withSetup(
8484
() =>
85-
useSpendableBalance([
85+
useSpendableBalances([
8686
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
8787
"5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
8888
]),
@@ -100,7 +100,9 @@ it("should return spendable balances array for multiple addresses", async () =>
100100
it("should return spendable balances array for an array of one address", async () => {
101101
const { result } = withSetup(
102102
() =>
103-
useSpendableBalance(["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"]),
103+
useSpendableBalances([
104+
"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
105+
]),
104106
{ [chainIdKey]: "test-chain-id" },
105107
);
106108

packages/vue/src/composables/use-balance.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function useSpendableBalance(
4545
/**
4646
* Composable for getting accounts’ spendable balances.
4747
*
48+
* @deprecated Use {@link useSpendableBalances} instead.
4849
* @param addresses - The account-addresses
4950
* @param options - Additional options
5051
* @returns The accounts’ spendable balances
@@ -121,3 +122,17 @@ export function useSpendableBalance(
121122
),
122123
);
123124
}
125+
126+
/**
127+
* Composable for getting accounts’ spendable balances.
128+
*
129+
* @param addresses - The account-addresses
130+
* @param options - Additional options
131+
* @returns The accounts’ spendable balances
132+
*/
133+
export function useSpendableBalances(
134+
addresses: MaybeRefOrGetter<SS58String[]>,
135+
options?: Options,
136+
) {
137+
return useSpendableBalance(addresses, options);
138+
}

packages/vue/src/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ it("should match inline snapshot", () =>
66
[
77
"useAccounts",
88
"useSpendableBalance",
9+
"useSpendableBalances",
910
"useBlock",
1011
"useChainId",
1112
"useChainIds",

0 commit comments

Comments
 (0)