flash-sdk 1.0.22 → 1.0.24
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 → dist}/PerpetualsClient.d.ts +2 -0
- package/{lib → dist}/constants/index.js +3 -3
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +10 -4
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/src/CustodyAccount.ts +0 -125
- package/src/OraclePrice.ts +0 -111
- package/src/PerpetualsClient.ts +0 -2112
- package/src/PoolAccount.ts +0 -491
- package/src/PoolConfig.json +0 -322
- package/src/PoolConfig.ts +0 -182
- package/src/PoolDataClient.ts +0 -173
- package/src/PositionAccount.ts +0 -58
- package/src/Token.ts +0 -1
- package/src/constants/index.ts +0 -21
- package/src/idl/perpetuals.ts +0 -7561
- package/src/index.ts +0 -19
- package/src/type-rules.md +0 -4
- package/src/types/index.ts +0 -290
- package/src/utils/index.ts +0 -234
- package/src/utils/rpc.ts +0 -162
- package/tsconfig.json +0 -23
- /package/{lib → dist}/CustodyAccount.d.ts +0 -0
- /package/{lib → dist}/CustodyAccount.js +0 -0
- /package/{lib → dist}/OraclePrice.d.ts +0 -0
- /package/{lib → dist}/OraclePrice.js +0 -0
- /package/{lib → dist}/PerpetualsClient.js +0 -0
- /package/{lib → dist}/PoolAccount.d.ts +0 -0
- /package/{lib → dist}/PoolAccount.js +0 -0
- /package/{lib → dist}/PoolConfig.d.ts +0 -0
- /package/{lib → dist}/PoolConfig.js +0 -0
- /package/{lib → dist}/PoolConfig.json +0 -0
- /package/{lib → dist}/PoolDataClient.d.ts +0 -0
- /package/{lib → dist}/PoolDataClient.js +0 -0
- /package/{lib → dist}/PositionAccount.d.ts +0 -0
- /package/{lib → dist}/PositionAccount.js +0 -0
- /package/{lib → dist}/Token.d.ts +0 -0
- /package/{lib → dist}/Token.js +0 -0
- /package/{lib → dist}/constants/index.d.ts +0 -0
- /package/{lib → dist}/idl/perpetuals.d.ts +0 -0
- /package/{lib → dist}/idl/perpetuals.js +0 -0
- /package/{lib → dist}/index.d.ts +0 -0
- /package/{lib → dist}/index.js +0 -0
- /package/{lib → dist}/types/index.d.ts +0 -0
- /package/{lib → dist}/types/index.js +0 -0
- /package/{lib → dist}/utils/index.d.ts +0 -0
- /package/{lib → dist}/utils/index.js +0 -0
- /package/{lib → dist}/utils/rpc.d.ts +0 -0
- /package/{lib → dist}/utils/rpc.js +0 -0
package/src/index.ts
DELETED
@@ -1,19 +0,0 @@
|
|
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
|
-
export * from './PoolConfig';
|
10
|
-
export * from './PoolDataClient';
|
11
|
-
export {IDL} from './idl/perpetuals';
|
12
|
-
export * from './utils/rpc';
|
13
|
-
// export { default as PoolConfigJson } from "./PoolConfig.json";
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
package/src/type-rules.md
DELETED
package/src/types/index.ts
DELETED
@@ -1,290 +0,0 @@
|
|
1
|
-
import { PublicKey } from "@solana/web3.js";
|
2
|
-
import { OraclePrice } from "../OraclePrice";
|
3
|
-
import { BN_ZERO } from "../constants";
|
4
|
-
import { BN } from "@coral-xyz/anchor";
|
5
|
-
|
6
|
-
export type PositionSide = "long" | "short";
|
7
|
-
|
8
|
-
// taken from drift
|
9
|
-
export function isVariant(object: any, type: string) {
|
10
|
-
return object.hasOwnProperty(type);
|
11
|
-
}
|
12
|
-
|
13
|
-
// isOneOfVariant(side, ['long', 'short']
|
14
|
-
export function isOneOfVariant(object: any, types: string[]) {
|
15
|
-
return types.reduce((result, type) => {
|
16
|
-
return result || object.hasOwnProperty(type);
|
17
|
-
}, false);
|
18
|
-
}
|
19
|
-
|
20
|
-
// simple type cannot be used as anchor somehow makes it a object while parseing
|
21
|
-
// export enum Side {
|
22
|
-
// None,
|
23
|
-
// Long,
|
24
|
-
// Short,
|
25
|
-
// }
|
26
|
-
export class Side {
|
27
|
-
static None = { none: {} };
|
28
|
-
static Long = { long: {} };
|
29
|
-
static Short = { short: {} };
|
30
|
-
}
|
31
|
-
|
32
|
-
export class AumCalcMode {
|
33
|
-
static Min = { min: {} };
|
34
|
-
static Max = { max: {} };
|
35
|
-
static Last = { last : {} };
|
36
|
-
static EMA = { ema : {} };
|
37
|
-
}
|
38
|
-
|
39
|
-
// =======================================================
|
40
|
-
// ================== POSITION ====================
|
41
|
-
// =======================================================
|
42
|
-
|
43
|
-
export interface Position {
|
44
|
-
owner: PublicKey,
|
45
|
-
pool: PublicKey,
|
46
|
-
custody: PublicKey,
|
47
|
-
// lockCustody: PublicKey,
|
48
|
-
|
49
|
-
openTime: BN,
|
50
|
-
updateTime: BN,
|
51
|
-
|
52
|
-
side: Side,
|
53
|
-
price: BN,
|
54
|
-
sizeUsd: BN,
|
55
|
-
collateralUsd: BN,
|
56
|
-
unrealizedProfitUsd: BN,
|
57
|
-
unrealizedLossUsd: BN,
|
58
|
-
cumulativeInterestSnapshot: BN,
|
59
|
-
lockedAmount: BN,
|
60
|
-
collateralAmount: BN,
|
61
|
-
|
62
|
-
bump: number
|
63
|
-
}
|
64
|
-
|
65
|
-
export const DEFAULT_POSITION : Position = {
|
66
|
-
owner: PublicKey.default,
|
67
|
-
pool: PublicKey.default,
|
68
|
-
custody: PublicKey.default,
|
69
|
-
// lockCustody: PublicKey.default,
|
70
|
-
|
71
|
-
openTime: BN_ZERO,
|
72
|
-
updateTime: BN_ZERO,
|
73
|
-
|
74
|
-
side: Side.Long,
|
75
|
-
price: BN_ZERO,
|
76
|
-
sizeUsd: BN_ZERO,
|
77
|
-
collateralUsd: BN_ZERO,
|
78
|
-
unrealizedProfitUsd: BN_ZERO,
|
79
|
-
unrealizedLossUsd: BN_ZERO,
|
80
|
-
cumulativeInterestSnapshot: BN_ZERO,
|
81
|
-
lockedAmount: BN_ZERO,
|
82
|
-
collateralAmount: BN_ZERO,
|
83
|
-
|
84
|
-
bump: 0
|
85
|
-
}
|
86
|
-
|
87
|
-
// =======================================================
|
88
|
-
// ================== POOL ========================
|
89
|
-
// =======================================================
|
90
|
-
export interface Pool {
|
91
|
-
name: string;
|
92
|
-
custodies: PublicKey[];
|
93
|
-
ratios: TokenRatios[];
|
94
|
-
aumUsd: BN;
|
95
|
-
bump: number;
|
96
|
-
lpTokenBump: number;
|
97
|
-
inceptionTime: BN;
|
98
|
-
}
|
99
|
-
|
100
|
-
export interface TokenRatios {
|
101
|
-
target: BN;
|
102
|
-
min: BN;
|
103
|
-
max: BN;
|
104
|
-
}
|
105
|
-
|
106
|
-
// =======================================================
|
107
|
-
// ================== CUSTODY =====================
|
108
|
-
// =======================================================
|
109
|
-
|
110
|
-
export interface Custody {
|
111
|
-
// static parameters
|
112
|
-
pool: PublicKey;
|
113
|
-
mint: PublicKey;
|
114
|
-
tokenAccount: PublicKey;
|
115
|
-
decimals: number;
|
116
|
-
isStable: boolean;
|
117
|
-
oracle: OracleParams;
|
118
|
-
pricing: PricingParams;
|
119
|
-
permissions: Permissions;
|
120
|
-
fees: Fees;
|
121
|
-
borrowRate: BorrowRateParams;
|
122
|
-
|
123
|
-
// dynamic variables
|
124
|
-
assets: Assets;
|
125
|
-
collectedFees: FeesStats;
|
126
|
-
volumeStats: VolumeStats;
|
127
|
-
tradeStats: TradeStats;
|
128
|
-
|
129
|
-
longPositions: PositionStats,
|
130
|
-
shortPositions: PositionStats,
|
131
|
-
borrowRateState: BorrowRateState,
|
132
|
-
|
133
|
-
// bumps for address validation
|
134
|
-
bump: number;
|
135
|
-
tokenAccountBump: number;
|
136
|
-
}
|
137
|
-
|
138
|
-
export interface BorrowRateParams {
|
139
|
-
// borrow rate params have implied RATE_DECIMALS decimals
|
140
|
-
baseRate: BN,
|
141
|
-
slope1: BN,
|
142
|
-
slope2: BN,
|
143
|
-
optimalUtilization: BN,
|
144
|
-
}
|
145
|
-
|
146
|
-
export interface BorrowRateState {
|
147
|
-
// borrow rates have implied RATE_DECIMALS decimals
|
148
|
-
currentRate: BN,
|
149
|
-
cumulativeInterest: BN,
|
150
|
-
lastUpdate: BN,
|
151
|
-
}
|
152
|
-
|
153
|
-
export interface PositionStats {
|
154
|
-
openPositions: BN,
|
155
|
-
collateralUsd: BN,
|
156
|
-
sizeUsd: BN,
|
157
|
-
lockedAmount: BN,
|
158
|
-
weightedPrice: BN,
|
159
|
-
totalQuantity: BN,
|
160
|
-
cumulativeInterestUsd: BN,
|
161
|
-
cumulativeInterestSnapshot: BN,
|
162
|
-
}
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
export interface Assets {
|
167
|
-
collateral: BN;
|
168
|
-
protocolFees: BN;
|
169
|
-
owned: BN;
|
170
|
-
locked: BN;
|
171
|
-
}
|
172
|
-
|
173
|
-
export interface FeesStats {
|
174
|
-
swapUsd: BN;
|
175
|
-
addLiquidityUsd: BN;
|
176
|
-
removeLiquidityUsd: BN;
|
177
|
-
openPositionUsd: BN;
|
178
|
-
closePositionUsd: BN;
|
179
|
-
liquidationUsd: BN;
|
180
|
-
}
|
181
|
-
|
182
|
-
export interface VolumeStats {
|
183
|
-
swapUsd: BN;
|
184
|
-
addLiquidityUsd: BN;
|
185
|
-
removeLiquidityUsd: BN;
|
186
|
-
openPositionUsd: BN;
|
187
|
-
closePositionUsd: BN;
|
188
|
-
liquidationUsd: BN;
|
189
|
-
}
|
190
|
-
|
191
|
-
export interface Fees {
|
192
|
-
mode: FeesMode;
|
193
|
-
ratioMult :BN;
|
194
|
-
utilizationMult : BN;
|
195
|
-
swapIn: BN,
|
196
|
-
swapOut: BN,
|
197
|
-
stableSwapIn: BN,
|
198
|
-
stableSwapOut: BN,
|
199
|
-
addLiquidity: BN,
|
200
|
-
removeLiquidity: BN,
|
201
|
-
openPosition: BN,
|
202
|
-
closePosition: BN,
|
203
|
-
liquidation: BN,
|
204
|
-
protocolShare: BN,
|
205
|
-
}
|
206
|
-
|
207
|
-
// export enum FeesMode {
|
208
|
-
// Fixed,
|
209
|
-
// Linear
|
210
|
-
// }
|
211
|
-
export class FeesMode {
|
212
|
-
static Fixed = { fixed: {} };
|
213
|
-
static Linear = { linear: {} };
|
214
|
-
}
|
215
|
-
|
216
|
-
export interface OracleParams {
|
217
|
-
oracleAccount: PublicKey;
|
218
|
-
oracleType: OracleType;
|
219
|
-
maxPriceError: BN;
|
220
|
-
maxPriceAgeSec: number;
|
221
|
-
}
|
222
|
-
|
223
|
-
// export enum OracleType {
|
224
|
-
// None,
|
225
|
-
// Test,
|
226
|
-
// Pyth,
|
227
|
-
// }
|
228
|
-
|
229
|
-
export class OracleType {
|
230
|
-
static None = { none: {} };
|
231
|
-
static Test = { test: {} };
|
232
|
-
static Pyth = { pyth: {} };
|
233
|
-
}
|
234
|
-
|
235
|
-
export interface Permissions {
|
236
|
-
allowSwap: boolean;
|
237
|
-
allowAddLiquidity: boolean;
|
238
|
-
allowRemoveLiquidity: boolean;
|
239
|
-
allowOpenPosition: boolean;
|
240
|
-
allowClosePosition: boolean;
|
241
|
-
allowPnlWithdrawal: boolean;
|
242
|
-
allowCollateralWithdrawal: boolean;
|
243
|
-
allowSizeChange: boolean;
|
244
|
-
}
|
245
|
-
|
246
|
-
export interface PricingParams {
|
247
|
-
useEma: boolean;
|
248
|
-
useUnrealizedPnlInAum: boolean;
|
249
|
-
|
250
|
-
tradeSpreadLong: BN;
|
251
|
-
tradeSpreadShort: BN;
|
252
|
-
swapSpread: BN;
|
253
|
-
|
254
|
-
minInitialLeverage: BN;
|
255
|
-
maxInitialLeverage: BN;
|
256
|
-
maxLeverage: BN;
|
257
|
-
|
258
|
-
maxPayoffMult: BN;
|
259
|
-
maxUtilization: BN;
|
260
|
-
// USD denominated values always have implied USD_DECIMALS decimals
|
261
|
-
maxPositionLockedUsd: BN;
|
262
|
-
maxTotalLockedUsd: BN;
|
263
|
-
}
|
264
|
-
|
265
|
-
export interface TradeStats {
|
266
|
-
profitUsd: BN;
|
267
|
-
lossUsd: BN;
|
268
|
-
oiLongUsd: BN;
|
269
|
-
oiShortUsd: BN;
|
270
|
-
}
|
271
|
-
|
272
|
-
// export interface Price {
|
273
|
-
// price: BN, //i64,
|
274
|
-
// conf?: BN,
|
275
|
-
// /// Exponent.
|
276
|
-
// expo: BN, // i32,
|
277
|
-
// /// Publish time.
|
278
|
-
// publish_time?: BN,//UnixTimestamp,
|
279
|
-
// }
|
280
|
-
|
281
|
-
export interface PriceFeed {
|
282
|
-
/// Unique identifier for this price.
|
283
|
-
// id: any,// PriceIdentifier,
|
284
|
-
/// Price.
|
285
|
-
price: OraclePrice,
|
286
|
-
/// Exponentially-weighted moving average (EMA) price.
|
287
|
-
ema_price: OraclePrice,
|
288
|
-
}
|
289
|
-
|
290
|
-
|
package/src/utils/index.ts
DELETED
@@ -1,234 +0,0 @@
|
|
1
|
-
import BN from "bn.js";
|
2
|
-
import { BN_ONE, BN_ZERO } from "../constants";
|
3
|
-
import { Connection, PublicKey } from "@solana/web3.js";
|
4
|
-
|
5
|
-
export const getUnixTs = () => {
|
6
|
-
return new Date().getTime() / 1000;
|
7
|
-
};
|
8
|
-
|
9
|
-
// 99999.123456
|
10
|
-
// 99999.123
|
11
|
-
export function toUiDecimals(
|
12
|
-
nativeAmount: BN | number | string,
|
13
|
-
decimals: number,
|
14
|
-
precision = 3,
|
15
|
-
commaSeperated = false
|
16
|
-
): string {
|
17
|
-
// TODO: remove BN and upgrade to bigint https://github.com/solana-labs/solana/issues/27440
|
18
|
-
|
19
|
-
if(precision> decimals){
|
20
|
-
throw "not allowed precision> decimals"
|
21
|
-
}
|
22
|
-
let r = '';
|
23
|
-
|
24
|
-
if (nativeAmount instanceof BN) {
|
25
|
-
const nativeAmountString = nativeAmount.toString();
|
26
|
-
// get decimals
|
27
|
-
const d = nativeAmountString.slice((decimals) * -1);
|
28
|
-
const p = d.slice(0 ,precision);
|
29
|
-
const nativeAmountWithoutDecimalsStr = nativeAmount.div(new BN( 10 ** decimals)).toString();
|
30
|
-
|
31
|
-
r = nativeAmountWithoutDecimalsStr + "." + p;
|
32
|
-
}
|
33
|
-
else if (typeof nativeAmount === "string") {
|
34
|
-
if( isNaN(Number(nativeAmount))){
|
35
|
-
throw "String No valid "
|
36
|
-
}
|
37
|
-
const d = nativeAmount.slice((decimals) * -1);
|
38
|
-
const p = d.slice(0 ,precision);
|
39
|
-
const nativeAmountWithoutDecimalsStr = (new BN(nativeAmount)).div(new BN( 10 ** decimals)).toString();
|
40
|
-
|
41
|
-
r = nativeAmountWithoutDecimalsStr + "." + p;
|
42
|
-
}
|
43
|
-
else if (typeof nativeAmount === "number") {
|
44
|
-
const d = nativeAmount.toString().slice((decimals) * -1);
|
45
|
-
const p = d.slice(0 ,precision);
|
46
|
-
const nativeAmountWithoutDecimalsStr = (new BN(nativeAmount)).div(new BN( 10 ** decimals)).toString();
|
47
|
-
r = nativeAmountWithoutDecimalsStr + "." + p;
|
48
|
-
}
|
49
|
-
else {
|
50
|
-
return 'type unknown'
|
51
|
-
}
|
52
|
-
|
53
|
-
if(commaSeperated){
|
54
|
-
return Number(r).toLocaleString();
|
55
|
-
} else {
|
56
|
-
return r;
|
57
|
-
}
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
export async function checkIfAccountExists(
|
62
|
-
account: PublicKey,
|
63
|
-
connection: Connection
|
64
|
-
): Promise<boolean> {
|
65
|
-
// console.log("connection print", connection);
|
66
|
-
let bal = await connection.getBalance(account);
|
67
|
-
if (bal > 0) {
|
68
|
-
return true;
|
69
|
-
} else {
|
70
|
-
return false;
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
// recheck ?? logic
|
75
|
-
export const scaleToExponent = (arg: BN, exponent: BN, target_exponent: BN) : BN => {
|
76
|
-
if (target_exponent.eq(exponent)) {
|
77
|
-
return arg;
|
78
|
-
}
|
79
|
-
let delta = target_exponent.sub(exponent);
|
80
|
-
if (delta.gt(BN_ZERO)) {
|
81
|
-
// checked_div(arg, checked_pow(10, delta as usize)?)
|
82
|
-
return arg.div( new BN(10).pow(delta) );
|
83
|
-
} else {
|
84
|
-
// checked_mul(arg, checked_pow(10, (-delta) as usize)?)
|
85
|
-
return arg.mul( new BN(10).pow(delta.muln(-1)))
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
// ceil(a/b) = ((a + b - 1) / b) , if a>=0
|
90
|
-
export const checkedCeilDiv = (arg1: BN, arg2: BN) => {
|
91
|
-
if (arg1.gt(BN_ZERO)){
|
92
|
-
if (arg1.eq(arg2) && !arg2.isZero()) {
|
93
|
-
return BN_ONE;
|
94
|
-
}
|
95
|
-
let res = (arg1.sub(BN_ONE)).div(arg2);
|
96
|
-
if(!res){
|
97
|
-
throw Error("error :: MathOverflow")
|
98
|
-
}
|
99
|
-
return res.add(BN_ONE);
|
100
|
-
} else {
|
101
|
-
let res = (arg1).div(arg2);
|
102
|
-
if(!res){
|
103
|
-
throw Error("error :: MathOverflow")
|
104
|
-
}
|
105
|
-
return res;
|
106
|
-
}
|
107
|
-
}
|
108
|
-
|
109
|
-
export const checkedDecimalCeilMul = (
|
110
|
-
coefficient1: BN,
|
111
|
-
exponent1: BN,
|
112
|
-
coefficient2: BN,
|
113
|
-
exponent2: BN,
|
114
|
-
target_exponent: BN,
|
115
|
-
) : BN => {
|
116
|
-
if (coefficient1.isZero() || coefficient2.isZero()) {
|
117
|
-
return BN_ZERO;
|
118
|
-
}
|
119
|
-
let target_power = (exponent1.add(exponent2)).sub(target_exponent);
|
120
|
-
if (target_power.gt(BN_ZERO) ){
|
121
|
-
// checked_as_u64(checked_mul(
|
122
|
-
// checked_mul(coefficient1 as u128, coefficient2 as u128)?,
|
123
|
-
// checked_pow(10u128, target_power as usize)?,
|
124
|
-
// )?)
|
125
|
-
|
126
|
-
return (coefficient1.mul(coefficient2)).mul(new BN(10).pow(target_power));
|
127
|
-
|
128
|
-
} else {
|
129
|
-
// checked_as_u64(checked_ceil_div(
|
130
|
-
// checked_mul(coefficient1 as u128, coefficient2 as u128)?,
|
131
|
-
// checked_pow(10u128, (-target_power) as usize)?,
|
132
|
-
// )?)
|
133
|
-
|
134
|
-
// ceil(a/b) = ((a + b - 1) / b) , if a>=0
|
135
|
-
//
|
136
|
-
const a = (coefficient1.mul(coefficient2));
|
137
|
-
const b = (new BN(10).pow(target_power.muln(-1)))
|
138
|
-
// return (a.add(b).subn(1)).div(b);
|
139
|
-
return checkedCeilDiv(a,b);
|
140
|
-
}
|
141
|
-
}
|
142
|
-
|
143
|
-
export const checkedDecimalMul = (
|
144
|
-
coefficient1: BN,
|
145
|
-
exponent1: BN,
|
146
|
-
coefficient2: BN,
|
147
|
-
exponent2: BN,
|
148
|
-
target_exponent: BN,
|
149
|
-
) : BN => {
|
150
|
-
if (coefficient1.isZero() || coefficient2.isZero()) {
|
151
|
-
return BN_ZERO;
|
152
|
-
}
|
153
|
-
let target_power = (exponent1.add(exponent2)).sub(target_exponent);
|
154
|
-
if (target_power.gt(BN_ZERO) ){
|
155
|
-
// checked_as_u64(checked_mul(
|
156
|
-
// checked_mul(coefficient1 as u128, coefficient2 as u128)?,
|
157
|
-
// checked_pow(10u128, target_power as usize)?,
|
158
|
-
// )?)
|
159
|
-
|
160
|
-
return (coefficient1.mul(coefficient2)).mul(new BN(10).pow(target_power));
|
161
|
-
|
162
|
-
} else {
|
163
|
-
// checked_as_u64(checked_div(
|
164
|
-
// checked_mul(coefficient1 as u128, coefficient2 as u128)?,
|
165
|
-
// checked_pow(10u128, (-target_power) as usize)?,
|
166
|
-
// )?)
|
167
|
-
|
168
|
-
return (coefficient1.mul(coefficient2))
|
169
|
-
.div
|
170
|
-
(new BN(10).pow(target_power.muln(-1)));
|
171
|
-
}
|
172
|
-
}
|
173
|
-
|
174
|
-
export const checkedDecimalDiv = (
|
175
|
-
coefficient1: BN,
|
176
|
-
exponent1: BN,
|
177
|
-
coefficient2: BN,
|
178
|
-
exponent2: BN,
|
179
|
-
target_exponent: BN,
|
180
|
-
) : BN => {
|
181
|
-
if (coefficient2.isZero()) {
|
182
|
-
// msg!("Error: Overflow in {} / {}", coefficient1, coefficient2);
|
183
|
-
// return err!(PerpetualsError::MathOverflow);
|
184
|
-
throw Error(`"Error: Overflow in ${coefficient1} / ${coefficient2}`)
|
185
|
-
}
|
186
|
-
if (coefficient1.isZero()) {
|
187
|
-
return BN_ZERO;
|
188
|
-
}
|
189
|
-
// compute scale factor for the dividend
|
190
|
-
let scale_factor = BN_ZERO;
|
191
|
-
let target_power = (exponent1.sub(exponent2)).sub(target_exponent);
|
192
|
-
if (exponent1.gt(BN_ZERO)) {
|
193
|
-
scale_factor = scale_factor.add(exponent1);
|
194
|
-
}
|
195
|
-
if (exponent2.lt(BN_ZERO)) {
|
196
|
-
scale_factor = scale_factor.sub(exponent2);
|
197
|
-
target_power = target_power.add(exponent2);
|
198
|
-
}
|
199
|
-
if (target_exponent.lt(BN_ZERO)) {
|
200
|
-
scale_factor = scale_factor.sub(target_exponent);
|
201
|
-
target_power = target_power.add(target_exponent);
|
202
|
-
}
|
203
|
-
let scaled_coeff1 = BN_ZERO;
|
204
|
-
if (scale_factor.gt(BN_ZERO)) {
|
205
|
-
// checked_mul(
|
206
|
-
// coefficient1 ,
|
207
|
-
// checked_pow(10u128, scale_factor as usize)?,
|
208
|
-
// )?
|
209
|
-
scaled_coeff1 = coefficient1.mul(
|
210
|
-
new BN(10).pow(scale_factor)
|
211
|
-
);
|
212
|
-
} else {
|
213
|
-
scaled_coeff1 = coefficient1;
|
214
|
-
};
|
215
|
-
|
216
|
-
if (target_power.gte(BN_ZERO)) {
|
217
|
-
// return (
|
218
|
-
// checked_mul(
|
219
|
-
// checked_div(scaled_coeff1, coefficient2),
|
220
|
-
// checked_pow(10u128, target_power),
|
221
|
-
// ))
|
222
|
-
return (scaled_coeff1.div(coefficient2))
|
223
|
-
.mul( new BN(10).pow(target_power) );
|
224
|
-
|
225
|
-
} else {
|
226
|
-
// return checked_as_u64(checked_div(
|
227
|
-
// checked_div(scaled_coeff1, coefficient2 as u128)?,
|
228
|
-
// checked_pow(10u128, (-target_power) as usize)?,
|
229
|
-
// ))
|
230
|
-
return (scaled_coeff1.div(coefficient2))
|
231
|
-
.div( new BN(10).pow(target_power.muln(-1)) );
|
232
|
-
|
233
|
-
}
|
234
|
-
}
|
package/src/utils/rpc.ts
DELETED
@@ -1,162 +0,0 @@
|
|
1
|
-
import { AnchorProvider } from '@coral-xyz/anchor';
|
2
|
-
import NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet';
|
3
|
-
import {
|
4
|
-
AddressLookupTableAccount,
|
5
|
-
Commitment,
|
6
|
-
ComputeBudgetProgram,
|
7
|
-
MessageV0,
|
8
|
-
Signer,
|
9
|
-
TransactionInstruction,
|
10
|
-
VersionedTransaction,
|
11
|
-
} from '@solana/web3.js';
|
12
|
-
|
13
|
-
export async function sendTransaction(
|
14
|
-
provider: AnchorProvider,
|
15
|
-
ixs: TransactionInstruction[],
|
16
|
-
alts: AddressLookupTableAccount[],
|
17
|
-
opts: any = {},
|
18
|
-
): Promise<string> {
|
19
|
-
const connection = provider.connection;
|
20
|
-
const latestBlockhash =
|
21
|
-
opts.latestBlockhash ??
|
22
|
-
(await connection.getLatestBlockhash(
|
23
|
-
opts.preflightCommitment ??
|
24
|
-
provider.opts.preflightCommitment ??
|
25
|
-
'finalized',
|
26
|
-
));
|
27
|
-
|
28
|
-
const payer = (provider as AnchorProvider).wallet;
|
29
|
-
|
30
|
-
if (opts.prioritizationFee) {
|
31
|
-
ixs = [createComputeBudgetIx(opts.prioritizationFee), ...ixs];
|
32
|
-
}
|
33
|
-
|
34
|
-
const message = MessageV0.compile({
|
35
|
-
payerKey: (provider as AnchorProvider).wallet.publicKey,
|
36
|
-
instructions: ixs,
|
37
|
-
recentBlockhash: latestBlockhash.blockhash,
|
38
|
-
addressLookupTableAccounts: alts,
|
39
|
-
});
|
40
|
-
|
41
|
-
let vtx = new VersionedTransaction(message);
|
42
|
-
if (opts?.additionalSigners?.length) {
|
43
|
-
vtx.sign([...opts?.additionalSigners]);
|
44
|
-
}
|
45
|
-
|
46
|
-
if (
|
47
|
-
typeof payer.signTransaction === 'function' &&
|
48
|
-
!(payer instanceof NodeWallet || payer.constructor.name == 'NodeWallet')
|
49
|
-
) {
|
50
|
-
vtx = (await payer.signTransaction(
|
51
|
-
vtx as any,
|
52
|
-
)) as unknown as VersionedTransaction;
|
53
|
-
} else {
|
54
|
-
// Maybe this path is only correct for NodeWallet?
|
55
|
-
vtx.sign([(payer as any).payer as Signer]);
|
56
|
-
}
|
57
|
-
|
58
|
-
const signature = await connection.sendRawTransaction(vtx.serialize(), {
|
59
|
-
skipPreflight: true, // mergedOpts.skipPreflight,
|
60
|
-
});
|
61
|
-
|
62
|
-
return signature;
|
63
|
-
}
|
64
|
-
|
65
|
-
|
66
|
-
export async function confirmTransaction(
|
67
|
-
provider: AnchorProvider,
|
68
|
-
signature: string,
|
69
|
-
opts: any = {},
|
70
|
-
): Promise<string> {
|
71
|
-
const connection = provider.connection;
|
72
|
-
const latestBlockhash =
|
73
|
-
opts.latestBlockhash ??
|
74
|
-
(await connection.getLatestBlockhash(
|
75
|
-
opts.preflightCommitment ??
|
76
|
-
provider.opts.preflightCommitment ??
|
77
|
-
'finalized',
|
78
|
-
));
|
79
|
-
|
80
|
-
const txConfirmationCommitment = opts.txConfirmationCommitment ?? 'processed';
|
81
|
-
let status: any;
|
82
|
-
if (
|
83
|
-
latestBlockhash.blockhash != null &&
|
84
|
-
latestBlockhash.lastValidBlockHeight != null
|
85
|
-
) {
|
86
|
-
status = (
|
87
|
-
await connection.confirmTransaction(
|
88
|
-
{
|
89
|
-
signature: signature,
|
90
|
-
blockhash: latestBlockhash.blockhash,
|
91
|
-
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
92
|
-
},
|
93
|
-
txConfirmationCommitment,
|
94
|
-
)
|
95
|
-
).value;
|
96
|
-
} else {
|
97
|
-
status = (
|
98
|
-
await connection.confirmTransaction(signature, txConfirmationCommitment)
|
99
|
-
).value;
|
100
|
-
}
|
101
|
-
|
102
|
-
if (status.err) {
|
103
|
-
console.warn('Tx status: ', status);
|
104
|
-
throw new TransactionFailError({
|
105
|
-
txid: signature,
|
106
|
-
message: `${JSON.stringify(status)}`,
|
107
|
-
});
|
108
|
-
}
|
109
|
-
return signature;
|
110
|
-
}
|
111
|
-
|
112
|
-
export const createComputeBudgetIx = (
|
113
|
-
microLamports: number,
|
114
|
-
): TransactionInstruction => {
|
115
|
-
const computeBudgetIx = ComputeBudgetProgram.setComputeUnitPrice({
|
116
|
-
microLamports,
|
117
|
-
});
|
118
|
-
return computeBudgetIx;
|
119
|
-
};
|
120
|
-
|
121
|
-
export class TransactionFailError extends Error {
|
122
|
-
message: string;
|
123
|
-
txid: string;
|
124
|
-
|
125
|
-
constructor({ txid, message }) {
|
126
|
-
super();
|
127
|
-
this.message = message;
|
128
|
-
this.txid = txid;
|
129
|
-
}
|
130
|
-
}
|
131
|
-
|
132
|
-
// const sendAndConfirmTransaction = async (
|
133
|
-
// ixs: TransactionInstruction[],
|
134
|
-
// opts: any = {},
|
135
|
-
// provider: AnchorProvider,
|
136
|
-
// postSendTxCallback?: Function,
|
137
|
-
// prioritizationFee?: number,
|
138
|
-
// txConfirmationCommitment?: Commitment
|
139
|
-
// ): Promise<string> => {
|
140
|
-
// return await sendTransaction(
|
141
|
-
// provider,
|
142
|
-
// ixs,
|
143
|
-
// opts.alts ?? [],
|
144
|
-
// {
|
145
|
-
// postSendTxCallback,
|
146
|
-
// prioritizationFee,
|
147
|
-
// txConfirmationCommitment,
|
148
|
-
// ...opts,
|
149
|
-
// },
|
150
|
-
// );
|
151
|
-
// }
|
152
|
-
|
153
|
-
// private async sendAndConfirmTransactionForGroup(
|
154
|
-
// group: Group,
|
155
|
-
// ixs: TransactionInstruction[],
|
156
|
-
// opts: any = {},
|
157
|
-
// ): Promise<string> {
|
158
|
-
// return await this.sendAndConfirmTransaction(ixs, {
|
159
|
-
// alts: group.addressLookupTablesList,
|
160
|
-
// ...opts,
|
161
|
-
// });
|
162
|
-
// }
|