-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wire up liquidity stat on MarketStats #240
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dffdb53
Move to root folder as a top level market value (like fixed APR)
ryangoree 2c2670d
Refactor getLiquidity to return a bigint and formatted
ryangoree 2daecfc
Edit getLiquidityQuery
ryangoree cbcd34a
Update MarketTable
ryangoree c4ec5c0
Edit liquidity stat
ryangoree 5deb4de
Add multiplyBigInt util
ryangoree File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 22 additions & 9 deletions
31
apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useLiquidity.ts
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,14 +1,27 @@ | ||
import { getLiquidityQuery } from "@hyperdrive/core"; | ||
import { useQuery, UseQueryResult } from "@tanstack/react-query"; | ||
import { QueryStatus, useQuery } from "@tanstack/react-query"; | ||
import { Address, usePublicClient } from "wagmi"; | ||
|
||
export type GetLiquidityResponse = { | ||
marketLiquidity: string; | ||
}; | ||
export function useLiquidity(hyperdriveAddress: Address): { | ||
liquidity: | ||
| { | ||
liquidity: bigint; | ||
formatted: string; | ||
} | ||
| undefined; | ||
liquidityStatus: QueryStatus; | ||
} { | ||
const publicClient = usePublicClient(); | ||
|
||
export function useLiquidity( | ||
hyperdriveAddress: Address, | ||
): UseQueryResult<GetLiquidityResponse> { | ||
const publicClient = usePublicClient() as any; | ||
return useQuery(getLiquidityQuery({ hyperdriveAddress, publicClient })); | ||
const { data, status } = useQuery( | ||
getLiquidityQuery({ | ||
hyperdriveAddress, | ||
publicClient: publicClient as any, | ||
}), | ||
); | ||
|
||
return { | ||
liquidity: data, | ||
liquidityStatus: status, | ||
}; | ||
} |
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
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,84 @@ | ||
/** | ||
* Multiply an array of bigints together and preserve the correct scaling for | ||
* decimals. | ||
* | ||
* @param options - The options to use. | ||
* @param list - The bigints to multiply. | ||
* @param decimals - The number of decimal places the bigints are scaled | ||
* to. If a value in `values` has a `decimals` property, that value will be used | ||
* instead. This is also the number of decimal places the result is scaled to. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // 5.00 * .50 = 2.50 | ||
* multiplyBigInt([500n, 50n], 2); | ||
* // 250n | ||
* | ||
* // With different decimals | ||
* // 5.00 * .500 = 2.50 | ||
* multiplyBigInt( | ||
* [ | ||
* 500n, | ||
* { | ||
* value: 500n, | ||
* decimals: 3 | ||
* } | ||
* ], | ||
* 2 | ||
* ); | ||
* // 250n | ||
* ``` | ||
*/ | ||
export function multiplyBigInt( | ||
list: ( | ||
| bigint | ||
| { | ||
value: bigint; | ||
decimals: number; | ||
} | ||
)[], | ||
decimals: number, | ||
): bigint { | ||
if (list.length === 0) { | ||
return 0n; | ||
} | ||
|
||
let totalValue: bigint; | ||
let totalDecimals: number; | ||
|
||
// Initialize the total value and decimals to the first value in the array. | ||
if (typeof list[0] === "bigint") { | ||
totalValue = list[0]; | ||
totalDecimals = decimals; | ||
} else { | ||
totalValue = list[0].value; | ||
totalDecimals = list[0].decimals; | ||
} | ||
|
||
// Multiply the total value by each subsequent value in the array. And add the | ||
// decimals together. | ||
for (const value of list.slice(1)) { | ||
if (typeof value === "bigint") { | ||
totalValue *= value; | ||
totalDecimals += decimals; | ||
} else { | ||
totalValue *= value.value; | ||
totalDecimals += value.decimals; | ||
} | ||
} | ||
|
||
// If the total decimals is less than the desired decimals, return 0. | ||
if (totalDecimals < decimals) { | ||
return 0n; | ||
} | ||
|
||
// If the total decimals is the same as the desired decimals, avoid doing any | ||
// extra work and return the total value. | ||
if (totalDecimals === decimals) { | ||
return totalValue; | ||
} | ||
|
||
// Remove the extra decimal places. | ||
return totalValue / 10n ** BigInt(totalDecimals - decimals); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just return the useQuery itself? Then we have access to all the query helper methods from the hook if we need them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a design decision @DannyDelott has made to reduce the return signature. If we change it here, we'd probably want to change it everywhere we're not returning the
useQuery
result.