flash-sdk 1.0.4 → 1.0.5

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 (100) hide show
  1. package/lib/CustodyAccount.js +9 -19
  2. package/lib/OraclePrice.d.ts +2 -0
  3. package/lib/OraclePrice.js +4 -0
  4. package/lib/PerpetualsClient.d.ts +26 -5
  5. package/lib/PerpetualsClient.js +586 -24
  6. package/lib/PoolAccount.d.ts +2 -1
  7. package/lib/PoolAccount.js +16 -29
  8. package/lib/PoolConfig.d.ts +7 -2
  9. package/lib/PoolConfig.js +15 -7
  10. package/lib/PoolConfig.json +4 -0
  11. package/lib/{PoolDisplayData.d.ts → PoolDataClient.d.ts} +3 -2
  12. package/lib/{PoolDisplayData.js → PoolDataClient.js} +13 -13
  13. package/lib/PositionAccount.d.ts +1 -0
  14. package/lib/Token.js +1 -0
  15. package/lib/constants/index.d.ts +1 -0
  16. package/lib/idl/perpetuals.d.ts +3675 -0
  17. package/{src/target/types → lib/idl}/perpetuals.js +140 -3
  18. package/lib/index.d.ts +3 -0
  19. package/lib/index.js +5 -0
  20. package/lib/tsconfig.tsbuildinfo +1 -1
  21. package/lib/types/index.d.ts +5 -3
  22. package/lib/types/index.js +21 -1
  23. package/lib/utils/index.d.ts +2 -0
  24. package/lib/utils/index.js +57 -1
  25. package/lib/utils/rpc.d.ts +13 -0
  26. package/lib/utils/rpc.js +217 -0
  27. package/package.json +1 -1
  28. package/src/CustodyAccount.ts +24 -23
  29. package/src/OraclePrice.ts +4 -4
  30. package/src/PerpetualsClient.ts +737 -96
  31. package/src/PoolAccount.ts +34 -32
  32. package/src/PoolConfig.json +4 -0
  33. package/src/PoolConfig.ts +21 -6
  34. package/src/{PoolDisplayData.ts → PoolDataClient.ts} +2 -2
  35. package/src/Token.ts +1 -0
  36. package/src/{target/types → idl}/perpetuals.ts +280 -6
  37. package/src/index.ts +3 -0
  38. package/src/types/index.ts +26 -3
  39. package/src/utils/index.ts +14 -0
  40. package/src/utils/rpc.ts +162 -0
  41. package/lib/CustodyAccount.d.ts.map +0 -1
  42. package/lib/CustodyAccount.js.map +0 -1
  43. package/lib/OraclePrice.d.ts.map +0 -1
  44. package/lib/OraclePrice.js.map +0 -1
  45. package/lib/PerpetualsClient.d.ts.map +0 -1
  46. package/lib/PerpetualsClient.js.map +0 -1
  47. package/lib/PoolAccount.d.ts.map +0 -1
  48. package/lib/PoolAccount.js.map +0 -1
  49. package/lib/PositionAccount.d.ts.map +0 -1
  50. package/lib/PositionAccount.js.map +0 -1
  51. package/lib/client/src/CustodyAccount.d.ts +0 -30
  52. package/lib/client/src/CustodyAccount.d.ts.map +0 -1
  53. package/lib/client/src/CustodyAccount.js +0 -64
  54. package/lib/client/src/CustodyAccount.js.map +0 -1
  55. package/lib/client/src/OraclePrice.d.ts +0 -19
  56. package/lib/client/src/OraclePrice.d.ts.map +0 -1
  57. package/lib/client/src/OraclePrice.js +0 -84
  58. package/lib/client/src/OraclePrice.js.map +0 -1
  59. package/lib/client/src/PerpetualsClient.d.ts +0 -1938
  60. package/lib/client/src/PerpetualsClient.d.ts.map +0 -1
  61. package/lib/client/src/PerpetualsClient.js +0 -1043
  62. package/lib/client/src/PerpetualsClient.js.map +0 -1
  63. package/lib/client/src/PoolAccount.d.ts +0 -35
  64. package/lib/client/src/PoolAccount.d.ts.map +0 -1
  65. package/lib/client/src/PoolAccount.js +0 -307
  66. package/lib/client/src/PoolAccount.js.map +0 -1
  67. package/lib/client/src/PositionAccount.d.ts +0 -27
  68. package/lib/client/src/PositionAccount.d.ts.map +0 -1
  69. package/lib/client/src/PositionAccount.js +0 -91
  70. package/lib/client/src/PositionAccount.js.map +0 -1
  71. package/lib/client/src/constants/index.d.ts +0 -12
  72. package/lib/client/src/constants/index.d.ts.map +0 -1
  73. package/lib/client/src/constants/index.js +0 -15
  74. package/lib/client/src/constants/index.js.map +0 -1
  75. package/lib/client/src/index.d.ts +0 -9
  76. package/lib/client/src/index.d.ts.map +0 -1
  77. package/lib/client/src/index.js +0 -26
  78. package/lib/client/src/index.js.map +0 -1
  79. package/lib/client/src/types/index.d.ts +0 -188
  80. package/lib/client/src/types/index.d.ts.map +0 -1
  81. package/lib/client/src/types/index.js +0 -57
  82. package/lib/client/src/types/index.js.map +0 -1
  83. package/lib/client/src/utils/helpers.d.ts +0 -8
  84. package/lib/client/src/utils/helpers.d.ts.map +0 -1
  85. package/lib/client/src/utils/helpers.js +0 -152
  86. package/lib/client/src/utils/helpers.js.map +0 -1
  87. package/lib/constants/index.d.ts.map +0 -1
  88. package/lib/constants/index.js.map +0 -1
  89. package/lib/index.d.ts.map +0 -1
  90. package/lib/index.js.map +0 -1
  91. package/lib/types/index.d.ts.map +0 -1
  92. package/lib/types/index.js.map +0 -1
  93. package/lib/utils/helpers.d.ts +0 -7
  94. package/lib/utils/helpers.d.ts.map +0 -1
  95. package/lib/utils/helpers.js +0 -151
  96. package/lib/utils/helpers.js.map +0 -1
  97. package/src/target/types/limit_order_cpi.js +0 -132
  98. package/src/target/types/limit_order_cpi.ts +0 -259
  99. /package/{src/target/idl/perpetuals.json → lib/Token.d.ts} +0 -0
  100. /package/src/{readme.md → type-rules.md} +0 -0
@@ -1,9 +1,45 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
2
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
40
  };
5
41
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.checkedDecimalDiv = exports.checkedDecimalMul = exports.checkedDecimalCeilMul = exports.checkedCeilDiv = exports.scaleToExponent = exports.toUiDecimals = exports.getUnixTs = void 0;
42
+ exports.checkedDecimalDiv = exports.checkedDecimalMul = exports.checkedDecimalCeilMul = exports.checkedCeilDiv = exports.scaleToExponent = exports.checkIfAccountExists = exports.toUiDecimals = exports.getUnixTs = void 0;
7
43
  var bn_js_1 = __importDefault(require("bn.js"));
8
44
  var constants_1 = require("../constants");
9
45
  var getUnixTs = function () {
@@ -54,6 +90,26 @@ function toUiDecimals(nativeAmount, decimals, precision, commaSeperated) {
54
90
  }
55
91
  }
56
92
  exports.toUiDecimals = toUiDecimals;
93
+ function checkIfAccountExists(account, connection) {
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ var bal;
96
+ return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0: return [4 /*yield*/, connection.getBalance(account)];
99
+ case 1:
100
+ bal = _a.sent();
101
+ if (bal > 0) {
102
+ return [2 /*return*/, true];
103
+ }
104
+ else {
105
+ return [2 /*return*/, false];
106
+ }
107
+ return [2 /*return*/];
108
+ }
109
+ });
110
+ });
111
+ }
112
+ exports.checkIfAccountExists = checkIfAccountExists;
57
113
  // recheck ?? logic
58
114
  var scaleToExponent = function (arg, exponent, target_exponent) {
59
115
  if (target_exponent.eq(exponent)) {
@@ -0,0 +1,13 @@
1
+ import { AnchorProvider } from '@coral-xyz/anchor';
2
+ import { AddressLookupTableAccount, TransactionInstruction } from '@solana/web3.js';
3
+ export declare function sendTransaction(provider: AnchorProvider, ixs: TransactionInstruction[], alts: AddressLookupTableAccount[], opts?: any): Promise<string>;
4
+ export declare function confirmTransaction(provider: AnchorProvider, signature: string, opts?: any): Promise<string>;
5
+ export declare const createComputeBudgetIx: (microLamports: number) => TransactionInstruction;
6
+ export declare class TransactionFailError extends Error {
7
+ message: string;
8
+ txid: string;
9
+ constructor({ txid, message }: {
10
+ txid: any;
11
+ message: any;
12
+ });
13
+ }
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
54
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
55
+ if (ar || !(i in from)) {
56
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
57
+ ar[i] = from[i];
58
+ }
59
+ }
60
+ return to.concat(ar || Array.prototype.slice.call(from));
61
+ };
62
+ var __importDefault = (this && this.__importDefault) || function (mod) {
63
+ return (mod && mod.__esModule) ? mod : { "default": mod };
64
+ };
65
+ Object.defineProperty(exports, "__esModule", { value: true });
66
+ exports.TransactionFailError = exports.createComputeBudgetIx = exports.confirmTransaction = exports.sendTransaction = void 0;
67
+ var nodewallet_1 = __importDefault(require("@coral-xyz/anchor/dist/cjs/nodewallet"));
68
+ var web3_js_1 = require("@solana/web3.js");
69
+ function sendTransaction(provider, ixs, alts, opts) {
70
+ var _a, _b, _c, _d;
71
+ if (opts === void 0) { opts = {}; }
72
+ return __awaiter(this, void 0, void 0, function () {
73
+ var connection, latestBlockhash, _e, payer, message, vtx, signature;
74
+ return __generator(this, function (_f) {
75
+ switch (_f.label) {
76
+ case 0:
77
+ connection = provider.connection;
78
+ if (!((_a = opts.latestBlockhash) !== null && _a !== void 0)) return [3 /*break*/, 1];
79
+ _e = _a;
80
+ return [3 /*break*/, 3];
81
+ case 1: return [4 /*yield*/, connection.getLatestBlockhash((_c = (_b = opts.preflightCommitment) !== null && _b !== void 0 ? _b : provider.opts.preflightCommitment) !== null && _c !== void 0 ? _c : 'finalized')];
82
+ case 2:
83
+ _e = (_f.sent());
84
+ _f.label = 3;
85
+ case 3:
86
+ latestBlockhash = _e;
87
+ payer = provider.wallet;
88
+ if (opts.prioritizationFee) {
89
+ ixs = __spreadArray([(0, exports.createComputeBudgetIx)(opts.prioritizationFee)], ixs, true);
90
+ }
91
+ message = web3_js_1.MessageV0.compile({
92
+ payerKey: provider.wallet.publicKey,
93
+ instructions: ixs,
94
+ recentBlockhash: latestBlockhash.blockhash,
95
+ addressLookupTableAccounts: alts,
96
+ });
97
+ vtx = new web3_js_1.VersionedTransaction(message);
98
+ if ((_d = opts === null || opts === void 0 ? void 0 : opts.additionalSigners) === null || _d === void 0 ? void 0 : _d.length) {
99
+ vtx.sign(__spreadArray([], opts === null || opts === void 0 ? void 0 : opts.additionalSigners, true));
100
+ }
101
+ if (!(typeof payer.signTransaction === 'function' &&
102
+ !(payer instanceof nodewallet_1.default || payer.constructor.name == 'NodeWallet'))) return [3 /*break*/, 5];
103
+ return [4 /*yield*/, payer.signTransaction(vtx)];
104
+ case 4:
105
+ vtx = (_f.sent());
106
+ return [3 /*break*/, 6];
107
+ case 5:
108
+ // Maybe this path is only correct for NodeWallet?
109
+ vtx.sign([payer.payer]);
110
+ _f.label = 6;
111
+ case 6: return [4 /*yield*/, connection.sendRawTransaction(vtx.serialize(), {
112
+ skipPreflight: true, // mergedOpts.skipPreflight,
113
+ })];
114
+ case 7:
115
+ signature = _f.sent();
116
+ return [2 /*return*/, signature];
117
+ }
118
+ });
119
+ });
120
+ }
121
+ exports.sendTransaction = sendTransaction;
122
+ function confirmTransaction(provider, signature, opts) {
123
+ var _a, _b, _c, _d;
124
+ if (opts === void 0) { opts = {}; }
125
+ return __awaiter(this, void 0, void 0, function () {
126
+ var connection, latestBlockhash, _e, txConfirmationCommitment, status;
127
+ return __generator(this, function (_f) {
128
+ switch (_f.label) {
129
+ case 0:
130
+ connection = provider.connection;
131
+ if (!((_a = opts.latestBlockhash) !== null && _a !== void 0)) return [3 /*break*/, 1];
132
+ _e = _a;
133
+ return [3 /*break*/, 3];
134
+ case 1: return [4 /*yield*/, connection.getLatestBlockhash((_c = (_b = opts.preflightCommitment) !== null && _b !== void 0 ? _b : provider.opts.preflightCommitment) !== null && _c !== void 0 ? _c : 'finalized')];
135
+ case 2:
136
+ _e = (_f.sent());
137
+ _f.label = 3;
138
+ case 3:
139
+ latestBlockhash = _e;
140
+ txConfirmationCommitment = (_d = opts.txConfirmationCommitment) !== null && _d !== void 0 ? _d : 'processed';
141
+ if (!(latestBlockhash.blockhash != null &&
142
+ latestBlockhash.lastValidBlockHeight != null)) return [3 /*break*/, 5];
143
+ return [4 /*yield*/, connection.confirmTransaction({
144
+ signature: signature,
145
+ blockhash: latestBlockhash.blockhash,
146
+ lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
147
+ }, txConfirmationCommitment)];
148
+ case 4:
149
+ status = (_f.sent()).value;
150
+ return [3 /*break*/, 7];
151
+ case 5: return [4 /*yield*/, connection.confirmTransaction(signature, txConfirmationCommitment)];
152
+ case 6:
153
+ status = (_f.sent()).value;
154
+ _f.label = 7;
155
+ case 7:
156
+ if (status.err) {
157
+ console.warn('Tx status: ', status);
158
+ throw new TransactionFailError({
159
+ txid: signature,
160
+ message: "".concat(JSON.stringify(status)),
161
+ });
162
+ }
163
+ return [2 /*return*/, signature];
164
+ }
165
+ });
166
+ });
167
+ }
168
+ exports.confirmTransaction = confirmTransaction;
169
+ var createComputeBudgetIx = function (microLamports) {
170
+ var computeBudgetIx = web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
171
+ microLamports: microLamports,
172
+ });
173
+ return computeBudgetIx;
174
+ };
175
+ exports.createComputeBudgetIx = createComputeBudgetIx;
176
+ var TransactionFailError = /** @class */ (function (_super) {
177
+ __extends(TransactionFailError, _super);
178
+ function TransactionFailError(_a) {
179
+ var txid = _a.txid, message = _a.message;
180
+ var _this = _super.call(this) || this;
181
+ _this.message = message;
182
+ _this.txid = txid;
183
+ return _this;
184
+ }
185
+ return TransactionFailError;
186
+ }(Error));
187
+ exports.TransactionFailError = TransactionFailError;
188
+ // const sendAndConfirmTransaction = async (
189
+ // ixs: TransactionInstruction[],
190
+ // opts: any = {},
191
+ // provider: AnchorProvider,
192
+ // postSendTxCallback?: Function,
193
+ // prioritizationFee?: number,
194
+ // txConfirmationCommitment?: Commitment
195
+ // ): Promise<string> => {
196
+ // return await sendTransaction(
197
+ // provider,
198
+ // ixs,
199
+ // opts.alts ?? [],
200
+ // {
201
+ // postSendTxCallback,
202
+ // prioritizationFee,
203
+ // txConfirmationCommitment,
204
+ // ...opts,
205
+ // },
206
+ // );
207
+ // }
208
+ // private async sendAndConfirmTransactionForGroup(
209
+ // group: Group,
210
+ // ixs: TransactionInstruction[],
211
+ // opts: any = {},
212
+ // ): Promise<string> {
213
+ // return await this.sendAndConfirmTransaction(ixs, {
214
+ // alts: group.addressLookupTablesList,
215
+ // ...opts,
216
+ // });
217
+ // }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flash-sdk",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "description": "Client to interact with the Flash program on Solana",
@@ -1,5 +1,5 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { Assets, FeesStats, Custody, Fees, PricingParams, TradeStats, Permissions, BorrowRateParams, OracleParams, VolumeStats, PositionStats, BorrowRateState, Side, isVariant, Position } from "./types";
2
+ import { Assets, FeesStats, Custody, Fees, PricingParams, TradeStats, Permissions, BorrowRateParams, OracleParams, VolumeStats, PositionStats, BorrowRateState, Side, isVariant, Position, DEFAULT_POSITION } from "./types";
3
3
  import { PositionAccount } from "./PositionAccount";
4
4
  import BN from "bn.js";
5
5
  import { BN_ZERO, RATE_POWER } from "./constants";
@@ -30,6 +30,7 @@ export class CustodyAccount implements Custody {
30
30
  longPositions: PositionStats;
31
31
  shortPositions: PositionStats;
32
32
  borrowRateState: BorrowRateState;
33
+
33
34
  bump: number;
34
35
  tokenAccountBump: number;
35
36
 
@@ -93,29 +94,29 @@ export class CustodyAccount implements Custody {
93
94
  } else {
94
95
  stats = this.shortPositions;
95
96
  };
96
- // if (!stats.openPositions.isNeg()) {
97
-
98
- // const obj: Position = {
99
- // side,
100
- // price:
101
- // stats.weighted_price.div(stats.total_quantity)
97
+ if (stats.openPositions.gt(BN_ZERO)) {
102
98
 
103
- // size_usd: stats.size_usd,
104
- // collateral_usd: stats.collateral_usd,
105
- // unrealized_loss_usd: stats.cumulative_interest_usd,
106
- // cumulative_interest_snapshot: stats.cumulative_interest_snapshot,
107
- // locked_amount: stats.locked_amount,
108
- // ..Position::default()
109
- // }
110
- // return new PositionAccount(
111
- // new PublicKey('dgfgfg'),
112
-
113
- // )
114
- // } else {
115
- // Ok(Position::default())
116
- // }
117
-
118
- throw Error("Incomplete")
99
+ const obj: Position = {
100
+ side,
101
+ price: stats.weightedPrice.div(stats.totalQuantity),
102
+ sizeUsd: stats.sizeUsd,
103
+ collateralUsd: stats.collateralUsd,
104
+ unrealizedLossUsd: stats.cumulativeInterestUsd,
105
+ cumulativeInterestSnapshot: stats.cumulativeInterestSnapshot,
106
+ lockedAmount: stats.lockedAmount,
107
+ ...DEFAULT_POSITION
108
+ } as Position;
109
+ return new PositionAccount(
110
+ PublicKey.default,
111
+ obj
112
+ )
113
+ } else {
114
+ return new PositionAccount(
115
+ PublicKey.default,
116
+ DEFAULT_POSITION
117
+ )
118
+ }
119
+
119
120
  }
120
121
 
121
122
 
@@ -101,8 +101,8 @@ export class OraclePrice {
101
101
  )
102
102
  }
103
103
 
104
- // get_pyth_price() {
105
-
106
- // }
107
-
104
+ toUiPrice(precision: number): number {
105
+ const x: BN = this.price.div(new BN(10).pow(new BN((this.exponent.toNumber()*-1) - precision)))
106
+ return x.toNumber()/10**precision
107
+ }
108
108
  }