@0dotxyz/p0-ts-sdk 2.8.4 → 2.9.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +305 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +218 -16
- package/dist/index.d.ts +218 -16
- package/dist/index.js +292 -11
- package/dist/index.js.map +1 -1
- package/dist/instructions.cjs +19 -1
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.d.cts +30 -1
- package/dist/instructions.d.ts +30 -1
- package/dist/instructions.js +19 -1
- package/dist/instructions.js.map +1 -1
- package/dist/{types-BsUi_Zrx.d.ts → types-Auh-s318.d.ts} +40 -1
- package/dist/{types-Bah-JjSM.d.cts → types-CvvHQs7G.d.cts} +40 -1
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var web3_js = require('@solana/web3.js');
|
|
4
4
|
var superstruct = require('superstruct');
|
|
5
|
+
var sha2 = require('@noble/hashes/sha2');
|
|
5
6
|
var BigNumber3 = require('bignumber.js');
|
|
6
7
|
var BN8 = require('bn.js');
|
|
7
8
|
var Decimal3 = require('decimal.js');
|
|
@@ -286,6 +287,7 @@ var PDA_BANK_FEE_STATE_SEED = Buffer.from("feestate");
|
|
|
286
287
|
var PDA_BANK_EMISSIONS_AUTH_SEED = Buffer.from("emissions_auth_seed");
|
|
287
288
|
var PDA_BANK_EMISSIONS_VAULT_SEED = Buffer.from("emissions_vault");
|
|
288
289
|
var PDA_MARGINFI_ACCOUNT_SEED = Buffer.from("marginfi_account");
|
|
290
|
+
var PDA_ORDER_SEED = Buffer.from("order");
|
|
289
291
|
function deriveBankLiquidityVaultAuthority(programId, bank) {
|
|
290
292
|
return web3_js.PublicKey.findProgramAddressSync(
|
|
291
293
|
[PDA_BANK_LIQUIDITY_VAULT_AUTH_SEED, bank.toBuffer()],
|
|
@@ -350,6 +352,14 @@ function deriveMarginfiAccount(programId, group, authority, accountIndex, thirdP
|
|
|
350
352
|
programId
|
|
351
353
|
);
|
|
352
354
|
}
|
|
355
|
+
function deriveOrderPda(programId, marginfiAccount, bankKeys) {
|
|
356
|
+
const sortedBankKeys = bankKeys.map((key) => key.toBuffer()).sort(Buffer.compare);
|
|
357
|
+
const bankKeysHash = Buffer.from(sha2.sha256(Buffer.concat(sortedBankKeys)));
|
|
358
|
+
return web3_js.PublicKey.findProgramAddressSync(
|
|
359
|
+
[PDA_ORDER_SEED, marginfiAccount.toBuffer(), bankKeysHash],
|
|
360
|
+
programId
|
|
361
|
+
);
|
|
362
|
+
}
|
|
353
363
|
var PYTH_PUSH_ORACLE_ID = new web3_js.PublicKey("pythWSnswVUd12oZpeFP8e9CVaEqJg25g1Vtc2biRsT");
|
|
354
364
|
var PYTH_SPONSORED_SHARD_ID = 0;
|
|
355
365
|
var MARGINFI_SPONSORED_SHARD_ID = 3301;
|
|
@@ -521,6 +531,19 @@ function shortenAddress(pubkey, chars = 4) {
|
|
|
521
531
|
function bpsToPercentile(bps) {
|
|
522
532
|
return bps / 1e4;
|
|
523
533
|
}
|
|
534
|
+
var U32_MAX = 4294967295;
|
|
535
|
+
var MAX_ORDER_SLIPPAGE_PERCENT = 10;
|
|
536
|
+
function percentToMaxSlippageU32(percent) {
|
|
537
|
+
if (!(percent > 0) || percent > MAX_ORDER_SLIPPAGE_PERCENT) {
|
|
538
|
+
throw new Error(
|
|
539
|
+
`Max slippage percent must be in (0, ${MAX_ORDER_SLIPPAGE_PERCENT}], got ${percent}`
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
return Math.round(percent / 100 * U32_MAX);
|
|
543
|
+
}
|
|
544
|
+
function maxSlippageU32ToPercent(maxSlippage) {
|
|
545
|
+
return maxSlippage / U32_MAX * 100;
|
|
546
|
+
}
|
|
524
547
|
var composeRemainingAccounts = (banksAndOracles) => {
|
|
525
548
|
banksAndOracles.sort((a, b) => {
|
|
526
549
|
const A = a[0].toBytes();
|
|
@@ -21460,6 +21483,9 @@ var TransactionType = /* @__PURE__ */ ((TransactionType2) => {
|
|
|
21460
21483
|
TransactionType2["UNSTAKE_LST"] = "UNSTAKE_LST";
|
|
21461
21484
|
TransactionType2["WITHDRAW_EMISSIONS"] = "WITHDRAW_EMISSIONS";
|
|
21462
21485
|
TransactionType2["LIQUIDATE_ACCOUNT"] = "LIQUIDATE_ACCOUNT";
|
|
21486
|
+
TransactionType2["PLACE_ORDER"] = "PLACE_ORDER";
|
|
21487
|
+
TransactionType2["CLOSE_ORDER"] = "CLOSE_ORDER";
|
|
21488
|
+
TransactionType2["UPDATE_ORDER"] = "UPDATE_ORDER";
|
|
21463
21489
|
TransactionType2["CREATE_PERM_BANK"] = "CREATE_PERM_BANK";
|
|
21464
21490
|
TransactionType2["CREATE_GROUP"] = "CREATE_GROUP";
|
|
21465
21491
|
TransactionType2["JUPITER_SWAP"] = "JUPITER_SWAP";
|
|
@@ -21546,6 +21572,14 @@ var TransactionConfigMap = {
|
|
|
21546
21572
|
["WITHDRAW_EMISSIONS" /* WITHDRAW_EMISSIONS */]: { label: () => "Withdraw emissions" },
|
|
21547
21573
|
// LIQUIDATE
|
|
21548
21574
|
["LIQUIDATE_ACCOUNT" /* LIQUIDATE_ACCOUNT */]: { label: () => "Liquidate account" },
|
|
21575
|
+
// ORDERS (TP/SL)
|
|
21576
|
+
["PLACE_ORDER" /* PLACE_ORDER */]: {
|
|
21577
|
+
label: ({ collateralToken, debtToken } = {}) => collateralToken && debtToken ? `Place TP/SL order on ${collateralToken}/${debtToken}` : "Place TP/SL order"
|
|
21578
|
+
},
|
|
21579
|
+
["CLOSE_ORDER" /* CLOSE_ORDER */]: { label: () => "Close TP/SL order" },
|
|
21580
|
+
["UPDATE_ORDER" /* UPDATE_ORDER */]: {
|
|
21581
|
+
label: ({ collateralToken, debtToken } = {}) => collateralToken && debtToken ? `Update TP/SL order on ${collateralToken}/${debtToken}` : "Update TP/SL order"
|
|
21582
|
+
},
|
|
21549
21583
|
// BANK and GROUPS
|
|
21550
21584
|
["CREATE_PERM_BANK" /* CREATE_PERM_BANK */]: { label: () => "Create permissionless bank" },
|
|
21551
21585
|
["CREATE_GROUP" /* CREATE_GROUP */]: { label: () => "Create marginfi group" },
|
|
@@ -71322,13 +71356,13 @@ function computeInterestRates(bank) {
|
|
|
71322
71356
|
const borrowingRate = baseInterestRate.times(new BigNumber3__default.default(1).plus(rateFee)).plus(fixedFee);
|
|
71323
71357
|
return { lendingRate, borrowingRate };
|
|
71324
71358
|
}
|
|
71325
|
-
var
|
|
71359
|
+
var U32_MAX2 = 4294967295;
|
|
71326
71360
|
function rateFromU32(rate) {
|
|
71327
|
-
const ratio = new BigNumber3__default.default(rate).div(
|
|
71361
|
+
const ratio = new BigNumber3__default.default(rate).div(U32_MAX2);
|
|
71328
71362
|
return ratio.times(10);
|
|
71329
71363
|
}
|
|
71330
71364
|
function utilFromU32(util) {
|
|
71331
|
-
return new BigNumber3__default.default(util).div(
|
|
71365
|
+
return new BigNumber3__default.default(util).div(U32_MAX2);
|
|
71332
71366
|
}
|
|
71333
71367
|
function calculateRateBetweenPoints(startX, startY, endX, endY, targetX) {
|
|
71334
71368
|
if (endX.lte(startX)) return startY;
|
|
@@ -71937,6 +71971,22 @@ async function makePulseHealthIx(mfProgram, accounts, remainingAccounts = []) {
|
|
|
71937
71971
|
marginfiAccount: accounts.marginfiAccount
|
|
71938
71972
|
}).remainingAccounts(remainingAccounts).instruction();
|
|
71939
71973
|
}
|
|
71974
|
+
async function makePlaceOrderIx(mfProgram, accounts, args, remainingAccounts = []) {
|
|
71975
|
+
const { marginfiAccount, feePayer, authority, order, globalFeeWallet, ...optionalAccounts } = accounts;
|
|
71976
|
+
return mfProgram.methods.marginfiAccountPlaceOrder(args.bankKeys, args.trigger).accounts({
|
|
71977
|
+
marginfiAccount,
|
|
71978
|
+
feePayer,
|
|
71979
|
+
order
|
|
71980
|
+
}).accountsPartial({ authority, globalFeeWallet, ...optionalAccounts }).remainingAccounts(remainingAccounts).instruction();
|
|
71981
|
+
}
|
|
71982
|
+
async function makeCloseOrderIx(mfProgram, accounts, remainingAccounts = []) {
|
|
71983
|
+
const { marginfiAccount, authority, order, feeRecipient, ...optionalAccounts } = accounts;
|
|
71984
|
+
return mfProgram.methods.marginfiAccountCloseOrder().accounts({
|
|
71985
|
+
authority,
|
|
71986
|
+
order,
|
|
71987
|
+
feeRecipient
|
|
71988
|
+
}).accountsPartial({ marginfiAccount, ...optionalAccounts }).remainingAccounts(remainingAccounts).instruction();
|
|
71989
|
+
}
|
|
71940
71990
|
var instructions = {
|
|
71941
71991
|
makeDepositIx,
|
|
71942
71992
|
makeJuplendDepositIx,
|
|
@@ -71962,7 +72012,9 @@ var instructions = {
|
|
|
71962
72012
|
makeLendingPoolConfigureBankOracleIx,
|
|
71963
72013
|
makeLendingPoolConfigureBankOracleScopeIx,
|
|
71964
72014
|
makeLendingPoolSetOraclePriceIx,
|
|
71965
|
-
makePulseHealthIx
|
|
72015
|
+
makePulseHealthIx,
|
|
72016
|
+
makePlaceOrderIx,
|
|
72017
|
+
makeCloseOrderIx
|
|
71966
72018
|
};
|
|
71967
72019
|
var instructions_default3 = instructions;
|
|
71968
72020
|
|
|
@@ -72128,9 +72180,25 @@ function decodeAccountRaw(encoded, idl) {
|
|
|
72128
72180
|
const coder = new anchor.BorshCoder(idl);
|
|
72129
72181
|
return coder.accounts.decode("marginfiAccount" /* MarginfiAccount */, encoded);
|
|
72130
72182
|
}
|
|
72183
|
+
function parseOrderRaw(orderAddress, orderRaw) {
|
|
72184
|
+
let trigger = "stopLoss";
|
|
72185
|
+
if ("takeProfit" in orderRaw.trigger) trigger = "takeProfit";
|
|
72186
|
+
if ("both" in orderRaw.trigger) trigger = "both";
|
|
72187
|
+
return {
|
|
72188
|
+
address: orderAddress,
|
|
72189
|
+
marginfiAccount: orderRaw.marginfiAccount,
|
|
72190
|
+
trigger,
|
|
72191
|
+
stopLoss: trigger === "takeProfit" ? null : wrappedI80F48toBigNumber(orderRaw.stopLoss),
|
|
72192
|
+
takeProfit: trigger === "stopLoss" ? null : wrappedI80F48toBigNumber(orderRaw.takeProfit),
|
|
72193
|
+
tags: [orderRaw.tags[0], orderRaw.tags[1]],
|
|
72194
|
+
createdAt: orderRaw.createdAt.toNumber(),
|
|
72195
|
+
maxSlippagePercent: maxSlippageU32ToPercent(orderRaw.maxSlippage)
|
|
72196
|
+
};
|
|
72197
|
+
}
|
|
72131
72198
|
function parseBalanceRaw(balanceRaw) {
|
|
72132
72199
|
const active = typeof balanceRaw.active === "number" ? balanceRaw.active === 1 : balanceRaw.active;
|
|
72133
72200
|
const bankPk = balanceRaw.bankPk;
|
|
72201
|
+
const tag = balanceRaw.tag;
|
|
72134
72202
|
const assetShares = wrappedI80F48toBigNumber(balanceRaw.assetShares);
|
|
72135
72203
|
const liabilityShares = wrappedI80F48toBigNumber(balanceRaw.liabilityShares);
|
|
72136
72204
|
const emissionsOutstanding = wrappedI80F48toBigNumber(balanceRaw.emissionsOutstanding);
|
|
@@ -72138,6 +72206,7 @@ function parseBalanceRaw(balanceRaw) {
|
|
|
72138
72206
|
return {
|
|
72139
72207
|
active,
|
|
72140
72208
|
bankPk,
|
|
72209
|
+
tag,
|
|
72141
72210
|
assetShares,
|
|
72142
72211
|
liabilityShares,
|
|
72143
72212
|
emissionsOutstanding,
|
|
@@ -72247,6 +72316,7 @@ function dtoToBalance(balanceDto) {
|
|
|
72247
72316
|
return {
|
|
72248
72317
|
active: balanceDto.active,
|
|
72249
72318
|
bankPk: new web3_js.PublicKey(balanceDto.bankPk),
|
|
72319
|
+
tag: balanceDto.tag ?? 0,
|
|
72250
72320
|
assetShares: new BigNumber3__default.default(balanceDto.assetShares),
|
|
72251
72321
|
liabilityShares: new BigNumber3__default.default(balanceDto.liabilityShares),
|
|
72252
72322
|
emissionsOutstanding: new BigNumber3__default.default(balanceDto.emissionsOutstanding),
|
|
@@ -72285,6 +72355,7 @@ function balanceToDto(balance) {
|
|
|
72285
72355
|
return {
|
|
72286
72356
|
active: balance.active,
|
|
72287
72357
|
bankPk: balance.bankPk.toBase58(),
|
|
72358
|
+
tag: balance.tag,
|
|
72288
72359
|
assetShares: balance.assetShares.toString(),
|
|
72289
72360
|
liabilityShares: balance.liabilityShares.toString(),
|
|
72290
72361
|
emissionsOutstanding: balance.emissionsOutstanding.toString(),
|
|
@@ -72802,6 +72873,7 @@ function createEmptyBalance(bankPk) {
|
|
|
72802
72873
|
const balance = {
|
|
72803
72874
|
active: false,
|
|
72804
72875
|
bankPk,
|
|
72876
|
+
tag: 0,
|
|
72805
72877
|
assetShares: new BigNumber3__default.default(0),
|
|
72806
72878
|
liabilityShares: new BigNumber3__default.default(0),
|
|
72807
72879
|
emissionsOutstanding: new BigNumber3__default.default(0),
|
|
@@ -73104,6 +73176,7 @@ function computeProjectedActiveBalancesNoCpi({
|
|
|
73104
73176
|
const projectedBalances = account.balances.map((b) => ({
|
|
73105
73177
|
active: b.active,
|
|
73106
73178
|
bankPk: b.bankPk,
|
|
73179
|
+
tag: b.tag,
|
|
73107
73180
|
assetShares: new BigNumber3__default.default(b.assetShares),
|
|
73108
73181
|
liabilityShares: new BigNumber3__default.default(b.liabilityShares),
|
|
73109
73182
|
emissionsOutstanding: new BigNumber3__default.default(b.emissionsOutstanding),
|
|
@@ -73377,6 +73450,36 @@ var fetchMarginfiAccountData = async (program, marginfiAccountPk, banksMap, bank
|
|
|
73377
73450
|
return { marginfiAccount };
|
|
73378
73451
|
}
|
|
73379
73452
|
};
|
|
73453
|
+
var fetchOrder = async (program, orderAddress) => {
|
|
73454
|
+
const orderRaw = await program.account.order.fetchNullable(orderAddress);
|
|
73455
|
+
if (!orderRaw) return null;
|
|
73456
|
+
return parseOrderRaw(orderAddress, orderRaw);
|
|
73457
|
+
};
|
|
73458
|
+
var fetchOrdersForAccount = async (program, marginfiAccount) => {
|
|
73459
|
+
const orders = await program.account.order.all([
|
|
73460
|
+
{
|
|
73461
|
+
memcmp: {
|
|
73462
|
+
bytes: marginfiAccount.toBase58(),
|
|
73463
|
+
offset: 8
|
|
73464
|
+
// first field after the discriminator
|
|
73465
|
+
}
|
|
73466
|
+
}
|
|
73467
|
+
]);
|
|
73468
|
+
return orders.map(({ publicKey: publicKey5, account }) => parseOrderRaw(publicKey5, account));
|
|
73469
|
+
};
|
|
73470
|
+
var resolveOrderBanks = (marginfiAccount, order) => {
|
|
73471
|
+
const taggedBalances = marginfiAccount.balances.filter(
|
|
73472
|
+
(balance) => balance.tag !== 0 && order.tags.includes(balance.tag)
|
|
73473
|
+
);
|
|
73474
|
+
const collateral = taggedBalances.find((balance) => balance.assetShares.gt(0));
|
|
73475
|
+
const debt = taggedBalances.find((balance) => balance.liabilityShares.gt(0));
|
|
73476
|
+
if (!collateral || !debt) {
|
|
73477
|
+
throw new Error(
|
|
73478
|
+
`Could not resolve banks for order ${order.address.toBase58()}: tagged balances are missing or closed`
|
|
73479
|
+
);
|
|
73480
|
+
}
|
|
73481
|
+
return { collateralBank: collateral.bankPk, debtBank: debt.bankPk };
|
|
73482
|
+
};
|
|
73380
73483
|
function randomDistinctIndices(count, maxExclusive) {
|
|
73381
73484
|
const chosen = /* @__PURE__ */ new Set();
|
|
73382
73485
|
while (chosen.size < count) {
|
|
@@ -78101,6 +78204,7 @@ function generateDummyAccount(group, authority, accountKey) {
|
|
|
78101
78204
|
const dummyBalances = Array(15).fill({
|
|
78102
78205
|
active: false,
|
|
78103
78206
|
bankPk: new web3_js.PublicKey("11111111111111111111111111111111"),
|
|
78207
|
+
tag: 0,
|
|
78104
78208
|
assetShares: dummyWrappedI80F48,
|
|
78105
78209
|
liabilityShares: dummyWrappedI80F48,
|
|
78106
78210
|
emissionsOutstanding: dummyWrappedI80F48,
|
|
@@ -78109,7 +78213,7 @@ function generateDummyAccount(group, authority, accountKey) {
|
|
|
78109
78213
|
const rawAccount = {
|
|
78110
78214
|
group,
|
|
78111
78215
|
authority,
|
|
78112
|
-
lendingAccount: { balances: dummyBalances },
|
|
78216
|
+
lendingAccount: { balances: dummyBalances, lastTagUsed: 0 },
|
|
78113
78217
|
healthCache: {
|
|
78114
78218
|
assetValue: {
|
|
78115
78219
|
value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
@@ -78131,20 +78235,20 @@ function generateDummyAccount(group, authority, accountKey) {
|
|
|
78131
78235
|
mrgnErr: 0
|
|
78132
78236
|
},
|
|
78133
78237
|
emissionsDestinationAccount: new web3_js.PublicKey("11111111111111111111111111111111"),
|
|
78134
|
-
accountFlags: new BN8__default.default([0, 0, 0])
|
|
78135
|
-
};
|
|
78238
|
+
accountFlags: new BN8__default.default([0, 0, 0])};
|
|
78136
78239
|
return parseMarginfiAccountRaw(accountKey, rawAccount);
|
|
78137
78240
|
}
|
|
78138
78241
|
|
|
78139
78242
|
// src/models/balance.ts
|
|
78140
78243
|
var Balance = class _Balance {
|
|
78141
|
-
constructor(active, bankPk, assetShares, liabilityShares, emissionsOutstanding, lastUpdate) {
|
|
78244
|
+
constructor(active, bankPk, assetShares, liabilityShares, emissionsOutstanding, lastUpdate, tag) {
|
|
78142
78245
|
this.active = active;
|
|
78143
78246
|
this.bankPk = bankPk;
|
|
78144
78247
|
this.assetShares = assetShares;
|
|
78145
78248
|
this.liabilityShares = liabilityShares;
|
|
78146
78249
|
this.emissionsOutstanding = emissionsOutstanding;
|
|
78147
78250
|
this.lastUpdate = lastUpdate;
|
|
78251
|
+
this.tag = tag;
|
|
78148
78252
|
}
|
|
78149
78253
|
static from(balanceRaw) {
|
|
78150
78254
|
const props = parseBalanceRaw(balanceRaw);
|
|
@@ -78154,7 +78258,8 @@ var Balance = class _Balance {
|
|
|
78154
78258
|
props.assetShares,
|
|
78155
78259
|
props.liabilityShares,
|
|
78156
78260
|
props.emissionsOutstanding,
|
|
78157
|
-
props.lastUpdate
|
|
78261
|
+
props.lastUpdate,
|
|
78262
|
+
props.tag
|
|
78158
78263
|
);
|
|
78159
78264
|
}
|
|
78160
78265
|
static fromBalanceType(balance) {
|
|
@@ -78164,7 +78269,8 @@ var Balance = class _Balance {
|
|
|
78164
78269
|
balance.assetShares,
|
|
78165
78270
|
balance.liabilityShares,
|
|
78166
78271
|
balance.emissionsOutstanding,
|
|
78167
|
-
balance.lastUpdate
|
|
78272
|
+
balance.lastUpdate,
|
|
78273
|
+
balance.tag
|
|
78168
78274
|
);
|
|
78169
78275
|
}
|
|
78170
78276
|
static createEmpty(bankPk) {
|
|
@@ -79128,6 +79234,41 @@ var MarginfiAccount = class _MarginfiAccount {
|
|
|
79128
79234
|
}
|
|
79129
79235
|
});
|
|
79130
79236
|
}
|
|
79237
|
+
// ----------------------------------------------------------------------------
|
|
79238
|
+
// Orders (take-profit / stop-loss)
|
|
79239
|
+
// ----------------------------------------------------------------------------
|
|
79240
|
+
/**
|
|
79241
|
+
* Creates a place-order instruction for this marginfi account.
|
|
79242
|
+
*
|
|
79243
|
+
* @see {@link makePlaceOrderIx} for detailed implementation
|
|
79244
|
+
*/
|
|
79245
|
+
async makePlaceOrderIx(params) {
|
|
79246
|
+
return makePlaceOrderIx2({ ...params, marginfiAccount: this });
|
|
79247
|
+
}
|
|
79248
|
+
/**
|
|
79249
|
+
* Creates a transaction placing a take-profit / stop-loss order on a collateral/debt pair.
|
|
79250
|
+
*
|
|
79251
|
+
* @see {@link makePlaceOrderTx} for detailed implementation
|
|
79252
|
+
*/
|
|
79253
|
+
async makePlaceOrderTx(params) {
|
|
79254
|
+
return makePlaceOrderTx({ ...params, marginfiAccount: this });
|
|
79255
|
+
}
|
|
79256
|
+
/**
|
|
79257
|
+
* Creates a transaction closing an existing order.
|
|
79258
|
+
*
|
|
79259
|
+
* @see {@link makeCloseOrderTx} for detailed implementation
|
|
79260
|
+
*/
|
|
79261
|
+
async makeCloseOrderTx(params) {
|
|
79262
|
+
return makeCloseOrderTx({ ...params, marginfiAccount: this });
|
|
79263
|
+
}
|
|
79264
|
+
/**
|
|
79265
|
+
* Creates a transaction replacing the pair's existing order with new thresholds.
|
|
79266
|
+
*
|
|
79267
|
+
* @see {@link makeUpdateOrderTx} for detailed implementation
|
|
79268
|
+
*/
|
|
79269
|
+
async makeUpdateOrderTx(params) {
|
|
79270
|
+
return makeUpdateOrderTx({ ...params, marginfiAccount: this });
|
|
79271
|
+
}
|
|
79131
79272
|
/**
|
|
79132
79273
|
* Creates a repay transaction.
|
|
79133
79274
|
*
|
|
@@ -81688,6 +81829,100 @@ async function makeBulkRepayTx(params) {
|
|
|
81688
81829
|
);
|
|
81689
81830
|
return { transactions, actionTxIndex: 0, mustBeAtomicBundle: false };
|
|
81690
81831
|
}
|
|
81832
|
+
function buildOrderTrigger(params) {
|
|
81833
|
+
const { stopLossUsd, takeProfitUsd } = params;
|
|
81834
|
+
const maxSlippage = percentToMaxSlippageU32(params.maxSlippagePercent);
|
|
81835
|
+
if (stopLossUsd && takeProfitUsd) {
|
|
81836
|
+
if (takeProfitUsd.lte(stopLossUsd)) {
|
|
81837
|
+
throw new Error(
|
|
81838
|
+
`Take-profit threshold (${takeProfitUsd}) must be above stop-loss threshold (${stopLossUsd})`
|
|
81839
|
+
);
|
|
81840
|
+
}
|
|
81841
|
+
return {
|
|
81842
|
+
both: {
|
|
81843
|
+
stopLoss: bigNumberToWrappedI80F48(stopLossUsd),
|
|
81844
|
+
takeProfit: bigNumberToWrappedI80F48(takeProfitUsd),
|
|
81845
|
+
maxSlippage
|
|
81846
|
+
}
|
|
81847
|
+
};
|
|
81848
|
+
}
|
|
81849
|
+
if (stopLossUsd) {
|
|
81850
|
+
return { stopLoss: { threshold: bigNumberToWrappedI80F48(stopLossUsd), maxSlippage } };
|
|
81851
|
+
}
|
|
81852
|
+
if (takeProfitUsd) {
|
|
81853
|
+
return { takeProfit: { threshold: bigNumberToWrappedI80F48(takeProfitUsd), maxSlippage } };
|
|
81854
|
+
}
|
|
81855
|
+
throw new Error("An order needs a stop-loss threshold, a take-profit threshold, or both");
|
|
81856
|
+
}
|
|
81857
|
+
async function makePlaceOrderIx2(params) {
|
|
81858
|
+
const { program, marginfiAccount, collateralBank, debtBank, trigger, feePayer } = params;
|
|
81859
|
+
const [order] = deriveOrderPda(program.programId, marginfiAccount.address, [
|
|
81860
|
+
collateralBank,
|
|
81861
|
+
debtBank
|
|
81862
|
+
]);
|
|
81863
|
+
const globalFeeWallet = params.globalFeeWallet ?? (await program.account.feeState.fetch(deriveFeeState(program.programId)[0])).globalFeeWallet;
|
|
81864
|
+
const placeOrderIx = await instructions_default3.makePlaceOrderIx(
|
|
81865
|
+
program,
|
|
81866
|
+
{
|
|
81867
|
+
marginfiAccount: marginfiAccount.address,
|
|
81868
|
+
feePayer: feePayer ?? marginfiAccount.authority,
|
|
81869
|
+
authority: marginfiAccount.authority,
|
|
81870
|
+
order,
|
|
81871
|
+
globalFeeWallet,
|
|
81872
|
+
group: marginfiAccount.group
|
|
81873
|
+
},
|
|
81874
|
+
{ bankKeys: [collateralBank, debtBank], trigger: buildOrderTrigger(trigger) }
|
|
81875
|
+
);
|
|
81876
|
+
return { instructions: [placeOrderIx], keys: [] };
|
|
81877
|
+
}
|
|
81878
|
+
async function makeCloseOrderIx2(params) {
|
|
81879
|
+
const { program, marginfiAccount, order, feeRecipient } = params;
|
|
81880
|
+
const closeOrderIx = await instructions_default3.makeCloseOrderIx(program, {
|
|
81881
|
+
marginfiAccount: marginfiAccount.address,
|
|
81882
|
+
authority: marginfiAccount.authority,
|
|
81883
|
+
order,
|
|
81884
|
+
feeRecipient: feeRecipient ?? marginfiAccount.authority,
|
|
81885
|
+
group: marginfiAccount.group
|
|
81886
|
+
});
|
|
81887
|
+
return { instructions: [closeOrderIx], keys: [] };
|
|
81888
|
+
}
|
|
81889
|
+
async function compileOrderTx(params, payerKey, ixs, type) {
|
|
81890
|
+
const blockhash = params.blockhash ?? (await params.connection.getLatestBlockhashAndContext("confirmed")).value.blockhash;
|
|
81891
|
+
return addTransactionMetadata(
|
|
81892
|
+
new web3_js.VersionedTransaction(
|
|
81893
|
+
new web3_js.TransactionMessage({
|
|
81894
|
+
instructions: ixs.flatMap((ix) => ix.instructions),
|
|
81895
|
+
payerKey,
|
|
81896
|
+
recentBlockhash: blockhash
|
|
81897
|
+
}).compileToV0Message(params.luts)
|
|
81898
|
+
),
|
|
81899
|
+
{ type, signers: ixs.flatMap((ix) => ix.keys), addressLookupTables: params.luts }
|
|
81900
|
+
);
|
|
81901
|
+
}
|
|
81902
|
+
async function makePlaceOrderTx(params) {
|
|
81903
|
+
const placeIxs = await makePlaceOrderIx2(params);
|
|
81904
|
+
const payerKey = params.feePayer ?? params.marginfiAccount.authority;
|
|
81905
|
+
return compileOrderTx(params, payerKey, [placeIxs], "PLACE_ORDER" /* PLACE_ORDER */);
|
|
81906
|
+
}
|
|
81907
|
+
async function makeCloseOrderTx(params) {
|
|
81908
|
+
const closeIxs = await makeCloseOrderIx2(params);
|
|
81909
|
+
return compileOrderTx(
|
|
81910
|
+
params,
|
|
81911
|
+
params.marginfiAccount.authority,
|
|
81912
|
+
[closeIxs],
|
|
81913
|
+
"CLOSE_ORDER" /* CLOSE_ORDER */
|
|
81914
|
+
);
|
|
81915
|
+
}
|
|
81916
|
+
async function makeUpdateOrderTx(params) {
|
|
81917
|
+
const [order] = deriveOrderPda(params.program.programId, params.marginfiAccount.address, [
|
|
81918
|
+
params.collateralBank,
|
|
81919
|
+
params.debtBank
|
|
81920
|
+
]);
|
|
81921
|
+
const closeIxs = await makeCloseOrderIx2({ ...params, order, feeRecipient: params.feePayer });
|
|
81922
|
+
const placeIxs = await makePlaceOrderIx2(params);
|
|
81923
|
+
const payerKey = params.feePayer ?? params.marginfiAccount.authority;
|
|
81924
|
+
return compileOrderTx(params, payerKey, [closeIxs, placeIxs], "UPDATE_ORDER" /* UPDATE_ORDER */);
|
|
81925
|
+
}
|
|
81691
81926
|
|
|
81692
81927
|
// src/services/account/services/account-simulation.service.ts
|
|
81693
81928
|
async function simulateAccountHealthCacheWithFallback(params) {
|
|
@@ -84548,6 +84783,52 @@ var MarginfiAccountWrapper = class {
|
|
|
84548
84783
|
opts
|
|
84549
84784
|
});
|
|
84550
84785
|
}
|
|
84786
|
+
// ----------------------------------------------------------------------------
|
|
84787
|
+
// Orders (take-profit / stop-loss)
|
|
84788
|
+
// ----------------------------------------------------------------------------
|
|
84789
|
+
/**
|
|
84790
|
+
* Creates a transaction placing a take-profit / stop-loss order on a collateral/debt pair.
|
|
84791
|
+
*
|
|
84792
|
+
* @param collateralBank - Bank of the asset-side balance
|
|
84793
|
+
* @param debtBank - Bank of the liability-side balance
|
|
84794
|
+
* @param trigger - USD pair-equity thresholds and max slippage
|
|
84795
|
+
*/
|
|
84796
|
+
async makePlaceOrderTx(collateralBank, debtBank, trigger) {
|
|
84797
|
+
return this.account.makePlaceOrderTx({
|
|
84798
|
+
program: this.client.program,
|
|
84799
|
+
collateralBank,
|
|
84800
|
+
debtBank,
|
|
84801
|
+
trigger,
|
|
84802
|
+
luts: this.client.addressLookupTables,
|
|
84803
|
+
connection: this.client.program.provider.connection
|
|
84804
|
+
});
|
|
84805
|
+
}
|
|
84806
|
+
/**
|
|
84807
|
+
* Creates a transaction replacing the pair's existing order with new thresholds.
|
|
84808
|
+
*/
|
|
84809
|
+
async makeUpdateOrderTx(collateralBank, debtBank, trigger) {
|
|
84810
|
+
return this.account.makeUpdateOrderTx({
|
|
84811
|
+
program: this.client.program,
|
|
84812
|
+
collateralBank,
|
|
84813
|
+
debtBank,
|
|
84814
|
+
trigger,
|
|
84815
|
+
luts: this.client.addressLookupTables,
|
|
84816
|
+
connection: this.client.program.provider.connection
|
|
84817
|
+
});
|
|
84818
|
+
}
|
|
84819
|
+
/**
|
|
84820
|
+
* Creates a transaction closing an existing order.
|
|
84821
|
+
*
|
|
84822
|
+
* @param order - The order PDA (from `fetchOrdersForAccount` or `deriveOrderPda`)
|
|
84823
|
+
*/
|
|
84824
|
+
async makeCloseOrderTx(order) {
|
|
84825
|
+
return this.account.makeCloseOrderTx({
|
|
84826
|
+
program: this.client.program,
|
|
84827
|
+
order,
|
|
84828
|
+
luts: this.client.addressLookupTables,
|
|
84829
|
+
connection: this.client.program.provider.connection
|
|
84830
|
+
});
|
|
84831
|
+
}
|
|
84551
84832
|
/**
|
|
84552
84833
|
* Creates a repay transaction with auto-injected client data.
|
|
84553
84834
|
*
|
|
@@ -85290,6 +85571,7 @@ exports.PDA_BANK_INSURANCE_VAULT_SEED = PDA_BANK_INSURANCE_VAULT_SEED;
|
|
|
85290
85571
|
exports.PDA_BANK_LIQUIDITY_VAULT_AUTH_SEED = PDA_BANK_LIQUIDITY_VAULT_AUTH_SEED;
|
|
85291
85572
|
exports.PDA_BANK_LIQUIDITY_VAULT_SEED = PDA_BANK_LIQUIDITY_VAULT_SEED;
|
|
85292
85573
|
exports.PDA_MARGINFI_ACCOUNT_SEED = PDA_MARGINFI_ACCOUNT_SEED;
|
|
85574
|
+
exports.PDA_ORDER_SEED = PDA_ORDER_SEED;
|
|
85293
85575
|
exports.PRIORITY_TX_SIZE = PRIORITY_TX_SIZE;
|
|
85294
85576
|
exports.PYTH_PRICE_CONF_INTERVALS = PYTH_PRICE_CONF_INTERVALS;
|
|
85295
85577
|
exports.PYTH_PUSH_ORACLE_ID = PYTH_PUSH_ORACLE_ID;
|
|
@@ -85339,6 +85621,7 @@ exports.bankRawToDto = bankRawToDto;
|
|
|
85339
85621
|
exports.bigNumberToWrappedI80F48 = bigNumberToWrappedI80F48;
|
|
85340
85622
|
exports.bpsToPercentile = bpsToPercentile;
|
|
85341
85623
|
exports.buildCollateralLegIxs = buildCollateralLegIxs;
|
|
85624
|
+
exports.buildOrderTrigger = buildOrderTrigger;
|
|
85342
85625
|
exports.calculateApyFromInterest = calculateApyFromInterest;
|
|
85343
85626
|
exports.calculateInterestFromApy = calculateInterestFromApy;
|
|
85344
85627
|
exports.capConfidenceInterval = capConfidenceInterval;
|
|
@@ -85433,6 +85716,7 @@ exports.deriveBankLiquidityVault = deriveBankLiquidityVault;
|
|
|
85433
85716
|
exports.deriveBankLiquidityVaultAuthority = deriveBankLiquidityVaultAuthority;
|
|
85434
85717
|
exports.deriveFeeState = deriveFeeState;
|
|
85435
85718
|
exports.deriveMarginfiAccount = deriveMarginfiAccount;
|
|
85719
|
+
exports.deriveOrderPda = deriveOrderPda;
|
|
85436
85720
|
exports.deserializeInstruction = deserializeInstruction;
|
|
85437
85721
|
exports.deserializeLut = deserializeLut;
|
|
85438
85722
|
exports.deserializeSwapEngineRequest = deserializeSwapEngineRequest;
|
|
@@ -85472,6 +85756,8 @@ exports.fetchOracleData = fetchOracleData;
|
|
|
85472
85756
|
exports.fetchOracleMultipliers = fetchOracleMultipliers;
|
|
85473
85757
|
exports.fetchOracleMultipliersFromAPI = fetchOracleMultipliersFromAPI;
|
|
85474
85758
|
exports.fetchOracleMultipliersFromChain = fetchOracleMultipliersFromChain;
|
|
85759
|
+
exports.fetchOrder = fetchOrder;
|
|
85760
|
+
exports.fetchOrdersForAccount = fetchOrdersForAccount;
|
|
85475
85761
|
exports.fetchProgramForMints = fetchProgramForMints;
|
|
85476
85762
|
exports.fetchPythOracleData = fetchPythOracleData;
|
|
85477
85763
|
exports.fetchPythOraclePricesFromAPI = fetchPythOraclePricesFromAPI;
|
|
@@ -85570,6 +85856,8 @@ exports.makeBulkWithdrawTx = makeBulkWithdrawTx;
|
|
|
85570
85856
|
exports.makeBundleTipIx = makeBundleTipIx;
|
|
85571
85857
|
exports.makeCloseMarginfiAccountIx = makeCloseMarginfiAccountIx;
|
|
85572
85858
|
exports.makeCloseMarginfiAccountTx = makeCloseMarginfiAccountTx;
|
|
85859
|
+
exports.makeCloseOrderIx = makeCloseOrderIx2;
|
|
85860
|
+
exports.makeCloseOrderTx = makeCloseOrderTx;
|
|
85573
85861
|
exports.makeCreateAccountIxWithProjection = makeCreateAccountIxWithProjection;
|
|
85574
85862
|
exports.makeCreateAccountTxWithProjection = makeCreateAccountTxWithProjection;
|
|
85575
85863
|
exports.makeCreateMarginfiAccountIx = makeCreateMarginfiAccountIx;
|
|
@@ -85594,6 +85882,8 @@ exports.makeLoopTx = makeLoopTx;
|
|
|
85594
85882
|
exports.makeMergeStakeAccountsTx = makeMergeStakeAccountsTx;
|
|
85595
85883
|
exports.makeMintStakedLstIx = makeMintStakedLstIx;
|
|
85596
85884
|
exports.makeMintStakedLstTx = makeMintStakedLstTx;
|
|
85885
|
+
exports.makePlaceOrderIx = makePlaceOrderIx2;
|
|
85886
|
+
exports.makePlaceOrderTx = makePlaceOrderTx;
|
|
85597
85887
|
exports.makePoolAddBankIx = makePoolAddBankIx3;
|
|
85598
85888
|
exports.makePoolConfigureBankIx = makePoolConfigureBankIx3;
|
|
85599
85889
|
exports.makePriorityFeeIx = makePriorityFeeIx;
|
|
@@ -85615,6 +85905,7 @@ exports.makeTxPriorityIx = makeTxPriorityIx;
|
|
|
85615
85905
|
exports.makeUnwrapSolIx = makeUnwrapSolIx;
|
|
85616
85906
|
exports.makeUpdateDriftMarketIxs = makeUpdateDriftMarketIxs;
|
|
85617
85907
|
exports.makeUpdateJupLendRateIxs = makeUpdateJupLendRateIxs;
|
|
85908
|
+
exports.makeUpdateOrderTx = makeUpdateOrderTx;
|
|
85618
85909
|
exports.makeVaultCompleteWithdrawalIx = makeVaultCompleteWithdrawalIx;
|
|
85619
85910
|
exports.makeVaultCompleteWithdrawalTx = makeVaultCompleteWithdrawalTx;
|
|
85620
85911
|
exports.makeVaultDepositIx = makeVaultDepositIx;
|
|
@@ -85629,6 +85920,7 @@ exports.makeWrapSolIxs = makeWrapSolIxs;
|
|
|
85629
85920
|
exports.mapJupiterQuoteToSwapQuoteResult = mapJupiterQuoteToSwapQuoteResult;
|
|
85630
85921
|
exports.mapPythBanksToOraclePrices = mapPythBanksToOraclePrices;
|
|
85631
85922
|
exports.marginfiAccountToDto = marginfiAccountToDto;
|
|
85923
|
+
exports.maxSlippageU32ToPercent = maxSlippageU32ToPercent;
|
|
85632
85924
|
exports.mergeBridgeQuotes = mergeBridgeQuotes;
|
|
85633
85925
|
exports.mergeBridgeQuotesDebt = mergeBridgeQuotesDebt;
|
|
85634
85926
|
exports.mergeBridgeQuotesLoop = mergeBridgeQuotesLoop;
|
|
@@ -85644,14 +85936,17 @@ exports.parseHealthCacheRaw = parseHealthCacheRaw;
|
|
|
85644
85936
|
exports.parseMarginfiAccountRaw = parseMarginfiAccountRaw;
|
|
85645
85937
|
exports.parseOperationalState = parseOperationalState;
|
|
85646
85938
|
exports.parseOracleSetup = parseOracleSetup;
|
|
85939
|
+
exports.parseOrderRaw = parseOrderRaw;
|
|
85647
85940
|
exports.parsePriceInfo = parseOraclePriceData;
|
|
85648
85941
|
exports.parseRiskTier = parseRiskTier;
|
|
85649
85942
|
exports.parseRpcPythPriceData = parseRpcPythPriceData;
|
|
85650
85943
|
exports.patchDepositAmount = patchDepositAmount;
|
|
85944
|
+
exports.percentToMaxSlippageU32 = percentToMaxSlippageU32;
|
|
85651
85945
|
exports.requireBank = requireBank;
|
|
85652
85946
|
exports.requireTokenProgram = requireTokenProgram;
|
|
85653
85947
|
exports.resolveAmount = resolveAmount;
|
|
85654
85948
|
exports.resolveBridgeCandidateBanks = resolveBridgeCandidateBanks;
|
|
85949
|
+
exports.resolveOrderBanks = resolveOrderBanks;
|
|
85655
85950
|
exports.resolvePinnedSwapRoute = resolvePinnedSwapRoute;
|
|
85656
85951
|
exports.resolveTokenProgramForMint = resolveTokenProgramForMint;
|
|
85657
85952
|
exports.resolveVaultTokenProgram = resolveVaultTokenProgram;
|