Add getCumulativeFunding to client. (#602)

* Add getCumulativeFunding to client.
This commit is contained in:
Nicholas Clarke 2023-10-09 01:27:44 -07:00 committed by GitHub
parent 817bf9bbc6
commit 81f4929648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 0 deletions

View File

@ -1277,6 +1277,11 @@ export class Serum3PositionDto {
) {}
}
export interface CumulativeFunding {
cumulativeLongFunding: number;
cumulativeShortFunding: number;
}
export class PerpPosition {
static PerpMarketIndexUnset = 65535;
static from(dto: PerpPositionDto): PerpPosition {
@ -1423,10 +1428,36 @@ export class PerpPosition {
}
return ZERO_I80F48();
}
public getUnsettledFundingUi(perpMarket: PerpMarket): number {
return toUiDecimalsForQuote(this.getUnsettledFunding(perpMarket));
}
/**
* @returns perp position cumulative funding, in quote token units.
* If the user paid $1 in funding for a short position, this would be -1e6.
* Caveat: This will only return cumulative interest since the perp position was last opened.
* If the perp position was closed and reopened multiple times it is necessary to add this result to
* cumulative funding at each of the prior perp position closings (from mango API) to get the all time
* cumulative funding.
*/
public getCumulativeFunding(perpMarket: PerpMarket): CumulativeFunding {
const funding = this.getUnsettledFunding(perpMarket).toNumber();
let cumulativeLongFunding = this.cumulativeLongFunding;
let cumulativeShortFunding = this.cumulativeShortFunding;
if (this.basePositionLots.toNumber() > 0) {
cumulativeLongFunding += funding;
} else {
cumulativeShortFunding -= funding;
}
return {
cumulativeLongFunding: cumulativeLongFunding,
cumulativeShortFunding: cumulativeShortFunding,
};
}
public getEquity(perpMarket: PerpMarket): I80F48 {
if (perpMarket.perpMarketIndex !== this.marketIndex) {
throw new Error("PerpPosition doesn't belong to the given market!");