-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue):
useNativeToken
& useSpendableBalance
composables
- Loading branch information
Showing
16 changed files
with
351 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@reactive-dot/core": minor | ||
"@reactive-dot/vue": minor | ||
"@reactive-dot/react": patch | ||
--- | ||
|
||
Added `useNativeToken` and `useSpendableBalance` composables. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { spendableBalance } from "./spendable-balance.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { spendableBalance } from "./spendable-balance"; | ||
import { expect, test } from "vitest"; | ||
|
||
// https://wiki.polkadot.network/docs/learn-account-balances | ||
test.each([ | ||
{ free: 100n, frozen: 0n, reserved: 0n, spendable: 99n }, | ||
{ free: 100n, frozen: 80n, reserved: 0n, spendable: 20n }, | ||
{ free: 80n, frozen: 80n, reserved: 20n, spendable: 20n }, | ||
])( | ||
"$spendable = $free - max($frozen - $reserved, existentialDeposit)", | ||
({ free, frozen, reserved, spendable }) => { | ||
const existentialDeposit = 1n; | ||
|
||
expect( | ||
spendableBalance({ | ||
free, | ||
frozen, | ||
reserved, | ||
existentialDeposit, | ||
includesExistentialDeposit: false, | ||
}), | ||
).toBe(spendable); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BigIntMath } from "@reactive-dot/utils"; | ||
|
||
type SpendableBalanceParam = { | ||
free: bigint; | ||
reserved: bigint; | ||
frozen: bigint; | ||
existentialDeposit: bigint; | ||
includesExistentialDeposit?: boolean; | ||
}; | ||
|
||
export function spendableBalance({ | ||
free, | ||
reserved, | ||
frozen, | ||
existentialDeposit, | ||
includesExistentialDeposit = false, | ||
}: SpendableBalanceParam) { | ||
return BigIntMath.max( | ||
0n, | ||
free - | ||
BigIntMath.max( | ||
frozen - reserved, | ||
includesExistentialDeposit ? 0n : existentialDeposit, | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import type { | ||
ChainComposableOptions, | ||
PromiseLikeAsyncState, | ||
} from "../types.js"; | ||
import { useAsyncData } from "./use-async-data.js"; | ||
import { internal_useChainId } from "./use-chain-id.js"; | ||
import { useLazyValue } from "./use-lazy-value.js"; | ||
import { useNativeTokenPromise } from "./use-native-token.js"; | ||
import { useQueryObservable } from "./use-query.js"; | ||
import { spendableBalance } from "@reactive-dot/core/internal/maths.js"; | ||
import { type DenominatedNumber } from "@reactive-dot/utils"; | ||
import type { SS58String } from "polkadot-api"; | ||
import { combineLatest, from } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
import { computed, type MaybeRefOrGetter, toValue } from "vue"; | ||
|
||
type SystemAccount = { | ||
nonce: number; | ||
consumers: number; | ||
providers: number; | ||
sufficients: number; | ||
data: { | ||
free: bigint; | ||
reserved: bigint; | ||
frozen: bigint; | ||
flags: bigint; | ||
}; | ||
}; | ||
|
||
type Options = ChainComposableOptions & { | ||
includesExistentialDeposit?: MaybeRefOrGetter<boolean>; | ||
}; | ||
|
||
/** | ||
* Composable for getting an account's spendable balance. | ||
* | ||
* @param address - The account's address | ||
* @param options - Additional options | ||
* @returns The account's spendable balance | ||
*/ | ||
export function useSpendableBalance( | ||
address: MaybeRefOrGetter<SS58String>, | ||
options?: Options, | ||
): PromiseLikeAsyncState<DenominatedNumber>; | ||
/** | ||
* Composable for getting accounts’ spendable balances. | ||
* | ||
* @param addresses - The account-addresses | ||
* @param options - Additional options | ||
* @returns The accounts’ spendable balances | ||
*/ | ||
export function useSpendableBalance( | ||
addresses: MaybeRefOrGetter<SS58String[]>, | ||
options?: Options, | ||
): PromiseLikeAsyncState<DenominatedNumber[]>; | ||
export function useSpendableBalance( | ||
addressOrAddresses: MaybeRefOrGetter<SS58String | SS58String[]>, | ||
options?: Options, | ||
): PromiseLikeAsyncState<DenominatedNumber | DenominatedNumber[]> { | ||
const chainId = internal_useChainId(options); | ||
|
||
const addresses = computed(() => { | ||
const addressOrAddressesValue = toValue(addressOrAddresses); | ||
return Array.isArray(addressOrAddressesValue) | ||
? toValue(addressOrAddressesValue) | ||
: [toValue(addressOrAddressesValue)]; | ||
}); | ||
|
||
const includesExistentialDeposit = computed( | ||
() => toValue(options?.includesExistentialDeposit) ?? false, | ||
); | ||
|
||
const dataObservable = useQueryObservable( | ||
(builder) => | ||
builder.getConstant("Balances", "ExistentialDeposit").readStorages( | ||
"System", | ||
"Account", | ||
addresses.value.map((address) => [address] as const), | ||
), | ||
options, | ||
); | ||
|
||
const nativeTokenPromise = useNativeTokenPromise(options); | ||
|
||
return useAsyncData( | ||
useLazyValue( | ||
computed(() => [ | ||
"spendable-balances", | ||
chainId.value, | ||
...addresses.value.toSorted(), | ||
]), | ||
() => { | ||
const includesExistentialDepositValue = | ||
includesExistentialDeposit.value; | ||
|
||
return combineLatest([ | ||
dataObservable.value, | ||
from(nativeTokenPromise.value), | ||
]).pipe( | ||
map(([[existentialDeposit, accounts], { amountFromPlanck }]) => | ||
(accounts as SystemAccount[]).map( | ||
({ data: { free, reserved, frozen } }) => | ||
amountFromPlanck( | ||
spendableBalance({ | ||
free, | ||
reserved, | ||
frozen, | ||
existentialDeposit: existentialDeposit as bigint, | ||
includesExistentialDeposit: includesExistentialDepositValue, | ||
}), | ||
), | ||
), | ||
), | ||
); | ||
}, | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ChainComposableOptions } from "../types.js"; | ||
import { useAsyncData } from "./use-async-data.js"; | ||
import { internal_useChainId } from "./use-chain-id.js"; | ||
import { useChainSpecDataPromise } from "./use-chain-spec-data.js"; | ||
import { useLazyValue } from "./use-lazy-value.js"; | ||
import { DenominatedNumber } from "@reactive-dot/utils"; | ||
import { computed } from "vue"; | ||
|
||
/** | ||
* Composable for getting the chain's native token | ||
* @param options - Additional options | ||
* @returns The chain's native token | ||
*/ | ||
export function useNativeToken(options?: ChainComposableOptions) { | ||
return useAsyncData(useNativeTokenPromise(options)); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function useNativeTokenPromise(options?: ChainComposableOptions) { | ||
const chainId = internal_useChainId(options); | ||
const chainSpecDataPromise = useChainSpecDataPromise(options); | ||
|
||
return useLazyValue( | ||
computed(() => ["native-token", chainId.value]), | ||
() => | ||
chainSpecDataPromise.value.then((chainSpecData) => ({ | ||
symbol: chainSpecData.properties.tokenSymbol as string, | ||
decimals: chainSpecData.properties.tokenDecimals as number, | ||
amountFromPlanck: (planck: bigint | number | string) => | ||
new DenominatedNumber( | ||
planck, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
), | ||
amountFromNumber: (number: number | string) => | ||
DenominatedNumber.fromNumber( | ||
number, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
), | ||
})), | ||
); | ||
} |
Oops, something went wrong.