-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show net funding and position value (perpetual only) (#530)
* Show net funding and position value (perpetual only) * FE changes * fix format * fix * move position value to user profile * fix * fix * Fix formatting, rename column --------- Co-authored-by: Tomasz Tórz <[email protected]>
- Loading branch information
1 parent
e9d06f7
commit 5275df7
Showing
11 changed files
with
255 additions
and
20 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
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,54 @@ | ||
import { State } from '@explorer/encoding' | ||
import { MerkleProof, PositionLeaf } from '@explorer/state' | ||
|
||
const FXP_BITS = 32n | ||
|
||
export interface PositionValue { | ||
fundingPayments: Record<string, bigint> | ||
positionValue: bigint | undefined | ||
} | ||
|
||
export function calculatePositionValue( | ||
merkleProof: MerkleProof<PositionLeaf>, | ||
state: State | ||
): PositionValue { | ||
const position = merkleProof.leaf | ||
const fundingPayments: Record<string, bigint> = {} | ||
let fxpBalance = position.collateralBalance << FXP_BITS | ||
|
||
// For each asset in the position | ||
for (const asset of position.assets) { | ||
// Find the current funding index for this asset | ||
const fundingIndex = state.indices.find( | ||
(idx) => idx.assetId === asset.assetId | ||
) | ||
if (!fundingIndex) { | ||
throw new Error( | ||
`Funding index not found for asset ${asset.assetId.toString()}` | ||
) | ||
} | ||
|
||
// Calculate funding payment | ||
const fundingPayment = | ||
asset.balance * (asset.fundingIndex - fundingIndex.value) | ||
|
||
// Find the current price for this asset | ||
const priceData = state.oraclePrices.find( | ||
(price) => price.assetId === asset.assetId | ||
) | ||
if (!priceData) { | ||
throw new Error(`Price not found for asset ${asset.assetId.toString()}`) | ||
} | ||
|
||
// Update the balance based on asset value and funding | ||
fxpBalance += asset.balance * priceData.price + fundingPayment | ||
|
||
// Store funding payment for this asset | ||
fundingPayments[asset.assetId.toString()] = fundingPayment >> FXP_BITS | ||
} | ||
|
||
return { | ||
fundingPayments, | ||
positionValue: fxpBalance >> FXP_BITS, | ||
} | ||
} |
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.