@symmetry-hq/sdk 1.0.1
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/src/constants.d.ts +23 -0
- package/dist/src/constants.js +38 -0
- package/dist/src/index.d.ts +804 -0
- package/dist/src/index.js +2097 -0
- package/dist/src/instructions/automation/auction.d.ts +6 -0
- package/dist/src/instructions/automation/auction.js +40 -0
- package/dist/src/instructions/automation/claimBounty.d.ts +12 -0
- package/dist/src/instructions/automation/claimBounty.js +44 -0
- package/dist/src/instructions/automation/flashSwap.d.ts +21 -0
- package/dist/src/instructions/automation/flashSwap.js +74 -0
- package/dist/src/instructions/automation/priceUpdate.d.ts +19 -0
- package/dist/src/instructions/automation/priceUpdate.js +89 -0
- package/dist/src/instructions/automation/rebalanceIntent.d.ts +32 -0
- package/dist/src/instructions/automation/rebalanceIntent.js +117 -0
- package/dist/src/instructions/automation/rebalanceSwap.d.ts +11 -0
- package/dist/src/instructions/automation/rebalanceSwap.js +42 -0
- package/dist/src/instructions/management/addBounty.d.ts +7 -0
- package/dist/src/instructions/management/addBounty.js +41 -0
- package/dist/src/instructions/management/admin.d.ts +9 -0
- package/dist/src/instructions/management/admin.js +53 -0
- package/dist/src/instructions/management/claimFees.d.ts +15 -0
- package/dist/src/instructions/management/claimFees.js +95 -0
- package/dist/src/instructions/management/createBasket.d.ts +21 -0
- package/dist/src/instructions/management/createBasket.js +98 -0
- package/dist/src/instructions/management/edit.d.ts +51 -0
- package/dist/src/instructions/management/edit.js +477 -0
- package/dist/src/instructions/management/luts.d.ts +30 -0
- package/dist/src/instructions/management/luts.js +99 -0
- package/dist/src/instructions/pda.d.ts +25 -0
- package/dist/src/instructions/pda.js +128 -0
- package/dist/src/instructions/user/deposit.d.ts +20 -0
- package/dist/src/instructions/user/deposit.js +100 -0
- package/dist/src/instructions/user/withdraw.d.ts +8 -0
- package/dist/src/instructions/user/withdraw.js +36 -0
- package/dist/src/jup.d.ts +49 -0
- package/dist/src/jup.js +80 -0
- package/dist/src/keeperMonitor.d.ts +52 -0
- package/dist/src/keeperMonitor.js +624 -0
- package/dist/src/layouts/basket.d.ts +191 -0
- package/dist/src/layouts/basket.js +51 -0
- package/dist/src/layouts/config.d.ts +281 -0
- package/dist/src/layouts/config.js +237 -0
- package/dist/src/layouts/fraction.d.ts +20 -0
- package/dist/src/layouts/fraction.js +164 -0
- package/dist/src/layouts/intents/bounty.d.ts +18 -0
- package/dist/src/layouts/intents/bounty.js +19 -0
- package/dist/src/layouts/intents/intent.d.ts +209 -0
- package/dist/src/layouts/intents/intent.js +97 -0
- package/dist/src/layouts/intents/rebalanceIntent.d.ts +212 -0
- package/dist/src/layouts/intents/rebalanceIntent.js +94 -0
- package/dist/src/layouts/lookupTable.d.ts +7 -0
- package/dist/src/layouts/lookupTable.js +10 -0
- package/dist/src/layouts/oracle.d.ts +63 -0
- package/dist/src/layouts/oracle.js +96 -0
- package/dist/src/states/basket.d.ts +14 -0
- package/dist/src/states/basket.js +479 -0
- package/dist/src/states/config.d.ts +3 -0
- package/dist/src/states/config.js +71 -0
- package/dist/src/states/intents/intent.d.ts +10 -0
- package/dist/src/states/intents/intent.js +316 -0
- package/dist/src/states/intents/rebalanceIntent.d.ts +42 -0
- package/dist/src/states/intents/rebalanceIntent.js +680 -0
- package/dist/src/states/oracles/constants.d.ts +9 -0
- package/dist/src/states/oracles/constants.js +15 -0
- package/dist/src/states/oracles/oracle.d.ts +24 -0
- package/dist/src/states/oracles/oracle.js +168 -0
- package/dist/src/states/oracles/pythOracle.d.ts +132 -0
- package/dist/src/states/oracles/pythOracle.js +609 -0
- package/dist/src/states/oracles/raydiumClmmOracle.d.ts +184 -0
- package/dist/src/states/oracles/raydiumClmmOracle.js +843 -0
- package/dist/src/states/oracles/raydiumCpmmOracle.d.ts +120 -0
- package/dist/src/states/oracles/raydiumCpmmOracle.js +540 -0
- package/dist/src/states/oracles/switchboardOracle.d.ts +0 -0
- package/dist/src/states/oracles/switchboardOracle.js +1 -0
- package/dist/src/states/withdrawBasketFees.d.ts +10 -0
- package/dist/src/states/withdrawBasketFees.js +154 -0
- package/dist/src/txUtils.d.ts +65 -0
- package/dist/src/txUtils.js +306 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +561 -0
- package/package.json +31 -0
- package/src/constants.ts +40 -0
- package/src/index.ts +2431 -0
- package/src/instructions/automation/auction.ts +55 -0
- package/src/instructions/automation/claimBounty.ts +69 -0
- package/src/instructions/automation/flashSwap.ts +104 -0
- package/src/instructions/automation/priceUpdate.ts +117 -0
- package/src/instructions/automation/rebalanceIntent.ts +181 -0
- package/src/instructions/management/addBounty.ts +55 -0
- package/src/instructions/management/admin.ts +72 -0
- package/src/instructions/management/claimFees.ts +129 -0
- package/src/instructions/management/createBasket.ts +138 -0
- package/src/instructions/management/edit.ts +602 -0
- package/src/instructions/management/luts.ts +157 -0
- package/src/instructions/pda.ts +151 -0
- package/src/instructions/user/deposit.ts +143 -0
- package/src/instructions/user/withdraw.ts +53 -0
- package/src/jup.ts +113 -0
- package/src/keeperMonitor.ts +585 -0
- package/src/layouts/basket.ts +233 -0
- package/src/layouts/config.ts +576 -0
- package/src/layouts/fraction.ts +164 -0
- package/src/layouts/intents/bounty.ts +35 -0
- package/src/layouts/intents/intent.ts +324 -0
- package/src/layouts/intents/rebalanceIntent.ts +306 -0
- package/src/layouts/lookupTable.ts +14 -0
- package/src/layouts/oracle.ts +157 -0
- package/src/states/basket.ts +527 -0
- package/src/states/config.ts +62 -0
- package/src/states/intents/intent.ts +311 -0
- package/src/states/intents/rebalanceIntent.ts +751 -0
- package/src/states/oracles/constants.ts +13 -0
- package/src/states/oracles/oracle.ts +212 -0
- package/src/states/oracles/pythOracle.ts +874 -0
- package/src/states/oracles/raydiumClmmOracle.ts +1193 -0
- package/src/states/oracles/raydiumCpmmOracle.ts +784 -0
- package/src/states/oracles/switchboardOracle.ts +0 -0
- package/src/states/withdrawBasketFees.ts +160 -0
- package/src/txUtils.ts +424 -0
- package/test.ts +609 -0
- package/tsconfig.json +101 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,2431 @@
|
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
|
+
|
|
3
|
+
import BN from 'bn.js';
|
|
4
|
+
import {
|
|
5
|
+
createAssociatedTokenAccountIdempotentInstruction, createAssociatedTokenAccountInstruction,
|
|
6
|
+
TOKEN_2022_PROGRAM_ID,
|
|
7
|
+
TOKEN_PROGRAM_ID,
|
|
8
|
+
} from '@solana/spl-token';
|
|
9
|
+
import {
|
|
10
|
+
ComputeBudgetProgram, Connection, Keypair, PublicKey, TransactionInstruction,
|
|
11
|
+
TransactionSignature
|
|
12
|
+
} from '@solana/web3.js';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
COMPUTE_UNITS, LUT_EXTEND_BATCH_SIZE, MINTS, PRIORITY_FEE, UPDATE_TOKEN_PRICES_MAX_ACCOUNTS
|
|
16
|
+
} from './constants';
|
|
17
|
+
import { claimBountyIx } from './instructions/automation/claimBounty';
|
|
18
|
+
import { flashDepositIx, flashWithdrawIx } from './instructions/automation/flashSwap';
|
|
19
|
+
import { updateTokenPricesIx } from './instructions/automation/priceUpdate';
|
|
20
|
+
import {
|
|
21
|
+
cancelRebalanceIx, createRebalanceIntentIx, initRebalanceIntentIx, resizeRebalanceIntentIx
|
|
22
|
+
} from './instructions/automation/rebalanceIntent';
|
|
23
|
+
import { addBountyIx } from './instructions/management/addBounty';
|
|
24
|
+
import { createGlobalConfigIx, editGlobalConfigIx } from './instructions/management/admin';
|
|
25
|
+
import { withdrawFeesIx, claimFeeTokensFromBasketIx, claimFeeTokensFromBasketIxs } from './instructions/management/claimFees';
|
|
26
|
+
import {
|
|
27
|
+
createBasketIx, createBasketStateAccountIx, resizeBasketStateIx
|
|
28
|
+
} from './instructions/management/createBasket';
|
|
29
|
+
import {
|
|
30
|
+
cancelIntentIx, createEditBasketIntentIx, executeEditBasketIntentIx
|
|
31
|
+
} from './instructions/management/edit';
|
|
32
|
+
import {
|
|
33
|
+
createBasketLookupTablesInstruction, extendBasketLookupTablesIx, overwriteBasketLookupTablesIx
|
|
34
|
+
} from './instructions/management/luts';
|
|
35
|
+
import {
|
|
36
|
+
getAta, getBasketFeesPda, getBasketState, getBasketTokenMintPda,
|
|
37
|
+
getIntentPda, getLookupTableAccount, getRebalanceIntentPda, getRentPayerPda, getWithdrawBasketFeesPda
|
|
38
|
+
} from './instructions/pda';
|
|
39
|
+
import { depositTokensIx, lockDepositsIx, mintBasketIx } from './instructions/user/deposit';
|
|
40
|
+
import { redeemTokensIx } from './instructions/user/withdraw';
|
|
41
|
+
import { getJupTokenLedgerAndSwapInstructions } from './jup';
|
|
42
|
+
import {
|
|
43
|
+
Basket, FormattedAccumulatedFees, FormattedAddTokenSettings, FormattedAsset,
|
|
44
|
+
FormattedAutomationSettings, FormattedBasket, FormattedCreatorSettings,
|
|
45
|
+
FormattedCustomRebalanceSettings, FormattedDepositsSettings, FormattedFeeSettings,
|
|
46
|
+
FormattedForceRebalanceSettings, FormattedLookupTables, FormattedLpSettings,
|
|
47
|
+
FormattedMakeDirectSwapSettings, FormattedManagersSettings, FormattedMetadataSettings,
|
|
48
|
+
FormattedOracle, FormattedOracleAggregator, FormattedOracleSettings, FormattedScheduleSettings,
|
|
49
|
+
FormattedUpdateWeightsSettings, WithdrawBasketFees
|
|
50
|
+
} from './layouts/basket';
|
|
51
|
+
import { FormattedGlobalConfig, GlobalConfig } from './layouts/config';
|
|
52
|
+
import { decimalToFraction } from './layouts/fraction';
|
|
53
|
+
import {
|
|
54
|
+
AddOrEditTokenInput, EditAddTokenSettings, EditAutomationSettings, EditCreatorSettings,
|
|
55
|
+
EditCustomRebalanceSettings, EditDepositsSettings, EditFeeSettings, EditForceRebalanceSettings,
|
|
56
|
+
EditLpSettings, EditMakeDirectSwapSettings, EditManagerSettings, EditMetadataSettings,
|
|
57
|
+
EditScheduleSettings, EditUpdateWeightsSettings, FormattedBounty, FormattedBountySchedule,
|
|
58
|
+
FormattedIntent, FormattedIntentStatus, FormattedTaskType, Intent, MakeDirectSwapInput,
|
|
59
|
+
OracleInput, Settings, TaskContext, TaskType, UpdateWeightsInput
|
|
60
|
+
} from './layouts/intents/intent';
|
|
61
|
+
import {
|
|
62
|
+
FormattedOraclePrice, FormattedRebalanceAction, FormattedRebalanceIntent,
|
|
63
|
+
FormattedRebalanceType, FormattedTaskCompletion, FormattedTokenAuction, MintData, PriceUpdatesData, RebalanceIntent,
|
|
64
|
+
RebalanceType, AuctionData,
|
|
65
|
+
RedeemData, DepositData, ClaimBountyData,
|
|
66
|
+
UIRebalanceIntent
|
|
67
|
+
} from './layouts/intents/rebalanceIntent';
|
|
68
|
+
import { FormattedOracleType, OracleType } from './layouts/oracle';
|
|
69
|
+
import {
|
|
70
|
+
BasketFilter, computeTokenMintsHash, fetchBasketsMultiple, fetchBasket, fetchBaskets,
|
|
71
|
+
loadBasketPrice
|
|
72
|
+
} from './states/basket';
|
|
73
|
+
import { fetchGlobalConfig } from './states/config';
|
|
74
|
+
import {
|
|
75
|
+
fetchIntent, fetchIntents, fetchIntentsMultiple, IntentFilter
|
|
76
|
+
} from './states/intents/intent';
|
|
77
|
+
import {
|
|
78
|
+
computeRebalanceIntentBountyAmount, fetchRebalanceIntent, fetchRebalanceIntents,
|
|
79
|
+
fetchRebalanceIntentsMultiple, RebalanceIntentFilter
|
|
80
|
+
} from './states/intents/rebalanceIntent';
|
|
81
|
+
import {
|
|
82
|
+
fetchWithdrawBasketFees, fetchWithdrawBasketFeesMultiple, fetchWithdrawBasketFeesList, WithdrawBasketFeesFilter
|
|
83
|
+
} from './states/withdrawBasketFees';
|
|
84
|
+
import {
|
|
85
|
+
buildPythPriceFeedUpdateIxs, fetchFeedIdsFromAccounts
|
|
86
|
+
} from './states/oracles/pythOracle';
|
|
87
|
+
import {
|
|
88
|
+
BasketCreationTx,
|
|
89
|
+
prepareTxPayloadBatchSequence, prepareVersionedTxs, sendTxPayloadBatchSequence,
|
|
90
|
+
sendVersionedTxs, signTxPayloadBatchSequence, signVersionedTxs, TxBatchData,
|
|
91
|
+
TxPayloadBatchSequence, VersionedTxs, Wallet, wrapWsolIxs
|
|
92
|
+
} from './txUtils';
|
|
93
|
+
|
|
94
|
+
export class SymmetryCore {
|
|
95
|
+
|
|
96
|
+
private sdkParams: {
|
|
97
|
+
connection: Connection,
|
|
98
|
+
network: "devnet" | "mainnet",
|
|
99
|
+
priorityFee: number,
|
|
100
|
+
//TODO: lower comput units for all txs
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Initializes the SymmetryCore SDK.
|
|
105
|
+
* @param params {connection: Connection, network: "devnet" | "mainnet", priorityFee?: number} - The parameters for the SDK.
|
|
106
|
+
* @param params.connection - The connection to the Solana network.
|
|
107
|
+
* @param params.network - The network to use (devnet or mainnet).
|
|
108
|
+
* @param params.priorityFee - Optional. Compute unit price in micro-lamports. Defaults to PRIORITY_FEE (25_000).
|
|
109
|
+
*/
|
|
110
|
+
constructor(params: {
|
|
111
|
+
connection: Connection,
|
|
112
|
+
network: "devnet" | "mainnet",
|
|
113
|
+
priorityFee?: number,
|
|
114
|
+
}) {
|
|
115
|
+
this.sdkParams = {
|
|
116
|
+
connection: params.connection,
|
|
117
|
+
network: params.network,
|
|
118
|
+
priorityFee: params.priorityFee ?? PRIORITY_FEE,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sets the priority fee for the SDK.
|
|
124
|
+
* @param {number} priorityFee - The priority fee in micro-lamports.
|
|
125
|
+
*/
|
|
126
|
+
setPriorityFee(priorityFee: number) {
|
|
127
|
+
this.sdkParams.priorityFee = priorityFee;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Fetches the global config account.
|
|
132
|
+
* @returns {Promise<GlobalConfig>} The global config.
|
|
133
|
+
*/
|
|
134
|
+
async fetchGlobalConfig(): Promise<GlobalConfig> {
|
|
135
|
+
return await fetchGlobalConfig(this.sdkParams.connection);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Fetches a basket by its public key.
|
|
140
|
+
* @param {string} basketPubkey - The public key of the basket.
|
|
141
|
+
* @returns {Promise<Basket>} The basket.
|
|
142
|
+
*/
|
|
143
|
+
async fetchBasket(basketPubkey: string): Promise<Basket> {
|
|
144
|
+
return await fetchBasket(this.sdkParams.connection, new PublicKey(basketPubkey));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Fetches multiple baskets by their public keys.
|
|
149
|
+
* @param {string[]} basketPubkeys - The public keys of the baskets.
|
|
150
|
+
* @returns {Promise<Map<string, Basket>>} A map of basket public keys to baskets.
|
|
151
|
+
*/
|
|
152
|
+
async fetchMultipleBaskets(basketPubkeys: string[]): Promise<Map<string, Basket>> {
|
|
153
|
+
return await fetchBasketsMultiple(this.sdkParams.connection, basketPubkeys.map(key => new PublicKey(key)));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Fetches all baskets.
|
|
158
|
+
* @param {BasketFilter} basketFilter - Optional. The filter to apply to the baskets.
|
|
159
|
+
* @param {"creator" | "host" | "manager"} basketFilter.type - The type of the basket filter ("creator", "host", "manager").
|
|
160
|
+
* @param {string} basketFilter.pubkey - The public key of the basket filter.
|
|
161
|
+
* @returns {Promise<Basket[]>} The baskets.
|
|
162
|
+
*/
|
|
163
|
+
async fetchAllBaskets(basketFilter?: BasketFilter): Promise<Basket[]> {
|
|
164
|
+
return await fetchBaskets(this.sdkParams.connection, basketFilter);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Fetches all baskets created by a given public key.
|
|
169
|
+
* @param {string} creatorPubkey - The public key of the creator.
|
|
170
|
+
* @returns {Promise<Basket[]>} The baskets.
|
|
171
|
+
*/
|
|
172
|
+
async fetchCreatedBaskets(creatorPubkey: string): Promise<Basket[]> {
|
|
173
|
+
return await fetchBaskets(this.sdkParams.connection, { type: "creator", pubkey: creatorPubkey });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Fetches all baskets hosted by a given public key.
|
|
178
|
+
* @param {string} hostPubkey - The public key of the host.
|
|
179
|
+
* @returns {Promise<Basket[]>} The baskets.
|
|
180
|
+
*/
|
|
181
|
+
async fetchHostedBaskets(hostPubkey: string): Promise<Basket[]> {
|
|
182
|
+
return await fetchBaskets(this.sdkParams.connection, { type: "host", pubkey: hostPubkey });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Fetches all baskets managed by a given public key.
|
|
187
|
+
* @param {string} managerPubkey - The public key of the manager.
|
|
188
|
+
* @returns {Promise<Basket[]>} The baskets.
|
|
189
|
+
*/
|
|
190
|
+
async fetchManagedBaskets(managerPubkey: string): Promise<Basket[]> {
|
|
191
|
+
return await fetchBaskets(this.sdkParams.connection, { type: "manager", pubkey: managerPubkey });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Derives baskets by their mints.
|
|
196
|
+
* @param {string[]} mints - The mint public keys of the baskets.
|
|
197
|
+
* @returns {Promise<Map<string, string>>} A map of basket mint public keys to basket public keys.
|
|
198
|
+
*/
|
|
199
|
+
async deriveBasketsByMints(mints: string[]): Promise<Map<string, string>> {
|
|
200
|
+
let result: Map<string, string> = new Map();
|
|
201
|
+
for (let basketId = 0; basketId < 50000; basketId++) {
|
|
202
|
+
let mint = getBasketTokenMintPda(basketId).toBase58();
|
|
203
|
+
if (mints.includes(mint)) {
|
|
204
|
+
result.set(mint, getBasketState(new PublicKey(mint)).toBase58());
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Fetches baskets by their mint public keys.
|
|
212
|
+
* @param {string[]} mints - The mint public keys of the baskets.
|
|
213
|
+
* @returns {Promise<Map<string, Basket>>} A map of basket mint public keys to baskets.
|
|
214
|
+
*/
|
|
215
|
+
async fetchBasketsFromMints(
|
|
216
|
+
mints: string[],
|
|
217
|
+
): Promise<Map<string, Basket>> {
|
|
218
|
+
let basketPubkeys: PublicKey[] = mints.map(mint => {
|
|
219
|
+
let res: PublicKey | null = null;
|
|
220
|
+
try { res = getBasketState(new PublicKey(mint)) } catch (e) { return null }
|
|
221
|
+
return res;
|
|
222
|
+
}).filter(mint => mint !== null);
|
|
223
|
+
const basketsMap = await fetchBasketsMultiple(this.sdkParams.connection, basketPubkeys);
|
|
224
|
+
let mintsMap: Map<string, Basket> = new Map();
|
|
225
|
+
for (let basket of basketsMap.values()) {
|
|
226
|
+
mintsMap.set(basket.mint.toBase58(), basket);
|
|
227
|
+
}
|
|
228
|
+
return mintsMap;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Loads the price of a basket.
|
|
233
|
+
* @param {Basket} basket - The basket.
|
|
234
|
+
* @returns {Promise<Basket>} The basket with the price.
|
|
235
|
+
*/
|
|
236
|
+
async loadBasketPrice(
|
|
237
|
+
basket: Basket,
|
|
238
|
+
): Promise<Basket> {
|
|
239
|
+
return await loadBasketPrice(basket, this.sdkParams.connection);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Fetches an intent by its public key.
|
|
244
|
+
* @param {string} intentPubkey - The public key of the intent.
|
|
245
|
+
* @returns {Promise<Intent>} The intent.
|
|
246
|
+
*/
|
|
247
|
+
async fetchIntent(intentPubkey: string): Promise<Intent> {
|
|
248
|
+
return await fetchIntent(this.sdkParams.connection, new PublicKey(intentPubkey));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Fetches multiple intents by their public keys.
|
|
253
|
+
* @param {string[]} intentPubkeys - The public keys of the intents.
|
|
254
|
+
* @returns {Promise<Map<string, Intent>>} A map of intent public keys to intents.
|
|
255
|
+
*/
|
|
256
|
+
async fetchMultipleIntents(intentPubkeys: string[]): Promise<Map<string, Intent>> {
|
|
257
|
+
return await fetchIntentsMultiple(this.sdkParams.connection, intentPubkeys.map(key => new PublicKey(key)));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Fetches all intents.
|
|
262
|
+
* @param {IntentFilter} filter - Optional. The filter to apply to the intents.
|
|
263
|
+
* @param {"creator" | "host" | "manager" | "basket" | "owner"} filter.type - The type of the intent filter ("creator", "host", "manager", "basket", "owner").
|
|
264
|
+
* @param {string} filter.pubkey - The public key of the intent filter.
|
|
265
|
+
* @returns {Promise<Intent[]>} The intents.
|
|
266
|
+
*/
|
|
267
|
+
async fetchAllIntents(filter?: IntentFilter): Promise<Intent[]> {
|
|
268
|
+
return await fetchIntents(this.sdkParams.connection, filter);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Fetches all intents created by a given public key.
|
|
273
|
+
* @param {string} creatorPubkey - The public key of the creator.
|
|
274
|
+
* @returns {Promise<Intent[]>} The intents.
|
|
275
|
+
*/
|
|
276
|
+
async fetchCreatedIntents(creatorPubkey: string): Promise<Intent[]> {
|
|
277
|
+
return await fetchIntents(this.sdkParams.connection, { type: "manager", pubkey: creatorPubkey });
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Fetches all intents for a given basket.
|
|
282
|
+
* @param {string} basketPubkey - The public key of the basket.
|
|
283
|
+
* @returns {Promise<Intent[]>} The intents.
|
|
284
|
+
*/
|
|
285
|
+
async fetchBasketIntents(basketPubkey: string): Promise<Intent[]> {
|
|
286
|
+
return await fetchIntents(this.sdkParams.connection, { type: "basket", pubkey: basketPubkey });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Fetches a rebalance intent by its public key.
|
|
291
|
+
* @param {string} rebalanceIntentPubkey - The public key of the rebalance intent.
|
|
292
|
+
* @returns {Promise<UIRebalanceIntent>} The rebalance intent.
|
|
293
|
+
*/
|
|
294
|
+
async fetchRebalanceIntent(rebalanceIntentPubkey: string): Promise<UIRebalanceIntent> {
|
|
295
|
+
return await fetchRebalanceIntent(this.sdkParams.connection, new PublicKey(rebalanceIntentPubkey));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Fetches multiple rebalance intents by their public keys.
|
|
300
|
+
* @param {string[]} rebalanceIntentPubkeys - The public keys of the rebalance intents.
|
|
301
|
+
* @returns {Promise<Map<string, UIRebalanceIntent>>} A map of rebalance intent public keys to rebalance intents.
|
|
302
|
+
*/
|
|
303
|
+
async fetchMultipleRebalanceIntents(rebalanceIntentPubkeys: string[]): Promise<Map<string, UIRebalanceIntent>> {
|
|
304
|
+
return await fetchRebalanceIntentsMultiple(this.sdkParams.connection, rebalanceIntentPubkeys.map(key => new PublicKey(key)));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Fetches all rebalance intents.
|
|
309
|
+
* @param {RebalanceIntentFilter} filter - Optional. The filter to apply to the rebalance intents.
|
|
310
|
+
* @param {"owner" | "basket"} filter.type - The type of the rebalance intent filter ("owner", "basket").
|
|
311
|
+
* @param {string} filter.pubkey - The public key of the rebalance intent filter.
|
|
312
|
+
* @returns {Promise<UIRebalanceIntent[]>} The rebalance intents.
|
|
313
|
+
*/
|
|
314
|
+
async fetchAllRebalanceIntents(filter?: RebalanceIntentFilter): Promise<UIRebalanceIntent[]> {
|
|
315
|
+
return await fetchRebalanceIntents(this.sdkParams.connection, filter);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Fetches all rebalance intents for a given owner.
|
|
320
|
+
* @param {string} ownerPubkey - The public key of the owner.
|
|
321
|
+
* @returns {Promise<UIRebalanceIntent[]>} The rebalance intents.
|
|
322
|
+
*/
|
|
323
|
+
async fetchOwnerRebalanceIntents(ownerPubkey: string): Promise<UIRebalanceIntent[]> {
|
|
324
|
+
return await fetchRebalanceIntents(this.sdkParams.connection, { type: "owner", pubkey: ownerPubkey });
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Fetches all rebalance intents for a given basket.
|
|
329
|
+
* @param {string} basketPubkey - The public key of the basket.
|
|
330
|
+
* @returns {Promise<UIRebalanceIntent[]>} The rebalance intents.
|
|
331
|
+
*/
|
|
332
|
+
async fetchBasketRebalanceIntents(basketPubkey: string): Promise<UIRebalanceIntent[]> {
|
|
333
|
+
return await fetchRebalanceIntents(this.sdkParams.connection, { type: "basket", pubkey: basketPubkey });
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Fetches a WithdrawBasketFees account by its public key.
|
|
338
|
+
* @param {string} withdrawBasketFeesPubkey - The public key of the WithdrawBasketFees account.
|
|
339
|
+
* @returns {Promise<WithdrawBasketFees>} The WithdrawBasketFees account.
|
|
340
|
+
*/
|
|
341
|
+
async fetchWithdrawBasketFees(withdrawBasketFeesPubkey: string): Promise<WithdrawBasketFees> {
|
|
342
|
+
return await fetchWithdrawBasketFees(this.sdkParams.connection, new PublicKey(withdrawBasketFeesPubkey));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Fetches multiple WithdrawBasketFees accounts by their public keys.
|
|
347
|
+
* @param {string[]} withdrawBasketFeesPubkeys - The public keys of the WithdrawBasketFees accounts.
|
|
348
|
+
* @returns {Promise<Map<string, WithdrawBasketFees>>} A map of WithdrawBasketFees public keys to WithdrawBasketFees accounts.
|
|
349
|
+
*/
|
|
350
|
+
async fetchMultipleWithdrawBasketFees(withdrawBasketFeesPubkeys: string[]): Promise<Map<string, WithdrawBasketFees>> {
|
|
351
|
+
return await fetchWithdrawBasketFeesMultiple(this.sdkParams.connection, withdrawBasketFeesPubkeys.map(key => new PublicKey(key)));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Fetches all WithdrawBasketFees accounts with optional filter.
|
|
356
|
+
* @param {WithdrawBasketFeesFilter} filter - Optional. The filter to apply to the WithdrawBasketFees accounts.
|
|
357
|
+
* @param {"basket" | "manager" | "creator" | "host" | "symmetry"} filter.type - The type of the filter.
|
|
358
|
+
* @param {string} filter.pubkey - The public key to filter by.
|
|
359
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
360
|
+
*/
|
|
361
|
+
async fetchAllWithdrawBasketFees(filter?: WithdrawBasketFeesFilter): Promise<WithdrawBasketFees[]> {
|
|
362
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, filter);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Fetches all WithdrawBasketFees accounts for a given basket.
|
|
367
|
+
* @param {string} basketPubkey - The public key of the basket.
|
|
368
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
369
|
+
*/
|
|
370
|
+
async fetchBasketWithdrawBasketFees(basketPubkey: string): Promise<WithdrawBasketFees[]> {
|
|
371
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, { type: "basket", pubkey: basketPubkey });
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Fetches all WithdrawBasketFees accounts for a given manager.
|
|
376
|
+
* @param {string} managerPubkey - The public key of the manager.
|
|
377
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
378
|
+
*/
|
|
379
|
+
async fetchManagerWithdrawBasketFees(managerPubkey: string): Promise<WithdrawBasketFees[]> {
|
|
380
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, { type: "manager", pubkey: managerPubkey });
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Fetches all WithdrawBasketFees accounts for a given creator.
|
|
385
|
+
* @param {string} creatorPubkey - The public key of the creator.
|
|
386
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
387
|
+
*/
|
|
388
|
+
async fetchCreatorWithdrawBasketFees(creatorPubkey: string): Promise<WithdrawBasketFees[]> {
|
|
389
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, { type: "creator", pubkey: creatorPubkey });
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Fetches all WithdrawBasketFees accounts for a given host.
|
|
394
|
+
* @param {string} hostPubkey - The public key of the host.
|
|
395
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
396
|
+
*/
|
|
397
|
+
async fetchHostWithdrawBasketFees(hostPubkey: string): Promise<WithdrawBasketFees[]> {
|
|
398
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, { type: "host", pubkey: hostPubkey });
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Fetches all WithdrawBasketFees accounts for a given symmetry fee collector.
|
|
403
|
+
* @param {string} symmetryPubkey - The public key of the symmetry fee collector.
|
|
404
|
+
* @returns {Promise<WithdrawBasketFees[]>} The WithdrawBasketFees accounts.
|
|
405
|
+
*/
|
|
406
|
+
async fetchSymmetryWithdrawBasketFees(symmetryPubkey: string): Promise<WithdrawBasketFees[]> {
|
|
407
|
+
return await fetchWithdrawBasketFeesList(this.sdkParams.connection, { type: "symmetry", pubkey: symmetryPubkey });
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async createGlobalConfigTx(params: {
|
|
411
|
+
signer: PublicKey,
|
|
412
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
413
|
+
let ix = createGlobalConfigIx({admin: params.signer});
|
|
414
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
415
|
+
payer: params.signer,
|
|
416
|
+
instructions: [
|
|
417
|
+
ix,
|
|
418
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
419
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
420
|
+
],
|
|
421
|
+
lookupTables: [],
|
|
422
|
+
}]]};
|
|
423
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
424
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
425
|
+
return txPayloadBatchSequence;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async editGlobalConfigTx(params: {
|
|
429
|
+
signer: PublicKey,
|
|
430
|
+
config: GlobalConfig,
|
|
431
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
432
|
+
let ix = editGlobalConfigIx(params);
|
|
433
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
434
|
+
payer: params.signer,
|
|
435
|
+
instructions: [
|
|
436
|
+
ix,
|
|
437
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
438
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
439
|
+
],
|
|
440
|
+
lookupTables: [],
|
|
441
|
+
}]]};
|
|
442
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
443
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
444
|
+
return txPayloadBatchSequence;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Creates a basket.
|
|
449
|
+
* @param {Object} params - The parameters for the basket creation.
|
|
450
|
+
* @param {string} params.creator - The public key of the creator.
|
|
451
|
+
* @param {string} params.start_price - The start price of the basket.
|
|
452
|
+
* @param {string} params.name - The name of the basket.
|
|
453
|
+
* @param {string} params.symbol - The symbol of the basket.
|
|
454
|
+
* @param {string} params.metadata_uri - The metadata URI of the basket.
|
|
455
|
+
* @param {Object} params.host_platform_params - Optional. The host platform parameters.
|
|
456
|
+
* @param {string} params.host_platform_params.host_pubkey - The public key of the host.
|
|
457
|
+
* @param {number} params.host_platform_params.host_deposit_fee_bps - The host deposit fee in basis points.
|
|
458
|
+
* @param {number} params.host_platform_params.host_withdraw_fee_bps - The host withdrawal fee in basis points.
|
|
459
|
+
* @param {number} params.host_platform_params.host_management_fee_bps - The host management fee in basis points.
|
|
460
|
+
* @param {number} params.host_platform_params.host_performance_fee_bps - The host performance fee in basis points.
|
|
461
|
+
* @returns {Promise<BasketCreationTx>} The basket creation transaction.
|
|
462
|
+
*/
|
|
463
|
+
async createBasketTx(params: {
|
|
464
|
+
creator: string,
|
|
465
|
+
start_price: string,
|
|
466
|
+
name: string,
|
|
467
|
+
symbol: string,
|
|
468
|
+
metadata_uri: string,
|
|
469
|
+
host_platform_params?: {
|
|
470
|
+
host_pubkey: string,
|
|
471
|
+
host_deposit_fee_bps: number,
|
|
472
|
+
host_withdraw_fee_bps: number,
|
|
473
|
+
host_management_fee_bps: number,
|
|
474
|
+
host_performance_fee_bps: number,
|
|
475
|
+
},
|
|
476
|
+
}): Promise<BasketCreationTx> {
|
|
477
|
+
const creator = new PublicKey(params.creator);
|
|
478
|
+
const host = new PublicKey(params.host_platform_params?.host_pubkey ?? params.creator);
|
|
479
|
+
const hostFees = {
|
|
480
|
+
hostDepositFeeBps: params.host_platform_params?.host_deposit_fee_bps ?? 0,
|
|
481
|
+
hostWithdrawalFeeBps: params.host_platform_params?.host_withdraw_fee_bps ?? 0,
|
|
482
|
+
hostManagementFeeBps: params.host_platform_params?.host_management_fee_bps ?? 0,
|
|
483
|
+
hostPerformanceFeeBps: params.host_platform_params?.host_performance_fee_bps ?? 0,
|
|
484
|
+
};
|
|
485
|
+
const metadataParams = {
|
|
486
|
+
name: params.name,
|
|
487
|
+
symbol: params.symbol,
|
|
488
|
+
uri: params.metadata_uri,
|
|
489
|
+
};
|
|
490
|
+
const startPrice = decimalToFraction(new Decimal(parseFloat(params.start_price) / 10 ** 6));
|
|
491
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
492
|
+
let bountyWsolAmount = 0;
|
|
493
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
494
|
+
let boundBounty = parseInt(globalConfig.bountyBondAmount.toString());
|
|
495
|
+
let minBasketAutomationBounty = parseInt(globalConfig.minBountyForBasketAutomation.toString());
|
|
496
|
+
bountyWsolAmount = boundBounty + minBasketAutomationBounty;
|
|
497
|
+
}
|
|
498
|
+
let wsolIxs = await wrapWsolIxs(
|
|
499
|
+
this.sdkParams.connection,
|
|
500
|
+
creator,
|
|
501
|
+
bountyWsolAmount,
|
|
502
|
+
);
|
|
503
|
+
let basketId = parseInt(globalConfig.totalNumberOfBaskets.toString());
|
|
504
|
+
let mint = getBasketTokenMintPda(basketId);
|
|
505
|
+
let basket = getBasketState(mint);
|
|
506
|
+
const slot = await this.sdkParams.connection.getSlot();
|
|
507
|
+
|
|
508
|
+
let ix = createBasketIx({
|
|
509
|
+
basket,
|
|
510
|
+
mint,
|
|
511
|
+
slot,
|
|
512
|
+
creator,
|
|
513
|
+
host,
|
|
514
|
+
startPrice: startPrice,
|
|
515
|
+
hostFees,
|
|
516
|
+
metadataParams,
|
|
517
|
+
network: this.sdkParams.network,
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
let preIx = createBasketStateAccountIx({
|
|
521
|
+
creator,
|
|
522
|
+
basket
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
let preIx2 = resizeBasketStateIx({
|
|
526
|
+
basket
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
let createBasketFeesAccountIx = createAssociatedTokenAccountInstruction(
|
|
530
|
+
creator,
|
|
531
|
+
getAta(getBasketFeesPda(basket), mint),
|
|
532
|
+
getBasketFeesPda(basket),
|
|
533
|
+
mint,
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
537
|
+
payer: creator,
|
|
538
|
+
instructions: [
|
|
539
|
+
...wsolIxs,
|
|
540
|
+
preIx, preIx2, preIx2, ix, createBasketFeesAccountIx,
|
|
541
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
542
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
543
|
+
],
|
|
544
|
+
lookupTables: [],
|
|
545
|
+
}]]};
|
|
546
|
+
|
|
547
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
548
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
549
|
+
return {
|
|
550
|
+
mint: mint.toBase58(),
|
|
551
|
+
basket: basket.toBase58(),
|
|
552
|
+
batches: txPayloadBatchSequence.batches,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Changes the creator of a basket.
|
|
558
|
+
* @param {TaskContext} context - The context of the task.
|
|
559
|
+
* @param {EditCreatorSettings} settings - The settings for the creator edit.
|
|
560
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
561
|
+
*/
|
|
562
|
+
async editCreatorTx(context: TaskContext, settings: EditCreatorSettings): Promise<TxPayloadBatchSequence> {
|
|
563
|
+
return this.openBasketIntentTx({ type: TaskType.EditCreator, context, settings });
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Creates an edit manager settings intent for a basket.
|
|
568
|
+
* @param {TaskContext} context - The context of the task.
|
|
569
|
+
* @param {EditManagerSettings} settings - The settings for the manager edit.
|
|
570
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
571
|
+
*/
|
|
572
|
+
async editManagersTx(context: TaskContext, settings: EditManagerSettings): Promise<TxPayloadBatchSequence> {
|
|
573
|
+
return this.openBasketIntentTx({ type: TaskType.EditManagerSettings, context, settings });
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Creates an edit schedule settings intent for a basket.
|
|
578
|
+
* @param {TaskContext} context - The context of the task.
|
|
579
|
+
* @param {EditScheduleSettings} settings - The settings for the schedule edit.
|
|
580
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
581
|
+
*/
|
|
582
|
+
async editScheduleTx(context: TaskContext, settings: EditScheduleSettings): Promise<TxPayloadBatchSequence> {
|
|
583
|
+
return this.openBasketIntentTx({ type: TaskType.EditScheduleSettings, context, settings });
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Creates an edit fees settings intent for a basket.
|
|
587
|
+
* @param {TaskContext} context - The context of the task.
|
|
588
|
+
* @param {EditFeeSettings} settings - The settings for the fee edit.
|
|
589
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
590
|
+
*/
|
|
591
|
+
async editFeesTx(context: TaskContext, settings: EditFeeSettings): Promise<TxPayloadBatchSequence> {
|
|
592
|
+
return this.openBasketIntentTx({ type: TaskType.EditFeeSettings, context, settings });
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Creates an edit automation settings intent for a basket.
|
|
596
|
+
* @param {TaskContext} context - The context of the task.
|
|
597
|
+
* @param {EditAutomationSettings} settings - The settings for the automation edit.
|
|
598
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
599
|
+
*/
|
|
600
|
+
async editAutomationTx(context: TaskContext, settings: EditAutomationSettings): Promise<TxPayloadBatchSequence> {
|
|
601
|
+
return this.openBasketIntentTx({ type: TaskType.EditAutomationSettings, context, settings });
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Creates an edit LP settings intent for a basket.
|
|
605
|
+
* @param {TaskContext} context - The context of the task.
|
|
606
|
+
* @param {EditLpSettings} settings - The settings for the LP edit.
|
|
607
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
608
|
+
*/
|
|
609
|
+
async editLpTx(context: TaskContext, settings: EditLpSettings): Promise<TxPayloadBatchSequence> {
|
|
610
|
+
return this.openBasketIntentTx({ type: TaskType.EditLpSettings, context, settings });
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Creates an edit metadata settings intent for a basket.
|
|
614
|
+
* @param {TaskContext} context - The context of the task.
|
|
615
|
+
* @param {EditMetadataSettings} settings - The settings for the metadata edit.
|
|
616
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
617
|
+
*/
|
|
618
|
+
async editMetadataTx(context: TaskContext, settings: EditMetadataSettings): Promise<TxPayloadBatchSequence> {
|
|
619
|
+
return this.openBasketIntentTx({ type: TaskType.EditMetadataSettings, context, settings });
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Creates an edit deposits settings intent for a basket.
|
|
623
|
+
* @param {TaskContext} context - The context of the task.
|
|
624
|
+
* @param {EditDepositsSettings} settings - The settings for the deposits edit.
|
|
625
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
626
|
+
*/
|
|
627
|
+
async editDepositsTx(context: TaskContext, settings: EditDepositsSettings): Promise<TxPayloadBatchSequence> {
|
|
628
|
+
return this.openBasketIntentTx({ type: TaskType.EditDepositsSettings, context, settings });
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Creates an edit force rebalance settings intent for a basket.
|
|
632
|
+
* @param {TaskContext} context - The context of the task.
|
|
633
|
+
* @param {EditForceRebalanceSettings} settings - The settings for the force rebalance edit.
|
|
634
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
635
|
+
*/
|
|
636
|
+
async editForceRebalanceTx(context: TaskContext, settings: EditForceRebalanceSettings): Promise<TxPayloadBatchSequence> {
|
|
637
|
+
return this.openBasketIntentTx({ type: TaskType.EditForceRebalanceSettings, context, settings });
|
|
638
|
+
}
|
|
639
|
+
/**
|
|
640
|
+
* Creates an edit custom rebalance settings intent for a basket.
|
|
641
|
+
* @param {TaskContext} context - The context of the task.
|
|
642
|
+
* @param {EditCustomRebalanceSettings} settings - The settings for the custom rebalance edit.
|
|
643
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
644
|
+
*/
|
|
645
|
+
async editCustomRebalanceTx(context: TaskContext, settings: EditCustomRebalanceSettings): Promise<TxPayloadBatchSequence> {
|
|
646
|
+
return this.openBasketIntentTx({ type: TaskType.EditCustomRebalanceSettings, context, settings });
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Creates an edit add token delay intent for a basket.
|
|
651
|
+
* @param {TaskContext} context - The context of the task.
|
|
652
|
+
* @param {EditAddTokenSettings} settings - The settings for the add token delay edit.
|
|
653
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
654
|
+
*/
|
|
655
|
+
async editAddTokenDelayTx(context: TaskContext, settings: EditAddTokenSettings): Promise<TxPayloadBatchSequence> {
|
|
656
|
+
return this.openBasketIntentTx({ type: TaskType.EditAddTokenDelay, context, settings });
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Creates an edit update weights delay intent for a basket.
|
|
660
|
+
* @param {TaskContext} context - The context of the task.
|
|
661
|
+
* @param {EditUpdateWeightsSettings} settings - The settings for the update weights delay edit.
|
|
662
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
663
|
+
*/
|
|
664
|
+
async editUpdateWeightsDelayTx(context: TaskContext, settings: EditUpdateWeightsSettings): Promise<TxPayloadBatchSequence> {
|
|
665
|
+
return this.openBasketIntentTx({ type: TaskType.EditUpdateWeightsDelay, context, settings });
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Creates an edit swap delay intent for a basket.
|
|
669
|
+
* @param {TaskContext} context - The context of the task.
|
|
670
|
+
* @param {EditMakeDirectSwapSettings} settings - The settings for the make direct swap delay edit.
|
|
671
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
672
|
+
*/
|
|
673
|
+
async editSwapDelayTx(context: TaskContext, settings: EditMakeDirectSwapSettings): Promise<TxPayloadBatchSequence> {
|
|
674
|
+
return this.openBasketIntentTx({ type: TaskType.EditMakeDirectSwapDelay, context, settings });
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Creates an add or edit token intent for a basket.
|
|
679
|
+
* @param {TaskContext} context - The context of the task.
|
|
680
|
+
* @param {AddOrEditTokenInput} settings - The settings for the add or edit token.
|
|
681
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
682
|
+
*/
|
|
683
|
+
async addOrEditTokenTx(context: TaskContext, settings: AddOrEditTokenInput): Promise<TxPayloadBatchSequence> {
|
|
684
|
+
return this.openBasketIntentTx({ type: TaskType.AddToken, context, settings });
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Creates a update weights intent for a basket.
|
|
688
|
+
* @param {TaskContext} context - The context of the task.
|
|
689
|
+
* @param {UpdateWeightsInput} settings - The settings for the update weights.
|
|
690
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
691
|
+
*/
|
|
692
|
+
async updateWeightsTx(context: TaskContext, settings: UpdateWeightsInput): Promise<TxPayloadBatchSequence> {
|
|
693
|
+
return this.openBasketIntentTx({ type: TaskType.UpdateWeights, context, settings });
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Creates a direct swap intent for a basket.
|
|
697
|
+
* @param {TaskContext} context - The context of the task.
|
|
698
|
+
* @param {MakeDirectSwapInput} settings - The settings for the make direct swap.
|
|
699
|
+
* @param {TransactionInstruction} jup_swap_ix - Optional. The JUP swap instruction.
|
|
700
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
701
|
+
*/
|
|
702
|
+
async makeDirectSwapTx(context: TaskContext, settings: MakeDirectSwapInput, jup_swap_ix?: TransactionInstruction): Promise<TxPayloadBatchSequence> {
|
|
703
|
+
return this.openBasketIntentTx({ type: TaskType.MakeDirectSwap, context, settings, jup_swap_ix: jup_swap_ix });
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Creates basket intent account.
|
|
708
|
+
* @param {Object} params - The parameters for the basket intent.
|
|
709
|
+
* @param {TaskContext} params.context - The context of the task.
|
|
710
|
+
* @param {TaskType} params.type - The type of the task.
|
|
711
|
+
* @param {Settings} params.settings - The settings for the task.
|
|
712
|
+
* @param {number} params.min_bounty_per_task - Optional. The minimum bounty per task.
|
|
713
|
+
* @param {number} params.max_bounty_per_task - Optional. The maximum bounty per task.
|
|
714
|
+
* @param {TransactionInstruction} params.jup_swap_ix - Optional. The JUP swap instruction (for MakeDirectSwap task).
|
|
715
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
716
|
+
*/
|
|
717
|
+
async openBasketIntentTx(params: {
|
|
718
|
+
context: TaskContext,
|
|
719
|
+
type: TaskType,
|
|
720
|
+
settings: Settings,
|
|
721
|
+
jup_swap_ix?: TransactionInstruction,
|
|
722
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
723
|
+
let basket = await this.fetchBasket(params.context.basket);
|
|
724
|
+
let manager = new PublicKey(params.context.manager);
|
|
725
|
+
let activationTimestamp = new BN(params.context.activation_timestamp ?? 0);
|
|
726
|
+
let expirationTimestamp = new BN(params.context.expiration_timestamp ?? 0);
|
|
727
|
+
let editType = params.type;
|
|
728
|
+
let editData = params.settings;
|
|
729
|
+
let modificationDelay = (() => {
|
|
730
|
+
switch(editType) {
|
|
731
|
+
case TaskType.EditCreator: return new BN(0);
|
|
732
|
+
case TaskType.EditManagerSettings: return basket.settings.managers.modificationDelay;
|
|
733
|
+
case TaskType.EditFeeSettings: return basket.settings.fees.modificationDelay;
|
|
734
|
+
case TaskType.EditScheduleSettings: return basket.settings.schedule.modificationDelay;
|
|
735
|
+
case TaskType.EditAutomationSettings: return basket.settings.automation.modificationDelay;
|
|
736
|
+
case TaskType.EditLpSettings: return basket.settings.lp.modificationDelay;
|
|
737
|
+
case TaskType.EditMetadataSettings: return basket.settings.metadata.modificationDelay;
|
|
738
|
+
case TaskType.EditDepositsSettings: return new BN(0);
|
|
739
|
+
case TaskType.EditForceRebalanceSettings: return basket.settings.forceRebalanceModificationDelay;
|
|
740
|
+
case TaskType.EditCustomRebalanceSettings: return basket.settings.customRebalanceModificationDelay;
|
|
741
|
+
case TaskType.EditAddTokenDelay: return basket.settings.addTokenDelay;
|
|
742
|
+
case TaskType.EditUpdateWeightsDelay: return basket.settings.updateWeightsDelay;
|
|
743
|
+
case TaskType.EditMakeDirectSwapDelay: return basket.settings.makeDirectSwapDelay;
|
|
744
|
+
case TaskType.AddToken: return basket.settings.addTokenDelay;
|
|
745
|
+
case TaskType.UpdateWeights: return basket.settings.updateWeightsDelay;
|
|
746
|
+
case TaskType.MakeDirectSwap: return basket.settings.makeDirectSwapDelay;
|
|
747
|
+
default: return new BN(0);
|
|
748
|
+
}
|
|
749
|
+
})();
|
|
750
|
+
|
|
751
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
752
|
+
let minBounty = params.context.min_bounty !== undefined
|
|
753
|
+
? new BN(params.context.min_bounty)
|
|
754
|
+
: globalConfig.bountyPerTask.minBounty;
|
|
755
|
+
let maxBounty = params.context.max_bounty !== undefined
|
|
756
|
+
? new BN(params.context.max_bounty)
|
|
757
|
+
: globalConfig.bountyPerTask.maxBounty;
|
|
758
|
+
if (parseInt(minBounty.toString()) > parseInt(maxBounty.toString()))
|
|
759
|
+
minBounty = maxBounty.div(new BN(2));
|
|
760
|
+
let bountyWsolAmount = 0;
|
|
761
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
762
|
+
let boundBounty = parseInt(globalConfig.bountyBondAmount.toString());
|
|
763
|
+
let maxBountyPerTask = parseInt(maxBounty.toString());
|
|
764
|
+
bountyWsolAmount = maxBountyPerTask + boundBounty;
|
|
765
|
+
}
|
|
766
|
+
let wsolIxs = await wrapWsolIxs(
|
|
767
|
+
this.sdkParams.connection,
|
|
768
|
+
manager,
|
|
769
|
+
bountyWsolAmount,
|
|
770
|
+
);
|
|
771
|
+
|
|
772
|
+
let intentSeedArray = Keypair.generate().publicKey.toBytes();
|
|
773
|
+
let tokenProgram: PublicKey | undefined = undefined;
|
|
774
|
+
if (editType === TaskType.AddToken) {
|
|
775
|
+
let tokenMint = new PublicKey((editData as AddOrEditTokenInput).token_mint);
|
|
776
|
+
intentSeedArray = tokenMint.toBytes();
|
|
777
|
+
let tokenInfo = await this.sdkParams.connection.getAccountInfo(tokenMint);
|
|
778
|
+
if (tokenInfo!.owner.equals(TOKEN_PROGRAM_ID) || tokenInfo!.owner.equals(TOKEN_2022_PROGRAM_ID)) {
|
|
779
|
+
tokenProgram = tokenInfo!.owner;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
let intent = getIntentPda(basket.ownAddress, intentSeedArray, editType);
|
|
784
|
+
|
|
785
|
+
let openBasketIntentIx = createEditBasketIntentIx({
|
|
786
|
+
manager: manager,
|
|
787
|
+
basket: basket,
|
|
788
|
+
intent: intent,
|
|
789
|
+
intentSeedArray: intentSeedArray,
|
|
790
|
+
editType: editType,
|
|
791
|
+
editData: editData,
|
|
792
|
+
activationTimestamp: activationTimestamp,
|
|
793
|
+
expirationTimestamp: expirationTimestamp,
|
|
794
|
+
minBounty: minBounty,
|
|
795
|
+
maxBounty: maxBounty,
|
|
796
|
+
tokenProgram: tokenProgram,
|
|
797
|
+
});
|
|
798
|
+
|
|
799
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
800
|
+
payer: manager,
|
|
801
|
+
instructions: [
|
|
802
|
+
...wsolIxs,
|
|
803
|
+
openBasketIntentIx,
|
|
804
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
805
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
806
|
+
],
|
|
807
|
+
lookupTables: [],
|
|
808
|
+
}]]};
|
|
809
|
+
|
|
810
|
+
// Only execute in the same transaction when there is no delay and no scheduled activation.
|
|
811
|
+
if (modificationDelay.eq(new BN(0)) && activationTimestamp.eq(new BN(0))) {
|
|
812
|
+
let executeBasketIntentIx = executeEditBasketIntentIx({
|
|
813
|
+
basket: basket.ownAddress,
|
|
814
|
+
intent: intent,
|
|
815
|
+
editType: editType,
|
|
816
|
+
manager: manager,
|
|
817
|
+
bountyMint: basket.settings.bountyMint,
|
|
818
|
+
keeper: manager,
|
|
819
|
+
basketMint: basket.mint,
|
|
820
|
+
});
|
|
821
|
+
if (editType == TaskType.MakeDirectSwap && params.jup_swap_ix) {
|
|
822
|
+
let flashParams = {
|
|
823
|
+
keeper: manager,
|
|
824
|
+
basket: basket.ownAddress,
|
|
825
|
+
rebalanceIntent: undefined,
|
|
826
|
+
intent: intent,
|
|
827
|
+
mintIn: new PublicKey((editData as MakeDirectSwapInput).from_token_mint),
|
|
828
|
+
mintOut: new PublicKey((editData as MakeDirectSwapInput).to_token_mint),
|
|
829
|
+
amountIn: new BN((editData as MakeDirectSwapInput).amount_from),
|
|
830
|
+
amountOut: new BN((editData as MakeDirectSwapInput).amount_to),
|
|
831
|
+
mode: undefined,
|
|
832
|
+
}
|
|
833
|
+
let ixWithdraw = flashWithdrawIx(flashParams);
|
|
834
|
+
let ixDeposit = flashDepositIx(flashParams);
|
|
835
|
+
txBatchData.batches.push([{
|
|
836
|
+
payer: manager,
|
|
837
|
+
instructions: [
|
|
838
|
+
createAssociatedTokenAccountIdempotentInstruction(manager, getAta(manager, flashParams.mintIn), manager, flashParams.mintIn),
|
|
839
|
+
createAssociatedTokenAccountIdempotentInstruction(manager, getAta(manager, flashParams.mintOut), manager, flashParams.mintOut),
|
|
840
|
+
ixWithdraw,
|
|
841
|
+
params.jup_swap_ix,
|
|
842
|
+
ixDeposit,
|
|
843
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
844
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
845
|
+
],
|
|
846
|
+
lookupTables: [],
|
|
847
|
+
}]);
|
|
848
|
+
txBatchData.batches.push([{
|
|
849
|
+
payer: manager,
|
|
850
|
+
instructions: [
|
|
851
|
+
executeBasketIntentIx,
|
|
852
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
853
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
854
|
+
],
|
|
855
|
+
lookupTables: [],
|
|
856
|
+
}]);
|
|
857
|
+
} else {
|
|
858
|
+
txBatchData.batches[0][0].instructions = [
|
|
859
|
+
...wsolIxs,
|
|
860
|
+
openBasketIntentIx,
|
|
861
|
+
executeBasketIntentIx,
|
|
862
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
863
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
864
|
+
];
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
869
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
870
|
+
return txPayloadBatchSequence;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Executes a basket intent. Basket intent will be executed immediately.
|
|
875
|
+
* @param {Object} params - The parameters for the basket intent.
|
|
876
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
877
|
+
* @param {string} params.intent - The public key of the intent.
|
|
878
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
879
|
+
*/
|
|
880
|
+
async executeBasketIntentTx(params: {
|
|
881
|
+
keeper: string,
|
|
882
|
+
intent: string,
|
|
883
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
884
|
+
let keeper = new PublicKey(params.keeper);
|
|
885
|
+
let intent = await this.fetchIntent(params.intent);
|
|
886
|
+
let basketMint: PublicKey | undefined;
|
|
887
|
+
if (intent.taskType === TaskType.EditMetadataSettings) {
|
|
888
|
+
let basket = await this.fetchBasket(intent.basket.toBase58());
|
|
889
|
+
basketMint = basket.mint;
|
|
890
|
+
}
|
|
891
|
+
let ix = executeEditBasketIntentIx({
|
|
892
|
+
basket: intent.basket,
|
|
893
|
+
intent: intent.ownAddress!,
|
|
894
|
+
editType: intent.taskType,
|
|
895
|
+
manager: intent.manager,
|
|
896
|
+
bountyMint: intent.bounty.bountyMint,
|
|
897
|
+
keeper: keeper,
|
|
898
|
+
basketMint: intent.taskType === TaskType.EditMetadataSettings ? basketMint : undefined,
|
|
899
|
+
});
|
|
900
|
+
|
|
901
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
902
|
+
payer: keeper,
|
|
903
|
+
instructions: [
|
|
904
|
+
ix,
|
|
905
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
906
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
907
|
+
],
|
|
908
|
+
lookupTables: [],
|
|
909
|
+
}]]};
|
|
910
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
911
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
912
|
+
return txPayloadBatchSequence;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Cancels a basket intent. Basket intent account will get closed.
|
|
917
|
+
* @param {Object} params - The parameters for the basket intent.
|
|
918
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
919
|
+
* @param {string} params.intent - The public key of the intent.
|
|
920
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
921
|
+
*/
|
|
922
|
+
async cancelBasketIntentTx(params: {
|
|
923
|
+
keeper: string,
|
|
924
|
+
intent: string,
|
|
925
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
926
|
+
let keeper = new PublicKey(params.keeper);
|
|
927
|
+
let intent = await this.fetchIntent(params.intent);
|
|
928
|
+
let ix = cancelIntentIx({
|
|
929
|
+
keeper: keeper,
|
|
930
|
+
basket: intent.basket,
|
|
931
|
+
intent: intent.ownAddress!,
|
|
932
|
+
editType: intent.taskType,
|
|
933
|
+
manager: intent.manager,
|
|
934
|
+
bountyMint: intent.bounty.bountyMint,
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
938
|
+
payer: keeper,
|
|
939
|
+
instructions: [
|
|
940
|
+
ix,
|
|
941
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
942
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
943
|
+
],
|
|
944
|
+
lookupTables: [],
|
|
945
|
+
}]]};
|
|
946
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
947
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
948
|
+
return txPayloadBatchSequence;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Cancels a rebalance intent. Rebalance intent status will be set to AuctionFinished.
|
|
953
|
+
* @param {Object} params - The parameters for the rebalance intent.
|
|
954
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
955
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent.
|
|
956
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
957
|
+
*/
|
|
958
|
+
async cancelRebalanceIntentTx(params: {
|
|
959
|
+
keeper: string,
|
|
960
|
+
rebalance_intent: string,
|
|
961
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
962
|
+
let keeper = new PublicKey(params.keeper);
|
|
963
|
+
let intent: RebalanceIntent = (await this.fetchRebalanceIntent(params.rebalance_intent)).chain_data;
|
|
964
|
+
let ix = cancelRebalanceIx({
|
|
965
|
+
keeper: keeper,
|
|
966
|
+
basket: intent.basket,
|
|
967
|
+
rebalanceIntent: intent.ownAddress!,
|
|
968
|
+
});
|
|
969
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
970
|
+
payer: keeper,
|
|
971
|
+
instructions: [
|
|
972
|
+
ix,
|
|
973
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
974
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
975
|
+
],
|
|
976
|
+
lookupTables: [],
|
|
977
|
+
}]]};
|
|
978
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
979
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
980
|
+
return txPayloadBatchSequence;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* Creates a rebalance intent for a user to deposit tokens into a basket.
|
|
985
|
+
*
|
|
986
|
+
* Batch layout:
|
|
987
|
+
*
|
|
988
|
+
* batch 0:
|
|
989
|
+
* * tx0: [create, resize, init rebalance intent],
|
|
990
|
+
*
|
|
991
|
+
* batch 1:
|
|
992
|
+
* * tx0: [deposit tokens ix],
|
|
993
|
+
* * tx1: [deposit tokens ix],
|
|
994
|
+
* * ...
|
|
995
|
+
*
|
|
996
|
+
* batch 2:
|
|
997
|
+
* * tx0: [lock deposits ix, start price updates ix],
|
|
998
|
+
*
|
|
999
|
+
* @param {Object} params - The parameters for the basket purchase.
|
|
1000
|
+
* @param {string} params.buyer - The public key of the buyer.
|
|
1001
|
+
* @param {string} params.basket_mint - The mint public key of the basket.
|
|
1002
|
+
* @param {{mint: string, amount: number}[]} params.contributions - The contributions to the basket.
|
|
1003
|
+
* @param {string} params.contributions[].mint - The mint public key of the contribution.
|
|
1004
|
+
* @param {number} params.contributions[].amount - The amount of the contribution.
|
|
1005
|
+
* @param {number} params.rebalance_slippage_bps - Optional. The rebalance slippage in basis points.
|
|
1006
|
+
* @param {number} params.per_trade_rebalance_slippage_bps - Optional. The per trade rebalance slippage in basis points.
|
|
1007
|
+
* @param {number} params.execution_start_time - Optional. The execution start time.
|
|
1008
|
+
* @param {number} params.min_bounty_amount - Optional. The minimum bounty amount.
|
|
1009
|
+
* @param {number} params.max_bounty_amount - Optional. The maximum bounty amount.
|
|
1010
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1011
|
+
*/
|
|
1012
|
+
async buyBasketTx(params: {
|
|
1013
|
+
buyer: string,
|
|
1014
|
+
basket_mint: string,
|
|
1015
|
+
contributions: {mint: string, amount: number}[],
|
|
1016
|
+
rebalance_slippage_bps?: number,
|
|
1017
|
+
per_trade_rebalance_slippage_bps?: number,
|
|
1018
|
+
execution_start_time?: number,
|
|
1019
|
+
min_bounty_amount?: number,
|
|
1020
|
+
max_bounty_amount?: number,
|
|
1021
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1022
|
+
let buyer = new PublicKey(params.buyer);
|
|
1023
|
+
let contributions = params.contributions.map(contribution => ({
|
|
1024
|
+
mint: new PublicKey(contribution.mint),
|
|
1025
|
+
amount: contribution.amount,
|
|
1026
|
+
}));
|
|
1027
|
+
let rebalanceSlippageBps = params.rebalance_slippage_bps ?? 100;
|
|
1028
|
+
let perTradeRebalanceSlippageBps = params.per_trade_rebalance_slippage_bps ?? 100;
|
|
1029
|
+
let executionStartTime = params.execution_start_time ?? 0;
|
|
1030
|
+
let minBountyAmount = params.min_bounty_amount ?? 0;
|
|
1031
|
+
let maxBountyAmount = params.max_bounty_amount ?? 0;
|
|
1032
|
+
|
|
1033
|
+
const basket = await this.fetchBasket(getBasketState(new PublicKey(params.basket_mint)).toBase58());
|
|
1034
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
1035
|
+
|
|
1036
|
+
let bountyWsolAmount = 0;
|
|
1037
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
1038
|
+
let boundBounty = parseInt(globalConfig.bountyBondAmount.toString());
|
|
1039
|
+
let maxBountyPerTask = parseInt(globalConfig.bountyPerTask.maxBounty.toString());
|
|
1040
|
+
let bountyPerPriceUpdateTaskDivisor = parseInt(globalConfig.bountyPerPriceUpdateTaskDivisor.toString());
|
|
1041
|
+
if (maxBountyAmount > maxBountyPerTask)
|
|
1042
|
+
maxBountyPerTask = maxBountyAmount;
|
|
1043
|
+
bountyWsolAmount = computeRebalanceIntentBountyAmount(
|
|
1044
|
+
RebalanceType.Deposit,
|
|
1045
|
+
basket.numTokens,
|
|
1046
|
+
boundBounty,
|
|
1047
|
+
maxBountyPerTask,
|
|
1048
|
+
Math.floor(maxBountyPerTask / bountyPerPriceUpdateTaskDivisor),
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
let wsolContributionAmount = params.contributions.find(contribution => contribution.mint === MINTS["mainnet"].WSOL.toBase58())?.amount ?? 0;
|
|
1052
|
+
|
|
1053
|
+
let wsolIxs = await wrapWsolIxs(
|
|
1054
|
+
this.sdkParams.connection,
|
|
1055
|
+
buyer,
|
|
1056
|
+
bountyWsolAmount + wsolContributionAmount,
|
|
1057
|
+
);
|
|
1058
|
+
|
|
1059
|
+
const rebalanceIntent = getRebalanceIntentPda(basket.ownAddress, buyer);
|
|
1060
|
+
|
|
1061
|
+
let rentPayer = getRentPayerPda();
|
|
1062
|
+
|
|
1063
|
+
let rentPayerAi = await this.sdkParams.connection.getAccountInfo(rentPayer);
|
|
1064
|
+
|
|
1065
|
+
let rebalanceIntentRentPayer = buyer;
|
|
1066
|
+
// TODO: check actual lamports required for rent
|
|
1067
|
+
if (rentPayerAi?.lamports ?? 0 > 500_000_000)
|
|
1068
|
+
rebalanceIntentRentPayer = rentPayer;
|
|
1069
|
+
let bountyMint = basket.settings.bountyMint;
|
|
1070
|
+
|
|
1071
|
+
// let lockDepositsIxs: TransactionInstruction[] = [
|
|
1072
|
+
// lockDepositsIx({
|
|
1073
|
+
// owner: buyer,
|
|
1074
|
+
// basket: basket.ownAddress,
|
|
1075
|
+
// })
|
|
1076
|
+
// ];
|
|
1077
|
+
|
|
1078
|
+
let txBatchData: TxBatchData = {batches: [
|
|
1079
|
+
[{
|
|
1080
|
+
payer: buyer,
|
|
1081
|
+
instructions: [
|
|
1082
|
+
...wsolIxs,
|
|
1083
|
+
createRebalanceIntentIx({
|
|
1084
|
+
signer: buyer,
|
|
1085
|
+
owner: buyer,
|
|
1086
|
+
basket: basket.ownAddress,
|
|
1087
|
+
}),
|
|
1088
|
+
resizeRebalanceIntentIx(rebalanceIntent),
|
|
1089
|
+
initRebalanceIntentIx({
|
|
1090
|
+
signer: buyer,
|
|
1091
|
+
owner: buyer,
|
|
1092
|
+
basket: basket.ownAddress,
|
|
1093
|
+
basketTokenMint: basket.mint,
|
|
1094
|
+
rebalanceIntentRentPayer: rebalanceIntentRentPayer,
|
|
1095
|
+
bountyMint: bountyMint,
|
|
1096
|
+
rebalanceType: RebalanceType.Deposit,
|
|
1097
|
+
basketRebalanceIntent: undefined,
|
|
1098
|
+
rebalanceSlippageBps: rebalanceSlippageBps,
|
|
1099
|
+
perTradeRebalanceSlippageBps: perTradeRebalanceSlippageBps,
|
|
1100
|
+
executionStartTime: executionStartTime,
|
|
1101
|
+
minBountyAmount: minBountyAmount,
|
|
1102
|
+
maxBountyAmount: maxBountyAmount,
|
|
1103
|
+
}),
|
|
1104
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1105
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1106
|
+
],
|
|
1107
|
+
lookupTables: [],
|
|
1108
|
+
}],
|
|
1109
|
+
depositTokensIx({
|
|
1110
|
+
owner: buyer,
|
|
1111
|
+
basket: basket.ownAddress,
|
|
1112
|
+
contributions: contributions,
|
|
1113
|
+
}).map(ix => {
|
|
1114
|
+
return {
|
|
1115
|
+
payer: buyer,
|
|
1116
|
+
instructions: [
|
|
1117
|
+
ix,
|
|
1118
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1119
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee})
|
|
1120
|
+
],
|
|
1121
|
+
lookupTables: [],
|
|
1122
|
+
};
|
|
1123
|
+
}),
|
|
1124
|
+
// [{
|
|
1125
|
+
// payer: buyer,
|
|
1126
|
+
// instructions: [
|
|
1127
|
+
// ...lockDepositsIxs,
|
|
1128
|
+
// ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1129
|
+
// ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1130
|
+
// ],
|
|
1131
|
+
// lookupTables: [],
|
|
1132
|
+
// }]
|
|
1133
|
+
]};
|
|
1134
|
+
|
|
1135
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1136
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1137
|
+
return txPayloadBatchSequence;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Deposits additional tokens into an existing user deposit rebalance intent.
|
|
1142
|
+
*
|
|
1143
|
+
* This is useful when the user already created a deposit intent (for example via buyBasketTx),
|
|
1144
|
+
* but has not called lockDepositsTx yet and wants to add more contributions.
|
|
1145
|
+
*
|
|
1146
|
+
* @param {Object} params - The parameters for depositing more tokens.
|
|
1147
|
+
* @param {string} params.buyer - The public key of the buyer / deposit owner.
|
|
1148
|
+
* @param {{mint: string, amount: number}[]} params.contributions - Extra token contributions.
|
|
1149
|
+
* @param {string} params.rebalance_intent - Optional. Existing rebalance intent pubkey.
|
|
1150
|
+
* @param {RebalanceIntent} params.rebalance_intent_chain_data - Optional. Existing rebalance intent chain data.
|
|
1151
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1152
|
+
*/
|
|
1153
|
+
async depositTokensTx(params: {
|
|
1154
|
+
buyer: string,
|
|
1155
|
+
contributions: {mint: string, amount: number}[],
|
|
1156
|
+
rebalance_intent?: string,
|
|
1157
|
+
rebalance_intent_chain_data?: RebalanceIntent,
|
|
1158
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1159
|
+
let buyer = new PublicKey(params.buyer);
|
|
1160
|
+
if (params.contributions.length === 0) {
|
|
1161
|
+
throw new Error("Contributions cannot be empty");
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
let contributions = params.contributions.map(contribution => ({
|
|
1165
|
+
mint: new PublicKey(contribution.mint),
|
|
1166
|
+
amount: contribution.amount,
|
|
1167
|
+
}));
|
|
1168
|
+
|
|
1169
|
+
let rebalanceIntentState: RebalanceIntent;
|
|
1170
|
+
if (params.rebalance_intent_chain_data) {
|
|
1171
|
+
rebalanceIntentState = params.rebalance_intent_chain_data;
|
|
1172
|
+
} else if (params.rebalance_intent) {
|
|
1173
|
+
rebalanceIntentState = (await this.fetchRebalanceIntent(params.rebalance_intent)).chain_data;
|
|
1174
|
+
} else {
|
|
1175
|
+
throw new Error("Provide one of rebalance_intent_chain_data or rebalance_intent");
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
let wsolContributionAmount = params.contributions
|
|
1179
|
+
.filter(contribution => contribution.mint === MINTS["mainnet"].WSOL.toBase58())
|
|
1180
|
+
.reduce((acc, contribution) => acc + contribution.amount, 0);
|
|
1181
|
+
|
|
1182
|
+
let wsolIxs = await wrapWsolIxs(
|
|
1183
|
+
this.sdkParams.connection,
|
|
1184
|
+
buyer,
|
|
1185
|
+
wsolContributionAmount,
|
|
1186
|
+
);
|
|
1187
|
+
|
|
1188
|
+
let txBatchData: TxBatchData = { batches: [] };
|
|
1189
|
+
|
|
1190
|
+
if (wsolIxs.length > 0) {
|
|
1191
|
+
txBatchData.batches.push([{
|
|
1192
|
+
payer: buyer,
|
|
1193
|
+
instructions: [
|
|
1194
|
+
...wsolIxs,
|
|
1195
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1196
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1197
|
+
],
|
|
1198
|
+
lookupTables: [],
|
|
1199
|
+
}]);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
txBatchData.batches.push(
|
|
1203
|
+
depositTokensIx({
|
|
1204
|
+
owner: buyer,
|
|
1205
|
+
basket: rebalanceIntentState.basket,
|
|
1206
|
+
contributions: contributions,
|
|
1207
|
+
}).map(ix => ({
|
|
1208
|
+
payer: buyer,
|
|
1209
|
+
instructions: [
|
|
1210
|
+
ix,
|
|
1211
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1212
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1213
|
+
],
|
|
1214
|
+
lookupTables: [],
|
|
1215
|
+
}))
|
|
1216
|
+
);
|
|
1217
|
+
|
|
1218
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1219
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1220
|
+
return txPayloadBatchSequence;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Locks deposits for a user and starts auction flow.
|
|
1225
|
+
* @param {Object} params - The parameters for the lock deposits.
|
|
1226
|
+
* @param {string} params.buyer - The public key of the buyer.
|
|
1227
|
+
* @param {string} params.basket_mint - The mint public key of the basket.
|
|
1228
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1229
|
+
*/
|
|
1230
|
+
async lockDepositsTx(params: {
|
|
1231
|
+
buyer: string,
|
|
1232
|
+
basket_mint: string,
|
|
1233
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1234
|
+
let buyer = new PublicKey(params.buyer);
|
|
1235
|
+
let basket = await this.fetchBasket(getBasketState(new PublicKey(params.basket_mint)).toBase58());
|
|
1236
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
1237
|
+
payer: buyer,
|
|
1238
|
+
instructions: [
|
|
1239
|
+
lockDepositsIx({
|
|
1240
|
+
owner: buyer,
|
|
1241
|
+
basket: basket.ownAddress,
|
|
1242
|
+
}),
|
|
1243
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1244
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1245
|
+
],
|
|
1246
|
+
lookupTables: [],
|
|
1247
|
+
}]]};
|
|
1248
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1249
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1250
|
+
return txPayloadBatchSequence;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/**
|
|
1254
|
+
* Creates a rebalance intent for a user to withdraw tokens from a basket.
|
|
1255
|
+
* @param {{Object}} params - The parameters for the basket sale.
|
|
1256
|
+
* @param {string} params.seller - The public key of the seller.
|
|
1257
|
+
* @param {string} params.basket_mint - The mint public key of the basket.
|
|
1258
|
+
* @param {number} params.withdraw_amount - The amount of the withdrawal.
|
|
1259
|
+
* @param {string[]} params.keep_tokens - The token mints to skip rebalancing (will directly be sent to the user without being rebalanced).
|
|
1260
|
+
* @param {number} params.rebalance_slippage_bps - Optional. The rebalance slippage in basis points.
|
|
1261
|
+
* @param {number} params.per_trade_rebalance_slippage_bps - Optional. The per trade rebalance slippage in basis points.
|
|
1262
|
+
* @param {number} params.execution_start_time - Optional. The execution start time.
|
|
1263
|
+
* @param {number} params.min_bounty_amount - Optional. The minimum bounty amount.
|
|
1264
|
+
* @param {number} params.max_bounty_amount - Optional. The maximum bounty amount.
|
|
1265
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1266
|
+
*/
|
|
1267
|
+
async sellBasketTx(params: {
|
|
1268
|
+
seller: string,
|
|
1269
|
+
basket_mint: string,
|
|
1270
|
+
withdraw_amount: number,
|
|
1271
|
+
keep_tokens: string[],
|
|
1272
|
+
rebalance_slippage_bps?: number,
|
|
1273
|
+
per_trade_rebalance_slippage_bps?: number,
|
|
1274
|
+
execution_start_time?: number,
|
|
1275
|
+
min_bounty_amount?: number,
|
|
1276
|
+
max_bounty_amount?: number,
|
|
1277
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1278
|
+
let seller = new PublicKey(params.seller);
|
|
1279
|
+
let basketMint = new PublicKey(params.basket_mint);
|
|
1280
|
+
let withdrawAmount = params.withdraw_amount;
|
|
1281
|
+
let keepTokens = params.keep_tokens;
|
|
1282
|
+
let rebalanceSlippageBps = params.rebalance_slippage_bps ?? 100;
|
|
1283
|
+
let perTradeRebalanceSlippageBps = params.per_trade_rebalance_slippage_bps ?? 100;
|
|
1284
|
+
let executionStartTime = params.execution_start_time ?? 0;
|
|
1285
|
+
let minBountyAmount = params.min_bounty_amount ?? 0;
|
|
1286
|
+
let maxBountyAmount = params.max_bounty_amount ?? 0;
|
|
1287
|
+
|
|
1288
|
+
const basket = await this.fetchBasket(getBasketState(basketMint).toBase58());
|
|
1289
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
1290
|
+
|
|
1291
|
+
let bountyWsolAmount = 0;
|
|
1292
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
1293
|
+
let boundBounty = parseInt(globalConfig.bountyBondAmount.toString());
|
|
1294
|
+
let maxBountyPerTask = parseInt(globalConfig.bountyPerTask.maxBounty.toString());
|
|
1295
|
+
let bountyPerPriceUpdateTaskDivisor = parseInt(globalConfig.bountyPerPriceUpdateTaskDivisor.toString());
|
|
1296
|
+
if (maxBountyAmount > maxBountyPerTask)
|
|
1297
|
+
maxBountyPerTask = maxBountyAmount;
|
|
1298
|
+
bountyWsolAmount = computeRebalanceIntentBountyAmount(
|
|
1299
|
+
RebalanceType.Deposit,
|
|
1300
|
+
basket.numTokens,
|
|
1301
|
+
boundBounty,
|
|
1302
|
+
maxBountyPerTask,
|
|
1303
|
+
Math.floor(maxBountyPerTask / bountyPerPriceUpdateTaskDivisor),
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
let wsolIxs = await wrapWsolIxs(
|
|
1308
|
+
this.sdkParams.connection,
|
|
1309
|
+
seller,
|
|
1310
|
+
bountyWsolAmount,
|
|
1311
|
+
);
|
|
1312
|
+
|
|
1313
|
+
const rebalanceIntent = getRebalanceIntentPda(basket.ownAddress, seller);
|
|
1314
|
+
|
|
1315
|
+
let rentPayer = getRentPayerPda();
|
|
1316
|
+
|
|
1317
|
+
let rentPayerAi = await this.sdkParams.connection.getAccountInfo(rentPayer);
|
|
1318
|
+
|
|
1319
|
+
let rebalanceIntentRentPayer = seller;
|
|
1320
|
+
// TODO: check actual lamports required for rent
|
|
1321
|
+
if (rentPayerAi?.lamports ?? 0 > 500_000_000)
|
|
1322
|
+
rebalanceIntentRentPayer = rentPayer;
|
|
1323
|
+
let bountyMint = basket.settings.bountyMint;
|
|
1324
|
+
|
|
1325
|
+
let keepAllTokensFlag = 1;
|
|
1326
|
+
for (let i = 0; i < basket.numTokens; i++) {
|
|
1327
|
+
if (!keepTokens.includes(basket.composition[i].mint.toBase58())) {
|
|
1328
|
+
keepAllTokensFlag = 0;
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
let tokenMintsHash = computeTokenMintsHash(
|
|
1334
|
+
basket.composition.slice(0, basket.numTokens)
|
|
1335
|
+
.map(asset => asset.mint.toBase58())
|
|
1336
|
+
);
|
|
1337
|
+
|
|
1338
|
+
let keepTokensBitmask = new BN(0);
|
|
1339
|
+
if (keepTokens && keepTokens.length > 0) {
|
|
1340
|
+
for (const mint of keepTokens) {
|
|
1341
|
+
let tokenIndex = -1;
|
|
1342
|
+
for (let i = 0; i < basket.numTokens; i++) {
|
|
1343
|
+
if (basket.composition[i].mint.toBase58() === mint) {
|
|
1344
|
+
tokenIndex = i;
|
|
1345
|
+
break;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
if (tokenIndex === -1) {
|
|
1349
|
+
throw new Error(`Token mint not in basket: ${mint}`);
|
|
1350
|
+
}
|
|
1351
|
+
keepTokensBitmask = keepTokensBitmask.or(new BN(1).shln(tokenIndex));
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
const basketRebalanceIntent = basket.settings.activeRebalance.gt(new BN(0))
|
|
1356
|
+
? getRebalanceIntentPda(basket.ownAddress, basket.ownAddress)
|
|
1357
|
+
: undefined;
|
|
1358
|
+
|
|
1359
|
+
let txBatchData: TxBatchData = {batches: [
|
|
1360
|
+
[{
|
|
1361
|
+
payer: seller,
|
|
1362
|
+
instructions: [
|
|
1363
|
+
...wsolIxs,
|
|
1364
|
+
createRebalanceIntentIx({
|
|
1365
|
+
signer: seller,
|
|
1366
|
+
owner: seller,
|
|
1367
|
+
basket: basket.ownAddress,
|
|
1368
|
+
}),
|
|
1369
|
+
resizeRebalanceIntentIx(rebalanceIntent),
|
|
1370
|
+
initRebalanceIntentIx({
|
|
1371
|
+
signer: seller,
|
|
1372
|
+
owner: seller,
|
|
1373
|
+
basket: basket.ownAddress,
|
|
1374
|
+
basketTokenMint: basketMint,
|
|
1375
|
+
rebalanceIntentRentPayer: rebalanceIntentRentPayer,
|
|
1376
|
+
bountyMint: bountyMint,
|
|
1377
|
+
rebalanceType: RebalanceType.Withdraw,
|
|
1378
|
+
basketRebalanceIntent: basketRebalanceIntent,
|
|
1379
|
+
rebalanceSlippageBps: rebalanceSlippageBps,
|
|
1380
|
+
perTradeRebalanceSlippageBps: perTradeRebalanceSlippageBps,
|
|
1381
|
+
executionStartTime: executionStartTime,
|
|
1382
|
+
minBountyAmount: minBountyAmount,
|
|
1383
|
+
maxBountyAmount: maxBountyAmount,
|
|
1384
|
+
withdrawParamsBurnAmount: withdrawAmount,
|
|
1385
|
+
withdrawParamsTokenMintsHash: tokenMintsHash,
|
|
1386
|
+
withdrawParamsKeepTokensBitmask: keepTokensBitmask,
|
|
1387
|
+
withdrawParamsKeepAllTokens: keepAllTokensFlag,
|
|
1388
|
+
}),
|
|
1389
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1390
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1391
|
+
],
|
|
1392
|
+
lookupTables: [],
|
|
1393
|
+
}],
|
|
1394
|
+
]};
|
|
1395
|
+
|
|
1396
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1397
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1398
|
+
return txPayloadBatchSequence;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/**
|
|
1402
|
+
* Creates a rebalance intent for a basket rebalance.
|
|
1403
|
+
* @param {Object} params - The parameters for the basket rebalance.
|
|
1404
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1405
|
+
* @param {string} params.basket_mint - The basket token mint public key.
|
|
1406
|
+
* @param {number} params.rebalance_slippage_bps - Optional. The rebalance slippage in basis points.
|
|
1407
|
+
* @param {number} params.per_trade_rebalance_slippage_bps - Optional. The per trade rebalance slippage in basis points.
|
|
1408
|
+
* @param {number} params.execution_start_time - Optional. The execution start time.
|
|
1409
|
+
* @param {number} params.min_bounty_amount - Optional. The minimum bounty amount.
|
|
1410
|
+
* @param {number} params.max_bounty_amount - Optional. The maximum bounty amount.
|
|
1411
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1412
|
+
*/
|
|
1413
|
+
async rebalanceBasketTx(params: {
|
|
1414
|
+
keeper: string,
|
|
1415
|
+
basket_mint: string,
|
|
1416
|
+
rebalance_slippage_bps?: number,
|
|
1417
|
+
per_trade_rebalance_slippage_bps?: number,
|
|
1418
|
+
execution_start_time?: number,
|
|
1419
|
+
min_bounty_amount?: number,
|
|
1420
|
+
max_bounty_amount?: number,
|
|
1421
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1422
|
+
let keeper = new PublicKey(params.keeper);
|
|
1423
|
+
let rebalanceSlippageBps = params.rebalance_slippage_bps ?? 100;
|
|
1424
|
+
let perTradeRebalanceSlippageBps = params.per_trade_rebalance_slippage_bps ?? 100;
|
|
1425
|
+
let executionStartTime = params.execution_start_time ?? 0;
|
|
1426
|
+
let minBountyAmount = params.min_bounty_amount ?? 0;
|
|
1427
|
+
let maxBountyAmount = params.max_bounty_amount ?? 0;
|
|
1428
|
+
|
|
1429
|
+
const basket = await this.fetchBasket(getBasketState(new PublicKey(params.basket_mint)).toBase58());
|
|
1430
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
1431
|
+
|
|
1432
|
+
let bountyWsolAmount = 0;
|
|
1433
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
1434
|
+
let boundBounty = parseInt(globalConfig.bountyBondAmount.toString());
|
|
1435
|
+
let maxBountyPerTask = parseInt(globalConfig.bountyPerTask.maxBounty.toString());
|
|
1436
|
+
let bountyPerPriceUpdateTaskDivisor = parseInt(globalConfig.bountyPerPriceUpdateTaskDivisor.toString());
|
|
1437
|
+
if (maxBountyAmount > maxBountyPerTask)
|
|
1438
|
+
maxBountyPerTask = maxBountyAmount;
|
|
1439
|
+
bountyWsolAmount = computeRebalanceIntentBountyAmount(
|
|
1440
|
+
RebalanceType.Basket,
|
|
1441
|
+
basket.numTokens,
|
|
1442
|
+
boundBounty,
|
|
1443
|
+
maxBountyPerTask,
|
|
1444
|
+
Math.floor(maxBountyPerTask / bountyPerPriceUpdateTaskDivisor),
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
let wsolIxs = await wrapWsolIxs(
|
|
1449
|
+
this.sdkParams.connection,
|
|
1450
|
+
keeper,
|
|
1451
|
+
bountyWsolAmount,
|
|
1452
|
+
);
|
|
1453
|
+
|
|
1454
|
+
const rebalanceIntent = getRebalanceIntentPda(basket.ownAddress, basket.ownAddress);
|
|
1455
|
+
|
|
1456
|
+
let rentPayer = getRentPayerPda();
|
|
1457
|
+
|
|
1458
|
+
let rentPayerAi = await this.sdkParams.connection.getAccountInfo(rentPayer);
|
|
1459
|
+
|
|
1460
|
+
let rebalanceIntentRentPayer = keeper;
|
|
1461
|
+
// TODO: check actual lamports required for rent
|
|
1462
|
+
if (rentPayerAi?.lamports ?? 0 > 500_000_000)
|
|
1463
|
+
rebalanceIntentRentPayer = rentPayer;
|
|
1464
|
+
let bountyMint = basket.settings.bountyMint;
|
|
1465
|
+
|
|
1466
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
1467
|
+
payer: keeper,
|
|
1468
|
+
instructions: [
|
|
1469
|
+
...wsolIxs,
|
|
1470
|
+
createRebalanceIntentIx({
|
|
1471
|
+
signer: keeper,
|
|
1472
|
+
owner: basket.ownAddress,
|
|
1473
|
+
basket: basket.ownAddress,
|
|
1474
|
+
}),
|
|
1475
|
+
resizeRebalanceIntentIx(rebalanceIntent),
|
|
1476
|
+
initRebalanceIntentIx({
|
|
1477
|
+
signer: keeper,
|
|
1478
|
+
owner: basket.ownAddress,
|
|
1479
|
+
basket: basket.ownAddress,
|
|
1480
|
+
basketTokenMint: basket.mint,
|
|
1481
|
+
rebalanceIntentRentPayer: rebalanceIntentRentPayer,
|
|
1482
|
+
bountyMint: bountyMint,
|
|
1483
|
+
rebalanceType: RebalanceType.Basket,
|
|
1484
|
+
basketRebalanceIntent: undefined,
|
|
1485
|
+
rebalanceSlippageBps: rebalanceSlippageBps,
|
|
1486
|
+
perTradeRebalanceSlippageBps: perTradeRebalanceSlippageBps,
|
|
1487
|
+
executionStartTime: executionStartTime,
|
|
1488
|
+
minBountyAmount: minBountyAmount,
|
|
1489
|
+
maxBountyAmount: maxBountyAmount,
|
|
1490
|
+
}),
|
|
1491
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1492
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1493
|
+
],
|
|
1494
|
+
lookupTables: [],
|
|
1495
|
+
}]]};
|
|
1496
|
+
|
|
1497
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1498
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1499
|
+
return txPayloadBatchSequence;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
/**
|
|
1503
|
+
* Build update token prices transactions.
|
|
1504
|
+
*
|
|
1505
|
+
* For pyth oracle accounts, fetches feed IDs, builds vaa [create init encode], [write verify],
|
|
1506
|
+
*
|
|
1507
|
+
* [update feed], [close vaa] instructions. For all tokens, builds [update token prices] instructions.
|
|
1508
|
+
*
|
|
1509
|
+
* returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
1510
|
+
*
|
|
1511
|
+
* Batch layout:
|
|
1512
|
+
*
|
|
1513
|
+
* batch 0:
|
|
1514
|
+
* * tx0: [vaa0CreateIx, vaa0InitIx, vaa0WriteIx],
|
|
1515
|
+
* * tx1: [vaa1CreateIx, vaa1InitIx, vaa1WriteIx],
|
|
1516
|
+
* * ...
|
|
1517
|
+
*
|
|
1518
|
+
* batch 1:
|
|
1519
|
+
* * tx0: [vaa0WriteVerifyIx, vaa0VerifyIx],
|
|
1520
|
+
* * tx1: [vaa1WriteVerifyIx, vaa1VerifyIx],
|
|
1521
|
+
* * ...
|
|
1522
|
+
*
|
|
1523
|
+
* batch 2:
|
|
1524
|
+
* * tx0: [updateFeed0Ix],
|
|
1525
|
+
* * tx1: [updateFeed1Ix],
|
|
1526
|
+
* * ...
|
|
1527
|
+
*
|
|
1528
|
+
* batch 3:
|
|
1529
|
+
* * tx0: [updateTokenPricesIx(max 10 tokens)],
|
|
1530
|
+
* * tx1: [updateTokenPricesIx(max 10 tokens)],
|
|
1531
|
+
* * ...
|
|
1532
|
+
* * txLast: [closeVaa0Ix, closeVaa1Ix, ...]
|
|
1533
|
+
*
|
|
1534
|
+
* @param {Object} params - The parameters for the update token prices.
|
|
1535
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1536
|
+
* @param {string} params.basket - The public key of the basket.
|
|
1537
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent.
|
|
1538
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1539
|
+
*/
|
|
1540
|
+
async updateTokenPricesTx(params: {
|
|
1541
|
+
keeper: string,
|
|
1542
|
+
basket: string,
|
|
1543
|
+
rebalance_intent: string,
|
|
1544
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1545
|
+
let keeper = new PublicKey(params.keeper);
|
|
1546
|
+
let rebalanceIntent = new PublicKey(params.rebalance_intent);
|
|
1547
|
+
let basketRebalanceIntent = getRebalanceIntentPda(new PublicKey(params.basket), new PublicKey(params.basket));
|
|
1548
|
+
let basket = await this.fetchBasket(params.basket);
|
|
1549
|
+
let pythPriceFeeds: PublicKey[] = [];
|
|
1550
|
+
let updateTokenPricesIxs: TransactionInstruction[] = [];
|
|
1551
|
+
for (let startIndex = 0; startIndex < basket.numTokens; startIndex++) {
|
|
1552
|
+
let allKeys: PublicKey[] = [];
|
|
1553
|
+
let tokenIndices: number[] = [];
|
|
1554
|
+
for (let endIndex = startIndex; endIndex < basket.numTokens; endIndex++) {
|
|
1555
|
+
let numAccounts = basket.composition[endIndex].oracleAggregator.oracles
|
|
1556
|
+
.slice(0, basket.composition[endIndex].oracleAggregator.numOracles)
|
|
1557
|
+
.reduce((acc, oracle) => acc + oracle.oracleSettings.numRequiredAccounts, 0);
|
|
1558
|
+
if (allKeys.length + numAccounts > UPDATE_TOKEN_PRICES_MAX_ACCOUNTS) break;
|
|
1559
|
+
if (tokenIndices.length == 20) break;
|
|
1560
|
+
startIndex = endIndex;
|
|
1561
|
+
tokenIndices.push(endIndex);
|
|
1562
|
+
let agg = basket.composition[endIndex].oracleAggregator;
|
|
1563
|
+
for (let i = 0; i < agg.numOracles; i++) {
|
|
1564
|
+
const oracleData = agg.oracles[i];
|
|
1565
|
+
for (let j = 0; j < oracleData.oracleSettings.numRequiredAccounts; j++) {
|
|
1566
|
+
const lutId = oracleData.accountsToLoadLutIds[j];
|
|
1567
|
+
const lutIdx = oracleData.accountsToLoadLutIndices[j];
|
|
1568
|
+
allKeys.push(basket.lutPubkeys![lutId].state.addresses[lutIdx]);
|
|
1569
|
+
if (oracleData.oracleSettings.oracleType === OracleType.Pyth)
|
|
1570
|
+
pythPriceFeeds.push(basket.lutPubkeys![lutId].state.addresses[lutIdx]);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
while (tokenIndices.length < 20) tokenIndices.push(0);
|
|
1575
|
+
// TODO: in last instruction we should include performance fee accounts
|
|
1576
|
+
let performanceFees = basket.settings.fees.hostPerformanceFeeBps + basket.settings.fees.creatorPerformanceFeeBps + basket.settings.fees.managersPerformanceFeeBps;
|
|
1577
|
+
updateTokenPricesIxs.push(
|
|
1578
|
+
updateTokenPricesIx({
|
|
1579
|
+
keeper: keeper,
|
|
1580
|
+
basket: basket.ownAddress,
|
|
1581
|
+
rebalanceIntent: rebalanceIntent,
|
|
1582
|
+
lookupTable0: basket.lookupTables.active[0],
|
|
1583
|
+
lookupTable1: basket.lookupTables.active[1],
|
|
1584
|
+
tokenIndices: tokenIndices,
|
|
1585
|
+
additionalOracleAccounts: allKeys,
|
|
1586
|
+
basketRebalanceIntent: basket.settings.activeRebalance.eq(new BN(0)) ? undefined : basketRebalanceIntent,
|
|
1587
|
+
basketMint: performanceFees > 0 ? basket.mint : undefined,
|
|
1588
|
+
}),
|
|
1589
|
+
);
|
|
1590
|
+
}
|
|
1591
|
+
const {feedIds }= await fetchFeedIdsFromAccounts(this.sdkParams.connection, pythPriceFeeds);
|
|
1592
|
+
const {
|
|
1593
|
+
vaaCreateInitEncodeIxs,
|
|
1594
|
+
vaaWriteVerifyIxs,
|
|
1595
|
+
updateFeedIxs,
|
|
1596
|
+
closeVaaIxs,
|
|
1597
|
+
} = await buildPythPriceFeedUpdateIxs(
|
|
1598
|
+
keeper,
|
|
1599
|
+
feedIds,
|
|
1600
|
+
);
|
|
1601
|
+
let txBatchData: TxBatchData = {batches: [
|
|
1602
|
+
[
|
|
1603
|
+
...vaaCreateInitEncodeIxs.map(ixs => ({
|
|
1604
|
+
payer: keeper,
|
|
1605
|
+
instructions: [
|
|
1606
|
+
...ixs.ixs,
|
|
1607
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1608
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1609
|
+
],
|
|
1610
|
+
lookupTables: [],
|
|
1611
|
+
})),
|
|
1612
|
+
],
|
|
1613
|
+
[
|
|
1614
|
+
...vaaWriteVerifyIxs.map(ixs => ({
|
|
1615
|
+
payer: keeper,
|
|
1616
|
+
instructions: [
|
|
1617
|
+
...ixs,
|
|
1618
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1619
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1620
|
+
],
|
|
1621
|
+
lookupTables: [],
|
|
1622
|
+
})),
|
|
1623
|
+
],
|
|
1624
|
+
[
|
|
1625
|
+
...updateFeedIxs.map(ix => ({
|
|
1626
|
+
payer: keeper,
|
|
1627
|
+
instructions: [
|
|
1628
|
+
ix,
|
|
1629
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1630
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1631
|
+
],
|
|
1632
|
+
lookupTables: [],
|
|
1633
|
+
})),
|
|
1634
|
+
],
|
|
1635
|
+
[
|
|
1636
|
+
...updateTokenPricesIxs.map(ix => ({
|
|
1637
|
+
payer: keeper,
|
|
1638
|
+
instructions: [
|
|
1639
|
+
ix,
|
|
1640
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1641
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1642
|
+
],
|
|
1643
|
+
lookupTables: [basket.lookupTables.active[0], basket.lookupTables.active[1]],
|
|
1644
|
+
})),
|
|
1645
|
+
{
|
|
1646
|
+
payer: keeper,
|
|
1647
|
+
instructions: [
|
|
1648
|
+
...closeVaaIxs,
|
|
1649
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1650
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1651
|
+
],
|
|
1652
|
+
lookupTables: [],
|
|
1653
|
+
}
|
|
1654
|
+
],
|
|
1655
|
+
]};
|
|
1656
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1657
|
+
for (let [txId, vaaCreateInitEncodeIx] of vaaCreateInitEncodeIxs.entries()) {
|
|
1658
|
+
versionedTxs.batches[0][txId].sign([vaaCreateInitEncodeIx.signer]);
|
|
1659
|
+
}
|
|
1660
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1661
|
+
return txPayloadBatchSequence;
|
|
1662
|
+
}
|
|
1663
|
+
|
|
1664
|
+
/**
|
|
1665
|
+
* Build Pyth price feed update transactions from on-chain price account pubkeys.
|
|
1666
|
+
* Fetches feed IDs from accounts, builds update instructions, and returns
|
|
1667
|
+
* TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
1668
|
+
*
|
|
1669
|
+
* Batch layout:
|
|
1670
|
+
*
|
|
1671
|
+
* batch 0:
|
|
1672
|
+
* * tx0: [vaa0CreateIx, vaa0InitIx, vaa0WriteIx],
|
|
1673
|
+
* * tx1: [vaa1CreateIx, vaa1InitIx, vaa1WriteIx],
|
|
1674
|
+
* * ...
|
|
1675
|
+
*
|
|
1676
|
+
* batch 1:
|
|
1677
|
+
* * tx0: [vaa0WriteVerifyIx, vaa0VerifyIx],
|
|
1678
|
+
* * tx1: [vaa1WriteVerifyIx, vaa1VerifyIx],
|
|
1679
|
+
* * ...
|
|
1680
|
+
*
|
|
1681
|
+
* batch 2:
|
|
1682
|
+
* * tx0: [updateFeed0Ix],
|
|
1683
|
+
* * tx1: [updateFeed1Ix],
|
|
1684
|
+
* * ...
|
|
1685
|
+
*
|
|
1686
|
+
* batch 3:
|
|
1687
|
+
* * txLast: [closeVaa0Ix, closeVaa1Ix, ...]
|
|
1688
|
+
*
|
|
1689
|
+
*/
|
|
1690
|
+
async updatePythPriceFeedsTx(params: {
|
|
1691
|
+
keeper: string,
|
|
1692
|
+
accounts: string[],
|
|
1693
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1694
|
+
const payer = new PublicKey(params.keeper);
|
|
1695
|
+
const priceAccounts = params.accounts.map(a => new PublicKey(a));
|
|
1696
|
+
const { feedIds } = await fetchFeedIdsFromAccounts(this.sdkParams.connection, priceAccounts);
|
|
1697
|
+
const {
|
|
1698
|
+
vaaCreateInitEncodeIxs,
|
|
1699
|
+
vaaWriteVerifyIxs,
|
|
1700
|
+
updateFeedIxs,
|
|
1701
|
+
closeVaaIxs,
|
|
1702
|
+
} = await buildPythPriceFeedUpdateIxs(
|
|
1703
|
+
payer,
|
|
1704
|
+
feedIds,
|
|
1705
|
+
);
|
|
1706
|
+
let txBatchData: TxBatchData = {batches: [
|
|
1707
|
+
[
|
|
1708
|
+
...vaaCreateInitEncodeIxs.map(ixs => ({
|
|
1709
|
+
payer: payer,
|
|
1710
|
+
instructions: [
|
|
1711
|
+
...ixs.ixs,
|
|
1712
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1713
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1714
|
+
],
|
|
1715
|
+
lookupTables: [],
|
|
1716
|
+
})),
|
|
1717
|
+
],
|
|
1718
|
+
[
|
|
1719
|
+
...vaaWriteVerifyIxs.map(ixs => ({
|
|
1720
|
+
payer: payer,
|
|
1721
|
+
instructions: [
|
|
1722
|
+
...ixs,
|
|
1723
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1724
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1725
|
+
],
|
|
1726
|
+
lookupTables: [],
|
|
1727
|
+
})),
|
|
1728
|
+
],
|
|
1729
|
+
[
|
|
1730
|
+
...updateFeedIxs.map(ix => ({
|
|
1731
|
+
payer: payer,
|
|
1732
|
+
instructions: [
|
|
1733
|
+
ix,
|
|
1734
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1735
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1736
|
+
],
|
|
1737
|
+
lookupTables: [],
|
|
1738
|
+
})),
|
|
1739
|
+
],
|
|
1740
|
+
[
|
|
1741
|
+
{
|
|
1742
|
+
payer: payer,
|
|
1743
|
+
instructions: [
|
|
1744
|
+
...closeVaaIxs,
|
|
1745
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1746
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1747
|
+
],
|
|
1748
|
+
lookupTables: [],
|
|
1749
|
+
}
|
|
1750
|
+
],
|
|
1751
|
+
]};
|
|
1752
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1753
|
+
for (let [txId, vaaCreateInitEncodeIx] of vaaCreateInitEncodeIxs.entries()) {
|
|
1754
|
+
versionedTxs.batches[0][txId].sign([vaaCreateInitEncodeIx.signer]);
|
|
1755
|
+
}
|
|
1756
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1757
|
+
return txPayloadBatchSequence;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
|
|
1761
|
+
/**
|
|
1762
|
+
* Build flash swap transactions (Rebalance or MakeDirectSwap).
|
|
1763
|
+
*
|
|
1764
|
+
* Withdraws amount_out tokens(mint_out) from basket, deposits amount_in tokens(mint_in) to basket.
|
|
1765
|
+
*
|
|
1766
|
+
* If jup_swap_ix is provided, uses it to swap amount_out tokens(mint_out) for amount_in tokens(mint_in).
|
|
1767
|
+
*
|
|
1768
|
+
* returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
1769
|
+
*
|
|
1770
|
+
* Batch layout: batch 0: tx: 0:
|
|
1771
|
+
* * create associated token account_mint_out
|
|
1772
|
+
* * create associated token account_mint_in
|
|
1773
|
+
* * jup_token_ledger_ix
|
|
1774
|
+
* * flash withdraw
|
|
1775
|
+
* * jup_swap_ix
|
|
1776
|
+
* * flash deposit
|
|
1777
|
+
* * set compute unit limit
|
|
1778
|
+
* * set compute unit price
|
|
1779
|
+
*
|
|
1780
|
+
* @param {Object} params - The parameters for the flash swap.
|
|
1781
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1782
|
+
* @param {string} params.basket - The public key of the basket.
|
|
1783
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent (for Rebalance).
|
|
1784
|
+
* @param {string} params.intent - The public key of the intent (for MakeDirectSwap).
|
|
1785
|
+
* @param {string} params.mint_in - The mint of the input token (deposited to basket).
|
|
1786
|
+
* @param {string} params.mint_out - The mint of the output token (withdrawn from basket).
|
|
1787
|
+
* @param {number} params.amount_in - The amount of the input token (deposited to basket).
|
|
1788
|
+
* @param {number} params.amount_out - The amount of the output token (withdrawn from basket).
|
|
1789
|
+
* @param {"exact_in" | "exact_out" | "ioc"} params.mode - The mode of the flash swap.
|
|
1790
|
+
* @param {TransactionInstruction} params.jup_token_ledger_ix - The jup token ledger ix.
|
|
1791
|
+
* @param {TransactionInstruction} params.jup_swap_ix - The jup swap ix.
|
|
1792
|
+
* @param {PublicKey[]} params.jup_address_lookup_table_addresses - The jup address lookup table addresses.
|
|
1793
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1794
|
+
*/
|
|
1795
|
+
async flashSwapTx(params: {
|
|
1796
|
+
keeper: string,
|
|
1797
|
+
basket: string,
|
|
1798
|
+
rebalance_intent?: string,
|
|
1799
|
+
intent?: string,
|
|
1800
|
+
mint_in: string,
|
|
1801
|
+
mint_out: string,
|
|
1802
|
+
amount_in: number,
|
|
1803
|
+
amount_out: number,
|
|
1804
|
+
mode?: number,
|
|
1805
|
+
jup_token_ledger_ix?: TransactionInstruction,
|
|
1806
|
+
jup_swap_ix?: TransactionInstruction,
|
|
1807
|
+
jup_address_lookup_table_addresses?: PublicKey[],
|
|
1808
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1809
|
+
let keeper = new PublicKey(params.keeper);
|
|
1810
|
+
let basket = new PublicKey(params.basket);
|
|
1811
|
+
let rebalanceIntent = params.rebalance_intent ? new PublicKey(params.rebalance_intent) : undefined;
|
|
1812
|
+
let intent = params.intent ? new PublicKey(params.intent) : undefined;
|
|
1813
|
+
let mintIn = new PublicKey(params.mint_in);
|
|
1814
|
+
let mintOut = new PublicKey(params.mint_out);
|
|
1815
|
+
let amountIn = new BN(params.amount_in);
|
|
1816
|
+
let amountOut = new BN(params.amount_out);
|
|
1817
|
+
let mode = params.mode ? params.mode : 0;
|
|
1818
|
+
let flashParams = {
|
|
1819
|
+
keeper: keeper,
|
|
1820
|
+
basket: basket,
|
|
1821
|
+
rebalanceIntent: rebalanceIntent,
|
|
1822
|
+
intent: intent,
|
|
1823
|
+
mintIn: mintIn,
|
|
1824
|
+
mintOut: mintOut,
|
|
1825
|
+
amountIn: amountIn,
|
|
1826
|
+
amountOut: amountOut,
|
|
1827
|
+
mode: mode,
|
|
1828
|
+
};
|
|
1829
|
+
let ixWithdraw = flashWithdrawIx(flashParams);
|
|
1830
|
+
let ixDeposit = flashDepositIx(flashParams);
|
|
1831
|
+
let ixs: TransactionInstruction[] = [];
|
|
1832
|
+
ixs = ixs.concat([
|
|
1833
|
+
createAssociatedTokenAccountIdempotentInstruction(keeper, getAta(keeper, mintIn), keeper, mintIn),
|
|
1834
|
+
createAssociatedTokenAccountIdempotentInstruction(keeper, getAta(keeper, mintOut), keeper, mintOut),
|
|
1835
|
+
]);
|
|
1836
|
+
if (params.jup_token_ledger_ix) {
|
|
1837
|
+
ixs.push(params.jup_token_ledger_ix);
|
|
1838
|
+
}
|
|
1839
|
+
ixs.push(ixWithdraw);
|
|
1840
|
+
if (params.jup_swap_ix) {
|
|
1841
|
+
ixs.push(params.jup_swap_ix);
|
|
1842
|
+
}
|
|
1843
|
+
ixs.push(ixDeposit);
|
|
1844
|
+
ixs = ixs.concat([
|
|
1845
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1846
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1847
|
+
]);
|
|
1848
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
1849
|
+
payer: keeper,
|
|
1850
|
+
instructions: ixs,
|
|
1851
|
+
lookupTables: params.jup_address_lookup_table_addresses ?? [],
|
|
1852
|
+
}]]};
|
|
1853
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1854
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1855
|
+
return txPayloadBatchSequence;
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
/**
|
|
1859
|
+
* Mints basket tokens for a user. Executable when the rebalance intent has finished its auctions.
|
|
1860
|
+
*
|
|
1861
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
1862
|
+
*
|
|
1863
|
+
* @param {Object} params - The parameters for the mint.
|
|
1864
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1865
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent.
|
|
1866
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1867
|
+
*/
|
|
1868
|
+
async mintTx(params: {
|
|
1869
|
+
keeper: string,
|
|
1870
|
+
rebalance_intent: string,
|
|
1871
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1872
|
+
let keeper = new PublicKey(params.keeper);
|
|
1873
|
+
let rebalanceIntent: RebalanceIntent = (await this.fetchRebalanceIntent(params.rebalance_intent)).chain_data;
|
|
1874
|
+
let basket = await this.fetchBasket(rebalanceIntent.basket.toBase58());
|
|
1875
|
+
let basketRebalanceIntent = getRebalanceIntentPda(basket.ownAddress, rebalanceIntent.owner);
|
|
1876
|
+
let ix = mintBasketIx({
|
|
1877
|
+
keeper: keeper,
|
|
1878
|
+
basket: basket.ownAddress,
|
|
1879
|
+
basketTokenMint: basket.mint,
|
|
1880
|
+
buyer: rebalanceIntent.owner,
|
|
1881
|
+
basketRebalanceIntent: basket.settings.activeRebalance.eq(new BN(0)) ? undefined : basketRebalanceIntent,
|
|
1882
|
+
});
|
|
1883
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
1884
|
+
payer: keeper,
|
|
1885
|
+
instructions: [
|
|
1886
|
+
ix,
|
|
1887
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1888
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1889
|
+
],
|
|
1890
|
+
lookupTables: [],
|
|
1891
|
+
}]]};
|
|
1892
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1893
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1894
|
+
return txPayloadBatchSequence;
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
/**
|
|
1898
|
+
* Redeems tokens from the rebalance intent with Withdraw status.
|
|
1899
|
+
*
|
|
1900
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
1901
|
+
*
|
|
1902
|
+
* @param {Object} params - The parameters for the redeem tokens.
|
|
1903
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1904
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent.
|
|
1905
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1906
|
+
*/
|
|
1907
|
+
async redeemTokensTx(params: {
|
|
1908
|
+
keeper: string,
|
|
1909
|
+
rebalance_intent: string,
|
|
1910
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1911
|
+
let keeper = new PublicKey(params.keeper);
|
|
1912
|
+
let rebalanceIntent: RebalanceIntent = (await this.fetchRebalanceIntent(params.rebalance_intent)).chain_data;
|
|
1913
|
+
let batchSize = 7;
|
|
1914
|
+
let ixs: TransactionInstruction[] = [];
|
|
1915
|
+
for (let batchStart = 0; batchStart < rebalanceIntent.tokens.length; batchStart += batchSize) {
|
|
1916
|
+
let tokenMints: PublicKey[] = [];
|
|
1917
|
+
for (let i = batchStart; i < rebalanceIntent.tokens.length && i < batchStart + batchSize; i++)
|
|
1918
|
+
if (rebalanceIntent.tokens[i].amount.gt(new BN(0)))
|
|
1919
|
+
tokenMints.push(rebalanceIntent.tokens[i].mint);
|
|
1920
|
+
if (tokenMints.length > 0) {
|
|
1921
|
+
ixs.push(
|
|
1922
|
+
redeemTokensIx({
|
|
1923
|
+
keeper: keeper,
|
|
1924
|
+
basket: rebalanceIntent.basket,
|
|
1925
|
+
owner: rebalanceIntent.owner,
|
|
1926
|
+
tokenMints: tokenMints,
|
|
1927
|
+
})
|
|
1928
|
+
);
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
let txBatchData: TxBatchData = {batches: [ixs.map(ix => ({
|
|
1932
|
+
payer: keeper,
|
|
1933
|
+
instructions: [
|
|
1934
|
+
ix,
|
|
1935
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
1936
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
1937
|
+
],
|
|
1938
|
+
lookupTables: [],
|
|
1939
|
+
}))]};
|
|
1940
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
1941
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
1942
|
+
return txPayloadBatchSequence;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
/**
|
|
1946
|
+
* Claims the bounty to keepers who have completed tasks for rebalance intent.
|
|
1947
|
+
*
|
|
1948
|
+
* Executebale when the rebalance intent has finished its auctions.
|
|
1949
|
+
*
|
|
1950
|
+
* For Withdraw status rebalance intent, tokens have to be redeemed first.
|
|
1951
|
+
*
|
|
1952
|
+
* Closes the rebalance intent if all keepers have claimed the bounty.
|
|
1953
|
+
*
|
|
1954
|
+
* @param {Object} params - The parameters for the claim bounty.
|
|
1955
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
1956
|
+
* @param {string} params.rebalance_intent - The public key of the rebalance intent.
|
|
1957
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
1958
|
+
*/
|
|
1959
|
+
async claimBountyTx(params: {
|
|
1960
|
+
keeper: string,
|
|
1961
|
+
rebalance_intent: string,
|
|
1962
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
1963
|
+
let keeper = new PublicKey(params.keeper);
|
|
1964
|
+
let rebalanceIntent: RebalanceIntent = (await this.fetchRebalanceIntent(params.rebalance_intent)).chain_data;
|
|
1965
|
+
let keepersArray: string[] = [];
|
|
1966
|
+
if (!rebalanceIntent.auctionCreationTask .completedBy.equals(PublicKey.default))
|
|
1967
|
+
keepersArray.push(rebalanceIntent.auctionCreationTask.completedBy.toBase58());
|
|
1968
|
+
if (!rebalanceIntent.cancelRebalanceTask.completedBy.equals(PublicKey.default))
|
|
1969
|
+
keepersArray.push(rebalanceIntent.cancelRebalanceTask.completedBy.toBase58());
|
|
1970
|
+
if (!rebalanceIntent.placeholderTask.completedBy.equals(PublicKey.default))
|
|
1971
|
+
keepersArray.push(rebalanceIntent.placeholderTask.completedBy.toBase58());
|
|
1972
|
+
if (!rebalanceIntent.finishPriceUpdateTask.completedBy.equals(PublicKey.default))
|
|
1973
|
+
keepersArray.push(rebalanceIntent.finishPriceUpdateTask.completedBy.toBase58());
|
|
1974
|
+
if (!rebalanceIntent.mintBasketTask.completedBy.equals(PublicKey.default))
|
|
1975
|
+
keepersArray.push(rebalanceIntent.mintBasketTask.completedBy.toBase58());
|
|
1976
|
+
for (let i = 0; i < rebalanceIntent.priceUpdateTasks.length; i++)
|
|
1977
|
+
if (!rebalanceIntent.priceUpdateTasks[i].completedBy.equals(PublicKey.default))
|
|
1978
|
+
keepersArray.push(rebalanceIntent.priceUpdateTasks[i].completedBy.toBase58());
|
|
1979
|
+
for (let i = 0; i < rebalanceIntent.tokenSettlementTasks.length; i++)
|
|
1980
|
+
if (!rebalanceIntent.tokenSettlementTasks[i].completedBy.equals(PublicKey.default))
|
|
1981
|
+
keepersArray.push(rebalanceIntent.tokenSettlementTasks[i].completedBy.toBase58());
|
|
1982
|
+
keepersArray = Array.from(new Set(keepersArray));
|
|
1983
|
+
let batchSize = 10;
|
|
1984
|
+
let ixs: TransactionInstruction[] = [];
|
|
1985
|
+
let wsolIxs = await wrapWsolIxs(this.sdkParams.connection, keeper, 0);
|
|
1986
|
+
ixs.push(...wsolIxs);
|
|
1987
|
+
for (let batchStart = 0; batchStart < keepersArray.length; batchStart += batchSize)
|
|
1988
|
+
ixs.push(claimBountyIx({
|
|
1989
|
+
keeper: keeper,
|
|
1990
|
+
basket: rebalanceIntent.basket, //@ts-ignore
|
|
1991
|
+
intent: rebalanceIntent.ownAddress,
|
|
1992
|
+
bountyMint: rebalanceIntent.bounty.bountyMint,
|
|
1993
|
+
bountyDepositor: rebalanceIntent.bounty.bountyDepositor,
|
|
1994
|
+
rentPayer: rebalanceIntent.rentPayer,
|
|
1995
|
+
keepers: keepersArray.map(keeper => new PublicKey(keeper)).slice(batchStart, batchStart + batchSize),
|
|
1996
|
+
})
|
|
1997
|
+
);
|
|
1998
|
+
let txBatchData: TxBatchData = {batches: [ixs.map(ix => ({
|
|
1999
|
+
payer: keeper,
|
|
2000
|
+
instructions: [
|
|
2001
|
+
ix,
|
|
2002
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
2003
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
2004
|
+
],
|
|
2005
|
+
lookupTables: [],
|
|
2006
|
+
}))]};
|
|
2007
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
2008
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
2009
|
+
return txPayloadBatchSequence;
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
/**
|
|
2013
|
+
* Adds bounty for automation to basket.
|
|
2014
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
2015
|
+
*
|
|
2016
|
+
* @param {Object} params - The parameters for the add bounty.
|
|
2017
|
+
* @param {string} params.keeper - The public key of the keeper.
|
|
2018
|
+
* @param {string} params.basket - The public key of the basket.
|
|
2019
|
+
* @param {number} params.amount - The amount of the bounty.
|
|
2020
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
2021
|
+
*/
|
|
2022
|
+
async addBountyTx(params: {
|
|
2023
|
+
keeper: string,
|
|
2024
|
+
basket: string,
|
|
2025
|
+
amount: number,
|
|
2026
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
2027
|
+
let keeper = new PublicKey(params.keeper);
|
|
2028
|
+
let basket = await this.fetchBasket(params.basket);
|
|
2029
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
2030
|
+
let bountyWsolAmount = 0;
|
|
2031
|
+
if (globalConfig.bountyMint.equals(MINTS["mainnet"].WSOL)) {
|
|
2032
|
+
bountyWsolAmount = params.amount;
|
|
2033
|
+
}
|
|
2034
|
+
let wsolIxs = await wrapWsolIxs(
|
|
2035
|
+
this.sdkParams.connection,
|
|
2036
|
+
keeper,
|
|
2037
|
+
bountyWsolAmount,
|
|
2038
|
+
);
|
|
2039
|
+
let ix = addBountyIx({
|
|
2040
|
+
keeper: keeper,
|
|
2041
|
+
basket: basket.ownAddress,
|
|
2042
|
+
bountyMint: basket.settings.bountyMint,
|
|
2043
|
+
amount: params.amount,
|
|
2044
|
+
});
|
|
2045
|
+
let txBatchData: TxBatchData = {batches: [[{
|
|
2046
|
+
payer: keeper,
|
|
2047
|
+
instructions: [
|
|
2048
|
+
...wsolIxs,
|
|
2049
|
+
ix,
|
|
2050
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
2051
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
2052
|
+
],
|
|
2053
|
+
lookupTables: [],
|
|
2054
|
+
}]]};
|
|
2055
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
2056
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
2057
|
+
return txPayloadBatchSequence;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
/**
|
|
2061
|
+
* Withdraws fees from the basket, initializes WithdrawBasketFees accounts, and claims the fee tokens
|
|
2062
|
+
* for all fee types that the claimer is authorized to claim (symmetry, creator, host, or manager fees).
|
|
2063
|
+
*
|
|
2064
|
+
* This function automatically determines which fee types the claimer can claim based on:
|
|
2065
|
+
* - Symmetry fees: if claimer equals globalConfig.symmetryFeeCollector
|
|
2066
|
+
* - Creator fees: if claimer equals basket.settings.creator
|
|
2067
|
+
* - Host fees: if claimer equals basket.settings.host
|
|
2068
|
+
* - Manager fees: if claimer is in basket.settings.managers.managers
|
|
2069
|
+
*
|
|
2070
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
2071
|
+
*
|
|
2072
|
+
* Batch layout:
|
|
2073
|
+
* - batch 0: One transaction per claimable fee type, each containing a withdrawFeesIx instruction.
|
|
2074
|
+
* All transactions in this batch can run in parallel. These create and populate the WithdrawBasketFees accounts.
|
|
2075
|
+
* - batch 1: Multiple transactions for claiming fee tokens, automatically batched based on account limits.
|
|
2076
|
+
* All transactions in this batch can run in parallel. These transfer tokens from WithdrawBasketFees accounts to claimers.
|
|
2077
|
+
*
|
|
2078
|
+
* Note: If not all tokens are claimed in batch 1 (e.g., due to account limits or errors),
|
|
2079
|
+
* use claimTokenFeesFromBasketTx to claim the remaining tokens.
|
|
2080
|
+
*
|
|
2081
|
+
* @param {Object} params - The parameters for withdrawing and claiming basket fees.
|
|
2082
|
+
* @param {string} params.claimer - The public key of the claimer.
|
|
2083
|
+
* @param {string} params.basket - The public key of the basket.
|
|
2084
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
2085
|
+
*/
|
|
2086
|
+
async withdrawBasketFeesTx(params: {
|
|
2087
|
+
claimer: string,
|
|
2088
|
+
basket: string,
|
|
2089
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
2090
|
+
let claimer = new PublicKey(params.claimer);
|
|
2091
|
+
let basket = await this.fetchBasket(params.basket);
|
|
2092
|
+
let globalConfig = await this.fetchGlobalConfig();
|
|
2093
|
+
|
|
2094
|
+
let claimableFeeTypes: number[] = [];
|
|
2095
|
+
let owners: PublicKey[][] = [];
|
|
2096
|
+
if (claimer.equals(globalConfig.symmetryFeeCollector))
|
|
2097
|
+
{ claimableFeeTypes.push(0); owners.push([globalConfig.symmetryFeeCollector]); }
|
|
2098
|
+
if (claimer.equals(basket.settings.creator))
|
|
2099
|
+
{ claimableFeeTypes.push(1); owners.push([basket.settings.creator]); }
|
|
2100
|
+
if (claimer.equals(basket.settings.host))
|
|
2101
|
+
{ claimableFeeTypes.push(2); owners.push([basket.settings.host]); }
|
|
2102
|
+
if (basket.settings.managers.managers.find(manager => manager.equals(claimer)))
|
|
2103
|
+
{ claimableFeeTypes.push(3); owners.push(basket.settings.managers.managers); }
|
|
2104
|
+
|
|
2105
|
+
let withdrawFeesIxs: TransactionInstruction[] = [];
|
|
2106
|
+
let claimTokenFeesFromBasketIxs: TransactionInstruction[] = [];
|
|
2107
|
+
claimableFeeTypes.forEach((feeType, index) => {
|
|
2108
|
+
withdrawFeesIxs.push(
|
|
2109
|
+
withdrawFeesIx({
|
|
2110
|
+
claimer,
|
|
2111
|
+
basketTokenMint: basket.mint,
|
|
2112
|
+
feeType: feeType,
|
|
2113
|
+
})
|
|
2114
|
+
);
|
|
2115
|
+
claimTokenFeesFromBasketIxs.push(
|
|
2116
|
+
...claimFeeTokensFromBasketIxs(
|
|
2117
|
+
claimer,
|
|
2118
|
+
basket.ownAddress,
|
|
2119
|
+
getWithdrawBasketFeesPda(basket.ownAddress, feeType),
|
|
2120
|
+
claimer,
|
|
2121
|
+
owners[index],
|
|
2122
|
+
basket.composition.map(composition => composition.mint),
|
|
2123
|
+
)
|
|
2124
|
+
);
|
|
2125
|
+
});
|
|
2126
|
+
|
|
2127
|
+
let txBatchData: TxBatchData = {batches: [
|
|
2128
|
+
withdrawFeesIxs.map(ix => ({
|
|
2129
|
+
payer: claimer,
|
|
2130
|
+
instructions: [
|
|
2131
|
+
ix,
|
|
2132
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
2133
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
2134
|
+
],
|
|
2135
|
+
lookupTables: [],
|
|
2136
|
+
})),
|
|
2137
|
+
claimTokenFeesFromBasketIxs.map(ix => ({
|
|
2138
|
+
payer: claimer,
|
|
2139
|
+
instructions: [
|
|
2140
|
+
ix,
|
|
2141
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
2142
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
2143
|
+
],
|
|
2144
|
+
lookupTables: [],
|
|
2145
|
+
})),
|
|
2146
|
+
]};
|
|
2147
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
2148
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
2149
|
+
return txPayloadBatchSequence;
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
/**
|
|
2153
|
+
* Claims fee tokens from a WithdrawBasketFees account.
|
|
2154
|
+
*
|
|
2155
|
+
* This function transfers accumulated fee tokens from the WithdrawBasketFees account
|
|
2156
|
+
* to the claimers (owners) based on their weights. The tokens are automatically batched
|
|
2157
|
+
* into multiple transactions if needed to respect Solana's account limit (30 accounts per transaction).
|
|
2158
|
+
*
|
|
2159
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
2160
|
+
*
|
|
2161
|
+
* @param {Object} params - The parameters for claiming basket fees.
|
|
2162
|
+
* @param {string} params.claimer - The public key of the claimer.
|
|
2163
|
+
* @param {string} params.basket - The public key of the basket.
|
|
2164
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
2165
|
+
*/
|
|
2166
|
+
async claimTokenFeesFromBasketTx(params: {
|
|
2167
|
+
claimer: string,
|
|
2168
|
+
withdrawBasketFees: string,
|
|
2169
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
2170
|
+
let claimer = new PublicKey(params.claimer);
|
|
2171
|
+
let withdrawBasketFees = await this.fetchWithdrawBasketFees(params.withdrawBasketFees);
|
|
2172
|
+
|
|
2173
|
+
let claimTokenFeesFromBasketIxs = claimFeeTokensFromBasketIxs(
|
|
2174
|
+
claimer,
|
|
2175
|
+
withdrawBasketFees.basket,
|
|
2176
|
+
withdrawBasketFees.ownAddress!,
|
|
2177
|
+
withdrawBasketFees.rentPayer,
|
|
2178
|
+
withdrawBasketFees.owners,
|
|
2179
|
+
withdrawBasketFees.accumulatedTokens,
|
|
2180
|
+
);
|
|
2181
|
+
|
|
2182
|
+
// Create batches: all claim instructions can run in parallel
|
|
2183
|
+
let txBatchData: TxBatchData = {batches: [
|
|
2184
|
+
claimTokenFeesFromBasketIxs.map(ix => ({
|
|
2185
|
+
payer: claimer,
|
|
2186
|
+
instructions: [
|
|
2187
|
+
ix,
|
|
2188
|
+
ComputeBudgetProgram.setComputeUnitLimit({units: COMPUTE_UNITS}),
|
|
2189
|
+
ComputeBudgetProgram.setComputeUnitPrice({microLamports: this.sdkParams.priorityFee}),
|
|
2190
|
+
],
|
|
2191
|
+
lookupTables: [],
|
|
2192
|
+
}))
|
|
2193
|
+
]};
|
|
2194
|
+
let versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
2195
|
+
let txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
2196
|
+
return txPayloadBatchSequence;
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
/**
|
|
2200
|
+
* Rewrites lookup tables for the basket. (Used when the basket lookup tables are full)
|
|
2201
|
+
*
|
|
2202
|
+
* Returns TxPayloadBatchSequence ready for signAndSendTxPayloadBatchSequence.
|
|
2203
|
+
*
|
|
2204
|
+
* Batch layout:
|
|
2205
|
+
*
|
|
2206
|
+
* batch 0:
|
|
2207
|
+
* * tx0: [create temp lookup tables],
|
|
2208
|
+
*
|
|
2209
|
+
* batch 1:
|
|
2210
|
+
* * tx0: [extend temp lookup tables (with oracles of active tokens)],
|
|
2211
|
+
* * tx1: [extend temp lookup tables (with oracles of active tokens)],
|
|
2212
|
+
* * ...
|
|
2213
|
+
*
|
|
2214
|
+
* batch 2:
|
|
2215
|
+
* * tx0: [overwrite lookup tables with temp, deactivate old lookup tables],
|
|
2216
|
+
*
|
|
2217
|
+
* @param {Object} params - The parameters for the rewrite lookup tables.
|
|
2218
|
+
* @param {string} params.signer - The public key of the signer.
|
|
2219
|
+
* @param {string} params.basket_mint - The mint of the basket.
|
|
2220
|
+
* @param {string[]} params.additional_accounts - The additional accounts.
|
|
2221
|
+
* @returns {Promise<TxPayloadBatchSequence>} The transaction payload batch sequence.
|
|
2222
|
+
*/
|
|
2223
|
+
async rewriteLookupTablesTx(params: {
|
|
2224
|
+
signer: string,
|
|
2225
|
+
basket_mint: string,
|
|
2226
|
+
additional_accounts: string[],
|
|
2227
|
+
}): Promise<TxPayloadBatchSequence> {
|
|
2228
|
+
const signer = new PublicKey(params.signer);
|
|
2229
|
+
const basketMint = new PublicKey(params.basket_mint);
|
|
2230
|
+
const additionalAccounts = params.additional_accounts.map(a => new PublicKey(a));
|
|
2231
|
+
|
|
2232
|
+
const basketAddress = getBasketState(basketMint);
|
|
2233
|
+
const basket = await this.fetchBasket(basketAddress.toBase58());
|
|
2234
|
+
|
|
2235
|
+
const activeLut0 = basket.lookupTables.active[0];
|
|
2236
|
+
const activeLut1 = basket.lookupTables.active[1];
|
|
2237
|
+
let oldTempLut0 = basket.lookupTables.temp[0];
|
|
2238
|
+
let oldTempLut1 = basket.lookupTables.temp[1];
|
|
2239
|
+
|
|
2240
|
+
const slot = await this.sdkParams.connection.getSlot("finalized");
|
|
2241
|
+
const newLut0 = getLookupTableAccount(basketAddress, slot);
|
|
2242
|
+
const newLut1 = getLookupTableAccount(basketAddress, slot - 1);
|
|
2243
|
+
|
|
2244
|
+
// Step 1: create lookup tables
|
|
2245
|
+
const createIx = createBasketLookupTablesInstruction({
|
|
2246
|
+
signer,
|
|
2247
|
+
basket: basketAddress,
|
|
2248
|
+
oldTempLookupTable0: oldTempLut0,
|
|
2249
|
+
oldTempLookupTable1: oldTempLut1,
|
|
2250
|
+
newTempLookupTable0: newLut0,
|
|
2251
|
+
newTempLookupTable1: newLut1,
|
|
2252
|
+
slot,
|
|
2253
|
+
});
|
|
2254
|
+
|
|
2255
|
+
const txBatchData: TxBatchData = { batches: [
|
|
2256
|
+
[{
|
|
2257
|
+
payer: signer,
|
|
2258
|
+
instructions: [
|
|
2259
|
+
createIx,
|
|
2260
|
+
ComputeBudgetProgram.setComputeUnitLimit({ units: COMPUTE_UNITS }),
|
|
2261
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: this.sdkParams.priorityFee }),
|
|
2262
|
+
],
|
|
2263
|
+
lookupTables: [],
|
|
2264
|
+
}],
|
|
2265
|
+
]};
|
|
2266
|
+
|
|
2267
|
+
// Step 2: extend LUTs in batches of ~20
|
|
2268
|
+
const BATCH_SIZE = LUT_EXTEND_BATCH_SIZE;
|
|
2269
|
+
for (let i = 0; i < additionalAccounts.length; i += BATCH_SIZE) {
|
|
2270
|
+
const batch = additionalAccounts.slice(i, i + BATCH_SIZE);
|
|
2271
|
+
const extendIx = extendBasketLookupTablesIx({
|
|
2272
|
+
signer,
|
|
2273
|
+
basket: basketAddress,
|
|
2274
|
+
tempLookupTable0: newLut0,
|
|
2275
|
+
tempLookupTable1: newLut1,
|
|
2276
|
+
additionalAccounts: batch,
|
|
2277
|
+
});
|
|
2278
|
+
txBatchData.batches.push([{
|
|
2279
|
+
payer: signer,
|
|
2280
|
+
instructions: [
|
|
2281
|
+
extendIx,
|
|
2282
|
+
ComputeBudgetProgram.setComputeUnitLimit({ units: COMPUTE_UNITS }),
|
|
2283
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: this.sdkParams.priorityFee }),
|
|
2284
|
+
],
|
|
2285
|
+
lookupTables: [],
|
|
2286
|
+
}]);
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
// Step 3: overwrite active LUTs
|
|
2290
|
+
const overwriteIx = overwriteBasketLookupTablesIx({
|
|
2291
|
+
signer,
|
|
2292
|
+
basket: basketAddress,
|
|
2293
|
+
tempLookupTable0: newLut0,
|
|
2294
|
+
tempLookupTable1: newLut1,
|
|
2295
|
+
activeLookupTable0: activeLut0,
|
|
2296
|
+
activeLookupTable1: activeLut1,
|
|
2297
|
+
});
|
|
2298
|
+
|
|
2299
|
+
txBatchData.batches.push([{
|
|
2300
|
+
payer: signer,
|
|
2301
|
+
instructions: [
|
|
2302
|
+
overwriteIx,
|
|
2303
|
+
ComputeBudgetProgram.setComputeUnitLimit({ units: COMPUTE_UNITS }),
|
|
2304
|
+
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: this.sdkParams.priorityFee }),
|
|
2305
|
+
],
|
|
2306
|
+
lookupTables: [],
|
|
2307
|
+
}]);
|
|
2308
|
+
|
|
2309
|
+
const versionedTxs = await prepareVersionedTxs(this.sdkParams.connection, txBatchData);
|
|
2310
|
+
const txPayloadBatchSequence = prepareTxPayloadBatchSequence(txBatchData, versionedTxs);
|
|
2311
|
+
return txPayloadBatchSequence;
|
|
2312
|
+
}
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
async signAndSendVersionedTxs(params: {
|
|
2316
|
+
versionedTxs: VersionedTxs,
|
|
2317
|
+
wallet: Wallet,
|
|
2318
|
+
}): Promise<TransactionSignature[][]> {
|
|
2319
|
+
let { versionedTxs, wallet } = params;
|
|
2320
|
+
versionedTxs = await signVersionedTxs(wallet, versionedTxs);
|
|
2321
|
+
return await sendVersionedTxs(this.sdkParams.connection, versionedTxs, false);
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
async signAndSendTxPayloadBatchSequence(params: {
|
|
2325
|
+
txPayloadBatchSequence: TxPayloadBatchSequence,
|
|
2326
|
+
wallet: Wallet,
|
|
2327
|
+
simulateTransactions?: boolean,
|
|
2328
|
+
}): Promise<TransactionSignature[][]> {
|
|
2329
|
+
let { txPayloadBatchSequence, wallet, simulateTransactions = false } = params;
|
|
2330
|
+
txPayloadBatchSequence = await signTxPayloadBatchSequence(wallet, txPayloadBatchSequence);
|
|
2331
|
+
return await sendTxPayloadBatchSequence(this.sdkParams.connection, txPayloadBatchSequence, simulateTransactions);
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
export {
|
|
2337
|
+
BasketCreationTx,
|
|
2338
|
+
TxPayloadBatchSequence,
|
|
2339
|
+
VersionedTxs,
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
export {
|
|
2343
|
+
GlobalConfig,
|
|
2344
|
+
Basket,
|
|
2345
|
+
BasketFilter,
|
|
2346
|
+
Intent,
|
|
2347
|
+
IntentFilter,
|
|
2348
|
+
RebalanceIntent,
|
|
2349
|
+
RebalanceIntentFilter
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
export {
|
|
2353
|
+
FormattedGlobalConfig,
|
|
2354
|
+
FormattedIntentStatus,
|
|
2355
|
+
FormattedTaskType,
|
|
2356
|
+
FormattedBounty,
|
|
2357
|
+
FormattedBountySchedule,
|
|
2358
|
+
FormattedIntent,
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
export {
|
|
2362
|
+
FormattedRebalanceType,
|
|
2363
|
+
FormattedRebalanceAction,
|
|
2364
|
+
FormattedOraclePrice,
|
|
2365
|
+
FormattedTokenAuction,
|
|
2366
|
+
FormattedTaskCompletion,
|
|
2367
|
+
FormattedRebalanceIntent,
|
|
2368
|
+
UIRebalanceIntent,
|
|
2369
|
+
DepositData,
|
|
2370
|
+
PriceUpdatesData,
|
|
2371
|
+
AuctionData,
|
|
2372
|
+
MintData,
|
|
2373
|
+
RedeemData,
|
|
2374
|
+
ClaimBountyData,
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
export {
|
|
2378
|
+
FormattedCreatorSettings,
|
|
2379
|
+
FormattedManagersSettings,
|
|
2380
|
+
FormattedFeeSettings,
|
|
2381
|
+
FormattedScheduleSettings,
|
|
2382
|
+
FormattedAutomationSettings,
|
|
2383
|
+
FormattedLpSettings,
|
|
2384
|
+
FormattedMetadataSettings,
|
|
2385
|
+
FormattedDepositsSettings,
|
|
2386
|
+
FormattedForceRebalanceSettings,
|
|
2387
|
+
FormattedCustomRebalanceSettings,
|
|
2388
|
+
FormattedAddTokenSettings,
|
|
2389
|
+
FormattedUpdateWeightsSettings,
|
|
2390
|
+
FormattedMakeDirectSwapSettings,
|
|
2391
|
+
FormattedAccumulatedFees,
|
|
2392
|
+
FormattedLookupTables,
|
|
2393
|
+
FormattedAsset,
|
|
2394
|
+
FormattedOracleSettings,
|
|
2395
|
+
FormattedOracleAggregator,
|
|
2396
|
+
FormattedOracle,
|
|
2397
|
+
FormattedOracleType,
|
|
2398
|
+
FormattedBasket,
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
export {
|
|
2402
|
+
EditCreatorSettings,
|
|
2403
|
+
EditManagerSettings,
|
|
2404
|
+
EditFeeSettings,
|
|
2405
|
+
EditScheduleSettings,
|
|
2406
|
+
EditAutomationSettings,
|
|
2407
|
+
EditLpSettings,
|
|
2408
|
+
EditMetadataSettings,
|
|
2409
|
+
EditDepositsSettings,
|
|
2410
|
+
EditForceRebalanceSettings,
|
|
2411
|
+
EditCustomRebalanceSettings,
|
|
2412
|
+
EditAddTokenSettings,
|
|
2413
|
+
EditUpdateWeightsSettings,
|
|
2414
|
+
EditMakeDirectSwapSettings,
|
|
2415
|
+
AddOrEditTokenInput,
|
|
2416
|
+
OracleInput,
|
|
2417
|
+
UpdateWeightsInput,
|
|
2418
|
+
MakeDirectSwapInput,
|
|
2419
|
+
Settings,
|
|
2420
|
+
TaskContext,
|
|
2421
|
+
TaskType,
|
|
2422
|
+
}
|
|
2423
|
+
|
|
2424
|
+
export {
|
|
2425
|
+
getJupTokenLedgerAndSwapInstructions,
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
export {
|
|
2429
|
+
KeeperMonitor,
|
|
2430
|
+
RebalanceHandler,
|
|
2431
|
+
} from './keeperMonitor';
|