-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hook for getting account's spendable balance
- Loading branch information
Showing
10 changed files
with
223 additions
and
26 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,5 @@ | ||
--- | ||
"@reactive-dot/utils": minor | ||
--- | ||
|
||
Add utilities for getting the minimum and/or maximum value from a list of big integers. |
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,5 @@ | ||
--- | ||
"@reactive-dot/react": minor | ||
--- | ||
|
||
Add hook for getting account's spendable balance. |
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,88 @@ | ||
import type { ChainHookOptions } from "./types.js"; | ||
import { useNativeTokenAmountFromPlanck } from "./use-native-token-amount.js"; | ||
import { useLazyLoadQuery } from "./use-query.js"; | ||
import { type DenominatedNumber, BigIntMath } from "@reactive-dot/utils"; | ||
import type { SS58String } from "polkadot-api"; | ||
|
||
type SystemAccount = { | ||
nonce: number; | ||
consumers: number; | ||
providers: number; | ||
sufficients: number; | ||
data: { | ||
free: bigint; | ||
reserved: bigint; | ||
frozen: bigint; | ||
flags: bigint; | ||
}; | ||
}; | ||
|
||
type Options = ChainHookOptions & { | ||
includesExistentialDeposit?: boolean; | ||
}; | ||
|
||
/** | ||
* Hook 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: SS58String, | ||
options?: Options, | ||
): DenominatedNumber; | ||
/** | ||
* Hook for getting accounts’ spendable balances. | ||
* | ||
* @param addresses - The account-addresses | ||
* @param options - Additional options | ||
* @returns The accounts’ spendable balances | ||
*/ | ||
export function useSpendableBalance( | ||
addresses: SS58String[], | ||
options?: Options, | ||
): DenominatedNumber[]; | ||
export function useSpendableBalance( | ||
addressOrAddresses: SS58String | SS58String[], | ||
options?: Options, | ||
): DenominatedNumber | DenominatedNumber[] { | ||
const addresses = Array.isArray(addressOrAddresses) | ||
? addressOrAddresses | ||
: [addressOrAddresses]; | ||
|
||
const [existentialDeposit, accounts] = useLazyLoadQuery( | ||
(builder) => | ||
builder.getConstant("Balances", "ExistentialDeposit").readStorages( | ||
"System", | ||
"Account", | ||
addresses.map((address) => [address]), | ||
), | ||
options, | ||
) as [bigint, SystemAccount[]]; | ||
|
||
const nativeTokenFromPlanck = useNativeTokenAmountFromPlanck(options); | ||
|
||
const includesExistentialDeposit = | ||
options?.includesExistentialDeposit ?? true; | ||
|
||
const spendableNativeTokenFromAccount = ({ | ||
data: { free, reserved, frozen }, | ||
}: SystemAccount) => | ||
nativeTokenFromPlanck( | ||
BigIntMath.max( | ||
0n, | ||
free - | ||
BigIntMath.max( | ||
frozen - reserved, | ||
includesExistentialDeposit ? 0n : existentialDeposit, | ||
), | ||
), | ||
); | ||
|
||
const spendableBalances = accounts.map(spendableNativeTokenFromAccount); | ||
|
||
return Array.isArray(addressOrAddresses) | ||
? spendableBalances | ||
: spendableBalances.at(0)!; | ||
} |
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,26 @@ | ||
import { BigIntMath } from "./big-int"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
describe("BigIntMath", () => { | ||
describe("min", () => { | ||
it("gets the correct minimum value", () => | ||
expect(BigIntMath.min(-1n, 0n, 1n)).toBe(-1n)); | ||
|
||
it("returns the first value if only one value is passed", () => | ||
expect(BigIntMath.min(1n)).toBe(1n)); | ||
|
||
it("returns 0n if no values is passed", () => | ||
expect(BigIntMath.min()).toBe(0n)); | ||
}); | ||
|
||
describe("max", () => { | ||
it("gets the correct maximum value", () => | ||
expect(BigIntMath.max(-1n, 0n, 1n)).toBe(1n)); | ||
|
||
it("returns the first value if only one value is passed", () => | ||
expect(BigIntMath.max(-1n)).toBe(-1n)); | ||
|
||
it("returns 0n if no values is passed", () => | ||
expect(BigIntMath.max()).toBe(0n)); | ||
}); | ||
}); |
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,23 @@ | ||
export const BigIntMath = Object.freeze({ | ||
min(...values: bigint[]) { | ||
if (values.length === 0) { | ||
return 0n; | ||
} | ||
|
||
return values.reduce((previousValue, currentValue) => | ||
currentValue < previousValue ? currentValue : previousValue, | ||
); | ||
}, | ||
max(...values: bigint[]) { | ||
if (values.length === 0) { | ||
return 0n; | ||
} | ||
|
||
return values.reduce((previousValue, currentValue) => | ||
currentValue > previousValue ? currentValue : previousValue, | ||
); | ||
}, | ||
get [Symbol.toStringTag]() { | ||
return "BigIntMath"; | ||
}, | ||
}); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { BigIntMath } from "./big-int.js"; | ||
export { DenominatedNumber } from "./denominated-number.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,3 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({}); |