flash-sdk 1.0.13 → 1.0.15
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.js +1 -1
- package/lib/PerpetualsClient.d.ts +8 -6
- package/lib/PerpetualsClient.js +172 -176
- package/lib/PoolAccount.js +26 -26
- package/lib/PoolConfig.json +7 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/CustodyAccount.ts +1 -1
- package/src/PerpetualsClient.ts +36 -44
- package/src/PoolAccount.ts +26 -26
- package/src/PoolConfig.json +7 -0
- package/lib/client/src/target/types/limit_order_cpi.d.ts +0 -130
- package/lib/client/src/target/types/limit_order_cpi.js +0 -132
- package/lib/client/src/target/types/perpetuals.d.ts +0 -3539
- package/lib/client/src/target/types/perpetuals.d.ts.map +0 -1
- package/lib/client/src/target/types/perpetuals.js +0 -3541
- package/lib/client/src/target/types/perpetuals.js.map +0 -1
- package/lib/target/types/perpetuals.d.ts +0 -3538
- package/lib/target/types/perpetuals.d.ts.map +0 -1
- package/lib/target/types/perpetuals.js +0 -3540
- package/lib/target/types/perpetuals.js.map +0 -1
package/lib/CustodyAccount.js
CHANGED
@@ -46,7 +46,7 @@ var CustodyAccount = /** @class */ (function () {
|
|
46
46
|
return constants_1.BN_ZERO;
|
47
47
|
}
|
48
48
|
var cumulative_interest = this.getCumulativeInterest(curtime);
|
49
|
-
console.log("cumulative_interest:",
|
49
|
+
// console.log("cumulative_interest:",cumulative_interest.toString())
|
50
50
|
var position_interest;
|
51
51
|
if (cumulative_interest.gt(position.cumulativeInterestSnapshot)) {
|
52
52
|
position_interest = cumulative_interest.sub(position.cumulativeInterestSnapshot);
|
@@ -1,3 +1,5 @@
|
|
1
|
+
/// <reference types="bn.js" />
|
2
|
+
/// <reference types="node" />
|
1
3
|
import { Program, AnchorProvider, BN } from "@coral-xyz/anchor";
|
2
4
|
import { PublicKey, TransactionInstruction, Commitment } from "@solana/web3.js";
|
3
5
|
import { PoolAccount } from "./PoolAccount";
|
@@ -1935,13 +1937,13 @@ export declare class PerpetualsClient {
|
|
1935
1937
|
getRemoveLiquidityAmountAndFee: (poolName: string, tokenMint: PublicKey, lpAmount: BN) => Promise<any>;
|
1936
1938
|
getEntryPriceAndFee: (poolName: string, tokenMint: PublicKey, collateral: BN, size: BN, side: PositionSide) => Promise<any>;
|
1937
1939
|
getExitPriceAndFee: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
|
1938
|
-
|
1939
|
-
getLiquidationPriceTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral
|
1940
|
-
|
1940
|
+
getLiquidationPriceView: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral?: BN, removeCollateral?: BN) => Promise<any>;
|
1941
|
+
getLiquidationPriceTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide, addCollateral?: BN, removeCollateral?: BN) => Promise<string>;
|
1942
|
+
getLiquidationPrice: (postionKey: PublicKey, postionData: Position, tokenPrice: OraclePrice, tokenEmaPrice: OraclePrice, custodyAccount: CustodyAccount, poolAccount: PoolAccount, currentTime: BN, addCollateral?: BN, removeCollateral?: BN) => OraclePrice;
|
1941
1943
|
getLiquidationState: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
|
1942
|
-
|
1944
|
+
getPnlView: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<any>;
|
1943
1945
|
getPnlTrx: (wallet: PublicKey, poolName: string, tokenMint: PublicKey, side: PositionSide) => Promise<string>;
|
1944
|
-
|
1946
|
+
getPnl: (postionKey: PublicKey, postionData: Position, tokenPrice: OraclePrice, tokenEmaPrice: OraclePrice, custodyAccount: CustodyAccount, poolAccount: PoolAccount, currentTime: BN) => {
|
1945
1947
|
profit: BN;
|
1946
1948
|
loss: BN;
|
1947
1949
|
};
|
@@ -1950,7 +1952,7 @@ export declare class PerpetualsClient {
|
|
1950
1952
|
getAumTrx: (poolName: string) => Promise<string>;
|
1951
1953
|
getAumSdk: (poolAccount: PoolAccount, token_prices: OraclePrice[], token_ema_prices: OraclePrice[], custodies: CustodyAccount[], aum_calc_mode: AumCalcMode, currentTime: BN) => BN;
|
1952
1954
|
openPosition: (payTokenSymbol: string, priceAfterSlippage: BN, collateral: BN, fee: BN, size: BN, side: Side, poolConfig: PoolConfig, createUserWSOLATA?: boolean) => Promise<TransactionInstruction[]>;
|
1953
|
-
closePosition: (receivingTokenSymbol: string, priceAfterSlippage: BN, side: Side, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
|
1955
|
+
closePosition: (receivingTokenSymbol: string, priceAfterSlippage: BN, side: Side, poolConfig: PoolConfig, closeUsersWSOLATA?: boolean) => Promise<TransactionInstruction[]>;
|
1954
1956
|
swap: (poolReceivingTokenSymbol: string, poolDispensingTokenSymbol: string, amountIn: BN, minAmountOut: BN, poolConfig: PoolConfig, unWrapSol?: boolean) => Promise<TransactionInstruction[]>;
|
1955
1957
|
addLiquidity: (payTokenSymbol: string, tokenAmountIn: BN, minLpAmountOut: BN, poolConfig: PoolConfig) => Promise<TransactionInstruction[]>;
|
1956
1958
|
removeLiquidity: (recieveTokenSymbol: string, liquidityAmountIn: BN, minTokenAmountOut: BN, poolConfig: PoolConfig, closeLpATA?: boolean) => Promise<TransactionInstruction[]>;
|
package/lib/PerpetualsClient.js
CHANGED
@@ -64,6 +64,7 @@ var js_sha256_1 = require("js-sha256");
|
|
64
64
|
var bs58_1 = require("bs58");
|
65
65
|
var PositionAccount_1 = require("./PositionAccount");
|
66
66
|
var types_1 = require("./types");
|
67
|
+
var OraclePrice_1 = require("./OraclePrice");
|
67
68
|
var perpetuals_1 = require("./idl/perpetuals");
|
68
69
|
var rpc_1 = require("./utils/rpc");
|
69
70
|
var utils_1 = require("./utils");
|
@@ -220,7 +221,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
220
221
|
wallet,
|
221
222
|
pool,
|
222
223
|
custody,
|
223
|
-
side === "long" ? [1] : [
|
224
|
+
side === "long" ? [1] : [2],
|
224
225
|
]).publicKey;
|
225
226
|
};
|
226
227
|
this.getPosition = function (postionKey) { return __awaiter(_this, void 0, void 0, function () {
|
@@ -773,96 +774,88 @@ var PerpetualsClient = /** @class */ (function () {
|
|
773
774
|
}
|
774
775
|
});
|
775
776
|
}); };
|
776
|
-
this.
|
777
|
-
|
778
|
-
|
779
|
-
return
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
.
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
var _c;
|
809
|
-
return __generator(this, function (_d) {
|
810
|
-
switch (_d.label) {
|
811
|
-
case 0:
|
812
|
-
_b = (_a = this.program.methods
|
813
|
-
.getLiquidationPrice({
|
814
|
-
addCollateral: addCollateral,
|
815
|
-
removeCollateral: removeCollateral,
|
816
|
-
}))
|
817
|
-
.accounts;
|
818
|
-
_c = {
|
819
|
-
perpetuals: this.perpetuals.publicKey,
|
820
|
-
pool: this.getPoolKey(poolName),
|
821
|
-
position: this.getPositionKey(wallet, poolName, tokenMint, side),
|
822
|
-
custody: this.getCustodyKey(poolName, tokenMint)
|
823
|
-
};
|
824
|
-
return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
|
825
|
-
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
|
826
|
-
_c)])
|
827
|
-
.rpc()
|
828
|
-
.catch(function (err) {
|
829
|
-
console.error(err);
|
830
|
-
throw err;
|
831
|
-
})];
|
832
|
-
case 2: return [2 /*return*/, _d.sent()];
|
833
|
-
}
|
777
|
+
this.getLiquidationPriceView = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) {
|
778
|
+
if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
|
779
|
+
if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
|
780
|
+
return __awaiter(_this, void 0, void 0, function () {
|
781
|
+
var _a, _b;
|
782
|
+
var _c;
|
783
|
+
return __generator(this, function (_d) {
|
784
|
+
switch (_d.label) {
|
785
|
+
case 0:
|
786
|
+
_b = (_a = this.program.methods
|
787
|
+
.getLiquidationPrice({
|
788
|
+
addCollateral: addCollateral,
|
789
|
+
removeCollateral: removeCollateral,
|
790
|
+
}))
|
791
|
+
.accounts;
|
792
|
+
_c = {
|
793
|
+
perpetuals: this.perpetuals.publicKey,
|
794
|
+
pool: this.getPoolKey(poolName),
|
795
|
+
position: this.getPositionKey(wallet, poolName, tokenMint, side),
|
796
|
+
custody: this.getCustodyKey(poolName, tokenMint)
|
797
|
+
};
|
798
|
+
return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
|
799
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
|
800
|
+
_c)])
|
801
|
+
.view()
|
802
|
+
.catch(function (err) {
|
803
|
+
console.error(err);
|
804
|
+
throw err;
|
805
|
+
})];
|
806
|
+
case 2: return [2 /*return*/, _d.sent()];
|
807
|
+
}
|
808
|
+
});
|
834
809
|
});
|
835
|
-
}
|
836
|
-
this.
|
837
|
-
|
838
|
-
|
839
|
-
return
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
.
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
810
|
+
};
|
811
|
+
this.getLiquidationPriceTrx = function (wallet, poolName, tokenMint, side, addCollateral, removeCollateral) {
|
812
|
+
if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
|
813
|
+
if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
|
814
|
+
return __awaiter(_this, void 0, void 0, function () {
|
815
|
+
var _a, _b;
|
816
|
+
var _c;
|
817
|
+
return __generator(this, function (_d) {
|
818
|
+
switch (_d.label) {
|
819
|
+
case 0:
|
820
|
+
_b = (_a = this.program.methods
|
821
|
+
.getLiquidationPrice({
|
822
|
+
addCollateral: addCollateral,
|
823
|
+
removeCollateral: removeCollateral,
|
824
|
+
}))
|
825
|
+
.accounts;
|
826
|
+
_c = {
|
827
|
+
perpetuals: this.perpetuals.publicKey,
|
828
|
+
pool: this.getPoolKey(poolName),
|
829
|
+
position: this.getPositionKey(wallet, poolName, tokenMint, side),
|
830
|
+
custody: this.getCustodyKey(poolName, tokenMint)
|
831
|
+
};
|
832
|
+
return [4 /*yield*/, this.getCustodyOracleAccountKey(poolName, tokenMint)];
|
833
|
+
case 1: return [4 /*yield*/, _b.apply(_a, [(_c.custodyOracleAccount = _d.sent(),
|
834
|
+
_c)])
|
835
|
+
.rpc()
|
836
|
+
.catch(function (err) {
|
837
|
+
console.error(err);
|
838
|
+
throw err;
|
839
|
+
})];
|
840
|
+
case 2: return [2 /*return*/, _d.sent()];
|
841
|
+
}
|
842
|
+
});
|
864
843
|
});
|
865
|
-
}
|
844
|
+
};
|
845
|
+
// TODO: currently just static, need to write
|
846
|
+
this.getLiquidationPrice = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime, addCollateral, removeCollateral) {
|
847
|
+
if (addCollateral === void 0) { addCollateral = new anchor_1.BN(0); }
|
848
|
+
if (removeCollateral === void 0) { removeCollateral = new anchor_1.BN(0); }
|
849
|
+
// const positionAccount = PositionAccount.from(postionKey, postionData);
|
850
|
+
var x;
|
851
|
+
if ((0, types_1.isVariant)(postionData.side, 'long')) {
|
852
|
+
x = tokenPrice.price.mul(new anchor_1.BN(90)).div(new anchor_1.BN(90));
|
853
|
+
}
|
854
|
+
else {
|
855
|
+
x = tokenPrice.price.mul(new anchor_1.BN(100)).div(new anchor_1.BN(90));
|
856
|
+
}
|
857
|
+
return new OraclePrice_1.OraclePrice({ price: x, exponent: tokenPrice.exponent });
|
858
|
+
};
|
866
859
|
this.getLiquidationState = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
|
867
860
|
var _a, _b;
|
868
861
|
var _c;
|
@@ -890,7 +883,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
890
883
|
}
|
891
884
|
});
|
892
885
|
}); };
|
893
|
-
this.
|
886
|
+
this.getPnlView = function (wallet, poolName, tokenMint, side) { return __awaiter(_this, void 0, void 0, function () {
|
894
887
|
var pos, _a, _b;
|
895
888
|
var _c;
|
896
889
|
return __generator(this, function (_d) {
|
@@ -948,7 +941,7 @@ var PerpetualsClient = /** @class */ (function () {
|
|
948
941
|
}
|
949
942
|
});
|
950
943
|
}); };
|
951
|
-
this.
|
944
|
+
this.getPnl = function (postionKey, postionData, tokenPrice, tokenEmaPrice, custodyAccount, poolAccount, currentTime) {
|
952
945
|
var positionAccount = PositionAccount_1.PositionAccount.from(postionKey, postionData);
|
953
946
|
// console.log("positionAccount:",positionAccount);
|
954
947
|
// console.log("side :", postionData.side, (isVariant(postionData.side, 'long')))
|
@@ -1043,7 +1036,8 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1043
1036
|
// TODO: handle SOL wrapping to WSOL and create a ATA - DONE
|
1044
1037
|
// TODO: Balance checks - DONE
|
1045
1038
|
// TODO: ATA check - else create - DONE
|
1046
|
-
//
|
1039
|
+
// Create WSOL Token account and not ATA and close it in end
|
1040
|
+
// TODO: close other Accounts - NOT NEEDED
|
1047
1041
|
this.openPosition = function (payTokenSymbol, priceAfterSlippage, collateral, fee, size, side, poolConfig, createUserWSOLATA // if false will also skip balance checks
|
1048
1042
|
) {
|
1049
1043
|
if (createUserWSOLATA === void 0) { createUserWSOLATA = true; }
|
@@ -1174,68 +1168,72 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1174
1168
|
// TODO: ATA check - else create - DONE
|
1175
1169
|
// TODO: for close Accounts - DONE BY ANCHOR
|
1176
1170
|
// TODO : if out token WSOL -> unwrap to SOL - DONE
|
1177
|
-
this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1171
|
+
this.closePosition = function (receivingTokenSymbol, priceAfterSlippage, side, poolConfig, closeUsersWSOLATA // to get back WSOL=>SOL
|
1172
|
+
) {
|
1173
|
+
if (closeUsersWSOLATA === void 0) { closeUsersWSOLATA = false; }
|
1174
|
+
return __awaiter(_this, void 0, void 0, function () {
|
1175
|
+
var publicKey, userReceivingTokenAccount, instructions, receivingTokenCustody, positionAccount, params, instruction, closeWsolATAIns, error_3;
|
1176
|
+
return __generator(this, function (_a) {
|
1177
|
+
switch (_a.label) {
|
1178
|
+
case 0:
|
1179
|
+
console.log("close position :::", receivingTokenSymbol, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey.toBase58());
|
1180
|
+
publicKey = this.provider.wallet.publicKey;
|
1181
|
+
return [4 /*yield*/, (0, spl_token_1.getAssociatedTokenAddress)(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey, publicKey)];
|
1182
|
+
case 1:
|
1183
|
+
userReceivingTokenAccount = _a.sent();
|
1184
|
+
instructions = [];
|
1185
|
+
_a.label = 2;
|
1186
|
+
case 2:
|
1187
|
+
_a.trys.push([2, 5, , 6]);
|
1188
|
+
return [4 /*yield*/, (0, utils_1.checkIfAccountExists)(userReceivingTokenAccount, this.provider.connection)];
|
1189
|
+
case 3:
|
1190
|
+
if (!(_a.sent())) {
|
1191
|
+
instructions.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(publicKey, userReceivingTokenAccount, publicKey, poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey));
|
1192
|
+
}
|
1193
|
+
receivingTokenCustody = poolConfig.custodies.find(function (i) { return i.mintKey.equals(poolConfig.getTokenFromSymbol(receivingTokenSymbol).mintKey); });
|
1194
|
+
positionAccount = web3_js_1.PublicKey.findProgramAddressSync([
|
1195
|
+
Buffer.from("position"),
|
1196
|
+
publicKey.toBuffer(),
|
1197
|
+
poolConfig.poolAddress.toBuffer(),
|
1198
|
+
receivingTokenCustody.custodyAccount.toBuffer(),
|
1199
|
+
(0, types_1.isVariant)(side, 'long') ? Buffer.from([1]) : Buffer.from([2]),
|
1200
|
+
], this.programId)[0];
|
1201
|
+
console.log("positionAccount:", positionAccount.toBase58());
|
1202
|
+
params = {
|
1203
|
+
price: priceAfterSlippage,
|
1204
|
+
};
|
1205
|
+
return [4 /*yield*/, this.program.methods
|
1206
|
+
.closePosition(params)
|
1207
|
+
.accounts({
|
1208
|
+
owner: publicKey,
|
1209
|
+
receivingAccount: userReceivingTokenAccount,
|
1210
|
+
transferAuthority: poolConfig.transferAuthority,
|
1211
|
+
perpetuals: poolConfig.perpetuals,
|
1212
|
+
pool: poolConfig.poolAddress,
|
1213
|
+
position: positionAccount,
|
1214
|
+
custody: receivingTokenCustody.custodyAccount,
|
1215
|
+
custodyOracleAccount: receivingTokenCustody.oracleAddress,
|
1216
|
+
custodyTokenAccount: receivingTokenCustody.tokenAccount,
|
1217
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1218
|
+
}).instruction()];
|
1219
|
+
case 4:
|
1220
|
+
instruction = _a.sent();
|
1221
|
+
instructions.push(instruction);
|
1222
|
+
// SOL is only retrievable by closing the token account and choosing the desired address to send the token account's lamports.
|
1223
|
+
if (receivingTokenSymbol == 'SOL' && closeUsersWSOLATA) {
|
1224
|
+
closeWsolATAIns = (0, spl_token_1.createCloseAccountInstruction)(userReceivingTokenAccount, publicKey, publicKey);
|
1225
|
+
instructions.push(closeWsolATAIns);
|
1226
|
+
}
|
1227
|
+
return [3 /*break*/, 6];
|
1228
|
+
case 5:
|
1229
|
+
error_3 = _a.sent();
|
1230
|
+
console.error("perpclient closePosition error:", error_3);
|
1231
|
+
return [3 /*break*/, 6];
|
1232
|
+
case 6: return [2 /*return*/, instructions];
|
1233
|
+
}
|
1234
|
+
});
|
1237
1235
|
});
|
1238
|
-
}
|
1236
|
+
};
|
1239
1237
|
// TODO: ATA check - else create - DONE
|
1240
1238
|
// TODO: handle SOL wrapping to WSOL and create a ATA - DONE
|
1241
1239
|
// TODO : Balance checks - NOT NEEDED
|
@@ -1466,35 +1464,33 @@ var PerpetualsClient = /** @class */ (function () {
|
|
1466
1464
|
throw "Insufficient Funds";
|
1467
1465
|
}
|
1468
1466
|
_f.label = 15;
|
1469
|
-
case 15:
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
.remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
|
1490
|
-
.instruction()];
|
1467
|
+
case 15: return [4 /*yield*/, this.program.methods
|
1468
|
+
.addLiquidity({
|
1469
|
+
amountIn: tokenAmountIn,
|
1470
|
+
minLpAmountOut: minLpAmountOut
|
1471
|
+
})
|
1472
|
+
.accounts({
|
1473
|
+
owner: publicKey,
|
1474
|
+
fundingAccount: userPayingTokenAccount,
|
1475
|
+
lpTokenAccount: lpTokenAccount,
|
1476
|
+
transferAuthority: poolConfig.transferAuthority,
|
1477
|
+
perpetuals: poolConfig.perpetuals,
|
1478
|
+
pool: poolConfig.poolAddress,
|
1479
|
+
custody: payTokenCustody.custodyAccount,
|
1480
|
+
custodyOracleAccount: payTokenCustody.oracleAddress,
|
1481
|
+
custodyTokenAccount: payTokenCustody.tokenAccount,
|
1482
|
+
lpTokenMint: poolConfig.lpTokenMint,
|
1483
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
1484
|
+
})
|
1485
|
+
.remainingAccounts(__spreadArray(__spreadArray([], custodyAccountMetas, true), custodyOracleAccountMetas, true))
|
1486
|
+
.instruction()];
|
1491
1487
|
case 16:
|
1492
1488
|
inx = _f.sent();
|
1493
1489
|
instructions.push(inx);
|
1494
1490
|
return [3 /*break*/, 18];
|
1495
1491
|
case 17:
|
1496
1492
|
err_3 = _f.sent();
|
1497
|
-
console.
|
1493
|
+
console.error("perpClient addLiquidity error:: ", err_3);
|
1498
1494
|
throw err_3;
|
1499
1495
|
case 18: return [2 /*return*/, instructions];
|
1500
1496
|
}
|
package/lib/PoolAccount.js
CHANGED
@@ -80,10 +80,10 @@ var PoolAccount = /** @class */ (function () {
|
|
80
80
|
PoolAccount.getPriceAfterSlippage = function (isEntry, slippageBps, token_price, token_ema_price, side, custody) {
|
81
81
|
if (isEntry) {
|
82
82
|
var current_price = this.getEntryPrice(token_price, token_ema_price, side, custody);
|
83
|
-
console.log("getEntryPrice current_price:",
|
83
|
+
// console.log("getEntryPrice current_price:",current_price.toString())
|
84
84
|
// TODO:: checked USE
|
85
85
|
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS));
|
86
|
-
console.log("getPriceAfterSlippage spread_i:",
|
86
|
+
// console.log("getPriceAfterSlippage spread_i:",spread_i.toString());
|
87
87
|
if ((0, types_1.isVariant)(side, 'long')) {
|
88
88
|
return current_price.add(spread_i);
|
89
89
|
}
|
@@ -100,10 +100,10 @@ var PoolAccount = /** @class */ (function () {
|
|
100
100
|
else {
|
101
101
|
// Opp in during close
|
102
102
|
var current_price = this.getExitPrice(token_price, token_ema_price, side, custody);
|
103
|
-
console.log("getExitPrice current_price:",
|
103
|
+
// console.log("getExitPrice current_price:",current_price.toString())
|
104
104
|
// TODO:: checked USE
|
105
105
|
var spread_i = (0, utils_1.checkedDecimalCeilMul)(current_price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), slippageBps, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS));
|
106
|
-
console.log("spread_i:",
|
106
|
+
// console.log("spread_i:",spread_i);
|
107
107
|
if ((0, types_1.isVariant)(side, 'long')) {
|
108
108
|
if (spread_i.lt(current_price)) {
|
109
109
|
return current_price.sub(spread_i);
|
@@ -120,7 +120,7 @@ var PoolAccount = /** @class */ (function () {
|
|
120
120
|
};
|
121
121
|
PoolAccount.getPrice = function (token_price, token_ema_price, side, spread) {
|
122
122
|
if ((0, types_1.isVariant)(side, 'long')) {
|
123
|
-
console.log("inside long")
|
123
|
+
// console.log("inside long")
|
124
124
|
var max_price = void 0;
|
125
125
|
if (token_price.cmp(token_ema_price)) {
|
126
126
|
max_price = token_price;
|
@@ -130,14 +130,14 @@ var PoolAccount = /** @class */ (function () {
|
|
130
130
|
}
|
131
131
|
;
|
132
132
|
var spread_i = (0, utils_1.checkedDecimalCeilMul)(max_price.price, max_price.exponent, spread, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), max_price.exponent);
|
133
|
-
console.log("getPrice spread_i:",
|
133
|
+
// console.log("getPrice spread_i:",spread_i.toString());
|
134
134
|
return new OraclePrice_1.OraclePrice({
|
135
135
|
price: max_price.price.add(spread_i),
|
136
136
|
exponent: max_price.exponent,
|
137
137
|
});
|
138
138
|
}
|
139
139
|
else {
|
140
|
-
console.log("inside short ")
|
140
|
+
// console.log("inside short ")
|
141
141
|
var min_price = void 0;
|
142
142
|
if (token_price.cmp(token_ema_price)) {
|
143
143
|
min_price = token_ema_price;
|
@@ -147,7 +147,7 @@ var PoolAccount = /** @class */ (function () {
|
|
147
147
|
}
|
148
148
|
;
|
149
149
|
var spread_i = (0, utils_1.checkedDecimalMul)(min_price.price, min_price.exponent, spread, new anchor_1.BN(-1 * constants_1.BPS_DECIMALS), min_price.exponent);
|
150
|
-
console.log("spread_i:",
|
150
|
+
// console.log("spread_i:",spread_i.toString())
|
151
151
|
var price = void 0;
|
152
152
|
if (spread_i.lt(min_price.price)) {
|
153
153
|
price = min_price.price.sub(spread_i);
|
@@ -179,9 +179,9 @@ var PoolAccount = /** @class */ (function () {
|
|
179
179
|
}
|
180
180
|
;
|
181
181
|
var exit_price = PoolAccount.getExitPrice(token_price, token_ema_price, position.side, custody);
|
182
|
-
console.log("exit_price:",
|
182
|
+
// console.log("exit_price:",exit_price, exit_price?.toString())
|
183
183
|
var size = token_ema_price.getTokenAmount(position.sizeUsd, custody.decimals);
|
184
|
-
console.log("size:",
|
184
|
+
// console.log("size:",size, size?.toString())
|
185
185
|
var exit_fee;
|
186
186
|
if (liquidation) {
|
187
187
|
exit_fee = this.getLiquidationFee(size, custody);
|
@@ -190,13 +190,13 @@ var PoolAccount = /** @class */ (function () {
|
|
190
190
|
exit_fee = this.getExitFee(size, custody);
|
191
191
|
}
|
192
192
|
;
|
193
|
-
console.log("exit_fee:",
|
193
|
+
// console.log("exit_fee:",exit_fee.toString())
|
194
194
|
var exit_fee_usd = token_ema_price.getAssetAmountUsd(exit_fee, custody.decimals);
|
195
|
-
console.log("exit_fee_usd:",
|
195
|
+
// console.log("exit_fee_usd:",exit_fee_usd.toString())
|
196
196
|
var interest_usd = custody.getInterestAmountUsd(position, curtime);
|
197
|
-
console.log("interest_usd:",
|
197
|
+
// console.log("interest_usd:",interest_usd.toString())
|
198
198
|
var unrealized_loss_usd = (exit_fee_usd.add(interest_usd)).add(position.unrealizedLossUsd);
|
199
|
-
console.log("unrealized_loss_usd:",
|
199
|
+
// console.log("unrealized_loss_usd:",unrealized_loss_usd.toString())
|
200
200
|
var price_diff_profit, price_diff_loss;
|
201
201
|
if ((0, types_1.isVariant)(position.side, 'long')) {
|
202
202
|
if (exit_price.gt(position.price)) {
|
@@ -217,19 +217,19 @@ var PoolAccount = /** @class */ (function () {
|
|
217
217
|
price_diff_loss = exit_price.sub(position.price);
|
218
218
|
}
|
219
219
|
;
|
220
|
-
console.log("1 price_diff_profit:",
|
221
|
-
console.log("2 price_diff_loss:",
|
220
|
+
// console.log("1 price_diff_profit:",price_diff_profit.toString())
|
221
|
+
// console.log("2 price_diff_loss:",price_diff_loss.toString())
|
222
222
|
var position_price = (0, utils_1.scaleToExponent)(position.price, new anchor_1.BN(-1 * constants_1.PRICE_DECIMALS), new anchor_1.BN(-1 * constants_1.USD_DECIMALS));
|
223
|
-
console.log("position_price:",
|
223
|
+
// console.log("position_price:",position_price.toString())
|
224
224
|
if (price_diff_profit.gt(constants_1.BN_ZERO)) {
|
225
225
|
var potential_profit_usd = (position.sizeUsd.mul(price_diff_profit)).div(position_price);
|
226
226
|
potential_profit_usd = potential_profit_usd.add(position.unrealizedProfitUsd);
|
227
|
-
console.log("potential_profit_usd:",
|
227
|
+
// console.log("potential_profit_usd:",potential_profit_usd.toString())
|
228
228
|
if (potential_profit_usd.gte(unrealized_loss_usd)) {
|
229
229
|
var cur_profit_usd = potential_profit_usd.sub(unrealized_loss_usd);
|
230
230
|
var max_profit_usd = min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
|
231
|
-
console.log("cur_profit_usd:",
|
232
|
-
console.log("max_profit_usd:",
|
231
|
+
// console.log("cur_profit_usd:",cur_profit_usd.toString())
|
232
|
+
// console.log("max_profit_usd:",max_profit_usd.toString())
|
233
233
|
return {
|
234
234
|
profit: anchor_1.BN.min(max_profit_usd, cur_profit_usd),
|
235
235
|
loss: constants_1.BN_ZERO,
|
@@ -237,8 +237,8 @@ var PoolAccount = /** @class */ (function () {
|
|
237
237
|
};
|
238
238
|
}
|
239
239
|
else {
|
240
|
-
console.log(" -- unrealized_loss_usd:",
|
241
|
-
console.log(" -- potential_profit_usd:",
|
240
|
+
// console.log(" -- unrealized_loss_usd:",unrealized_loss_usd.toString())
|
241
|
+
// console.log(" -- potential_profit_usd:",potential_profit_usd.toString())
|
242
242
|
return {
|
243
243
|
profit: constants_1.BN_ZERO,
|
244
244
|
loss: unrealized_loss_usd.sub(potential_profit_usd),
|
@@ -249,9 +249,9 @@ var PoolAccount = /** @class */ (function () {
|
|
249
249
|
else {
|
250
250
|
var potential_loss_usd = (0, utils_1.checkedCeilDiv)(position.sizeUsd.mul(price_diff_loss), position_price);
|
251
251
|
potential_loss_usd = potential_loss_usd.add(unrealized_loss_usd);
|
252
|
-
console.log("potential_loss_usd:",
|
252
|
+
// console.log("potential_loss_usd:",potential_loss_usd.toString())
|
253
253
|
if (potential_loss_usd.gte(position.unrealizedProfitUsd)) {
|
254
|
-
console.log("position.unrealizedProfitUsd:",
|
254
|
+
// console.log("position.unrealizedProfitUsd:",position.unrealizedProfitUsd.toString())
|
255
255
|
return {
|
256
256
|
profit: constants_1.BN_ZERO,
|
257
257
|
loss: potential_loss_usd.sub(position.unrealizedProfitUsd),
|
@@ -261,8 +261,8 @@ var PoolAccount = /** @class */ (function () {
|
|
261
261
|
else {
|
262
262
|
var cur_profit_usd = position.unrealizedProfitUsd.sub(potential_loss_usd);
|
263
263
|
var max_profit_usd = min_price.getAssetAmountUsd(position.lockedAmount, custody.decimals);
|
264
|
-
console.log("cur_profit_usd:",
|
265
|
-
console.log("max_profit_usd:",
|
264
|
+
// console.log("cur_profit_usd:",cur_profit_usd.toString())
|
265
|
+
// console.log("max_profit_usd:",max_profit_usd.toString())
|
266
266
|
return {
|
267
267
|
profit: anchor_1.BN.min(max_profit_usd, cur_profit_usd),
|
268
268
|
loss: constants_1.BN_ZERO,
|