@drift-labs/sdk 2.110.0-beta.22 → 2.110.0-beta.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/VERSION +1 -1
- package/lib/browser/driftClient.d.ts +2 -0
- package/lib/browser/driftClient.js +56 -0
- package/lib/browser/idl/drift.json +85 -0
- package/lib/browser/math/spotBalance.js +7 -1
- package/lib/node/driftClient.d.ts +2 -0
- package/lib/node/driftClient.js +56 -0
- package/lib/node/idl/drift.json +85 -0
- package/lib/node/math/spotBalance.js +7 -1
- package/package.json +1 -1
- package/src/driftClient.ts +132 -0
- package/src/idl/drift.json +85 -0
- package/src/math/spotBalance.ts +25 -10
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.110.0-beta.
|
|
1
|
+
2.110.0-beta.24
|
|
@@ -343,6 +343,8 @@ export declare class DriftClient {
|
|
|
343
343
|
*/
|
|
344
344
|
transferDeposit(amount: BN, marketIndex: number, fromSubAccountId: number, toSubAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
345
345
|
getTransferDepositIx(amount: BN, marketIndex: number, fromSubAccountId: number, toSubAccountId: number): Promise<TransactionInstruction>;
|
|
346
|
+
transferPools(depositFromMarketIndex: number, depositToMarketIndex: number, borrowFromMarketIndex: number, borrowToMarketIndex: number, depositAmount: BN | undefined, borrowAmount: BN | undefined, fromSubAccountId: number, toSubAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
347
|
+
getTransferPoolsIx(depositFromMarketIndex: number, depositToMarketIndex: number, borrowFromMarketIndex: number, borrowToMarketIndex: number, depositAmount: BN | undefined, borrowAmount: BN | undefined, fromSubAccountId: number, toSubAccountId: number): Promise<TransactionInstruction>;
|
|
346
348
|
updateSpotMarketCumulativeInterest(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
347
349
|
updateSpotMarketCumulativeInterestIx(marketIndex: number): Promise<TransactionInstruction>;
|
|
348
350
|
settleLP(settleeUserAccountPublicKey: PublicKey, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
@@ -1716,6 +1716,62 @@ class DriftClient {
|
|
|
1716
1716
|
remainingAccounts,
|
|
1717
1717
|
});
|
|
1718
1718
|
}
|
|
1719
|
+
async transferPools(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId, txParams) {
|
|
1720
|
+
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId), txParams), [], this.opts);
|
|
1721
|
+
if (fromSubAccountId === this.activeSubAccountId ||
|
|
1722
|
+
toSubAccountId === this.activeSubAccountId) {
|
|
1723
|
+
this.spotMarketLastSlotCache.set(depositFromMarketIndex, slot);
|
|
1724
|
+
this.spotMarketLastSlotCache.set(depositToMarketIndex, slot);
|
|
1725
|
+
this.spotMarketLastSlotCache.set(borrowFromMarketIndex, slot);
|
|
1726
|
+
this.spotMarketLastSlotCache.set(borrowToMarketIndex, slot);
|
|
1727
|
+
}
|
|
1728
|
+
return txSig;
|
|
1729
|
+
}
|
|
1730
|
+
async getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId) {
|
|
1731
|
+
const fromUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, fromSubAccountId);
|
|
1732
|
+
const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toSubAccountId);
|
|
1733
|
+
const userAccounts = [
|
|
1734
|
+
this.getUserAccount(fromSubAccountId),
|
|
1735
|
+
this.getUserAccount(toSubAccountId),
|
|
1736
|
+
];
|
|
1737
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1738
|
+
userAccounts,
|
|
1739
|
+
useMarketLastSlotCache: true,
|
|
1740
|
+
writableSpotMarketIndexes: [
|
|
1741
|
+
depositFromMarketIndex,
|
|
1742
|
+
depositToMarketIndex,
|
|
1743
|
+
borrowFromMarketIndex,
|
|
1744
|
+
borrowToMarketIndex,
|
|
1745
|
+
],
|
|
1746
|
+
});
|
|
1747
|
+
const tokenPrograms = new Set();
|
|
1748
|
+
const depositFromSpotMarket = this.getSpotMarketAccount(depositFromMarketIndex);
|
|
1749
|
+
const borrowFromSpotMarket = this.getSpotMarketAccount(borrowFromMarketIndex);
|
|
1750
|
+
tokenPrograms.add(this.getTokenProgramForSpotMarket(depositFromSpotMarket).toBase58());
|
|
1751
|
+
tokenPrograms.add(this.getTokenProgramForSpotMarket(borrowFromSpotMarket).toBase58());
|
|
1752
|
+
for (const tokenProgram of tokenPrograms) {
|
|
1753
|
+
remainingAccounts.push({
|
|
1754
|
+
isSigner: false,
|
|
1755
|
+
isWritable: false,
|
|
1756
|
+
pubkey: new web3_js_1.PublicKey(tokenProgram),
|
|
1757
|
+
});
|
|
1758
|
+
}
|
|
1759
|
+
return await this.program.instruction.transferPools(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount !== null && depositAmount !== void 0 ? depositAmount : null, borrowAmount !== null && borrowAmount !== void 0 ? borrowAmount : null, {
|
|
1760
|
+
accounts: {
|
|
1761
|
+
authority: this.wallet.publicKey,
|
|
1762
|
+
fromUser,
|
|
1763
|
+
toUser,
|
|
1764
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1765
|
+
state: await this.getStatePublicKey(),
|
|
1766
|
+
depositFromSpotMarketVault: this.getSpotMarketAccount(depositFromMarketIndex).vault,
|
|
1767
|
+
depositToSpotMarketVault: this.getSpotMarketAccount(depositToMarketIndex).vault,
|
|
1768
|
+
borrowFromSpotMarketVault: this.getSpotMarketAccount(borrowFromMarketIndex).vault,
|
|
1769
|
+
borrowToSpotMarketVault: this.getSpotMarketAccount(borrowToMarketIndex).vault,
|
|
1770
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1771
|
+
},
|
|
1772
|
+
remainingAccounts,
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1719
1775
|
async updateSpotMarketCumulativeInterest(marketIndex, txParams) {
|
|
1720
1776
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.updateSpotMarketCumulativeInterestIx(marketIndex), txParams), [], this.opts);
|
|
1721
1777
|
return txSig;
|
|
@@ -452,6 +452,91 @@
|
|
|
452
452
|
}
|
|
453
453
|
]
|
|
454
454
|
},
|
|
455
|
+
{
|
|
456
|
+
"name": "transferPools",
|
|
457
|
+
"accounts": [
|
|
458
|
+
{
|
|
459
|
+
"name": "fromUser",
|
|
460
|
+
"isMut": true,
|
|
461
|
+
"isSigner": false
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
"name": "toUser",
|
|
465
|
+
"isMut": true,
|
|
466
|
+
"isSigner": false
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
"name": "userStats",
|
|
470
|
+
"isMut": true,
|
|
471
|
+
"isSigner": false
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
"name": "authority",
|
|
475
|
+
"isMut": false,
|
|
476
|
+
"isSigner": true
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
"name": "state",
|
|
480
|
+
"isMut": false,
|
|
481
|
+
"isSigner": false
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
"name": "depositFromSpotMarketVault",
|
|
485
|
+
"isMut": true,
|
|
486
|
+
"isSigner": false
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
"name": "depositToSpotMarketVault",
|
|
490
|
+
"isMut": true,
|
|
491
|
+
"isSigner": false
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
"name": "borrowFromSpotMarketVault",
|
|
495
|
+
"isMut": true,
|
|
496
|
+
"isSigner": false
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
"name": "borrowToSpotMarketVault",
|
|
500
|
+
"isMut": true,
|
|
501
|
+
"isSigner": false
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
"name": "driftSigner",
|
|
505
|
+
"isMut": false,
|
|
506
|
+
"isSigner": false
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
"args": [
|
|
510
|
+
{
|
|
511
|
+
"name": "depositFromMarketIndex",
|
|
512
|
+
"type": "u16"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"name": "depositToMarketIndex",
|
|
516
|
+
"type": "u16"
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
"name": "borrowFromMarketIndex",
|
|
520
|
+
"type": "u16"
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
"name": "borrowToMarketIndex",
|
|
524
|
+
"type": "u16"
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
"name": "depositAmount",
|
|
528
|
+
"type": {
|
|
529
|
+
"option": "u64"
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
"name": "borrowAmount",
|
|
534
|
+
"type": {
|
|
535
|
+
"option": "u64"
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
},
|
|
455
540
|
{
|
|
456
541
|
"name": "placePerpOrder",
|
|
457
542
|
"accounts": [
|
|
@@ -342,7 +342,13 @@ function calculateWithdrawLimit(spotMarket, now) {
|
|
|
342
342
|
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
343
343
|
.div(sinceLast.add(sinceStart));
|
|
344
344
|
const lesserDepositAmount = anchor_1.BN.min(marketDepositTokenAmount, depositTokenTwapLive);
|
|
345
|
-
|
|
345
|
+
let maxBorrowTokensTwap;
|
|
346
|
+
if (spotMarket.poolId == 0) {
|
|
347
|
+
maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(6)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(10)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(5))))); // main pool between ~15-80% utilization with 10% friction on twap
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(2)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(3)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(10))))); // isolated pool between ~50-90% utilization with 33% friction on twap
|
|
351
|
+
}
|
|
346
352
|
const minDepositTokensTwap = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(4)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
|
|
347
353
|
const { minDepositTokensForUtilization, maxBorrowTokensForUtilization } = calculateTokenUtilizationLimits(marketDepositTokenAmount, marketBorrowTokenAmount, spotMarket);
|
|
348
354
|
const minDepositTokens = anchor_1.BN.max(minDepositTokensForUtilization, minDepositTokensTwap);
|
|
@@ -343,6 +343,8 @@ export declare class DriftClient {
|
|
|
343
343
|
*/
|
|
344
344
|
transferDeposit(amount: BN, marketIndex: number, fromSubAccountId: number, toSubAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
345
345
|
getTransferDepositIx(amount: BN, marketIndex: number, fromSubAccountId: number, toSubAccountId: number): Promise<TransactionInstruction>;
|
|
346
|
+
transferPools(depositFromMarketIndex: number, depositToMarketIndex: number, borrowFromMarketIndex: number, borrowToMarketIndex: number, depositAmount: BN | undefined, borrowAmount: BN | undefined, fromSubAccountId: number, toSubAccountId: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
347
|
+
getTransferPoolsIx(depositFromMarketIndex: number, depositToMarketIndex: number, borrowFromMarketIndex: number, borrowToMarketIndex: number, depositAmount: BN | undefined, borrowAmount: BN | undefined, fromSubAccountId: number, toSubAccountId: number): Promise<TransactionInstruction>;
|
|
346
348
|
updateSpotMarketCumulativeInterest(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
347
349
|
updateSpotMarketCumulativeInterestIx(marketIndex: number): Promise<TransactionInstruction>;
|
|
348
350
|
settleLP(settleeUserAccountPublicKey: PublicKey, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
package/lib/node/driftClient.js
CHANGED
|
@@ -1716,6 +1716,62 @@ class DriftClient {
|
|
|
1716
1716
|
remainingAccounts,
|
|
1717
1717
|
});
|
|
1718
1718
|
}
|
|
1719
|
+
async transferPools(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId, txParams) {
|
|
1720
|
+
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(await this.getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId), txParams), [], this.opts);
|
|
1721
|
+
if (fromSubAccountId === this.activeSubAccountId ||
|
|
1722
|
+
toSubAccountId === this.activeSubAccountId) {
|
|
1723
|
+
this.spotMarketLastSlotCache.set(depositFromMarketIndex, slot);
|
|
1724
|
+
this.spotMarketLastSlotCache.set(depositToMarketIndex, slot);
|
|
1725
|
+
this.spotMarketLastSlotCache.set(borrowFromMarketIndex, slot);
|
|
1726
|
+
this.spotMarketLastSlotCache.set(borrowToMarketIndex, slot);
|
|
1727
|
+
}
|
|
1728
|
+
return txSig;
|
|
1729
|
+
}
|
|
1730
|
+
async getTransferPoolsIx(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount, borrowAmount, fromSubAccountId, toSubAccountId) {
|
|
1731
|
+
const fromUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, fromSubAccountId);
|
|
1732
|
+
const toUser = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, toSubAccountId);
|
|
1733
|
+
const userAccounts = [
|
|
1734
|
+
this.getUserAccount(fromSubAccountId),
|
|
1735
|
+
this.getUserAccount(toSubAccountId),
|
|
1736
|
+
];
|
|
1737
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1738
|
+
userAccounts,
|
|
1739
|
+
useMarketLastSlotCache: true,
|
|
1740
|
+
writableSpotMarketIndexes: [
|
|
1741
|
+
depositFromMarketIndex,
|
|
1742
|
+
depositToMarketIndex,
|
|
1743
|
+
borrowFromMarketIndex,
|
|
1744
|
+
borrowToMarketIndex,
|
|
1745
|
+
],
|
|
1746
|
+
});
|
|
1747
|
+
const tokenPrograms = new Set();
|
|
1748
|
+
const depositFromSpotMarket = this.getSpotMarketAccount(depositFromMarketIndex);
|
|
1749
|
+
const borrowFromSpotMarket = this.getSpotMarketAccount(borrowFromMarketIndex);
|
|
1750
|
+
tokenPrograms.add(this.getTokenProgramForSpotMarket(depositFromSpotMarket).toBase58());
|
|
1751
|
+
tokenPrograms.add(this.getTokenProgramForSpotMarket(borrowFromSpotMarket).toBase58());
|
|
1752
|
+
for (const tokenProgram of tokenPrograms) {
|
|
1753
|
+
remainingAccounts.push({
|
|
1754
|
+
isSigner: false,
|
|
1755
|
+
isWritable: false,
|
|
1756
|
+
pubkey: new web3_js_1.PublicKey(tokenProgram),
|
|
1757
|
+
});
|
|
1758
|
+
}
|
|
1759
|
+
return await this.program.instruction.transferPools(depositFromMarketIndex, depositToMarketIndex, borrowFromMarketIndex, borrowToMarketIndex, depositAmount !== null && depositAmount !== void 0 ? depositAmount : null, borrowAmount !== null && borrowAmount !== void 0 ? borrowAmount : null, {
|
|
1760
|
+
accounts: {
|
|
1761
|
+
authority: this.wallet.publicKey,
|
|
1762
|
+
fromUser,
|
|
1763
|
+
toUser,
|
|
1764
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1765
|
+
state: await this.getStatePublicKey(),
|
|
1766
|
+
depositFromSpotMarketVault: this.getSpotMarketAccount(depositFromMarketIndex).vault,
|
|
1767
|
+
depositToSpotMarketVault: this.getSpotMarketAccount(depositToMarketIndex).vault,
|
|
1768
|
+
borrowFromSpotMarketVault: this.getSpotMarketAccount(borrowFromMarketIndex).vault,
|
|
1769
|
+
borrowToSpotMarketVault: this.getSpotMarketAccount(borrowToMarketIndex).vault,
|
|
1770
|
+
driftSigner: this.getSignerPublicKey(),
|
|
1771
|
+
},
|
|
1772
|
+
remainingAccounts,
|
|
1773
|
+
});
|
|
1774
|
+
}
|
|
1719
1775
|
async updateSpotMarketCumulativeInterest(marketIndex, txParams) {
|
|
1720
1776
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.updateSpotMarketCumulativeInterestIx(marketIndex), txParams), [], this.opts);
|
|
1721
1777
|
return txSig;
|
package/lib/node/idl/drift.json
CHANGED
|
@@ -452,6 +452,91 @@
|
|
|
452
452
|
}
|
|
453
453
|
]
|
|
454
454
|
},
|
|
455
|
+
{
|
|
456
|
+
"name": "transferPools",
|
|
457
|
+
"accounts": [
|
|
458
|
+
{
|
|
459
|
+
"name": "fromUser",
|
|
460
|
+
"isMut": true,
|
|
461
|
+
"isSigner": false
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
"name": "toUser",
|
|
465
|
+
"isMut": true,
|
|
466
|
+
"isSigner": false
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
"name": "userStats",
|
|
470
|
+
"isMut": true,
|
|
471
|
+
"isSigner": false
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
"name": "authority",
|
|
475
|
+
"isMut": false,
|
|
476
|
+
"isSigner": true
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
"name": "state",
|
|
480
|
+
"isMut": false,
|
|
481
|
+
"isSigner": false
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
"name": "depositFromSpotMarketVault",
|
|
485
|
+
"isMut": true,
|
|
486
|
+
"isSigner": false
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
"name": "depositToSpotMarketVault",
|
|
490
|
+
"isMut": true,
|
|
491
|
+
"isSigner": false
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
"name": "borrowFromSpotMarketVault",
|
|
495
|
+
"isMut": true,
|
|
496
|
+
"isSigner": false
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
"name": "borrowToSpotMarketVault",
|
|
500
|
+
"isMut": true,
|
|
501
|
+
"isSigner": false
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
"name": "driftSigner",
|
|
505
|
+
"isMut": false,
|
|
506
|
+
"isSigner": false
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
"args": [
|
|
510
|
+
{
|
|
511
|
+
"name": "depositFromMarketIndex",
|
|
512
|
+
"type": "u16"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"name": "depositToMarketIndex",
|
|
516
|
+
"type": "u16"
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
"name": "borrowFromMarketIndex",
|
|
520
|
+
"type": "u16"
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
"name": "borrowToMarketIndex",
|
|
524
|
+
"type": "u16"
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
"name": "depositAmount",
|
|
528
|
+
"type": {
|
|
529
|
+
"option": "u64"
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
"name": "borrowAmount",
|
|
534
|
+
"type": {
|
|
535
|
+
"option": "u64"
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
},
|
|
455
540
|
{
|
|
456
541
|
"name": "placePerpOrder",
|
|
457
542
|
"accounts": [
|
|
@@ -342,7 +342,13 @@ function calculateWithdrawLimit(spotMarket, now) {
|
|
|
342
342
|
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
343
343
|
.div(sinceLast.add(sinceStart));
|
|
344
344
|
const lesserDepositAmount = anchor_1.BN.min(marketDepositTokenAmount, depositTokenTwapLive);
|
|
345
|
-
|
|
345
|
+
let maxBorrowTokensTwap;
|
|
346
|
+
if (spotMarket.poolId == 0) {
|
|
347
|
+
maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(6)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(10)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(5))))); // main pool between ~15-80% utilization with 10% friction on twap
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
maxBorrowTokensTwap = anchor_1.BN.max(spotMarket.withdrawGuardThreshold, anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(2)), borrowTokenTwapLive.add(lesserDepositAmount.div(new anchor_1.BN(3)))), lesserDepositAmount.sub(lesserDepositAmount.div(new anchor_1.BN(10))))); // isolated pool between ~50-90% utilization with 33% friction on twap
|
|
351
|
+
}
|
|
346
352
|
const minDepositTokensTwap = depositTokenTwapLive.sub(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(4)), anchor_1.BN.min(spotMarket.withdrawGuardThreshold, depositTokenTwapLive)));
|
|
347
353
|
const { minDepositTokensForUtilization, maxBorrowTokensForUtilization } = calculateTokenUtilizationLimits(marketDepositTokenAmount, marketBorrowTokenAmount, spotMarket);
|
|
348
354
|
const minDepositTokens = anchor_1.BN.max(minDepositTokensForUtilization, minDepositTokensTwap);
|
package/package.json
CHANGED
package/src/driftClient.ts
CHANGED
|
@@ -3154,6 +3154,138 @@ export class DriftClient {
|
|
|
3154
3154
|
});
|
|
3155
3155
|
}
|
|
3156
3156
|
|
|
3157
|
+
public async transferPools(
|
|
3158
|
+
depositFromMarketIndex: number,
|
|
3159
|
+
depositToMarketIndex: number,
|
|
3160
|
+
borrowFromMarketIndex: number,
|
|
3161
|
+
borrowToMarketIndex: number,
|
|
3162
|
+
depositAmount: BN | undefined,
|
|
3163
|
+
borrowAmount: BN | undefined,
|
|
3164
|
+
fromSubAccountId: number,
|
|
3165
|
+
toSubAccountId: number,
|
|
3166
|
+
txParams?: TxParams
|
|
3167
|
+
): Promise<TransactionSignature> {
|
|
3168
|
+
const { txSig, slot } = await this.sendTransaction(
|
|
3169
|
+
await this.buildTransaction(
|
|
3170
|
+
await this.getTransferPoolsIx(
|
|
3171
|
+
depositFromMarketIndex,
|
|
3172
|
+
depositToMarketIndex,
|
|
3173
|
+
borrowFromMarketIndex,
|
|
3174
|
+
borrowToMarketIndex,
|
|
3175
|
+
depositAmount,
|
|
3176
|
+
borrowAmount,
|
|
3177
|
+
fromSubAccountId,
|
|
3178
|
+
toSubAccountId
|
|
3179
|
+
),
|
|
3180
|
+
txParams
|
|
3181
|
+
),
|
|
3182
|
+
[],
|
|
3183
|
+
this.opts
|
|
3184
|
+
);
|
|
3185
|
+
|
|
3186
|
+
if (
|
|
3187
|
+
fromSubAccountId === this.activeSubAccountId ||
|
|
3188
|
+
toSubAccountId === this.activeSubAccountId
|
|
3189
|
+
) {
|
|
3190
|
+
this.spotMarketLastSlotCache.set(depositFromMarketIndex, slot);
|
|
3191
|
+
this.spotMarketLastSlotCache.set(depositToMarketIndex, slot);
|
|
3192
|
+
this.spotMarketLastSlotCache.set(borrowFromMarketIndex, slot);
|
|
3193
|
+
this.spotMarketLastSlotCache.set(borrowToMarketIndex, slot);
|
|
3194
|
+
}
|
|
3195
|
+
return txSig;
|
|
3196
|
+
}
|
|
3197
|
+
|
|
3198
|
+
public async getTransferPoolsIx(
|
|
3199
|
+
depositFromMarketIndex: number,
|
|
3200
|
+
depositToMarketIndex: number,
|
|
3201
|
+
borrowFromMarketIndex: number,
|
|
3202
|
+
borrowToMarketIndex: number,
|
|
3203
|
+
depositAmount: BN | undefined,
|
|
3204
|
+
borrowAmount: BN | undefined,
|
|
3205
|
+
fromSubAccountId: number,
|
|
3206
|
+
toSubAccountId: number
|
|
3207
|
+
): Promise<TransactionInstruction> {
|
|
3208
|
+
const fromUser = await getUserAccountPublicKey(
|
|
3209
|
+
this.program.programId,
|
|
3210
|
+
this.wallet.publicKey,
|
|
3211
|
+
fromSubAccountId
|
|
3212
|
+
);
|
|
3213
|
+
const toUser = await getUserAccountPublicKey(
|
|
3214
|
+
this.program.programId,
|
|
3215
|
+
this.wallet.publicKey,
|
|
3216
|
+
toSubAccountId
|
|
3217
|
+
);
|
|
3218
|
+
|
|
3219
|
+
const userAccounts = [
|
|
3220
|
+
this.getUserAccount(fromSubAccountId),
|
|
3221
|
+
this.getUserAccount(toSubAccountId),
|
|
3222
|
+
];
|
|
3223
|
+
|
|
3224
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
3225
|
+
userAccounts,
|
|
3226
|
+
useMarketLastSlotCache: true,
|
|
3227
|
+
writableSpotMarketIndexes: [
|
|
3228
|
+
depositFromMarketIndex,
|
|
3229
|
+
depositToMarketIndex,
|
|
3230
|
+
borrowFromMarketIndex,
|
|
3231
|
+
borrowToMarketIndex,
|
|
3232
|
+
],
|
|
3233
|
+
});
|
|
3234
|
+
|
|
3235
|
+
const tokenPrograms = new Set<string>();
|
|
3236
|
+
const depositFromSpotMarket = this.getSpotMarketAccount(
|
|
3237
|
+
depositFromMarketIndex
|
|
3238
|
+
);
|
|
3239
|
+
const borrowFromSpotMarket = this.getSpotMarketAccount(
|
|
3240
|
+
borrowFromMarketIndex
|
|
3241
|
+
);
|
|
3242
|
+
|
|
3243
|
+
tokenPrograms.add(
|
|
3244
|
+
this.getTokenProgramForSpotMarket(depositFromSpotMarket).toBase58()
|
|
3245
|
+
);
|
|
3246
|
+
tokenPrograms.add(
|
|
3247
|
+
this.getTokenProgramForSpotMarket(borrowFromSpotMarket).toBase58()
|
|
3248
|
+
);
|
|
3249
|
+
|
|
3250
|
+
for (const tokenProgram of tokenPrograms) {
|
|
3251
|
+
remainingAccounts.push({
|
|
3252
|
+
isSigner: false,
|
|
3253
|
+
isWritable: false,
|
|
3254
|
+
pubkey: new PublicKey(tokenProgram),
|
|
3255
|
+
});
|
|
3256
|
+
}
|
|
3257
|
+
|
|
3258
|
+
return await this.program.instruction.transferPools(
|
|
3259
|
+
depositFromMarketIndex,
|
|
3260
|
+
depositToMarketIndex,
|
|
3261
|
+
borrowFromMarketIndex,
|
|
3262
|
+
borrowToMarketIndex,
|
|
3263
|
+
depositAmount ?? null,
|
|
3264
|
+
borrowAmount ?? null,
|
|
3265
|
+
{
|
|
3266
|
+
accounts: {
|
|
3267
|
+
authority: this.wallet.publicKey,
|
|
3268
|
+
fromUser,
|
|
3269
|
+
toUser,
|
|
3270
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
3271
|
+
state: await this.getStatePublicKey(),
|
|
3272
|
+
depositFromSpotMarketVault: this.getSpotMarketAccount(
|
|
3273
|
+
depositFromMarketIndex
|
|
3274
|
+
).vault,
|
|
3275
|
+
depositToSpotMarketVault:
|
|
3276
|
+
this.getSpotMarketAccount(depositToMarketIndex).vault,
|
|
3277
|
+
borrowFromSpotMarketVault: this.getSpotMarketAccount(
|
|
3278
|
+
borrowFromMarketIndex
|
|
3279
|
+
).vault,
|
|
3280
|
+
borrowToSpotMarketVault:
|
|
3281
|
+
this.getSpotMarketAccount(borrowToMarketIndex).vault,
|
|
3282
|
+
driftSigner: this.getSignerPublicKey(),
|
|
3283
|
+
},
|
|
3284
|
+
remainingAccounts,
|
|
3285
|
+
}
|
|
3286
|
+
);
|
|
3287
|
+
}
|
|
3288
|
+
|
|
3157
3289
|
public async updateSpotMarketCumulativeInterest(
|
|
3158
3290
|
marketIndex: number,
|
|
3159
3291
|
txParams?: TxParams
|
package/src/idl/drift.json
CHANGED
|
@@ -452,6 +452,91 @@
|
|
|
452
452
|
}
|
|
453
453
|
]
|
|
454
454
|
},
|
|
455
|
+
{
|
|
456
|
+
"name": "transferPools",
|
|
457
|
+
"accounts": [
|
|
458
|
+
{
|
|
459
|
+
"name": "fromUser",
|
|
460
|
+
"isMut": true,
|
|
461
|
+
"isSigner": false
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
"name": "toUser",
|
|
465
|
+
"isMut": true,
|
|
466
|
+
"isSigner": false
|
|
467
|
+
},
|
|
468
|
+
{
|
|
469
|
+
"name": "userStats",
|
|
470
|
+
"isMut": true,
|
|
471
|
+
"isSigner": false
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
"name": "authority",
|
|
475
|
+
"isMut": false,
|
|
476
|
+
"isSigner": true
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
"name": "state",
|
|
480
|
+
"isMut": false,
|
|
481
|
+
"isSigner": false
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
"name": "depositFromSpotMarketVault",
|
|
485
|
+
"isMut": true,
|
|
486
|
+
"isSigner": false
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
"name": "depositToSpotMarketVault",
|
|
490
|
+
"isMut": true,
|
|
491
|
+
"isSigner": false
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
"name": "borrowFromSpotMarketVault",
|
|
495
|
+
"isMut": true,
|
|
496
|
+
"isSigner": false
|
|
497
|
+
},
|
|
498
|
+
{
|
|
499
|
+
"name": "borrowToSpotMarketVault",
|
|
500
|
+
"isMut": true,
|
|
501
|
+
"isSigner": false
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
"name": "driftSigner",
|
|
505
|
+
"isMut": false,
|
|
506
|
+
"isSigner": false
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
"args": [
|
|
510
|
+
{
|
|
511
|
+
"name": "depositFromMarketIndex",
|
|
512
|
+
"type": "u16"
|
|
513
|
+
},
|
|
514
|
+
{
|
|
515
|
+
"name": "depositToMarketIndex",
|
|
516
|
+
"type": "u16"
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
"name": "borrowFromMarketIndex",
|
|
520
|
+
"type": "u16"
|
|
521
|
+
},
|
|
522
|
+
{
|
|
523
|
+
"name": "borrowToMarketIndex",
|
|
524
|
+
"type": "u16"
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
"name": "depositAmount",
|
|
528
|
+
"type": {
|
|
529
|
+
"option": "u64"
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
{
|
|
533
|
+
"name": "borrowAmount",
|
|
534
|
+
"type": {
|
|
535
|
+
"option": "u64"
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
]
|
|
539
|
+
},
|
|
455
540
|
{
|
|
456
541
|
"name": "placePerpOrder",
|
|
457
542
|
"accounts": [
|
package/src/math/spotBalance.ts
CHANGED
|
@@ -554,16 +554,31 @@ export function calculateWithdrawLimit(
|
|
|
554
554
|
marketDepositTokenAmount,
|
|
555
555
|
depositTokenTwapLive
|
|
556
556
|
);
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
557
|
+
let maxBorrowTokensTwap;
|
|
558
|
+
|
|
559
|
+
if (spotMarket.poolId == 0) {
|
|
560
|
+
maxBorrowTokensTwap = BN.max(
|
|
561
|
+
spotMarket.withdrawGuardThreshold,
|
|
562
|
+
BN.min(
|
|
563
|
+
BN.max(
|
|
564
|
+
marketDepositTokenAmount.div(new BN(6)),
|
|
565
|
+
borrowTokenTwapLive.add(lesserDepositAmount.div(new BN(10)))
|
|
566
|
+
),
|
|
567
|
+
lesserDepositAmount.sub(lesserDepositAmount.div(new BN(5)))
|
|
568
|
+
)
|
|
569
|
+
); // main pool between ~15-80% utilization with 10% friction on twap
|
|
570
|
+
} else {
|
|
571
|
+
maxBorrowTokensTwap = BN.max(
|
|
572
|
+
spotMarket.withdrawGuardThreshold,
|
|
573
|
+
BN.min(
|
|
574
|
+
BN.max(
|
|
575
|
+
marketDepositTokenAmount.div(new BN(2)),
|
|
576
|
+
borrowTokenTwapLive.add(lesserDepositAmount.div(new BN(3)))
|
|
577
|
+
),
|
|
578
|
+
lesserDepositAmount.sub(lesserDepositAmount.div(new BN(10)))
|
|
579
|
+
)
|
|
580
|
+
); // isolated pool between ~50-90% utilization with 33% friction on twap
|
|
581
|
+
}
|
|
567
582
|
|
|
568
583
|
const minDepositTokensTwap = depositTokenTwapLive.sub(
|
|
569
584
|
BN.max(
|