Skip to content

Commit 569f241

Browse files
Merge pull request #9 from valorem-labs-inc/soft-quote
Soft quote
2 parents e5ba332 + 9e53e24 commit 569f241

File tree

5 files changed

+196
-36
lines changed

5 files changed

+196
-36
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ on:
88
- 'v**'
99
- 'releases/v**'
1010

11+
permissions:
12+
packages: read
13+
contents: read
14+
1115
jobs:
1216
build-and-test:
1317
runs-on: ubuntu-latest
1418
env:
15-
CR_PAT: ${{ secrets.GITHUB_TOKEN }}
19+
GHP_PAT: ${{ secrets.GITHUB_TOKEN }}
1620

1721
steps:
1822
- name: Checkout repo

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
"source": "src/index.ts",
1212
"types": "dist/types.d.ts",
1313
"scripts": {
14-
"build": "pnpm clean && pnpm codegen && pnpm parcel build --no-cache",
14+
"build": "pnpm clean && pnpm generate && pnpm parcel build --no-cache",
1515
"ci:release": "pnpm build && pnpm changeset publish",
1616
"clean": "rm -rf ./dist",
1717
"codegen": "rm -rf ./src/lib/codegen && wagmi generate && npx buf generate",
1818
"format": "prettier --write \"**/*.{ts,tsx,md,json}\"",
1919
"gen-docs": "typedoc",
20+
"generate": "rm -rf ./src/lib/codegen && pnpm generate:wagmi && pnpm generate:grpc",
21+
"generate:grpc": "npx buf generate",
22+
"generate:wagmi": "wagmi generate",
2023
"lint": "eslint .",
2124
"lint:fix": "eslint . --fix",
2225
"prepare": "git submodule update --init --recursive && pnpm codegen",
@@ -61,7 +64,7 @@
6164
"@connectrpc/connect-query": "0.5.3",
6265
"@connectrpc/connect-web": "^1.1.2",
6366
"@tanstack/react-query": "^4.36.1",
64-
"@valorem-labs-inc/sdk": "^0.0.4",
67+
"@valorem-labs-inc/sdk": "^0.0.8",
6568
"@wagmi/core": "1.4.7",
6669
"abitype": "0.8.7",
6770
"connectkit": "^1.5.3",

pnpm-lock.yaml

Lines changed: 55 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hooks/useSoftQuote.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import type {
2+
OptionType,
3+
ParsedSoftQuoteResponse,
4+
} from '@valorem-labs-inc/sdk';
5+
import {
6+
CLEAR_ADDRESS,
7+
ItemType,
8+
QuoteRequest,
9+
SoftQuote,
10+
SEAPORT_ADDRESS,
11+
parseSoftQuoteResponse,
12+
toH160,
13+
toH256,
14+
} from '@valorem-labs-inc/sdk';
15+
import { useQueryClient } from '@tanstack/react-query';
16+
import { useMemo } from 'react';
17+
import { useAccount, useChainId } from 'wagmi';
18+
import { hexToBigInt } from 'viem';
19+
import { useStream } from './useStream';
20+
import { usePromiseClient } from './usePromiseClient';
21+
22+
/**
23+
* Configuration for the useSoftQuote hook.
24+
* quoteRequest - An object or instance containing the details for requesting a quote.
25+
* enabled - Flag to enable the hook.
26+
* timeoutMs - Timeout for the quote request in milliseconds.
27+
* onError - Callback function for handling errors.
28+
*/
29+
export interface UseRFQConfig {
30+
quoteRequest:
31+
| QuoteRequest
32+
| {
33+
tokenId: OptionType['tokenId'];
34+
action: QuoteRequest['action'];
35+
amount: bigint;
36+
}
37+
| undefined;
38+
enabled?: boolean;
39+
timeoutMs?: number;
40+
onError?: (err: Error) => void;
41+
}
42+
43+
/**
44+
* Return type of the useSoftQuote hook.
45+
* quotes - Array of parsed quote responses.
46+
* responses - Array of raw quote responses.
47+
* openStream - Function to open the stream for receiving quotes.
48+
* resetAndRestartStream - Function to reset and restart the quote stream.
49+
* abortStream - Function to abort the quote stream.
50+
* error - Error object if an error occurred during the RFQ process.
51+
*/
52+
export interface UseRFQReturn {
53+
quotes?: ParsedSoftQuoteResponse[];
54+
responses?: ParsedSoftQuoteResponse[];
55+
openStream: () => Promise<() => void>;
56+
resetAndRestartStream: () => void;
57+
abortStream: () => void;
58+
error?: Error;
59+
}
60+
61+
/**
62+
* Hook to manage the useSoftQuote process in the Valorem trading environment.
63+
* It handles sending quote requests to market makers and receiving their responses.
64+
* @param config - Configuration for the RFQ process.
65+
* @returns An object containing the quotes, response management functions, and any errors.
66+
*/
67+
export const useSoftQuote = ({
68+
quoteRequest,
69+
enabled,
70+
timeoutMs = 15000,
71+
onError,
72+
}: UseRFQConfig): UseRFQReturn => {
73+
const grpcClient = usePromiseClient(SoftQuote);
74+
const queryClient = useQueryClient();
75+
const { address } = useAccount();
76+
const chainId = useChainId();
77+
78+
const request = useMemo(() => {
79+
if (quoteRequest === undefined) return undefined;
80+
81+
// pre-constructed quote request
82+
if (quoteRequest instanceof QuoteRequest) return quoteRequest;
83+
84+
// construct quote request from quote request config
85+
if (address === undefined) return undefined;
86+
const { tokenId, action, amount } = quoteRequest;
87+
if (tokenId === undefined) return undefined;
88+
89+
return new QuoteRequest({
90+
ulid: undefined,
91+
takerAddress: toH160(address),
92+
itemType: ItemType.ERC1155,
93+
tokenAddress: toH160(CLEAR_ADDRESS),
94+
identifierOrCriteria: toH256(tokenId),
95+
amount: toH256(amount),
96+
action,
97+
chainId: toH256(BigInt(chainId)),
98+
seaportAddress: toH160(hexToBigInt(SEAPORT_ADDRESS)),
99+
});
100+
}, [address, chainId, quoteRequest]);
101+
102+
const {
103+
data,
104+
responses,
105+
openStream,
106+
resetAndRestartStream,
107+
abortStream,
108+
error,
109+
} = useStream<typeof SoftQuote, ParsedSoftQuoteResponse>({
110+
queryClient,
111+
queryKey: ['useSoftQuote'],
112+
grpcClient,
113+
method: 'webTaker',
114+
request,
115+
enabled: enabled && request !== undefined,
116+
keepAlive: true,
117+
timeoutMs,
118+
parseResponse: parseSoftQuoteResponse,
119+
onError,
120+
});
121+
122+
return {
123+
quotes: data,
124+
responses,
125+
openStream,
126+
resetAndRestartStream,
127+
abortStream,
128+
error,
129+
};
130+
};

0 commit comments

Comments
 (0)