-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server side api to query LPs by price and pair
- Loading branch information
1 parent
76461e0
commit 688867b
Showing
3 changed files
with
87 additions
and
1 deletion.
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,58 @@ | ||
// pages/api/lp/positionsByPrice/[...params].ts | ||
import { testnetConstants } from "../../../../constants/configConstants"; | ||
import { tokenConfigMapOnSymbol } from "@/constants/tokenConstants"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { LiquidityPositionQuerier } from "@/utils/protos/services/dex/liquidity-positions"; | ||
import { | ||
DirectedTradingPair, | ||
Position, | ||
} from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb"; | ||
import { base64ToUint8Array } from "@/utils/math/base64"; | ||
|
||
export default async function positionsByPriceHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const params = req.query.params as string[]; | ||
|
||
const token1 = params[0] || null; | ||
const token2 = params[1] || null; | ||
const limit = params[2] || null; | ||
|
||
try { | ||
if (!token1 || !token2 || !limit) { | ||
return res.status(400).json({ error: "Invalid query parameters" }); | ||
} | ||
|
||
// Get token 1 & 2 | ||
const asset1Token = tokenConfigMapOnSymbol[token1]; | ||
const asset2Token = tokenConfigMapOnSymbol[token2]; | ||
|
||
const lp_querier = new LiquidityPositionQuerier({ | ||
grpcEndpoint: testnetConstants.grpcEndpoint, | ||
}); | ||
|
||
const tradingPair = new DirectedTradingPair({ | ||
start: { | ||
inner: base64ToUint8Array(asset1Token.inner), | ||
}, | ||
end: { | ||
inner: base64ToUint8Array(asset2Token.inner), | ||
}, | ||
}); | ||
|
||
const data = await lp_querier.liquidityPositionsByPrice( | ||
tradingPair, | ||
Number(limit) | ||
); | ||
|
||
res.status(200).json(data as Position[]); | ||
} catch (error) { | ||
console.error("Error getting liquidty positions by price grpc data:", error); | ||
res | ||
.status(500) | ||
.json({ | ||
error: `Error getting liquidty positions by price grpc data: ${error}`, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { PositionId, Position } from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb"; | ||
import { PositionId, Position, DirectedTradingPair } from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb"; | ||
|
||
export interface LiquidityPositionQuerierInterface { | ||
liquidityPositionById(id: PositionId): Promise<Position | undefined>; | ||
liquidityPositionsByPrice(directedTradingPair: DirectedTradingPair, limit: number): Promise<Position[] | undefined>; | ||
} |