Skip to content

Commit

Permalink
Add getPresentValue to ReadHyperdrive (#993)
Browse files Browse the repository at this point in the history
* Rename getLiquidity -> getIdleLiquidity

* Add getPresentValue to ReadHyperdrive

* Fix issues from new hyperwasm names
  • Loading branch information
DannyDelott authored Apr 19, 2024
1 parent a218f5f commit 069f3c7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
12 changes: 8 additions & 4 deletions apps/hyperdrive-sdk-docs/docs/sdk/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ AMM](https://www.github.com/delvtech/hyperdrive).
The SDK has been designed to seamlessly integrate with multiple web3 libraries.
Choose one below to get started:

- [Viem Quickstart](#viem-quickstart)
- [Ethers Quickstart](#ethers-quickstart)
- [Web3.js Quickstart](#web3js-quickstart)
- [Getting Started](#getting-started)
- [Viem Quickstart](#viem-quickstart)
- [Install](#install)
- [Using the `ReadHyperdrive`](#using-the-readhyperdrive)
- [Using the `ReadWriteHyperdrive`](#using-the-readwritehyperdrive)
- [Ethers Quickstart](#ethers-quickstart)
- [Web3.js Quickstart](#web3js-quickstart)

## Viem Quickstart

Expand Down Expand Up @@ -50,7 +54,7 @@ const hyperdrive = createReadHyperdrive({
});

// 3. Get data from the contracts
const liquidity = await hyperdrive.getLiquidity();
const liquidity = await hyperdrive.getIdleLiquidity();
```

### Using the `ReadWriteHyperdrive`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function useLiquidity({
queryKey: makeQueryKey("liquidity", { hyperdriveAddress }),
queryFn: queryEnabled
? async () => {
const liquidity = await readHyperdrive.getLiquidity();
const liquidity = await readHyperdrive.getIdleLiquidity();
return {
liquidity: liquidity,
formatted: dnum.format([liquidity, decimals], { digits: 0 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function useMarketRowData(): UseQueryResult<MarketTableRowData[]> {
baseTokenAddress: hyperdrive.baseToken,
tokens: appConfig.tokens,
});
const liquidity = await readHyperdrive.getLiquidity();
const liquidity = await readHyperdrive.getIdleLiquidity();

const longApr = await readHyperdrive.getSpotRate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ export class ReadHyperdrive extends ReadModel {
}

/**
* This function retrieves the available market liquidity
* This function retrieves the market liquidity available for trading and LP
* removal
*/
async getLiquidity(options?: ContractReadOptions): Promise<bigint> {
async getIdleLiquidity(options?: ContractReadOptions): Promise<bigint> {
const poolConfig = await this.getPoolConfig(options);
const poolInfo = await this.getPoolInfo(options);

Expand All @@ -171,6 +172,24 @@ export class ReadHyperdrive extends ReadModel {
return BigInt(liquidityString);
}

/**
* Gets the total present value of the pool.
* @param options
* @returns
*/
async getPresentValue(options?: ContractReadOptions): Promise<bigint> {
const poolConfig = await this.getPoolConfig(options);
const poolInfo = await this.getPoolInfo(options);

const liquidityString = hyperwasm.presentValue(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
Math.floor(Date.now() / 1000).toString(),
);

return BigInt(liquidityString);
}

/**
* Gets the yield accrued on an amount of bonds shorted in a given checkpoint.
* Note that shorts stop accruing yield once they reach maturity.
Expand Down Expand Up @@ -804,7 +823,7 @@ export class ReadHyperdrive extends ReadModel {
const stringifiedPoolInfo = convertBigIntsToStrings(poolInfo);
const stringifiedPoolConfig = convertBigIntsToStrings(poolConfig);

const maxBondsOut = hyperwasm.getMaxShort(
const maxBondsOut = hyperwasm.maxShort(
stringifiedPoolInfo,
stringifiedPoolConfig,
MAX_UINT256.toString(),
Expand Down Expand Up @@ -856,7 +875,7 @@ export class ReadHyperdrive extends ReadModel {
const stringifiedPoolInfo = convertBigIntsToStrings(poolInfo);
const stringifiedPoolConfig = convertBigIntsToStrings(poolConfig);

const maxBaseIn = hyperwasm.getMaxLong(
const maxBaseIn = hyperwasm.maxLong(
stringifiedPoolInfo,
stringifiedPoolConfig,
MAX_UINT256.toString(),
Expand Down Expand Up @@ -1185,7 +1204,7 @@ export class ReadHyperdrive extends ReadModel {
}

const spotPriceAfterOpen = BigInt(
hyperwasm.calcSpotPriceAfterLong(
hyperwasm.spotPriceAfterLong(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
depositAmountConvertedToBase.toString(),
Expand All @@ -1212,7 +1231,7 @@ export class ReadHyperdrive extends ReadModel {
);

const curveFeeInBonds = BigInt(
hyperwasm.getOpenLongCurveFees(
hyperwasm.openLongCurveFee(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
depositAmountConvertedToBase.toString(),
Expand Down Expand Up @@ -1285,7 +1304,7 @@ export class ReadHyperdrive extends ReadModel {
}

const spotPriceAfterOpen = BigInt(
hyperwasm.calcSpotPriceAfterShort(
hyperwasm.spotPriceAfterShort(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
amountOfBondsToShort.toString(),
Expand All @@ -1306,11 +1325,11 @@ export class ReadHyperdrive extends ReadModel {
)[0];

const curveFeeInBase = BigInt(
hyperwasm.getOpenShortCurveFees(
hyperwasm.openShortCurveFee(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
amountOfBondsToShort.toString(),
hyperwasm.getSpotPrice(
hyperwasm.spotPrice(
convertBigIntsToStrings(poolInfo),
convertBigIntsToStrings(poolConfig),
),
Expand Down

0 comments on commit 069f3c7

Please sign in to comment.