Skip to content

Commit

Permalink
🐛 (Spot): update useSpotPrice to use new proto
Browse files Browse the repository at this point in the history
  • Loading branch information
nickadamson committed Jan 11, 2024
1 parent 6580e92 commit ba71c46
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/hooks/useSpotPrice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useMemo, useState } from 'react';
import type { Address } from 'viem';
import type { UseQueryResult } from '@tanstack/react-query';
import { useChainId } from 'wagmi';
Expand Down Expand Up @@ -100,8 +100,15 @@ export function useSpotPrice<TTokens extends TokensArr>({
return new SpotPriceRequest({ spotPriceInfo });
}, [chainId, spotPriceRequest]);

const [spotPrices, setSpotPrices] = useState<
Record<string, Price | undefined>
>({
WETH: undefined,
USDC: undefined,
});

const service = createQueryService({ service: Spot });
const { data, ...rest } = useStream(
const { data: _data, ...rest } = useStream(
{
...Spot.methods.getSpotPrice,
service: {
Expand All @@ -112,33 +119,24 @@ export function useSpotPrice<TTokens extends TokensArr>({
request,
{
enabled,
onResponse(response) {
const { tokenAddress, spotPrice } = response;
const price = spotPrice !== undefined ? fromH256(spotPrice) : undefined;
const address =
tokenAddress !== undefined
? fromH160ToAddress(tokenAddress)
: undefined;

if (price && address) {
const symbol = addressToSymbolMap.get(address);
if (symbol) {
setSpotPrices((prev) => ({ ...prev, [symbol]: price }));
}
}
},
},
);

const spotPrices = useMemo(() => {
if (data?.responses === undefined) return undefined;
const prices: Record<string, Price | undefined> = {};
const latestResponse = data.responses[0];

latestResponse.spotPriceInfo.forEach((info) => {
const { tokenAddress, spotPrice } = info;
const price = spotPrice !== undefined ? fromH256(spotPrice) : undefined;
const address =
tokenAddress !== undefined
? fromH160ToAddress(tokenAddress)
: undefined;

if (price && address) {
const symbol = addressToSymbolMap.get(address);
if (symbol) {
prices[symbol] = price;
}
}
});

return prices;
}, [addressToSymbolMap, data?.responses]);

return {
spotPrices,
...(rest as Omit<
Expand Down

0 comments on commit ba71c46

Please sign in to comment.