Skip to content

Commit 9c7f6d7

Browse files
MazyGioryangoree
andauthored
fix lp apy formula (#1349)
* fix lp apy formula * Update packages/hyperdrive-js-core/src/hyperdrive/base/ReadHyperdrive.ts Co-authored-by: Ryan Goree <[email protected]> * delete comment --------- Co-authored-by: Ryan Goree <[email protected]>
1 parent f718679 commit 9c7f6d7

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/hyperdrive-js-core/src/hyperdrive/base/ReadHyperdrive.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
import { HyperdriveSdkError } from "src/errors/HyperdriveSdkError";
2222
import { getBlockFromReadOptions } from "src/evm-client/utils/getBlockFromReadOptions";
2323
import { getBlockOrThrow } from "src/evm-client/utils/getBlockOrThrow";
24-
import { fixed, ln } from "src/fixed-point";
24+
import { fixed } from "src/fixed-point";
2525
import { HyperdriveAbi, hyperdriveAbi } from "src/hyperdrive/base/abi";
2626
import { MAX_ITERATIONS, NULL_BYTES } from "src/hyperdrive/constants";
2727
import { calculateAprFromPrice } from "src/hyperdrive/utils/calculateAprFromPrice";
@@ -2023,7 +2023,7 @@ export class ReadHyperdrive extends ReadModel {
20232023
* p_1 = to lpSharePrice
20242024
* t = term length in fractions of a year
20252025
*
2026-
* r = ln(p_1 / p_0) / t
2026+
* r = (p_1 / p_0) ^ (1 / t) - 1
20272027
*/
20282028
function calculateLpApy({
20292029
startingLpSharePrice,
@@ -2036,5 +2036,8 @@ function calculateLpApy({
20362036
}): number {
20372037
const priceRatio = fixed(endingLpSharePrice).div(startingLpSharePrice);
20382038
const yearFraction = fixed(timeFrame).div(SECONDS_PER_YEAR);
2039-
return ln(priceRatio).div(yearFraction).toNumber();
2039+
return priceRatio
2040+
.pow(fixed(1e18).div(yearFraction))
2041+
.sub(fixed(1e18))
2042+
.toNumber();
20402043
}

0 commit comments

Comments
 (0)