Open a Position
Overview
In this short guide, we'll use a
ReadWriteHyperdrive
instance to open a long
position, locking in a fixed rate (Read more about long
positions
in the Hyperdrive
docs).
The steps will be similar for short and LP positions.
This guide assumes you've already installed an implementation of the Hyperdrive TypeScript SDK and created a
ReadWriteHyperdrive
instance. Checkout the Getting Started guide to learn how.
1. Get the current vault share price
We'll start by getting the current share price of the pool using the
getPoolInfo
method which
will be used as the minimum share price for the open long transaction:
import { hyperdrive } from "./hyperdrive.ts";
const { vaultSharePrice } = await hyperdrive.getPoolInfo();
2. Calculate slippage tolerance
Use the
previewOpenLong
method to get the expected proceeds and adjust to match your slippage tolerance:
// Import path depends on the Hyperdrive SDK implementation you have installed.
import { adjustAmountByPercentage } from "@delvtech/hyperdrive-viem";
// 1. Preview the transaction
const { bondProceeds } = await hyperdrive.previewOpenLong({
amountIn: 10000000000000000000000n,
asBase: true, // <- Or false to deposit share tokens
decimals: 18,
});
// 2. Adjust by your slippage tolerance
const minBondsOut = adjustAmountByPercentage({
amount: bondProceeds,
percentage: parseUnits("1", 18), // <- Adjust for your tolerance
decimals: 18,
direction: "down",
});
3. Open the position
Use the openLong
method to
send the transaction with the minimum expected share price and your slippage
tolerance.
const pendingTransactionHash = await readWriteHyperdrive.openLong({
amount: 10000000000000000000000n,
asBase: true,
destination: "0x123abc...",
minSharePrice: vaultSharePrice,
minBondsOut,
});