flash-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/CustodyAccount.d.ts +29 -0
- package/lib/CustodyAccount.d.ts.map +1 -0
- package/lib/CustodyAccount.js +64 -0
- package/lib/CustodyAccount.js.map +1 -0
- package/lib/OraclePrice.d.ts +18 -0
- package/lib/OraclePrice.d.ts.map +1 -0
- package/lib/OraclePrice.js +83 -0
- package/lib/OraclePrice.js.map +1 -0
- package/lib/PerpetualsClient.d.ts +1937 -0
- package/lib/PerpetualsClient.d.ts.map +1 -0
- package/lib/PerpetualsClient.js +1013 -0
- package/lib/PerpetualsClient.js.map +1 -0
- package/lib/PoolAccount.d.ts +34 -0
- package/lib/PoolAccount.d.ts.map +1 -0
- package/lib/PoolAccount.js +306 -0
- package/lib/PoolAccount.js.map +1 -0
- package/lib/PositionAccount.d.ts +26 -0
- package/lib/PositionAccount.d.ts.map +1 -0
- package/lib/PositionAccount.js +90 -0
- package/lib/PositionAccount.js.map +1 -0
- package/lib/client/src/CustodyAccount.d.ts +30 -0
- package/lib/client/src/CustodyAccount.d.ts.map +1 -0
- package/lib/client/src/CustodyAccount.js +64 -0
- package/lib/client/src/CustodyAccount.js.map +1 -0
- package/lib/client/src/OraclePrice.d.ts +19 -0
- package/lib/client/src/OraclePrice.d.ts.map +1 -0
- package/lib/client/src/OraclePrice.js +84 -0
- package/lib/client/src/OraclePrice.js.map +1 -0
- package/lib/client/src/PerpetualsClient.d.ts +1940 -0
- package/lib/client/src/PerpetualsClient.d.ts.map +1 -0
- package/lib/client/src/PerpetualsClient.js +1013 -0
- package/lib/client/src/PerpetualsClient.js.map +1 -0
- package/lib/client/src/PoolAccount.d.ts +35 -0
- package/lib/client/src/PoolAccount.d.ts.map +1 -0
- package/lib/client/src/PoolAccount.js +307 -0
- package/lib/client/src/PoolAccount.js.map +1 -0
- package/lib/client/src/PositionAccount.d.ts +27 -0
- package/lib/client/src/PositionAccount.d.ts.map +1 -0
- package/lib/client/src/PositionAccount.js +91 -0
- package/lib/client/src/PositionAccount.js.map +1 -0
- package/lib/client/src/constants/index.d.ts +12 -0
- package/lib/client/src/constants/index.d.ts.map +1 -0
- package/lib/client/src/constants/index.js +15 -0
- package/lib/client/src/constants/index.js.map +1 -0
- package/lib/client/src/index.d.ts +8 -0
- package/lib/client/src/index.d.ts.map +1 -0
- package/lib/client/src/index.js +24 -0
- package/lib/client/src/index.js.map +1 -0
- package/lib/client/src/target/types/perpetuals.d.ts +3539 -0
- package/lib/client/src/target/types/perpetuals.d.ts.map +1 -0
- package/lib/client/src/target/types/perpetuals.js +3541 -0
- package/lib/client/src/target/types/perpetuals.js.map +1 -0
- package/lib/client/src/types/index.d.ts +188 -0
- package/lib/client/src/types/index.d.ts.map +1 -0
- package/lib/client/src/types/index.js +57 -0
- package/lib/client/src/types/index.js.map +1 -0
- package/lib/client/src/utils/helpers.d.ts +8 -0
- package/lib/client/src/utils/helpers.d.ts.map +1 -0
- package/lib/client/src/utils/helpers.js +152 -0
- package/lib/client/src/utils/helpers.js.map +1 -0
- package/lib/constants/index.d.ts +11 -0
- package/lib/constants/index.d.ts.map +1 -0
- package/lib/constants/index.js +14 -0
- package/lib/constants/index.js.map +1 -0
- package/lib/index.d.ts +8 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +24 -0
- package/lib/index.js.map +1 -0
- package/lib/target/types/perpetuals.d.ts +3538 -0
- package/lib/target/types/perpetuals.d.ts.map +1 -0
- package/lib/target/types/perpetuals.js +3540 -0
- package/lib/target/types/perpetuals.js.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types/index.d.ts +187 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +56 -0
- package/lib/types/index.js.map +1 -0
- package/lib/utils/helpers.d.ts +7 -0
- package/lib/utils/helpers.d.ts.map +1 -0
- package/lib/utils/helpers.js +151 -0
- package/lib/utils/helpers.js.map +1 -0
- package/lib/utils/index.d.ts +7 -0
- package/lib/utils/index.js +151 -0
- package/package.json +35 -0
- package/readme.md +4 -0
- package/src/CustodyAccount.ts +90 -0
- package/src/OraclePrice.ts +108 -0
- package/src/PerpetualsClient.ts +983 -0
- package/src/PoolAccount.ts +434 -0
- package/src/PositionAccount.ts +57 -0
- package/src/constants/index.ts +17 -0
- package/src/index.ts +14 -0
- package/src/readme.md +4 -0
- package/src/target/types/perpetuals.js +3540 -0
- package/src/target/types/perpetuals.ts +7075 -0
- package/src/types/index.ts +260 -0
- package/src/utils/index.ts +169 -0
- package/tsconfig.json +23 -0
@@ -0,0 +1,434 @@
|
|
1
|
+
|
2
|
+
import { BN_ZERO, BPS_DECIMALS, BPS_POWER, PERCENTAGE_DECIMALS, PRICE_DECIMALS, USD_DECIMALS } from "./constants";
|
3
|
+
import { BN } from "@coral-xyz/anchor";
|
4
|
+
import { Mint } from "@solana/spl-token";
|
5
|
+
import { Custody, FeesMode, Pool, Side, TokenRatios, isVariant } from "./types";
|
6
|
+
import { PublicKey } from "@solana/web3.js";
|
7
|
+
import {CustodyAccount} from "./CustodyAccount";
|
8
|
+
import { OraclePrice } from "./OraclePrice";
|
9
|
+
import { PositionAccount } from "./PositionAccount";
|
10
|
+
import { checkedCeilDiv, checkedDecimalCeilMul, checkedDecimalMul, scaleToExponent } from "./utils";
|
11
|
+
|
12
|
+
|
13
|
+
export class PoolAccount {
|
14
|
+
|
15
|
+
publicKey: PublicKey;
|
16
|
+
|
17
|
+
// all Pool Type data IMP:: SHOULD MATCH NAMES
|
18
|
+
name: string;
|
19
|
+
custodies: PublicKey[];
|
20
|
+
ratios: TokenRatios[];
|
21
|
+
aumUsd: BN;
|
22
|
+
bump: number;
|
23
|
+
lpTokenBump: number;
|
24
|
+
inceptionTime: BN;
|
25
|
+
|
26
|
+
|
27
|
+
constructor(publicKey: PublicKey, parseData: Pool) {
|
28
|
+
this.publicKey = publicKey;
|
29
|
+
Object.assign(this, parseData);
|
30
|
+
}
|
31
|
+
|
32
|
+
static from(publicKey: PublicKey, parseData: Pool): PoolAccount {
|
33
|
+
return new PoolAccount(publicKey, parseData);
|
34
|
+
}
|
35
|
+
|
36
|
+
|
37
|
+
updatePoolData(parseData: Pool) {
|
38
|
+
Object.assign(this, parseData);
|
39
|
+
}
|
40
|
+
|
41
|
+
getTokenId(custodyKey: PublicKey) : number {
|
42
|
+
return this.custodies.findIndex(i => i.toBase58()==custodyKey.toBase58())
|
43
|
+
}
|
44
|
+
|
45
|
+
// loadlpData(lpTokenInfo : Mint){
|
46
|
+
// this.lpTokenInfo = lpTokenInfo
|
47
|
+
// }
|
48
|
+
|
49
|
+
getAddLiquidityFee(
|
50
|
+
tokenId: number,
|
51
|
+
amount: BN,
|
52
|
+
custody: CustodyAccount,
|
53
|
+
tokenPrice: OraclePrice,
|
54
|
+
) : BN {
|
55
|
+
return this.getFee(
|
56
|
+
tokenId,
|
57
|
+
custody.fees.addLiquidity,
|
58
|
+
amount,
|
59
|
+
new BN(0),
|
60
|
+
custody,
|
61
|
+
tokenPrice,
|
62
|
+
)
|
63
|
+
}
|
64
|
+
|
65
|
+
// TODO
|
66
|
+
getFee(
|
67
|
+
tokenId: number,
|
68
|
+
baseFee: BN,
|
69
|
+
amountAdd: BN,
|
70
|
+
amountRemove: BN,
|
71
|
+
custody: CustodyAccount,
|
72
|
+
tokenPrice: OraclePrice,
|
73
|
+
) : BN {
|
74
|
+
if (custody.fees.mode === FeesMode.Fixed) {
|
75
|
+
// return Self::get_fee_amount(base_fee, std::cmp::max(amount_add, amount_remove));
|
76
|
+
return this.getFeeAmount(baseFee,BN.max(amountAdd, amountRemove))
|
77
|
+
}
|
78
|
+
// === NOW FEE MODE IS FeesMode.Linear
|
79
|
+
// if token ratio is improved:
|
80
|
+
// fee = base_fee / ratio_fee
|
81
|
+
// otherwise:
|
82
|
+
// fee = base_fee * ratio_fee
|
83
|
+
// where:
|
84
|
+
// if new_ratio < ratios.target:
|
85
|
+
// ratio_fee = 1 + custody.fees.ratio_mult * (ratios.target - new_ratio) / (ratios.target - ratios.min);
|
86
|
+
// otherwise:
|
87
|
+
// ratio_fee = 1 + custody.fees.ratio_mult * (new_ratio - ratios.target) / (ratios.max - ratios.target);
|
88
|
+
|
89
|
+
let ratios = this.ratios[tokenId];
|
90
|
+
let current_ratio = this.getCurrentRatio(custody, tokenPrice);
|
91
|
+
throw Error("TODODO");
|
92
|
+
// let new_ratio = self.get_new_ratio(amount_add, amount_remove, custody, token_price)?;
|
93
|
+
|
94
|
+
// let improved = match new_ratio.cmp(&ratios.target) {
|
95
|
+
// Ordering::Less => {
|
96
|
+
// new_ratio > current_ratio
|
97
|
+
// || (current_ratio > ratios.target
|
98
|
+
// && current_ratio - ratios.target > ratios.target - new_ratio)
|
99
|
+
// }
|
100
|
+
// Ordering::Greater => {
|
101
|
+
// new_ratio < current_ratio
|
102
|
+
// || (current_ratio < ratios.target
|
103
|
+
// && ratios.target - current_ratio > new_ratio - ratios.target)
|
104
|
+
// }
|
105
|
+
// Ordering::Equal => current_ratio != ratios.target,
|
106
|
+
// };
|
107
|
+
|
108
|
+
// let ratio_fee = if new_ratio <= ratios.target {
|
109
|
+
// if ratios.target == ratios.min {
|
110
|
+
// Perpetuals::BPS_POWER
|
111
|
+
// } else {
|
112
|
+
// math::checked_add(
|
113
|
+
// Perpetuals::BPS_POWER,
|
114
|
+
// math::checked_div(
|
115
|
+
// math::checked_mul(
|
116
|
+
// custody.fees.ratio_mult as u128,
|
117
|
+
// math::checked_sub(ratios.target, new_ratio)? as u128,
|
118
|
+
// )?,
|
119
|
+
// math::checked_sub(ratios.target, ratios.min)? as u128,
|
120
|
+
// )?,
|
121
|
+
// )?
|
122
|
+
// }
|
123
|
+
// } else if ratios.target == ratios.max {
|
124
|
+
// Perpetuals::BPS_POWER
|
125
|
+
// } else {
|
126
|
+
// math::checked_add(
|
127
|
+
// Perpetuals::BPS_POWER,
|
128
|
+
// math::checked_div(
|
129
|
+
// math::checked_mul(
|
130
|
+
// custody.fees.ratio_mult as u128,
|
131
|
+
// math::checked_sub(new_ratio, ratios.target)? as u128,
|
132
|
+
// )?,
|
133
|
+
// math::checked_sub(ratios.max, ratios.target)? as u128,
|
134
|
+
// )?,
|
135
|
+
// )?
|
136
|
+
// };
|
137
|
+
|
138
|
+
// let fee = if improved {
|
139
|
+
// math::checked_div(
|
140
|
+
// math::checked_mul(base_fee as u128, Perpetuals::BPS_POWER)?,
|
141
|
+
// ratio_fee,
|
142
|
+
// )?
|
143
|
+
// } else {
|
144
|
+
// math::checked_div(
|
145
|
+
// math::checked_mul(base_fee as u128, ratio_fee)?,
|
146
|
+
// Perpetuals::BPS_POWER,
|
147
|
+
// )?
|
148
|
+
// };
|
149
|
+
|
150
|
+
// Self::get_fee_amount(
|
151
|
+
// math::checked_as_u64(fee)?,
|
152
|
+
// std::cmp::max(amount_add, amount_remove),
|
153
|
+
// )
|
154
|
+
|
155
|
+
|
156
|
+
return new BN(0);
|
157
|
+
}
|
158
|
+
|
159
|
+
getFeeAmount(fee: BN, amount: BN) : BN {
|
160
|
+
if (fee.isZero() || amount.isZero() ){
|
161
|
+
return new BN(0);
|
162
|
+
}
|
163
|
+
|
164
|
+
return checkedCeilDiv(
|
165
|
+
(amount.mul(fee)),
|
166
|
+
new BN(BPS_POWER)
|
167
|
+
)
|
168
|
+
}
|
169
|
+
|
170
|
+
getExitFee( size: BN, custody: CustodyAccount) : BN {
|
171
|
+
return this.getFeeAmount(custody.fees.closePosition, size)
|
172
|
+
}
|
173
|
+
|
174
|
+
getLiquidationFee( size: BN, custody: CustodyAccount) : BN {
|
175
|
+
return this.getFeeAmount(custody.fees.closePosition, size);
|
176
|
+
}
|
177
|
+
|
178
|
+
getCurrentRatio(
|
179
|
+
custody : CustodyAccount,
|
180
|
+
tokenPrice : OraclePrice
|
181
|
+
) :BN {
|
182
|
+
if (this.aumUsd.isZero()) {
|
183
|
+
return new BN(0);
|
184
|
+
}
|
185
|
+
// let ratio = math::checked_as_u64(math::checked_div(
|
186
|
+
// math::checked_mul(
|
187
|
+
// token_price.get_asset_amount_usd(custody.assets.owned, custody.decimals)? as u128,
|
188
|
+
// Perpetuals::BPS_POWER,
|
189
|
+
// )?,
|
190
|
+
// self.aum_usd,
|
191
|
+
// )?)?;
|
192
|
+
|
193
|
+
// Ok(std::cmp::min(ratio, Perpetuals::BPS_POWER as u64))
|
194
|
+
}
|
195
|
+
|
196
|
+
getExitPrice(
|
197
|
+
token_price: OraclePrice,
|
198
|
+
token_ema_price: OraclePrice,
|
199
|
+
side: Side,
|
200
|
+
custody: CustodyAccount,
|
201
|
+
) : BN {
|
202
|
+
let price = this.getPrice(
|
203
|
+
token_price,
|
204
|
+
token_ema_price,
|
205
|
+
(isVariant(side, 'long')) ? Side.Short : Side.Long,
|
206
|
+
(isVariant(side, 'long')) ? custody.pricing.tradeSpreadShort : custody.pricing.tradeSpreadLong,
|
207
|
+
);
|
208
|
+
return price.scale_to_exponent(new BN(-1*PRICE_DECIMALS)).price
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
getPrice(
|
213
|
+
token_price: OraclePrice,
|
214
|
+
token_ema_price: OraclePrice,
|
215
|
+
side: Side,
|
216
|
+
spread: BN,
|
217
|
+
) : OraclePrice {
|
218
|
+
if (isVariant(side, 'long')) {
|
219
|
+
console.log("inside long")
|
220
|
+
let max_price : OraclePrice;
|
221
|
+
if (token_price.cmp(token_ema_price)) {
|
222
|
+
max_price = token_price;
|
223
|
+
} else {
|
224
|
+
max_price = token_ema_price;
|
225
|
+
};
|
226
|
+
|
227
|
+
// Ok(OraclePrice {
|
228
|
+
// price: math::checked_add(
|
229
|
+
// max_price.price,
|
230
|
+
// math::checked_decimal_ceil_mul(
|
231
|
+
// max_price.price,
|
232
|
+
// max_price.exponent,
|
233
|
+
// spread,
|
234
|
+
// -(Perpetuals::BPS_DECIMALS as i32),
|
235
|
+
// max_price.exponent,
|
236
|
+
// )?,
|
237
|
+
// )?,
|
238
|
+
// exponent: max_price.exponent,
|
239
|
+
// })
|
240
|
+
let x = checkedDecimalCeilMul(
|
241
|
+
max_price.price,
|
242
|
+
max_price.exponent,
|
243
|
+
spread,
|
244
|
+
new BN(-1 * BPS_DECIMALS),
|
245
|
+
max_price.exponent,
|
246
|
+
);
|
247
|
+
console.log("x:",x);
|
248
|
+
return new OraclePrice({
|
249
|
+
price : max_price.price.add(
|
250
|
+
checkedDecimalCeilMul(
|
251
|
+
max_price.price,
|
252
|
+
max_price.exponent,
|
253
|
+
spread,
|
254
|
+
new BN(-1 * BPS_DECIMALS),
|
255
|
+
max_price.exponent,
|
256
|
+
)
|
257
|
+
),
|
258
|
+
exponent : max_price.exponent,
|
259
|
+
})
|
260
|
+
} else {
|
261
|
+
console.log("inside short ")
|
262
|
+
|
263
|
+
let min_price:OraclePrice;
|
264
|
+
if (token_price.cmp(token_ema_price) ) {
|
265
|
+
min_price = token_ema_price;
|
266
|
+
} else {
|
267
|
+
min_price = token_price;
|
268
|
+
};
|
269
|
+
console.log("min_price:",min_price.price.toString())
|
270
|
+
|
271
|
+
let spread_i = checkedDecimalMul(
|
272
|
+
min_price.price,
|
273
|
+
min_price.exponent,
|
274
|
+
spread,
|
275
|
+
new BN(-1 * BPS_DECIMALS),
|
276
|
+
min_price.exponent,
|
277
|
+
);
|
278
|
+
console.log("spread_i:",spread_i.toString())
|
279
|
+
|
280
|
+
let price : BN;
|
281
|
+
if (spread_i.lt(min_price.price) ){
|
282
|
+
price = min_price.price.sub(spread_i)
|
283
|
+
} else {
|
284
|
+
price = BN_ZERO;
|
285
|
+
};
|
286
|
+
|
287
|
+
return new OraclePrice({
|
288
|
+
price : price,
|
289
|
+
exponent : min_price.exponent
|
290
|
+
})
|
291
|
+
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
getPnlUsd(
|
296
|
+
position: PositionAccount,
|
297
|
+
token_price: OraclePrice,
|
298
|
+
token_ema_price: OraclePrice,
|
299
|
+
custody: CustodyAccount,
|
300
|
+
curtime: BN,
|
301
|
+
liquidation: boolean,
|
302
|
+
) {
|
303
|
+
|
304
|
+
if (position.sizeUsd.isZero() || position.price.isZero()){
|
305
|
+
return {
|
306
|
+
profit: BN_ZERO,
|
307
|
+
loss: BN_ZERO,
|
308
|
+
exitFee: BN_ZERO
|
309
|
+
}
|
310
|
+
}
|
311
|
+
let min_price: OraclePrice;
|
312
|
+
if (token_price.cmp(token_ema_price)) {
|
313
|
+
min_price = token_price
|
314
|
+
} else {
|
315
|
+
min_price = token_ema_price
|
316
|
+
};
|
317
|
+
|
318
|
+
|
319
|
+
let exit_price = this.getExitPrice(token_price, token_ema_price, position.side, custody);
|
320
|
+
console.log("exit_price:",exit_price, exit_price?.toString())
|
321
|
+
|
322
|
+
|
323
|
+
let size = token_ema_price.getTokenAmount(position.sizeUsd, custody.decimals);
|
324
|
+
console.log("size:",size, size?.toString())
|
325
|
+
|
326
|
+
let exit_fee :BN;
|
327
|
+
if (liquidation) {
|
328
|
+
exit_fee = this.getLiquidationFee(size, custody)
|
329
|
+
} else {
|
330
|
+
exit_fee = this.getExitFee(size, custody)
|
331
|
+
};
|
332
|
+
console.log("exit_fee:",exit_fee.toString())
|
333
|
+
|
334
|
+
let exit_fee_usd = token_ema_price.getAssetAmountUsd(exit_fee, custody.decimals);
|
335
|
+
console.log("exit_fee_usd:",exit_fee_usd.toString())
|
336
|
+
|
337
|
+
let interest_usd = custody.getInterestAmountUsd(position, curtime);
|
338
|
+
console.log("interest_usd:",interest_usd.toString())
|
339
|
+
|
340
|
+
let unrealized_loss_usd = (exit_fee_usd.add(interest_usd)).add(position.unrealizedLossUsd);
|
341
|
+
console.log("unrealized_loss_usd:",unrealized_loss_usd.toString())
|
342
|
+
|
343
|
+
let price_diff_profit, price_diff_loss ;
|
344
|
+
if (isVariant(position.side, 'long')) {
|
345
|
+
if (exit_price.gt(position.price)) {
|
346
|
+
price_diff_profit = exit_price.sub(position.price)
|
347
|
+
price_diff_loss = BN_ZERO
|
348
|
+
} else {
|
349
|
+
price_diff_profit = BN_ZERO;
|
350
|
+
price_diff_loss = position.price.sub(exit_price);
|
351
|
+
}
|
352
|
+
} else if (exit_price.lt(position.price) ){
|
353
|
+
price_diff_profit = position.price.sub(exit_price);
|
354
|
+
price_diff_loss = BN_ZERO
|
355
|
+
} else {
|
356
|
+
price_diff_profit = BN_ZERO;
|
357
|
+
price_diff_loss = exit_price.sub(position.price);
|
358
|
+
};
|
359
|
+
console.log("1 price_diff_profit:",price_diff_profit.toString())
|
360
|
+
console.log("2 price_diff_loss:",price_diff_loss.toString())
|
361
|
+
|
362
|
+
|
363
|
+
let position_price = scaleToExponent(
|
364
|
+
position.price,
|
365
|
+
new BN(-1 * PRICE_DECIMALS),
|
366
|
+
new BN(-1 * USD_DECIMALS ),
|
367
|
+
);
|
368
|
+
console.log("position_price:",position_price.toString())
|
369
|
+
|
370
|
+
if (price_diff_profit.gt(BN_ZERO)) {
|
371
|
+
|
372
|
+
let potential_profit_usd = (position.sizeUsd.mul(price_diff_profit)).div(position_price);
|
373
|
+
|
374
|
+
potential_profit_usd = potential_profit_usd.add(position.unrealizedProfitUsd);
|
375
|
+
console.log("potential_profit_usd:",potential_profit_usd.toString())
|
376
|
+
|
377
|
+
|
378
|
+
if (potential_profit_usd.gte(unrealized_loss_usd)) {
|
379
|
+
let cur_profit_usd = potential_profit_usd.sub(unrealized_loss_usd);
|
380
|
+
let max_profit_usd =
|
381
|
+
min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
|
382
|
+
|
383
|
+
console.log("cur_profit_usd:",cur_profit_usd.toString())
|
384
|
+
console.log("max_profit_usd:",max_profit_usd.toString())
|
385
|
+
|
386
|
+
return {
|
387
|
+
profit: BN.min(max_profit_usd, cur_profit_usd),
|
388
|
+
loss: BN_ZERO,
|
389
|
+
exitFee: exit_fee
|
390
|
+
}
|
391
|
+
} else {
|
392
|
+
console.log(" -- unrealized_loss_usd:",unrealized_loss_usd.toString())
|
393
|
+
console.log(" -- potential_profit_usd:",potential_profit_usd.toString())
|
394
|
+
|
395
|
+
return {
|
396
|
+
profit: BN_ZERO,
|
397
|
+
loss: unrealized_loss_usd.sub(potential_profit_usd),
|
398
|
+
exitFee: exit_fee
|
399
|
+
}
|
400
|
+
}
|
401
|
+
} else {
|
402
|
+
let potential_loss_usd = checkedCeilDiv(
|
403
|
+
position.sizeUsd.mul(price_diff_loss),
|
404
|
+
position_price,
|
405
|
+
);
|
406
|
+
|
407
|
+
potential_loss_usd = potential_loss_usd.add(unrealized_loss_usd);
|
408
|
+
console.log("potential_loss_usd:",potential_loss_usd.toString())
|
409
|
+
|
410
|
+
|
411
|
+
if ( potential_loss_usd.gte(position.unrealizedProfitUsd) ) {
|
412
|
+
console.log("position.unrealizedProfitUsd:",position.unrealizedProfitUsd.toString())
|
413
|
+
|
414
|
+
return {
|
415
|
+
profit: BN_ZERO,
|
416
|
+
loss: potential_loss_usd.sub(position.unrealizedProfitUsd),
|
417
|
+
exitFee: exit_fee
|
418
|
+
}
|
419
|
+
} else {
|
420
|
+
let cur_profit_usd = position.unrealizedProfitUsd.sub(potential_loss_usd);
|
421
|
+
let max_profit_usd = min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
|
422
|
+
console.log("cur_profit_usd:",cur_profit_usd.toString())
|
423
|
+
console.log("max_profit_usd:",max_profit_usd.toString())
|
424
|
+
|
425
|
+
return {
|
426
|
+
profit: BN.min(max_profit_usd, cur_profit_usd),
|
427
|
+
loss: BN_ZERO,
|
428
|
+
exitFee: exit_fee
|
429
|
+
}
|
430
|
+
}
|
431
|
+
}
|
432
|
+
} //getPnlUsd
|
433
|
+
|
434
|
+
} // Pool
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { BN } from "@coral-xyz/anchor";
|
2
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
3
|
+
import { isVariant, Position, Side } from "./types";
|
4
|
+
|
5
|
+
|
6
|
+
export class PositionAccount {
|
7
|
+
|
8
|
+
public publicKey: PublicKey;
|
9
|
+
|
10
|
+
// all Position Type data IMP:: SHOULD MATCH NAMES
|
11
|
+
public owner: PublicKey;
|
12
|
+
public pool: PublicKey;
|
13
|
+
public custody: PublicKey;
|
14
|
+
//public lockCustody: PublicKey,
|
15
|
+
public openTime: BN;
|
16
|
+
public updateTime: BN;
|
17
|
+
|
18
|
+
public side: Side;
|
19
|
+
public price: BN;
|
20
|
+
public sizeUsd: BN;
|
21
|
+
public collateralUsd: BN;
|
22
|
+
public unrealizedProfitUsd: BN;
|
23
|
+
public unrealizedLossUsd: BN;
|
24
|
+
public cumulativeInterestSnapshot: BN;
|
25
|
+
public lockedAmount: BN;
|
26
|
+
public collateralAmount: BN;
|
27
|
+
// extra
|
28
|
+
|
29
|
+
constructor( publicKey : PublicKey, parseData : Position) {
|
30
|
+
this.publicKey = publicKey;
|
31
|
+
Object.assign(this, parseData);
|
32
|
+
}
|
33
|
+
|
34
|
+
static from(publicKey: PublicKey,parseData : Position): PositionAccount {
|
35
|
+
return new PositionAccount( publicKey,parseData);
|
36
|
+
}
|
37
|
+
|
38
|
+
updatePositionData(position: Position) {
|
39
|
+
Object.assign(this, { ...position })
|
40
|
+
}
|
41
|
+
|
42
|
+
static async getLiquidationPrice( poolKey: PublicKey, custodyKey: PublicKey, position: PublicKey) {
|
43
|
+
try {
|
44
|
+
} catch (error) {
|
45
|
+
return new BN(0);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
static async getPnl( poolKey: PublicKey, custodyKey: PublicKey, position: PublicKey) {
|
50
|
+
try {
|
51
|
+
} catch (error) {
|
52
|
+
return new BN(0);
|
53
|
+
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { BN } from "bn.js";
|
2
|
+
|
3
|
+
export const PERCENTAGE_DECIMALS = 4; // stableCoinPercentage
|
4
|
+
export const PRICE_DECIMALS = 6; //
|
5
|
+
export const USD_DECIMALS = 6; //
|
6
|
+
|
7
|
+
|
8
|
+
export const BPS_DECIMALS = 4; //
|
9
|
+
export const BPS_POWER = 10**(BPS_DECIMALS);
|
10
|
+
|
11
|
+
export const LP_DECIMALS = USD_DECIMALS;
|
12
|
+
|
13
|
+
export const RATE_DECIMALS = 9; // borow rate
|
14
|
+
export const RATE_POWER = 10**(RATE_DECIMALS);
|
15
|
+
|
16
|
+
export const BN_ZERO = new BN(0);
|
17
|
+
export const BN_ONE = new BN(1);
|
package/src/index.ts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
export * from './PoolAccount';
|
2
|
+
export * from './CustodyAccount';
|
3
|
+
export * from './PositionAccount';
|
4
|
+
export * from './OraclePrice';
|
5
|
+
export * from './constants';
|
6
|
+
export * from './types';
|
7
|
+
export * from './utils';
|
8
|
+
export * from './PerpetualsClient';
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
|
package/src/readme.md
ADDED