-
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.
Migrate grpc queries server side (#7)
* Migrate grpc queries server side * fix lp view * Shielded pool -> server side * fix rendering for if position not found * Move sim queries server side
- Loading branch information
1 parent
cb08f28
commit e554bf0
Showing
9 changed files
with
301 additions
and
232 deletions.
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
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,28 @@ | ||
// pages/api/lp/[lp_nft_id]/position.ts | ||
import { testnetConstants } from "../../../../constants/configConstants"; | ||
import { LiquidityPositionQuerier } from "../../../../utils/protos/services/dex/liquidity-positions"; | ||
import { | ||
PositionId, | ||
Position, | ||
} from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb"; | ||
|
||
export default async function liquidityPositionDataHandler(req: any, res: any) { | ||
const { lp_nft_id } = req.query; | ||
|
||
const lp_querier = new LiquidityPositionQuerier({ | ||
grpcEndpoint: testnetConstants.grpcEndpoint, | ||
}); | ||
|
||
try { | ||
const positionId = new PositionId({ | ||
altBech32m: lp_nft_id, | ||
}); | ||
|
||
const data = await lp_querier.liquidityPositionById(positionId); | ||
|
||
res.status(200).json(data as Position); | ||
} catch (error) { | ||
console.error("Error fetching liquidity position grpc data:", error); | ||
res.status(500).json({"error": "Error fetching liquidity position grpc data"}); | ||
} | ||
} |
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,31 @@ | ||
// pages/api/shieldedPool/[token_inner].ts | ||
import { testnetConstants } from "../../../constants/configConstants"; | ||
import { ShieldedPoolQuerier } from "../../../utils/protos/services/app/shielded-pool"; | ||
import { base64ToUint8Array } from "../../../utils/math/base64"; | ||
import { | ||
AssetId, | ||
Metadata, | ||
} from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb"; | ||
|
||
export default async function assetMetadataHandler(req: any, res: any) { | ||
const { token_inner } = req.query; | ||
|
||
const decodedTokenInner = decodeURIComponent(token_inner); | ||
|
||
const pool_querier = new ShieldedPoolQuerier({ | ||
grpcEndpoint: testnetConstants.grpcEndpoint, | ||
}); | ||
|
||
try { | ||
const positionId = new AssetId({ | ||
inner: base64ToUint8Array(decodedTokenInner), | ||
}); | ||
|
||
const data = await pool_querier.assetMetadata(positionId); | ||
|
||
res.status(200).json(data as Metadata); | ||
} catch (error) { | ||
console.error("Error fetching asset metadata grpc data:", error); | ||
res.status(500).json({"error": "Error fetching asset metadata grpc data"}); | ||
} | ||
} |
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,94 @@ | ||
// pages/api/simulations/[...params].ts | ||
import { testnetConstants } from "../../../constants/configConstants"; | ||
import { SimulationQuerier } from "@/utils/protos/services/dex/simulated-trades"; | ||
import { base64ToUint8Array } from "../../../utils/math/base64"; | ||
import { | ||
SimulateTradeRequest, | ||
SimulateTradeRequest_Routing_SingleHop, | ||
SimulateTradeResponse, | ||
SwapExecution, | ||
} from "@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/dex/v1/dex_pb"; | ||
import { tokenConfigMapOnSymbol } from "@/constants/tokenConstants"; | ||
import { joinLoHi, splitLoHi } from "@/utils/math/hiLo"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function simulationHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const params = req.query.params as string[]; | ||
|
||
const token1 = params[0] || null; | ||
const token2 = params[1] || null; | ||
const amountIn = params[2] || null; | ||
const singleHop = params[3] || null; | ||
|
||
let isSingleHop = false; | ||
|
||
try { | ||
if (!token1 || !token2 || !amountIn) { | ||
return res.status(400).json({ error: "Invalid query parameters" }); | ||
} | ||
|
||
if (String(singleHop).toLocaleLowerCase() === "singlehop") { | ||
isSingleHop = true; | ||
} | ||
|
||
// Get token 1 & 2 | ||
const asset1Token = tokenConfigMapOnSymbol[token1]; | ||
const asset2Token = tokenConfigMapOnSymbol[token2]; | ||
|
||
const sim_querier = new SimulationQuerier({ | ||
grpcEndpoint: testnetConstants.grpcEndpoint, | ||
}); | ||
|
||
const amtIn = splitLoHi(BigInt(Number(amountIn) * 10 ** asset1Token.decimals)); | ||
|
||
let simRequest = new SimulateTradeRequest({}); | ||
if (!isSingleHop) { | ||
simRequest = new SimulateTradeRequest({ | ||
input: { | ||
assetId: { | ||
inner: base64ToUint8Array(asset1Token.inner), | ||
}, | ||
amount: { | ||
lo: amtIn.lo, | ||
hi: amtIn.hi, | ||
}, | ||
}, | ||
output: { | ||
inner: base64ToUint8Array(asset2Token.inner), | ||
}, | ||
}); | ||
} else { | ||
simRequest = new SimulateTradeRequest({ | ||
input: { | ||
assetId: { | ||
inner: base64ToUint8Array(asset1Token.inner), | ||
}, | ||
amount: { | ||
lo: amtIn.lo, | ||
hi: amtIn.hi, | ||
}, | ||
}, | ||
output: { | ||
inner: base64ToUint8Array(asset2Token.inner), | ||
}, | ||
routing: { | ||
setting: { | ||
case: "singleHop", | ||
value: SimulateTradeRequest_Routing_SingleHop, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
const data = await sim_querier.simulateTrade(simRequest); | ||
res.status(200).json(data as SwapExecution); | ||
} catch (error) { | ||
console.error("Error simulation trade grpc data:", error); | ||
res | ||
.status(500) | ||
.json({ error: `Error simualtion trade 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
Oops, something went wrong.