-
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: hooks for getting native token amount
- Loading branch information
Showing
5 changed files
with
147 additions
and
18 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/react": minor | ||
--- | ||
|
||
Add hooks for converting planck or number to native token amount. |
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,100 @@ | ||
import type { ChainHookOptions } from "./types.js"; | ||
import { useChainSpecData } from "./use-chain-spec-data.js"; | ||
import { DenominatedNumber } from "@reactive-dot/utils"; | ||
|
||
/** | ||
* Hook for returning the native token amount from a planck value | ||
* | ||
* @param planck - The planck value | ||
* @param options - Additional options | ||
* @returns The native token amount | ||
*/ | ||
export function useNativeTokenAmountFromPlanck( | ||
planck: bigint, | ||
options?: ChainHookOptions, | ||
): DenominatedNumber; | ||
/** | ||
* Hook for returning a function that converts planck value to native token amount | ||
* | ||
* @param options - Additional options | ||
* @returns Function for getting the native token amount from a planck value | ||
*/ | ||
export function useNativeTokenAmountFromPlanck( | ||
options?: ChainHookOptions, | ||
): (planck: bigint | number | string) => DenominatedNumber; | ||
export function useNativeTokenAmountFromPlanck( | ||
planckOrOptions?: bigint | number | string | ChainHookOptions, | ||
maybeOptions?: ChainHookOptions, | ||
): | ||
| DenominatedNumber | ||
| ((planck: bigint | number | string) => DenominatedNumber) { | ||
const options = | ||
typeof planckOrOptions === "object" ? planckOrOptions : maybeOptions; | ||
|
||
const chainSpecData = useChainSpecData(options); | ||
|
||
switch (typeof planckOrOptions) { | ||
case "bigint": | ||
case "number": | ||
case "string": | ||
return new DenominatedNumber( | ||
planckOrOptions, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
); | ||
default: | ||
return (planck: bigint | number | string) => | ||
new DenominatedNumber( | ||
planck, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Hook for returning the native token amount from a number value | ||
* | ||
* @param planck - The number value | ||
* @param options - Additional options | ||
* @returns The native token amount | ||
*/ | ||
export function useNativeTokenAmountFromNumber( | ||
number: number | string, | ||
options?: ChainHookOptions, | ||
): DenominatedNumber; | ||
/** | ||
* Hook for returning a function that converts number value to native token amount | ||
* | ||
* @param options - Additional options | ||
* @returns Function for getting the native token amount from a number value | ||
*/ | ||
export function useNativeTokenAmountFromNumber( | ||
options?: ChainHookOptions, | ||
): (number: number | string) => DenominatedNumber; | ||
export function useNativeTokenAmountFromNumber( | ||
numberOrOptions?: number | string | ChainHookOptions, | ||
maybeOptions?: ChainHookOptions, | ||
): DenominatedNumber | ((planck: number) => DenominatedNumber) { | ||
const options = | ||
typeof numberOrOptions === "object" ? numberOrOptions : maybeOptions; | ||
|
||
const chainSpecData = useChainSpecData(options); | ||
|
||
switch (typeof numberOrOptions) { | ||
case "number": | ||
case "string": | ||
return new DenominatedNumber( | ||
numberOrOptions, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
); | ||
default: | ||
return (number: number) => | ||
DenominatedNumber.fromNumber( | ||
number, | ||
chainSpecData.properties.tokenDecimals, | ||
chainSpecData.properties.tokenSymbol, | ||
); | ||
} | ||
} |
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