-
Notifications
You must be signed in to change notification settings - Fork 0
Soft quote #9
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
Merged
Merged
Soft quote #9
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3560ebb
feat: update trade-interfaces submodule to v1.2.0
albertocevallos 674df47
chore: update local codegen, add script command
albertocevallos 454251c
feat: add useSoftQuote hook
albertocevallos 2022e7d
chore: update usequery key
albertocevallos 12909dd
ci: fix permissions
nickadamson 0a3bd50
fix: remove unnecessary cli script for grpc codegen
albertocevallos 9c70e75
fix: update scripts
albertocevallos 9e53e24
Merge branch 'soft-quote' of https://github.com/valorem-labs-inc/reac…
albertocevallos 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
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,130 @@ | ||
import type { | ||
OptionType, | ||
ParsedSoftQuoteResponse, | ||
} from '@valorem-labs-inc/sdk'; | ||
import { | ||
CLEAR_ADDRESS, | ||
ItemType, | ||
QuoteRequest, | ||
SoftQuote, | ||
SEAPORT_ADDRESS, | ||
parseSoftQuoteResponse, | ||
toH160, | ||
toH256, | ||
} from '@valorem-labs-inc/sdk'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useMemo } from 'react'; | ||
import { useAccount, useChainId } from 'wagmi'; | ||
import { hexToBigInt } from 'viem'; | ||
import { useStream } from './useStream'; | ||
import { usePromiseClient } from './usePromiseClient'; | ||
|
||
/** | ||
* Configuration for the useSoftQuote hook. | ||
* quoteRequest - An object or instance containing the details for requesting a quote. | ||
* enabled - Flag to enable the hook. | ||
* timeoutMs - Timeout for the quote request in milliseconds. | ||
* onError - Callback function for handling errors. | ||
*/ | ||
export interface UseRFQConfig { | ||
quoteRequest: | ||
| QuoteRequest | ||
| { | ||
tokenId: OptionType['tokenId']; | ||
action: QuoteRequest['action']; | ||
amount: bigint; | ||
} | ||
| undefined; | ||
enabled?: boolean; | ||
timeoutMs?: number; | ||
onError?: (err: Error) => void; | ||
} | ||
|
||
/** | ||
* Return type of the useSoftQuote hook. | ||
* quotes - Array of parsed quote responses. | ||
* responses - Array of raw quote responses. | ||
* openStream - Function to open the stream for receiving quotes. | ||
* resetAndRestartStream - Function to reset and restart the quote stream. | ||
* abortStream - Function to abort the quote stream. | ||
* error - Error object if an error occurred during the RFQ process. | ||
*/ | ||
export interface UseRFQReturn { | ||
quotes?: ParsedSoftQuoteResponse[]; | ||
responses?: ParsedSoftQuoteResponse[]; | ||
openStream: () => Promise<() => void>; | ||
resetAndRestartStream: () => void; | ||
abortStream: () => void; | ||
error?: Error; | ||
} | ||
|
||
/** | ||
* Hook to manage the useSoftQuote process in the Valorem trading environment. | ||
* It handles sending quote requests to market makers and receiving their responses. | ||
* @param config - Configuration for the RFQ process. | ||
* @returns An object containing the quotes, response management functions, and any errors. | ||
*/ | ||
export const useSoftQuote = ({ | ||
quoteRequest, | ||
enabled, | ||
timeoutMs = 15000, | ||
onError, | ||
}: UseRFQConfig): UseRFQReturn => { | ||
const grpcClient = usePromiseClient(SoftQuote); | ||
const queryClient = useQueryClient(); | ||
const { address } = useAccount(); | ||
const chainId = useChainId(); | ||
|
||
const request = useMemo(() => { | ||
if (quoteRequest === undefined) return undefined; | ||
|
||
// pre-constructed quote request | ||
if (quoteRequest instanceof QuoteRequest) return quoteRequest; | ||
|
||
// construct quote request from quote request config | ||
if (address === undefined) return undefined; | ||
const { tokenId, action, amount } = quoteRequest; | ||
if (tokenId === undefined) return undefined; | ||
|
||
return new QuoteRequest({ | ||
ulid: undefined, | ||
takerAddress: toH160(address), | ||
itemType: ItemType.ERC1155, | ||
tokenAddress: toH160(CLEAR_ADDRESS), | ||
identifierOrCriteria: toH256(tokenId), | ||
amount: toH256(amount), | ||
action, | ||
chainId: toH256(BigInt(chainId)), | ||
seaportAddress: toH160(hexToBigInt(SEAPORT_ADDRESS)), | ||
}); | ||
}, [address, chainId, quoteRequest]); | ||
|
||
const { | ||
data, | ||
responses, | ||
openStream, | ||
resetAndRestartStream, | ||
abortStream, | ||
error, | ||
} = useStream<typeof SoftQuote, ParsedSoftQuoteResponse>({ | ||
queryClient, | ||
queryKey: ['useSoftQuote'], | ||
grpcClient, | ||
method: 'webTaker', | ||
request, | ||
enabled: enabled && request !== undefined, | ||
keepAlive: true, | ||
timeoutMs, | ||
parseResponse: parseSoftQuoteResponse, | ||
onError, | ||
}); | ||
|
||
return { | ||
quotes: data, | ||
responses, | ||
openStream, | ||
resetAndRestartStream, | ||
abortStream, | ||
error, | ||
}; | ||
}; |
Submodule trade-interfaces
updated
19 files
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.
Uh oh!
There was an error while loading. Please reload this page.