-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPerpPosition.ts
88 lines (65 loc) · 2.44 KB
/
PerpPosition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { makeAutoObservable } from "mobx";
import { BN } from "@compolabs/spark-orderbook-ts-sdk";
import { FuelNetwork } from "@blockchain";
import { Token } from "./Token";
interface PerpPositionParams {
baseTokenAddress: string;
lastTwPremiumGrowthGlobal: BN;
takerOpenNational: BN;
takerPositionSize: BN;
}
export class PerpPosition {
readonly baseToken: Token;
readonly quoteToken: Token;
readonly lastTwPremiumGrowthGlobal: BN;
readonly takerOpenNational: BN;
readonly takerPositionSize: BN;
private _markPrice = BN.ZERO;
setMarkPrice = (price: BN) => (this._markPrice = price);
private _imRatio = BN.ZERO;
setImRatio = (ratio: BN) => (this._imRatio = ratio);
_pendingFundingPayment = BN.ZERO;
setPendingFundingPayment = (payment: BN) => (this._pendingFundingPayment = payment);
constructor(params: PerpPositionParams) {
const bcNetwork = FuelNetwork.getInstance();
this.baseToken = bcNetwork.getTokenByAssetId(params.baseTokenAddress);
this.quoteToken = bcNetwork.getTokenBySymbol("USDC");
this.lastTwPremiumGrowthGlobal = params.lastTwPremiumGrowthGlobal;
this.takerOpenNational = BN.formatUnits(params.takerOpenNational, this.baseToken.decimals);
this.takerPositionSize = BN.formatUnits(params.takerPositionSize, this.baseToken.decimals);
makeAutoObservable(this);
}
get symbol(): string {
return `${this.baseToken.symbol}-PERP`;
}
get entrySizeValue() {
return this.takerPositionSize.multipliedBy(this.markPrice);
}
get markPrice() {
return BN.formatUnits(this._markPrice, this.quoteToken.decimals);
}
get imRatio() {
return BN.formatUnits(this._imRatio, this.quoteToken.decimals);
}
get pendingFundingPayment() {
return BN.formatUnits(this._pendingFundingPayment, this.quoteToken.decimals);
}
get margin() {
return this.entrySizeValue.multipliedBy(this.imRatio);
}
get type() {
return this.takerPositionSize.isPositive() ? "Long" : "Short";
}
get entryPrice() {
return this.takerOpenNational.div(this.takerPositionSize);
}
get unrealizedPnl() {
return this.takerPositionSize.multipliedBy(this.markPrice).plus(this.takerOpenNational);
}
get unrealizedPnlPercent() {
return this.takerPositionSize.multipliedBy(this.markPrice).dividedBy(this.takerOpenNational).minus(1);
}
get isUnrealizedPnlInProfit() {
return this.takerOpenNational.multipliedBy(this.takerPositionSize).multipliedBy(this.markPrice).isPositive();
}
}