Skip to content

Commit

Permalink
fix calcs for when using shares on open long preview (#1013)
Browse files Browse the repository at this point in the history
* fix calcs for bonds received and net fixed rate when using shares on open long transaction preview

* revert Long object properties

* fix Yield Accrued calc when using shares

* type fixes

* add TODO to update sdk's Long interface
  • Loading branch information
MazyGio authored Apr 25, 2024
1 parent 443ce46 commit 1ee677b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export class ReadHyperdriveModel implements IReadHyperdriveModel {
}> {
return this.readHyperdrive.previewOpenLong({
amountIn: sharesAmount,
asBase: true,
asBase: false,
options,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ export function OpenLongForm({
),
),
}}
asBase={activeToken.address === baseToken.address}
vaultSharePrice={poolInfo?.vaultSharePrice}
/>
}
disclaimer={(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as dnum from "dnum";
import { ReactElement } from "react";
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays";
import { formatRate } from "src/base/formatRate";
import { convertSharesToBase } from "src/hyperdrive/convertSharesToBase";
import { useAppConfig } from "src/ui/appconfig/useAppConfig";
import { LabelValue } from "src/ui/base/components/LabelValue";
import { formatBalance } from "src/ui/base/formatting/formatBalance";
Expand All @@ -21,6 +22,8 @@ interface OpenLongPreviewProps {
spotRateAfterOpen: bigint | undefined;
activeToken: TokenConfig<any>;
curveFee: bigint | undefined;
asBase: boolean;
vaultSharePrice: bigint | undefined;
}

export function OpenLongPreview({
Expand All @@ -29,6 +32,8 @@ export function OpenLongPreview({
spotRateAfterOpen,
activeToken,
curveFee,
asBase,
vaultSharePrice,
}: OpenLongPreviewProps): ReactElement {
const appConfig = useAppConfig();
const baseToken = findBaseToken({
Expand Down Expand Up @@ -94,7 +99,15 @@ export function OpenLongPreview({
calculateAprFromPrice({
positionDuration:
hyperdrive.poolConfig.positionDuration || 0n,
baseAmount: long.baseAmountPaid,
baseAmount: asBase
? long.baseAmountPaid
: // TODO: move sharesAmountPaid into the sdk's Long interface
// instead of converting here
convertSharesToBase({
sharesAmount: long.baseAmountPaid,
vaultSharePrice: vaultSharePrice,
decimals: activeToken.decimals,
}),
bondAmount: long.bondAmount,
}),
baseToken.decimals,
Expand Down Expand Up @@ -155,7 +168,15 @@ export function OpenLongPreview({
{long.bondAmount > long.baseAmountPaid ? "+" : ""}
{long.baseAmountPaid
? `${formatBalance({
balance: long.bondAmount - long.baseAmountPaid,
balance:
long.bondAmount -
(asBase
? long.baseAmountPaid
: convertSharesToBase({
sharesAmount: long.baseAmountPaid,
vaultSharePrice: vaultSharePrice,
decimals: baseToken.decimals,
})),
decimals: baseToken.decimals,
places: baseToken.places,
})} ${baseToken.symbol}`
Expand Down

0 comments on commit 1ee677b

Please sign in to comment.