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.
Files changed (98) hide show
  1. package/lib/CustodyAccount.d.ts +29 -0
  2. package/lib/CustodyAccount.d.ts.map +1 -0
  3. package/lib/CustodyAccount.js +64 -0
  4. package/lib/CustodyAccount.js.map +1 -0
  5. package/lib/OraclePrice.d.ts +18 -0
  6. package/lib/OraclePrice.d.ts.map +1 -0
  7. package/lib/OraclePrice.js +83 -0
  8. package/lib/OraclePrice.js.map +1 -0
  9. package/lib/PerpetualsClient.d.ts +1937 -0
  10. package/lib/PerpetualsClient.d.ts.map +1 -0
  11. package/lib/PerpetualsClient.js +1013 -0
  12. package/lib/PerpetualsClient.js.map +1 -0
  13. package/lib/PoolAccount.d.ts +34 -0
  14. package/lib/PoolAccount.d.ts.map +1 -0
  15. package/lib/PoolAccount.js +306 -0
  16. package/lib/PoolAccount.js.map +1 -0
  17. package/lib/PositionAccount.d.ts +26 -0
  18. package/lib/PositionAccount.d.ts.map +1 -0
  19. package/lib/PositionAccount.js +90 -0
  20. package/lib/PositionAccount.js.map +1 -0
  21. package/lib/client/src/CustodyAccount.d.ts +30 -0
  22. package/lib/client/src/CustodyAccount.d.ts.map +1 -0
  23. package/lib/client/src/CustodyAccount.js +64 -0
  24. package/lib/client/src/CustodyAccount.js.map +1 -0
  25. package/lib/client/src/OraclePrice.d.ts +19 -0
  26. package/lib/client/src/OraclePrice.d.ts.map +1 -0
  27. package/lib/client/src/OraclePrice.js +84 -0
  28. package/lib/client/src/OraclePrice.js.map +1 -0
  29. package/lib/client/src/PerpetualsClient.d.ts +1940 -0
  30. package/lib/client/src/PerpetualsClient.d.ts.map +1 -0
  31. package/lib/client/src/PerpetualsClient.js +1013 -0
  32. package/lib/client/src/PerpetualsClient.js.map +1 -0
  33. package/lib/client/src/PoolAccount.d.ts +35 -0
  34. package/lib/client/src/PoolAccount.d.ts.map +1 -0
  35. package/lib/client/src/PoolAccount.js +307 -0
  36. package/lib/client/src/PoolAccount.js.map +1 -0
  37. package/lib/client/src/PositionAccount.d.ts +27 -0
  38. package/lib/client/src/PositionAccount.d.ts.map +1 -0
  39. package/lib/client/src/PositionAccount.js +91 -0
  40. package/lib/client/src/PositionAccount.js.map +1 -0
  41. package/lib/client/src/constants/index.d.ts +12 -0
  42. package/lib/client/src/constants/index.d.ts.map +1 -0
  43. package/lib/client/src/constants/index.js +15 -0
  44. package/lib/client/src/constants/index.js.map +1 -0
  45. package/lib/client/src/index.d.ts +8 -0
  46. package/lib/client/src/index.d.ts.map +1 -0
  47. package/lib/client/src/index.js +24 -0
  48. package/lib/client/src/index.js.map +1 -0
  49. package/lib/client/src/target/types/perpetuals.d.ts +3539 -0
  50. package/lib/client/src/target/types/perpetuals.d.ts.map +1 -0
  51. package/lib/client/src/target/types/perpetuals.js +3541 -0
  52. package/lib/client/src/target/types/perpetuals.js.map +1 -0
  53. package/lib/client/src/types/index.d.ts +188 -0
  54. package/lib/client/src/types/index.d.ts.map +1 -0
  55. package/lib/client/src/types/index.js +57 -0
  56. package/lib/client/src/types/index.js.map +1 -0
  57. package/lib/client/src/utils/helpers.d.ts +8 -0
  58. package/lib/client/src/utils/helpers.d.ts.map +1 -0
  59. package/lib/client/src/utils/helpers.js +152 -0
  60. package/lib/client/src/utils/helpers.js.map +1 -0
  61. package/lib/constants/index.d.ts +11 -0
  62. package/lib/constants/index.d.ts.map +1 -0
  63. package/lib/constants/index.js +14 -0
  64. package/lib/constants/index.js.map +1 -0
  65. package/lib/index.d.ts +8 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +24 -0
  68. package/lib/index.js.map +1 -0
  69. package/lib/target/types/perpetuals.d.ts +3538 -0
  70. package/lib/target/types/perpetuals.d.ts.map +1 -0
  71. package/lib/target/types/perpetuals.js +3540 -0
  72. package/lib/target/types/perpetuals.js.map +1 -0
  73. package/lib/tsconfig.tsbuildinfo +1 -0
  74. package/lib/types/index.d.ts +187 -0
  75. package/lib/types/index.d.ts.map +1 -0
  76. package/lib/types/index.js +56 -0
  77. package/lib/types/index.js.map +1 -0
  78. package/lib/utils/helpers.d.ts +7 -0
  79. package/lib/utils/helpers.d.ts.map +1 -0
  80. package/lib/utils/helpers.js +151 -0
  81. package/lib/utils/helpers.js.map +1 -0
  82. package/lib/utils/index.d.ts +7 -0
  83. package/lib/utils/index.js +151 -0
  84. package/package.json +35 -0
  85. package/readme.md +4 -0
  86. package/src/CustodyAccount.ts +90 -0
  87. package/src/OraclePrice.ts +108 -0
  88. package/src/PerpetualsClient.ts +983 -0
  89. package/src/PoolAccount.ts +434 -0
  90. package/src/PositionAccount.ts +57 -0
  91. package/src/constants/index.ts +17 -0
  92. package/src/index.ts +14 -0
  93. package/src/readme.md +4 -0
  94. package/src/target/types/perpetuals.js +3540 -0
  95. package/src/target/types/perpetuals.ts +7075 -0
  96. package/src/types/index.ts +260 -0
  97. package/src/utils/index.ts +169 -0
  98. package/tsconfig.json +23 -0
@@ -0,0 +1,260 @@
1
+ import { BN } from "@project-serum/anchor";
2
+ import { PublicKey } from "@solana/web3.js";
3
+ import { OraclePrice } from "../OraclePrice";
4
+
5
+ export type PositionSide = "long" | "short";
6
+
7
+ // taken from drift
8
+ export function isVariant(object: any, type: string) {
9
+ return object.hasOwnProperty(type);
10
+ }
11
+
12
+ // isOneOfVariant(side, ['long', 'short']
13
+ export function isOneOfVariant(object: any, types: string[]) {
14
+ return types.reduce((result, type) => {
15
+ return result || object.hasOwnProperty(type);
16
+ }, false);
17
+ }
18
+
19
+ // simple type cannot be used as anchor somehow makes it a object while parseing
20
+ // export enum Side {
21
+ // None,
22
+ // Long,
23
+ // Short,
24
+ // }
25
+ export class Side {
26
+ static None = { none: {} };
27
+ static Long = { long: {} };
28
+ static Short = { short: {} };
29
+ }
30
+
31
+ // =======================================================
32
+ // ================== POSITION ====================
33
+ // =======================================================
34
+
35
+ export interface Position {
36
+ owner: PublicKey,
37
+ pool: PublicKey,
38
+ custody: PublicKey,
39
+ // lockCustody: PublicKey,
40
+
41
+ openTime: BN,
42
+ updateTime: BN,
43
+
44
+ side: Side,
45
+ price: BN,
46
+ sizeUsd: BN,
47
+ collateralUsd: BN,
48
+ unrealizedProfitUsd: BN,
49
+ unrealizedLossUsd: BN,
50
+ cumulativeInterestSnapshot: BN,
51
+ lockedAmount: BN,
52
+ collateralAmount: BN,
53
+
54
+ bump: number
55
+ }
56
+
57
+ // =======================================================
58
+ // ================== POOL ========================
59
+ // =======================================================
60
+ export interface Pool {
61
+ name: string;
62
+ custodies: PublicKey[];
63
+ ratios: TokenRatios[];
64
+ aumUsd: BN;
65
+ bump: number;
66
+ lpTokenBump: number;
67
+ inceptionTime: BN;
68
+ }
69
+
70
+ export interface TokenRatios {
71
+ target: BN;
72
+ min: BN;
73
+ max: BN;
74
+ }
75
+
76
+ // =======================================================
77
+ // ================== CUSTODY =====================
78
+ // =======================================================
79
+
80
+ export interface Custody {
81
+ // static parameters
82
+ pool: PublicKey;
83
+ mint: PublicKey;
84
+ tokenAccount: PublicKey;
85
+ decimals: number;
86
+ isStable: boolean;
87
+ oracle: OracleParams;
88
+ pricing: PricingParams;
89
+ permissions: Permissions;
90
+ fees: Fees;
91
+ borrowRate: BorrowRateParams;
92
+
93
+ // dynamic variables
94
+ assets: Assets;
95
+ collectedFees: FeesStats;
96
+ volumeStats: VolumeStats;
97
+ tradeStats: TradeStats;
98
+
99
+ longPositions: PositionStats,
100
+ shortPositions: PositionStats,
101
+ borrowRateState: BorrowRateState,
102
+
103
+ // bumps for address validation
104
+ bump: number;
105
+ tokenAccountBump: number;
106
+ }
107
+
108
+ export interface BorrowRateParams {
109
+ // borrow rate params have implied RATE_DECIMALS decimals
110
+ baseRate: BN,
111
+ slope1: BN,
112
+ slope2: BN,
113
+ optimalUtilization: BN,
114
+ }
115
+
116
+ export interface BorrowRateState {
117
+ // borrow rates have implied RATE_DECIMALS decimals
118
+ currentRate: BN,
119
+ cumulativeInterest: BN,
120
+ lastUpdate: BN,
121
+ }
122
+
123
+ export interface PositionStats {
124
+ openPositions: BN,
125
+ collateralUsd: BN,
126
+ sizeUsd: BN,
127
+ lockedAmount: BN,
128
+ weightedLeverage: BN,
129
+ totalLeverage: BN,
130
+ cumulativeInterestUsd: BN,
131
+ cumulativeInterestSnapshot: BN,
132
+ }
133
+
134
+
135
+
136
+ export interface Assets {
137
+ collateral: BN;
138
+ protocolFees: BN;
139
+ owned: BN;
140
+ locked: BN;
141
+ }
142
+
143
+ export interface FeesStats {
144
+ swapUsd: BN;
145
+ addLiquidityUsd: BN;
146
+ removeLiquidityUsd: BN;
147
+ openPositionUsd: BN;
148
+ closePositionUsd: BN;
149
+ liquidationUsd: BN;
150
+ }
151
+
152
+ export interface VolumeStats {
153
+ swapUsd: BN;
154
+ addLiquidityUsd: BN;
155
+ removeLiquidityUsd: BN;
156
+ openPositionUsd: BN;
157
+ closePositionUsd: BN;
158
+ liquidationUsd: BN;
159
+ }
160
+
161
+ export interface Fees {
162
+ mode: FeesMode;
163
+ ratioMult :BN;
164
+ utilizationMult : BN;
165
+ swapIn: BN,
166
+ swapOut: BN,
167
+ stableSwapIn: BN,
168
+ stableSwapOut: BN,
169
+ addLiquidity: BN,
170
+ removeLiquidity: BN,
171
+ openPosition: BN,
172
+ closePosition: BN,
173
+ liquidation: BN,
174
+ protocolShare: BN,
175
+ }
176
+
177
+ // export enum FeesMode {
178
+ // Fixed,
179
+ // Linear
180
+ // }
181
+ export class FeesMode {
182
+ static Fixed = { fixed: {} };
183
+ static Linear = { linear: {} };
184
+ }
185
+
186
+ export interface OracleParams {
187
+ oracleAccount: PublicKey;
188
+ oracleType: OracleType;
189
+ maxPriceError: BN;
190
+ maxPriceAgeSec: number;
191
+ }
192
+
193
+ // export enum OracleType {
194
+ // None,
195
+ // Test,
196
+ // Pyth,
197
+ // }
198
+
199
+ export class OracleType {
200
+ static None = { none: {} };
201
+ static Test = { test: {} };
202
+ static Pyth = { pyth: {} };
203
+ }
204
+
205
+ export interface Permissions {
206
+ allowSwap: boolean;
207
+ allowAddLiquidity: boolean;
208
+ allowRemoveLiquidity: boolean;
209
+ allowOpenPosition: boolean;
210
+ allowClosePosition: boolean;
211
+ allowPnlWithdrawal: boolean;
212
+ allowCollateralWithdrawal: boolean;
213
+ allowSizeChange: boolean;
214
+ }
215
+
216
+ export interface PricingParams {
217
+ useEma: boolean;
218
+ useUnrealizedPnlInAum: boolean;
219
+
220
+ tradeSpreadLong: BN;
221
+ tradeSpreadShort: BN;
222
+ swapSpread: BN;
223
+
224
+ minInitialLeverage: BN;
225
+ maxInitialLeverage: BN;
226
+ maxLeverage: BN;
227
+
228
+ maxPayoffMult: BN;
229
+ maxUtilization: BN;
230
+ // USD denominated values always have implied USD_DECIMALS decimals
231
+ maxPositionLockedUsd: BN;
232
+ maxTotalLockedUsd: BN;
233
+ }
234
+
235
+ export interface TradeStats {
236
+ profitUsd: BN;
237
+ lossUsd: BN;
238
+ oiLongUsd: BN;
239
+ oiShortUsd: BN;
240
+ }
241
+
242
+ // export interface Price {
243
+ // price: BN, //i64,
244
+ // conf?: BN,
245
+ // /// Exponent.
246
+ // expo: BN, // i32,
247
+ // /// Publish time.
248
+ // publish_time?: BN,//UnixTimestamp,
249
+ // }
250
+
251
+ export interface PriceFeed {
252
+ /// Unique identifier for this price.
253
+ // id: any,// PriceIdentifier,
254
+ /// Price.
255
+ price: OraclePrice,
256
+ /// Exponentially-weighted moving average (EMA) price.
257
+ ema_price: OraclePrice,
258
+ }
259
+
260
+
@@ -0,0 +1,169 @@
1
+ import BN from "bn.js";
2
+ import { BN_ONE, BN_ZERO } from "../constants";
3
+
4
+ export const getUnixTs = () => {
5
+ return new Date().getTime() / 1000;
6
+ };
7
+
8
+
9
+ // recheck ?? logic
10
+ export const scaleToExponent = (arg: BN, exponent: BN, target_exponent: BN) : BN => {
11
+ if (target_exponent.eq(exponent)) {
12
+ return arg;
13
+ }
14
+ let delta = target_exponent.sub(exponent);
15
+ if (delta.gt(BN_ZERO)) {
16
+ // checked_div(arg, checked_pow(10, delta as usize)?)
17
+ return arg.div( new BN(10).pow(delta) );
18
+ } else {
19
+ // checked_mul(arg, checked_pow(10, (-delta) as usize)?)
20
+ return arg.mul( new BN(10).pow(delta.muln(-1)))
21
+ }
22
+ }
23
+
24
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
25
+ export const checkedCeilDiv = (arg1: BN, arg2: BN) => {
26
+ if (arg1.gt(BN_ZERO)){
27
+ if (arg1.eq(arg2) && !arg2.isZero()) {
28
+ return BN_ONE;
29
+ }
30
+ let res = (arg1.sub(BN_ONE)).div(arg2);
31
+ if(!res){
32
+ throw Error("error :: MathOverflow")
33
+ }
34
+ return res.add(BN_ONE);
35
+ } else {
36
+ let res = (arg1).div(arg2);
37
+ if(!res){
38
+ throw Error("error :: MathOverflow")
39
+ }
40
+ return res;
41
+ }
42
+ }
43
+
44
+ export const checkedDecimalCeilMul = (
45
+ coefficient1: BN,
46
+ exponent1: BN,
47
+ coefficient2: BN,
48
+ exponent2: BN,
49
+ target_exponent: BN,
50
+ ) : BN => {
51
+ if (coefficient1.isZero() || coefficient2.isZero()) {
52
+ return BN_ZERO;
53
+ }
54
+ let target_power = (exponent1.add(exponent2)).sub(target_exponent);
55
+ if (target_power.gt(BN_ZERO) ){
56
+ // checked_as_u64(checked_mul(
57
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
58
+ // checked_pow(10u128, target_power as usize)?,
59
+ // )?)
60
+
61
+ return (coefficient1.mul(coefficient2)).mul(new BN(10).pow(target_power));
62
+
63
+ } else {
64
+ // checked_as_u64(checked_ceil_div(
65
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
66
+ // checked_pow(10u128, (-target_power) as usize)?,
67
+ // )?)
68
+
69
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
70
+ //
71
+ const a = (coefficient1.mul(coefficient2));
72
+ const b = (new BN(10).pow(target_power.muln(-1)))
73
+ // return (a.add(b).subn(1)).div(b);
74
+ return checkedCeilDiv(a,b);
75
+ }
76
+ }
77
+
78
+ export const checkedDecimalMul = (
79
+ coefficient1: BN,
80
+ exponent1: BN,
81
+ coefficient2: BN,
82
+ exponent2: BN,
83
+ target_exponent: BN,
84
+ ) : BN => {
85
+ if (coefficient1.isZero() || coefficient2.isZero()) {
86
+ return BN_ZERO;
87
+ }
88
+ let target_power = (exponent1.add(exponent2)).sub(target_exponent);
89
+ if (target_power.gt(BN_ZERO) ){
90
+ // checked_as_u64(checked_mul(
91
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
92
+ // checked_pow(10u128, target_power as usize)?,
93
+ // )?)
94
+
95
+ return (coefficient1.mul(coefficient2)).mul(new BN(10).pow(target_power));
96
+
97
+ } else {
98
+ // checked_as_u64(checked_div(
99
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
100
+ // checked_pow(10u128, (-target_power) as usize)?,
101
+ // )?)
102
+
103
+ return (coefficient1.mul(coefficient2))
104
+ .div
105
+ (new BN(10).pow(target_power.muln(-1)));
106
+ }
107
+ }
108
+
109
+ export const checkedDecimalDiv = (
110
+ coefficient1: BN,
111
+ exponent1: BN,
112
+ coefficient2: BN,
113
+ exponent2: BN,
114
+ target_exponent: BN,
115
+ ) : BN => {
116
+ if (coefficient2.isZero()) {
117
+ // msg!("Error: Overflow in {} / {}", coefficient1, coefficient2);
118
+ // return err!(PerpetualsError::MathOverflow);
119
+ throw Error(`"Error: Overflow in ${coefficient1} / ${coefficient2}`)
120
+ }
121
+ if (coefficient1.isZero()) {
122
+ return BN_ZERO;
123
+ }
124
+ // compute scale factor for the dividend
125
+ let scale_factor = BN_ZERO;
126
+ let target_power = (exponent1.sub(exponent2)).sub(target_exponent);
127
+ if (exponent1.gt(BN_ZERO)) {
128
+ scale_factor = scale_factor.add(exponent1);
129
+ }
130
+ if (exponent2.lt(BN_ZERO)) {
131
+ scale_factor = scale_factor.sub(exponent2);
132
+ target_power = target_power.add(exponent2);
133
+ }
134
+ if (target_exponent.lt(BN_ZERO)) {
135
+ scale_factor = scale_factor.sub(target_exponent);
136
+ target_power = target_power.add(target_exponent);
137
+ }
138
+ let scaled_coeff1 = BN_ZERO;
139
+ if (scale_factor.gt(BN_ZERO)) {
140
+ // checked_mul(
141
+ // coefficient1 ,
142
+ // checked_pow(10u128, scale_factor as usize)?,
143
+ // )?
144
+ scaled_coeff1 = coefficient1.mul(
145
+ new BN(10).pow(scale_factor)
146
+ );
147
+ } else {
148
+ scaled_coeff1 = coefficient1;
149
+ };
150
+
151
+ if (target_power.gte(BN_ZERO)) {
152
+ // return (
153
+ // checked_mul(
154
+ // checked_div(scaled_coeff1, coefficient2),
155
+ // checked_pow(10u128, target_power),
156
+ // ))
157
+ return (scaled_coeff1.div(coefficient2))
158
+ .mul( new BN(10).pow(target_power) );
159
+
160
+ } else {
161
+ // return checked_as_u64(checked_div(
162
+ // checked_div(scaled_coeff1, coefficient2 as u128)?,
163
+ // checked_pow(10u128, (-target_power) as usize)?,
164
+ // ))
165
+ return (scaled_coeff1.div(coefficient2))
166
+ .div( new BN(10).pow(target_power.muln(-1)) );
167
+
168
+ }
169
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "incremental": true, /* Enable incremental compilation */
4
+ "target": "es5", /* Specify ECMAScript target version: */
5
+ // "lib": ["es2019"],
6
+ "module": "commonjs", /* 'none', 'commonjs', 'amd', 'system', etc */
7
+ "declaration": true, /* Concatenate & emit output to single file.*/
8
+ "outDir": "lib", /* Redirect output to the directory. */
9
+ "esModuleInterop": true, /* Enables intero between CommonJS and ES */
10
+ "skipLibCheck": true, /* Skip type checking of declaration files. */
11
+ "forceConsistentCasingInFileNames": true, /* Disallow inconsistently */
12
+ "allowJs": true,
13
+ "checkJs": true,
14
+
15
+ // "declarationMap": true,
16
+ // "noImplicitAny": false,
17
+ // "resolveJsonModule": true,
18
+ // "sourceMap": true,
19
+
20
+ },
21
+ "include": ["./src/**/*"],
22
+ "exclude": ["./src/**/*.test.js", "node_modules", "**/node_modules"]
23
+ }