@minswap/noodles-sdk 0.0.4 → 0.0.7
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/README.md +5 -0
- package/dist/index.cjs +1596 -0
- package/dist/index.d.ts +908 -0
- package/dist/index.js +1421 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1421 @@
|
|
|
1
|
+
import { BluefinXTx, Config, buildTx, getQuote } from "@7kprotocol/sdk-ts/cjs";
|
|
2
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
3
|
+
import bignumber from "bignumber.js";
|
|
4
|
+
import { Aftermath, Helpers } from "aftermath-ts-sdk";
|
|
5
|
+
import { buildSwapPTBFromQuote, getCoins, getQuote as astros_aggregator_sdk_getQuote } from "@naviprotocol/astros-aggregator-sdk";
|
|
6
|
+
import { AggregatorClient, Env } from "@cetusprotocol/aggregator-sdk";
|
|
7
|
+
import bn from "bn.js";
|
|
8
|
+
import { AggregatorQuoter, Coin, Commission, CommissionType, TradeBuilder } from "@flowx-finance/sdk";
|
|
9
|
+
import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
|
|
10
|
+
import { normalizeStructTag } from "@mysten/sui/utils";
|
|
11
|
+
import tiny_invariant from "@minswap/tiny-invariant";
|
|
12
|
+
import { MemezPumpSDK } from "@interest-protocol/memez-fun-sdk";
|
|
13
|
+
import { Network } from "@interest-protocol/sui-core-sdk";
|
|
14
|
+
class _7k_SevenKAggregator {
|
|
15
|
+
_config;
|
|
16
|
+
_suiClient;
|
|
17
|
+
constructor(config, suiClient){
|
|
18
|
+
this._config = config;
|
|
19
|
+
this._suiClient = suiClient;
|
|
20
|
+
}
|
|
21
|
+
async getTradeRoute({ coinInAmount, coinInType, coinOutType, tradeFee }) {
|
|
22
|
+
try {
|
|
23
|
+
if (this._config.sevenKApiKey) Config.setApiKey(this._config.sevenKApiKey);
|
|
24
|
+
const tradeFeePercent = tradeFee ? tradeFee.tradeFeePercent : this._config.defaultTradeFee.tradeFeePercent;
|
|
25
|
+
return getQuote({
|
|
26
|
+
tokenIn: coinInType,
|
|
27
|
+
tokenOut: coinOutType,
|
|
28
|
+
amountIn: coinInAmount.toString(),
|
|
29
|
+
commissionBps: 10000 * tradeFeePercent
|
|
30
|
+
});
|
|
31
|
+
} catch (_err) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async getTransaction({ walletAddress, completeRoute, slippage, tradeFee }) {
|
|
36
|
+
let tradeFeePercent;
|
|
37
|
+
let tradeFeeRecipientAddress;
|
|
38
|
+
if (tradeFee) {
|
|
39
|
+
tradeFeePercent = tradeFee.tradeFeePercent;
|
|
40
|
+
tradeFeeRecipientAddress = tradeFee.tradeFeeRecipientAddress;
|
|
41
|
+
} else {
|
|
42
|
+
tradeFeePercent = this._config.defaultTradeFee.tradeFeePercent;
|
|
43
|
+
tradeFeeRecipientAddress = this._config.defaultTradeFee.tradeFeeRecipientAddress;
|
|
44
|
+
}
|
|
45
|
+
const transaction = await buildTx({
|
|
46
|
+
quoteResponse: completeRoute,
|
|
47
|
+
slippage,
|
|
48
|
+
accountAddress: walletAddress,
|
|
49
|
+
commission: {
|
|
50
|
+
partner: tradeFeeRecipientAddress,
|
|
51
|
+
commissionBps: 10000 * tradeFeePercent
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
if (transaction.tx instanceof BluefinXTx) {
|
|
55
|
+
const newTransaction = Transaction.from(transaction.tx.txBytes);
|
|
56
|
+
return newTransaction.build({
|
|
57
|
+
client: this._suiClient
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
transaction.tx.setSender(walletAddress);
|
|
61
|
+
return transaction.tx.build({
|
|
62
|
+
client: this._suiClient
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
(function(SevenKAggregator) {
|
|
67
|
+
function toCommonTradeRoutes(tradeRouteResponse) {
|
|
68
|
+
const routes = [];
|
|
69
|
+
if (!tradeRouteResponse.routes || 0 === tradeRouteResponse.routes.length) return null;
|
|
70
|
+
for (const route of tradeRouteResponse.routes){
|
|
71
|
+
const paths = [];
|
|
72
|
+
const allTokenDecimal = {};
|
|
73
|
+
for (const path of route.hops){
|
|
74
|
+
for (const t of path.pool.allTokens)allTokenDecimal[t.address] = t.decimal;
|
|
75
|
+
const pathDecimalIn = allTokenDecimal[path.tokenIn] ?? 0;
|
|
76
|
+
const pathDecimalOut = allTokenDecimal[path.tokenOut] ?? 0;
|
|
77
|
+
const pathAmountIn = BigInt(new bignumber(path.tokenInAmount).multipliedBy(10 ** pathDecimalIn).toFixed(0, 3));
|
|
78
|
+
const pathAmountOut = BigInt(new bignumber(path.tokenOutAmount).multipliedBy(10 ** pathDecimalOut).toFixed(0, 3));
|
|
79
|
+
paths.push({
|
|
80
|
+
poolAddress: path.poolId,
|
|
81
|
+
coinIn: path.tokenIn,
|
|
82
|
+
coinOut: path.tokenOut,
|
|
83
|
+
amountIn: pathAmountIn,
|
|
84
|
+
amountOut: pathAmountOut
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (0 === paths.length) continue;
|
|
88
|
+
const routeDecimalIn = allTokenDecimal[route.tokenIn] ?? 0;
|
|
89
|
+
const routeDecimalOut = allTokenDecimal[route.tokenOut] ?? 0;
|
|
90
|
+
const routeAmountIn = BigInt(new bignumber(route.tokenInAmount).multipliedBy(10 ** routeDecimalIn).toFixed(0, 3));
|
|
91
|
+
const routeAmountOut = BigInt(new bignumber(route.tokenOutAmount).multipliedBy(10 ** routeDecimalOut).toFixed(0, 3));
|
|
92
|
+
routes.push({
|
|
93
|
+
paths: paths,
|
|
94
|
+
coinIn: route.tokenIn,
|
|
95
|
+
coinOut: route.tokenOut,
|
|
96
|
+
amountIn: routeAmountIn,
|
|
97
|
+
amountOut: routeAmountOut
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (0 === routes.length) return null;
|
|
101
|
+
return {
|
|
102
|
+
coinIn: tradeRouteResponse.tokenIn,
|
|
103
|
+
coinOut: tradeRouteResponse.tokenOut,
|
|
104
|
+
amountIn: BigInt(tradeRouteResponse.swapAmountWithDecimal),
|
|
105
|
+
amountOut: BigInt(tradeRouteResponse.returnAmountWithDecimal),
|
|
106
|
+
routes: routes
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
SevenKAggregator.toCommonTradeRoutes = toCommonTradeRoutes;
|
|
110
|
+
})(_7k_SevenKAggregator || (_7k_SevenKAggregator = {}));
|
|
111
|
+
class aftermath_AftermathAggregator {
|
|
112
|
+
_config;
|
|
113
|
+
_aftermathClient;
|
|
114
|
+
_suiClient;
|
|
115
|
+
constructor(config, suiClient){
|
|
116
|
+
this._config = config;
|
|
117
|
+
this._suiClient = suiClient;
|
|
118
|
+
}
|
|
119
|
+
async aftermathClient() {
|
|
120
|
+
if (this._aftermathClient) return this._aftermathClient;
|
|
121
|
+
if (this._config.aftermathApiEndpoint) this._aftermathClient = new Aftermath(this._config.aftermathApiEndpoint);
|
|
122
|
+
else this._aftermathClient = new Aftermath("MAINNET");
|
|
123
|
+
await this._aftermathClient.init();
|
|
124
|
+
if (this._config.aftermathApiKey) {
|
|
125
|
+
const keypair = Helpers.keypairFromPrivateKey(this._config.aftermathApiKey);
|
|
126
|
+
await this._aftermathClient.Auth().init({
|
|
127
|
+
async signMessageCallback ({ message }) {
|
|
128
|
+
const { signature } = await keypair.signPersonalMessage(message);
|
|
129
|
+
return {
|
|
130
|
+
signature
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
walletAddress: keypair.toSuiAddress()
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return this._aftermathClient;
|
|
137
|
+
}
|
|
138
|
+
async getTradeRoute({ coinInAmount, coinInType, coinOutType, tradeFee }) {
|
|
139
|
+
let externalFee;
|
|
140
|
+
externalFee = tradeFee ? {
|
|
141
|
+
recipient: tradeFee.tradeFeeRecipientAddress,
|
|
142
|
+
feePercentage: tradeFee.tradeFeePercent
|
|
143
|
+
} : {
|
|
144
|
+
recipient: this._config.defaultTradeFee.tradeFeeRecipientAddress,
|
|
145
|
+
feePercentage: this._config.defaultTradeFee.tradeFeePercent
|
|
146
|
+
};
|
|
147
|
+
const client = await this.aftermathClient();
|
|
148
|
+
return client.Router().getCompleteTradeRouteGivenAmountIn({
|
|
149
|
+
coinInAmount,
|
|
150
|
+
coinInType,
|
|
151
|
+
coinOutType,
|
|
152
|
+
externalFee
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async getTransaction({ completeRoute, walletAddress, slippage }) {
|
|
156
|
+
const client = await this.aftermathClient();
|
|
157
|
+
const transaction = await client.Router().getTransactionForCompleteTradeRoute({
|
|
158
|
+
completeRoute,
|
|
159
|
+
walletAddress,
|
|
160
|
+
slippage,
|
|
161
|
+
isSponsoredTx: false
|
|
162
|
+
});
|
|
163
|
+
transaction.setSender(walletAddress);
|
|
164
|
+
return transaction.build({
|
|
165
|
+
client: this._suiClient
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
(function(AftermathAggregator) {
|
|
170
|
+
function toCommonTradeRoutes(tradeRoute) {
|
|
171
|
+
if (0 === tradeRoute.routes.length) return null;
|
|
172
|
+
const routes = [];
|
|
173
|
+
for (const route of tradeRoute.routes){
|
|
174
|
+
const paths = [];
|
|
175
|
+
for (const path of route.paths)paths.push({
|
|
176
|
+
poolAddress: path.poolId,
|
|
177
|
+
coinIn: path.coinIn.type,
|
|
178
|
+
coinOut: path.coinOut.type,
|
|
179
|
+
amountIn: BigInt(path.coinIn.amount),
|
|
180
|
+
amountOut: BigInt(path.coinOut.amount)
|
|
181
|
+
});
|
|
182
|
+
if (0 !== paths.length) routes.push({
|
|
183
|
+
paths: paths,
|
|
184
|
+
coinIn: route.coinIn.type,
|
|
185
|
+
coinOut: route.coinOut.type,
|
|
186
|
+
amountIn: route.coinIn.amount,
|
|
187
|
+
amountOut: route.coinOut.amount
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
if (0 === routes.length) return null;
|
|
191
|
+
return {
|
|
192
|
+
coinIn: tradeRoute.coinIn.type,
|
|
193
|
+
coinOut: tradeRoute.coinOut.type,
|
|
194
|
+
amountIn: tradeRoute.coinIn.amount,
|
|
195
|
+
amountOut: tradeRoute.coinOut.amount,
|
|
196
|
+
routes: routes
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
AftermathAggregator.toCommonTradeRoutes = toCommonTradeRoutes;
|
|
200
|
+
})(aftermath_AftermathAggregator || (aftermath_AftermathAggregator = {}));
|
|
201
|
+
function isSuiCoin(coin) {
|
|
202
|
+
return "0x2::sui::SUI" === coin || "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI" === coin;
|
|
203
|
+
}
|
|
204
|
+
class astros_AstrosAggregator {
|
|
205
|
+
_config;
|
|
206
|
+
_suiClient;
|
|
207
|
+
constructor(config, suiClient){
|
|
208
|
+
this._config = config;
|
|
209
|
+
this._suiClient = suiClient;
|
|
210
|
+
}
|
|
211
|
+
async getTradeRoute({ coinInAmount, coinInType, coinOutType, tradeFee }) {
|
|
212
|
+
try {
|
|
213
|
+
let serviceFee;
|
|
214
|
+
serviceFee = tradeFee ? {
|
|
215
|
+
fee: tradeFee.tradeFeePercent,
|
|
216
|
+
receiverAddress: tradeFee.tradeFeeRecipientAddress
|
|
217
|
+
} : {
|
|
218
|
+
fee: this._config.defaultTradeFee.tradeFeePercent,
|
|
219
|
+
receiverAddress: this._config.defaultTradeFee.tradeFeeRecipientAddress
|
|
220
|
+
};
|
|
221
|
+
return astros_aggregator_sdk_getQuote(coinInType, coinOutType, coinInAmount, this._config.astrosApiKey, {
|
|
222
|
+
byAmountIn: true,
|
|
223
|
+
serviceFee: serviceFee
|
|
224
|
+
});
|
|
225
|
+
} catch (_err) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
returnMergedCoins(txb, coins, amount) {
|
|
230
|
+
if (coins.length < 2) return txb.object(coins[0].coinObjectId);
|
|
231
|
+
let mergedBalance = 0;
|
|
232
|
+
const mergeList = [];
|
|
233
|
+
coins.sort((a, b)=>Number(b.balance) - Number(a.balance)).slice(1).forEach((coin)=>{
|
|
234
|
+
if (mergedBalance >= amount) return;
|
|
235
|
+
mergedBalance += Number(coin.balance);
|
|
236
|
+
mergeList.push(coin.coinObjectId);
|
|
237
|
+
});
|
|
238
|
+
const baseObj = coins[0].coinObjectId;
|
|
239
|
+
txb.mergeCoins(baseObj, mergeList);
|
|
240
|
+
return txb.object(baseObj);
|
|
241
|
+
}
|
|
242
|
+
async getTransaction({ completeRoute: quote, walletAddress, slippage, tradeFee }) {
|
|
243
|
+
let serviceFee;
|
|
244
|
+
serviceFee = tradeFee ? {
|
|
245
|
+
fee: tradeFee.tradeFeePercent,
|
|
246
|
+
receiverAddress: tradeFee.tradeFeeRecipientAddress
|
|
247
|
+
} : {
|
|
248
|
+
fee: this._config.defaultTradeFee.tradeFeePercent,
|
|
249
|
+
receiverAddress: this._config.defaultTradeFee.tradeFeeRecipientAddress
|
|
250
|
+
};
|
|
251
|
+
const minAmountOut = new bignumber(quote.amount_out).multipliedBy(1 - slippage - serviceFee.fee).toNumber();
|
|
252
|
+
const txb = new Transaction();
|
|
253
|
+
let coinIn;
|
|
254
|
+
if (isSuiCoin(quote.from)) coinIn = txb.splitCoins(txb.gas, [
|
|
255
|
+
txb.pure.u64(quote.amount_in)
|
|
256
|
+
]);
|
|
257
|
+
else {
|
|
258
|
+
const coins = await getCoins(this._suiClient, walletAddress, quote.from);
|
|
259
|
+
const mergedCoins = this.returnMergedCoins(txb, coins.data, Number.parseFloat(quote.amount_in));
|
|
260
|
+
coinIn = txb.splitCoins(mergedCoins, [
|
|
261
|
+
txb.pure.u64(quote.amount_in)
|
|
262
|
+
]);
|
|
263
|
+
}
|
|
264
|
+
const coinB = await buildSwapPTBFromQuote(walletAddress, txb, minAmountOut, coinIn, quote, void 0, false, this._config.astrosApiKey, {
|
|
265
|
+
serviceFee: serviceFee
|
|
266
|
+
});
|
|
267
|
+
txb.transferObjects([
|
|
268
|
+
coinB
|
|
269
|
+
], walletAddress);
|
|
270
|
+
txb.setSender(walletAddress);
|
|
271
|
+
return txb.build({
|
|
272
|
+
client: this._suiClient
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
(function(AstrosAggregator) {
|
|
277
|
+
function toCommonTradeRoutes(tradeRoute, coinInType, coinOutType) {
|
|
278
|
+
if (0 === tradeRoute.routes.length) return null;
|
|
279
|
+
const routes = [];
|
|
280
|
+
for (const route of tradeRoute.routes){
|
|
281
|
+
const paths = [];
|
|
282
|
+
for (const path of route.path)paths.push({
|
|
283
|
+
poolAddress: path.id,
|
|
284
|
+
coinIn: path.from,
|
|
285
|
+
coinOut: path.target,
|
|
286
|
+
amountIn: BigInt(path.amount_in),
|
|
287
|
+
amountOut: BigInt(path.amount_out)
|
|
288
|
+
});
|
|
289
|
+
if (0 !== paths.length) routes.push({
|
|
290
|
+
amountIn: BigInt(route.amount_in),
|
|
291
|
+
amountOut: BigInt(route.amount_out),
|
|
292
|
+
coinIn: paths[0].coinIn,
|
|
293
|
+
coinOut: paths[paths.length - 1].coinOut,
|
|
294
|
+
paths: paths
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
if (0 === routes.length) return null;
|
|
298
|
+
return {
|
|
299
|
+
coinIn: coinInType,
|
|
300
|
+
coinOut: coinOutType,
|
|
301
|
+
amountIn: BigInt(tradeRoute.amount_in),
|
|
302
|
+
amountOut: BigInt(tradeRoute.amount_out),
|
|
303
|
+
routes: routes
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
AstrosAggregator.toCommonTradeRoutes = toCommonTradeRoutes;
|
|
307
|
+
})(astros_AstrosAggregator || (astros_AstrosAggregator = {}));
|
|
308
|
+
(function(TradeFeeOptions) {
|
|
309
|
+
function equals(a, b) {
|
|
310
|
+
return a.tradeFeePercent === b.tradeFeePercent && a.tradeFeeRecipientAddress === b.tradeFeeRecipientAddress;
|
|
311
|
+
}
|
|
312
|
+
TradeFeeOptions.equals = equals;
|
|
313
|
+
})(types_TradeFeeOptions || (types_TradeFeeOptions = {}));
|
|
314
|
+
var types_TradeFeeOptions;
|
|
315
|
+
class cetus_CetusAggregator {
|
|
316
|
+
_config;
|
|
317
|
+
_suiClient;
|
|
318
|
+
_cetusClient;
|
|
319
|
+
constructor(config, suiClient){
|
|
320
|
+
this._config = config;
|
|
321
|
+
this._suiClient = suiClient;
|
|
322
|
+
}
|
|
323
|
+
cetusClient(walletAddress, tradeFee) {
|
|
324
|
+
if (tradeFee && !types_TradeFeeOptions.equals(tradeFee, this._config.defaultTradeFee)) return new AggregatorClient({
|
|
325
|
+
client: this._suiClient,
|
|
326
|
+
overlayFeeReceiver: tradeFee.tradeFeeRecipientAddress,
|
|
327
|
+
overlayFeeRate: tradeFee.tradeFeePercent,
|
|
328
|
+
env: Env.Mainnet,
|
|
329
|
+
signer: walletAddress,
|
|
330
|
+
...this._config.cetusApiKey && this._config.cetusApiEndpoint ? {
|
|
331
|
+
apiKey: this._config.cetusApiKey,
|
|
332
|
+
endpoint: this._config.cetusApiEndpoint
|
|
333
|
+
} : {},
|
|
334
|
+
partner: this._config.cetusPartnerId
|
|
335
|
+
});
|
|
336
|
+
if (this._cetusClient) return this._cetusClient;
|
|
337
|
+
this._cetusClient = new AggregatorClient({
|
|
338
|
+
client: this._suiClient,
|
|
339
|
+
overlayFeeReceiver: this._config.defaultTradeFee.tradeFeeRecipientAddress,
|
|
340
|
+
overlayFeeRate: this._config.defaultTradeFee.tradeFeePercent,
|
|
341
|
+
env: Env.Mainnet,
|
|
342
|
+
signer: walletAddress,
|
|
343
|
+
...this._config.cetusApiKey && this._config.cetusApiEndpoint ? {
|
|
344
|
+
apiKey: this._config.cetusApiKey,
|
|
345
|
+
endpoint: this._config.cetusApiEndpoint
|
|
346
|
+
} : {},
|
|
347
|
+
partner: this._config.cetusPartnerId
|
|
348
|
+
});
|
|
349
|
+
return this._cetusClient;
|
|
350
|
+
}
|
|
351
|
+
async getTradeRoute({ coinInAmount, coinInType, coinOutType, tradeFee }) {
|
|
352
|
+
const client = this.cetusClient(void 0, tradeFee);
|
|
353
|
+
return client.findRouters({
|
|
354
|
+
from: coinInType,
|
|
355
|
+
target: coinOutType,
|
|
356
|
+
amount: new bn(coinInAmount.toString()),
|
|
357
|
+
byAmountIn: true
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
async getTransaction({ completeRoute: routers, walletAddress, slippage, tradeFee }) {
|
|
361
|
+
const txb = new Transaction();
|
|
362
|
+
const client = this.cetusClient(walletAddress, tradeFee);
|
|
363
|
+
await client.fastRouterSwap({
|
|
364
|
+
router: routers,
|
|
365
|
+
txb,
|
|
366
|
+
slippage
|
|
367
|
+
});
|
|
368
|
+
await client.devInspectTransactionBlock(txb);
|
|
369
|
+
txb.setSender(walletAddress);
|
|
370
|
+
return txb.build({
|
|
371
|
+
client: this._suiClient
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
(function(CetusAggregator) {
|
|
376
|
+
function toCommonTradeRoutes(tradeRoute, coinInType, coinOutType) {
|
|
377
|
+
if (0 === tradeRoute.paths.length) return null;
|
|
378
|
+
const routes = [];
|
|
379
|
+
const allPaths = [];
|
|
380
|
+
let currentRoute = [];
|
|
381
|
+
for(let i = 0; i < tradeRoute.paths.length; i++){
|
|
382
|
+
const currentPath = tradeRoute.paths[i];
|
|
383
|
+
const tradePath = {
|
|
384
|
+
poolAddress: currentPath.id,
|
|
385
|
+
amountIn: BigInt(currentPath.amountIn),
|
|
386
|
+
amountOut: BigInt(currentPath.amountOut),
|
|
387
|
+
coinIn: currentPath.from,
|
|
388
|
+
coinOut: currentPath.target
|
|
389
|
+
};
|
|
390
|
+
currentRoute.push(tradePath);
|
|
391
|
+
if (i === tradeRoute.paths.length - 1) allPaths.push([
|
|
392
|
+
...currentRoute
|
|
393
|
+
]);
|
|
394
|
+
else {
|
|
395
|
+
const nextPath = tradeRoute.paths[i + 1];
|
|
396
|
+
if (currentPath.target !== nextPath.from) {
|
|
397
|
+
allPaths.push([
|
|
398
|
+
...currentRoute
|
|
399
|
+
]);
|
|
400
|
+
currentRoute = [];
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
for (const pathGroup of allPaths)if (0 !== pathGroup.length) routes.push({
|
|
405
|
+
amountIn: pathGroup[0].amountIn,
|
|
406
|
+
amountOut: pathGroup[pathGroup.length - 1].amountOut,
|
|
407
|
+
coinIn: pathGroup[0].coinIn,
|
|
408
|
+
coinOut: pathGroup[pathGroup.length - 1].coinOut,
|
|
409
|
+
paths: pathGroup
|
|
410
|
+
});
|
|
411
|
+
if (0 === routes.length) return null;
|
|
412
|
+
return {
|
|
413
|
+
coinIn: coinInType,
|
|
414
|
+
coinOut: coinOutType,
|
|
415
|
+
amountIn: BigInt(tradeRoute.amountIn.toString(10)),
|
|
416
|
+
amountOut: BigInt(tradeRoute.amountOut.toString(10)),
|
|
417
|
+
routes: routes
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
CetusAggregator.toCommonTradeRoutes = toCommonTradeRoutes;
|
|
421
|
+
})(cetus_CetusAggregator || (cetus_CetusAggregator = {}));
|
|
422
|
+
var common_SupportedAggregator = /*#__PURE__*/ function(SupportedAggregator) {
|
|
423
|
+
SupportedAggregator["ASTROS"] = "ASTROS";
|
|
424
|
+
SupportedAggregator["AFTERMATH"] = "AFTERMATH";
|
|
425
|
+
SupportedAggregator["SEVENK"] = "7K";
|
|
426
|
+
SupportedAggregator["CETUS"] = "CETUS";
|
|
427
|
+
SupportedAggregator["FLOWX"] = "FLOWX";
|
|
428
|
+
return SupportedAggregator;
|
|
429
|
+
}({});
|
|
430
|
+
function getInputPercent(percent) {
|
|
431
|
+
return 1e6 * percent;
|
|
432
|
+
}
|
|
433
|
+
class flowx_FlowXAggregator {
|
|
434
|
+
_config;
|
|
435
|
+
_suiClient;
|
|
436
|
+
constructor(config, suiClient){
|
|
437
|
+
this._config = config;
|
|
438
|
+
this._suiClient = suiClient;
|
|
439
|
+
}
|
|
440
|
+
getCommission(coinInType, tradeFee) {
|
|
441
|
+
let tradeFeePercent;
|
|
442
|
+
let tradeFeeRecipientAddress;
|
|
443
|
+
if (tradeFee) {
|
|
444
|
+
tradeFeePercent = tradeFee.tradeFeePercent;
|
|
445
|
+
tradeFeeRecipientAddress = tradeFee.tradeFeeRecipientAddress;
|
|
446
|
+
} else {
|
|
447
|
+
tradeFeePercent = this._config.defaultTradeFee.tradeFeePercent;
|
|
448
|
+
tradeFeeRecipientAddress = this._config.defaultTradeFee.tradeFeeRecipientAddress;
|
|
449
|
+
}
|
|
450
|
+
return new Commission(tradeFeeRecipientAddress, new Coin(coinInType), CommissionType.PERCENTAGE, getInputPercent(tradeFeePercent), true);
|
|
451
|
+
}
|
|
452
|
+
async getTradeRoute({ coinInAmount, coinInType, coinOutType, tradeFee }) {
|
|
453
|
+
const quoter = new AggregatorQuoter("mainnet", this._config.flowxApiKey);
|
|
454
|
+
try {
|
|
455
|
+
return quoter.getRoutes({
|
|
456
|
+
tokenIn: coinInType,
|
|
457
|
+
tokenOut: coinOutType,
|
|
458
|
+
amountIn: coinInAmount.toString(),
|
|
459
|
+
commission: this.getCommission(coinInType, tradeFee)
|
|
460
|
+
});
|
|
461
|
+
} catch (_err) {
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
async getTransaction({ walletAddress, completeRoute, slippage, tradeFee }) {
|
|
466
|
+
const tradeBuilder = new TradeBuilder("mainnet", completeRoute.routes);
|
|
467
|
+
const result = tradeBuilder.sender(walletAddress).slippage(getInputPercent(slippage)).commission(this.getCommission(completeRoute.coinIn.coinType, tradeFee)).build();
|
|
468
|
+
return result.buildTransaction({
|
|
469
|
+
client: this._suiClient
|
|
470
|
+
}).then((tx)=>{
|
|
471
|
+
tx.setSender(walletAddress);
|
|
472
|
+
return tx.build({
|
|
473
|
+
client: this._suiClient
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
(function(FlowXAggregator) {
|
|
479
|
+
function toCommonTradeRoutes(tradeRoute) {
|
|
480
|
+
if (0 === tradeRoute.routes.length) return null;
|
|
481
|
+
const routes = [];
|
|
482
|
+
for (const route of tradeRoute.routes){
|
|
483
|
+
const paths = [];
|
|
484
|
+
for (const path of route.paths)paths.push({
|
|
485
|
+
amountIn: BigInt(path.amountIn.toString()),
|
|
486
|
+
amountOut: BigInt(path.amountOut.toString()),
|
|
487
|
+
coinIn: path.input.coinType,
|
|
488
|
+
coinOut: path.output.coinType,
|
|
489
|
+
poolAddress: path.pool.id
|
|
490
|
+
});
|
|
491
|
+
if (0 !== paths.length) routes.push({
|
|
492
|
+
amountIn: BigInt(route.amountIn.toString()),
|
|
493
|
+
amountOut: BigInt(route.amountOut.toString()),
|
|
494
|
+
coinIn: route.input.coinType,
|
|
495
|
+
coinOut: route.output.coinType,
|
|
496
|
+
paths: paths
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
if (0 === routes.length) return null;
|
|
500
|
+
return {
|
|
501
|
+
coinIn: tradeRoute.coinIn.coinType,
|
|
502
|
+
coinOut: tradeRoute.coinOut.coinType,
|
|
503
|
+
amountIn: BigInt(tradeRoute.amountIn.toString()),
|
|
504
|
+
amountOut: BigInt(tradeRoute.amountOut.toString()),
|
|
505
|
+
routes: routes
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
FlowXAggregator.toCommonTradeRoutes = toCommonTradeRoutes;
|
|
509
|
+
})(flowx_FlowXAggregator || (flowx_FlowXAggregator = {}));
|
|
510
|
+
(function(BlastFunConstants) {
|
|
511
|
+
BlastFunConstants.PACKAGE_ID_V4 = "0x7e6aa6e179466ab2814425a780b122575296d011119fa69d27f289f5a28814bd";
|
|
512
|
+
BlastFunConstants.MEMEZ_AV_OBJECT_ID = "0x2319e3e76dfad73d8f4684bdbf42be4f32d8ce4521dd61becc8261dc918d82c0";
|
|
513
|
+
})(constants_BlastFunConstants || (constants_BlastFunConstants = {}));
|
|
514
|
+
var constants_BlastFunConstants;
|
|
515
|
+
const BLUEFIN_PACKAGE_ID = "0x62412b7268c35f3808336aee57a52836501f40b8ba5d936f8ad275e672befd04";
|
|
516
|
+
const SUI_TYPE = "0x2::sui::SUI";
|
|
517
|
+
const SUI_FULL_TYPE = "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI";
|
|
518
|
+
const USDC_TOKEN_TYPE = "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN";
|
|
519
|
+
const NATIVE_USDC_TOKEN_TYPE = "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC";
|
|
520
|
+
const SUI_METADATA_OBJECT_ID = "0x9258181f5ceac8dbffb7030890243caed69a9599d2886d957a9cb7656af3bdb3";
|
|
521
|
+
const CLOCK_OBJECT_ID = "0x0000000000000000000000000000000000000000000000000000000000000006";
|
|
522
|
+
const config_suiClient = new SuiClient({
|
|
523
|
+
url: getFullnodeUrl("mainnet")
|
|
524
|
+
});
|
|
525
|
+
function getSuiClient() {
|
|
526
|
+
return config_suiClient;
|
|
527
|
+
}
|
|
528
|
+
const orderByKey = (array, key, sortBy)=>{
|
|
529
|
+
if (!array?.length) return;
|
|
530
|
+
let swapped;
|
|
531
|
+
do {
|
|
532
|
+
swapped = false;
|
|
533
|
+
for(let i = 0; i < array.length - 1; i++){
|
|
534
|
+
const a = BigInt(array[i][key]);
|
|
535
|
+
const b = BigInt(array[i + 1][key]);
|
|
536
|
+
if ("desc" === sortBy ? a < b : a > b) {
|
|
537
|
+
const temp = array[i];
|
|
538
|
+
array[i] = array[i + 1];
|
|
539
|
+
array[i + 1] = temp;
|
|
540
|
+
swapped = true;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}while (swapped);
|
|
544
|
+
return array;
|
|
545
|
+
};
|
|
546
|
+
const getCoinObjectIdsByAmount = async (address, amount, coinType)=>{
|
|
547
|
+
let coinBalances = [];
|
|
548
|
+
let hasNextPage = true;
|
|
549
|
+
let nextCursor;
|
|
550
|
+
while(hasNextPage)try {
|
|
551
|
+
const coins = await getSuiClient().getCoins({
|
|
552
|
+
owner: address,
|
|
553
|
+
coinType,
|
|
554
|
+
cursor: nextCursor
|
|
555
|
+
});
|
|
556
|
+
coinBalances = [
|
|
557
|
+
...coinBalances,
|
|
558
|
+
...coins.data
|
|
559
|
+
];
|
|
560
|
+
hasNextPage = coins.hasNextPage;
|
|
561
|
+
nextCursor = coins.nextCursor;
|
|
562
|
+
} catch (error) {
|
|
563
|
+
console.error("Error fetching data:", error);
|
|
564
|
+
hasNextPage = false;
|
|
565
|
+
}
|
|
566
|
+
const coinObj = orderByKey(coinBalances.map((item)=>({
|
|
567
|
+
...item,
|
|
568
|
+
balance: item.balance
|
|
569
|
+
})), "balance", "desc") ?? [];
|
|
570
|
+
let balance = "0";
|
|
571
|
+
const objectIds = [];
|
|
572
|
+
const objectCoins = [];
|
|
573
|
+
for (const coin of coinObj){
|
|
574
|
+
balance = (BigInt(coin.balance) + BigInt(balance)).toString(10);
|
|
575
|
+
objectIds.push(coin.coinObjectId);
|
|
576
|
+
objectCoins.push(coin);
|
|
577
|
+
if (BigInt(balance) >= BigInt(amount)) break;
|
|
578
|
+
}
|
|
579
|
+
return {
|
|
580
|
+
objectIds,
|
|
581
|
+
balance,
|
|
582
|
+
objectCoins
|
|
583
|
+
};
|
|
584
|
+
};
|
|
585
|
+
async function addGasFee(params) {
|
|
586
|
+
const { inheritTx, sender, feeAmount, suiInputAmount } = params;
|
|
587
|
+
const tx = inheritTx || new Transaction();
|
|
588
|
+
tx.setGasBudget(feeAmount);
|
|
589
|
+
const { objectCoins } = await getCoinObjectIdsByAmount(sender, (BigInt(suiInputAmount || "0") + feeAmount).toString(), SUI_FULL_TYPE);
|
|
590
|
+
tx.setGasPayment(objectCoins.map((c)=>({
|
|
591
|
+
objectId: c.coinObjectId,
|
|
592
|
+
version: c.version,
|
|
593
|
+
digest: c.digest
|
|
594
|
+
})));
|
|
595
|
+
return tx;
|
|
596
|
+
}
|
|
597
|
+
(function(BluefinTx) {
|
|
598
|
+
function transferOrDestroyZeroCoin(tx, coinType, coin, address) {
|
|
599
|
+
tx.moveCall({
|
|
600
|
+
target: `${BLUEFIN_PACKAGE_ID}::utils::transfer_or_destroy`,
|
|
601
|
+
typeArguments: [
|
|
602
|
+
coinType
|
|
603
|
+
],
|
|
604
|
+
arguments: [
|
|
605
|
+
coin,
|
|
606
|
+
tx.pure.address(address)
|
|
607
|
+
]
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
BluefinTx.transferOrDestroyZeroCoin = transferOrDestroyZeroCoin;
|
|
611
|
+
})(bluefin_BluefinTx || (bluefin_BluefinTx = {}));
|
|
612
|
+
var bluefin_BluefinTx;
|
|
613
|
+
async function getSplitCoinForTx(params) {
|
|
614
|
+
const { account, amount, splits, coinType, inheritTx, inspecTransaction, isSponsored = false } = params;
|
|
615
|
+
const tx = inheritTx ?? new Transaction();
|
|
616
|
+
const { objectIds } = await getCoinObjectIdsByAmount(account, amount, coinType);
|
|
617
|
+
const coinObjectId = objectIds[0];
|
|
618
|
+
if (normalizeStructTag(coinType) === normalizeStructTag(SUI_FULL_TYPE) && !isSponsored) {
|
|
619
|
+
let coin;
|
|
620
|
+
if (inspecTransaction) {
|
|
621
|
+
if (objectIds.length > 1) tx.mergeCoins(tx.object(coinObjectId), objectIds.slice(1).map((item)=>tx.object(item)));
|
|
622
|
+
coin = tx.splitCoins(tx.object(coinObjectId), splits);
|
|
623
|
+
} else coin = tx.splitCoins(tx.gas, splits);
|
|
624
|
+
return {
|
|
625
|
+
tx,
|
|
626
|
+
coinData: coin
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
if (objectIds.length > 1) tx.mergeCoins(tx.object(coinObjectId), objectIds.slice(1).map((item)=>tx.object(item)));
|
|
630
|
+
const coinData = tx.splitCoins(tx.object(coinObjectId), splits);
|
|
631
|
+
return {
|
|
632
|
+
tx,
|
|
633
|
+
coinData
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
const BASE_BPS = 10000;
|
|
637
|
+
function getSplitCoinsAfterFee(amount, fee) {
|
|
638
|
+
const amountNum = BigInt(amount);
|
|
639
|
+
const feeNum = BigInt(Number(fee) * BASE_BPS);
|
|
640
|
+
if (feeNum === BigInt(0)) return [
|
|
641
|
+
amountNum.toString()
|
|
642
|
+
];
|
|
643
|
+
const feeAmount = amountNum * feeNum / BigInt(BASE_BPS);
|
|
644
|
+
const remainingAmount = amountNum - feeAmount;
|
|
645
|
+
return [
|
|
646
|
+
remainingAmount.toString(),
|
|
647
|
+
feeAmount.toString()
|
|
648
|
+
];
|
|
649
|
+
}
|
|
650
|
+
function getAmountAfterFee(amount, fee) {
|
|
651
|
+
if (!fee || Number(fee) <= 0) return BigInt(amount);
|
|
652
|
+
const amountNum = BigInt(amount);
|
|
653
|
+
const feeNum = BigInt(Number(fee) * BASE_BPS);
|
|
654
|
+
if (feeNum === BigInt(0)) return amountNum;
|
|
655
|
+
const feeAmount = amountNum * feeNum / BigInt(BASE_BPS);
|
|
656
|
+
return amountNum - feeAmount;
|
|
657
|
+
}
|
|
658
|
+
async function getNeededGasFee(tx, sender, bufferPercent) {
|
|
659
|
+
const { effects: { gasUsed, status } } = await getSuiClient().devInspectTransactionBlock({
|
|
660
|
+
sender,
|
|
661
|
+
transactionBlock: tx
|
|
662
|
+
});
|
|
663
|
+
if ("success" !== status.status) throw new Error(`Transaction failed during dev inspect: ${status.error ?? ""}`);
|
|
664
|
+
const fee = BigInt(gasUsed.computationCost) + BigInt(gasUsed.storageCost) - BigInt(gasUsed.storageRebate);
|
|
665
|
+
const feeNum = BigInt(Number(bufferPercent) * BASE_BPS);
|
|
666
|
+
return fee * (BigInt(BASE_BPS) + feeNum) / BigInt(BASE_BPS);
|
|
667
|
+
}
|
|
668
|
+
async function getMoveObjectContent(objectId) {
|
|
669
|
+
const moveObject = await getMoveObject(objectId);
|
|
670
|
+
return moveObject.fields;
|
|
671
|
+
}
|
|
672
|
+
async function getMoveObject(objectId) {
|
|
673
|
+
const client = getSuiClient();
|
|
674
|
+
const objRes = await client.getObject({
|
|
675
|
+
id: objectId,
|
|
676
|
+
options: {
|
|
677
|
+
showContent: true
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
const content = objRes.data?.content;
|
|
681
|
+
tiny_invariant(content, "Pool object not found or has no content");
|
|
682
|
+
tiny_invariant("moveObject" === content.dataType, "Object is not a Move object");
|
|
683
|
+
return content;
|
|
684
|
+
}
|
|
685
|
+
function adaptSuiClient(client) {
|
|
686
|
+
return client;
|
|
687
|
+
}
|
|
688
|
+
function adaptTransaction(transaction) {
|
|
689
|
+
return transaction;
|
|
690
|
+
}
|
|
691
|
+
function createCompatibleSuiClient(client) {
|
|
692
|
+
return new Proxy(client, {
|
|
693
|
+
get (target, prop) {
|
|
694
|
+
const value = target[prop];
|
|
695
|
+
if ("function" == typeof value) return value.bind(target);
|
|
696
|
+
return value;
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
(function(MathUtils) {
|
|
701
|
+
(function(U64) {
|
|
702
|
+
function mulDivUp(a, b, c) {
|
|
703
|
+
let roundUp = BigInt(0);
|
|
704
|
+
if (a * b % c > BigInt(0)) roundUp = BigInt(1);
|
|
705
|
+
return a * b / c + roundUp;
|
|
706
|
+
}
|
|
707
|
+
U64.mulDivUp = mulDivUp;
|
|
708
|
+
})(MathUtils.U64 || (MathUtils.U64 = {}));
|
|
709
|
+
})(package_MathUtils || (package_MathUtils = {}));
|
|
710
|
+
(function(Bps) {
|
|
711
|
+
function calcUp(bps, amount) {
|
|
712
|
+
const bpsValue = BigInt(bps.pos0);
|
|
713
|
+
const denominator = BigInt(10000);
|
|
714
|
+
let roundUp;
|
|
715
|
+
roundUp = bpsValue * amount % denominator !== BigInt(0) ? BigInt(1) : BigInt(0);
|
|
716
|
+
return bpsValue * amount / denominator + roundUp;
|
|
717
|
+
}
|
|
718
|
+
Bps.calcUp = calcUp;
|
|
719
|
+
function sub(lhs, rhs) {
|
|
720
|
+
tiny_invariant(BigInt(lhs.pos0) >= BigInt(rhs.pos0), "Bps underflow");
|
|
721
|
+
return {
|
|
722
|
+
pos0: (BigInt(lhs.pos0) - BigInt(rhs.pos0)).toString()
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
Bps.sub = sub;
|
|
726
|
+
function value(bps) {
|
|
727
|
+
return BigInt(bps.pos0);
|
|
728
|
+
}
|
|
729
|
+
Bps.value = value;
|
|
730
|
+
function new_(bps) {
|
|
731
|
+
return {
|
|
732
|
+
pos0: assertOverflow(bps).toString()
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
Bps.new_ = new_;
|
|
736
|
+
function assertOverflow(bps) {
|
|
737
|
+
tiny_invariant(BigInt(bps) <= BigInt(10000), "Bps overflow");
|
|
738
|
+
return BigInt(bps);
|
|
739
|
+
}
|
|
740
|
+
Bps.assertOverflow = assertOverflow;
|
|
741
|
+
})(package_Bps || (package_Bps = {}));
|
|
742
|
+
(function(BlastFunPackage) {
|
|
743
|
+
(function(MemezFun) {
|
|
744
|
+
async function isSniperProtectedMemezFun(poolId) {
|
|
745
|
+
const memezFun = await getMoveObjectContent(poolId);
|
|
746
|
+
return memezFun.public_key.length > 0;
|
|
747
|
+
}
|
|
748
|
+
MemezFun.isSniperProtectedMemezFun = isSniperProtectedMemezFun;
|
|
749
|
+
})(BlastFunPackage.MemezFun || (BlastFunPackage.MemezFun = {}));
|
|
750
|
+
(function(MemezFees) {
|
|
751
|
+
function takeWithDiscount(fee, amount, discountBps) {
|
|
752
|
+
if ("Value" === fee.variant) {
|
|
753
|
+
const amountRequired = BigInt(fee.fields.pos0);
|
|
754
|
+
if (amountRequired === BigInt(0)) return BigInt(0);
|
|
755
|
+
if (amount >= amountRequired) return amountRequired;
|
|
756
|
+
throw new Error(`Insufficient amount to cover fee. Required: ${amountRequired}, provided: ${amount}`);
|
|
757
|
+
}
|
|
758
|
+
if ("Percentage" === fee.variant) {
|
|
759
|
+
const feeBps = fee.fields.pos0.fields;
|
|
760
|
+
if (BigInt(feeBps.pos0) === BigInt(0)) return BigInt(0);
|
|
761
|
+
const discountedFee = package_Bps.calcUp(package_Bps.sub(feeBps, discountBps), amount);
|
|
762
|
+
if (amount >= discountedFee) return discountedFee;
|
|
763
|
+
throw new Error(`Insufficient amount to cover fee. Required: ${discountedFee}, provided: ${amount}`);
|
|
764
|
+
}
|
|
765
|
+
throw new Error("Unknown fee variant");
|
|
766
|
+
}
|
|
767
|
+
MemezFees.takeWithDiscount = takeWithDiscount;
|
|
768
|
+
function calculateWithDiscount(fee, discountBps, amount) {
|
|
769
|
+
if ("Value" === fee.variant) return BigInt(fee.fields.pos0);
|
|
770
|
+
if ("Percentage" === fee.variant) {
|
|
771
|
+
const feeBps = fee.fields.pos0.fields;
|
|
772
|
+
return package_Bps.calcUp(package_Bps.sub(feeBps, discountBps), amount);
|
|
773
|
+
}
|
|
774
|
+
throw new Error("Unknown fee variant");
|
|
775
|
+
}
|
|
776
|
+
MemezFees.calculateWithDiscount = calculateWithDiscount;
|
|
777
|
+
})(BlastFunPackage.MemezFees || (BlastFunPackage.MemezFees = {}));
|
|
778
|
+
(function(MemezBurner) {
|
|
779
|
+
function calculate(burner, poolMemeBalance) {
|
|
780
|
+
const burnerFee = package_Bps.value(burner.fee.fields);
|
|
781
|
+
if (burnerFee === BigInt(0) || poolMemeBalance === BigInt(0)) return package_Bps.new_(0);
|
|
782
|
+
if (poolMemeBalance >= BigInt(burner.target_liquidity)) return package_Bps.new_(0);
|
|
783
|
+
const baseBps = BigInt(10000);
|
|
784
|
+
return package_Bps.new_(package_MathUtils.U64.mulDivUp(package_Bps.value(burner.fee.fields), package_MathUtils.U64.mulDivUp(poolMemeBalance, baseBps, BigInt(burner.target_liquidity)), baseBps));
|
|
785
|
+
}
|
|
786
|
+
MemezBurner.calculate = calculate;
|
|
787
|
+
})(BlastFunPackage.MemezBurner || (BlastFunPackage.MemezBurner = {}));
|
|
788
|
+
(function(MemezPump) {
|
|
789
|
+
async function getPumpState(memezFun) {
|
|
790
|
+
return await getMoveObjectContent(memezFun.inner_state);
|
|
791
|
+
}
|
|
792
|
+
MemezPump.getPumpState = getPumpState;
|
|
793
|
+
function pump(pumpState, quoteAmount) {
|
|
794
|
+
const constantProduct = pumpState.constant_product.fields;
|
|
795
|
+
const quoteSwapFee = BlastFunPackage.MemezFees.takeWithDiscount(constantProduct.quote_swap_fee, quoteAmount, constantProduct.quote_referrer_fee.fields);
|
|
796
|
+
const quoteAfterFee = quoteAmount - quoteSwapFee;
|
|
797
|
+
const memeBalance = BigInt(constantProduct.meme_balance);
|
|
798
|
+
const quoteBalance = BigInt(constantProduct.quote_balance);
|
|
799
|
+
const virtualLiquidity = BigInt(constantProduct.virtual_liquidity);
|
|
800
|
+
const memeOutExpected = memeBalance * quoteAfterFee / (virtualLiquidity + quoteBalance + quoteAfterFee);
|
|
801
|
+
const memeSwapFee = BlastFunPackage.MemezFees.takeWithDiscount(constantProduct.meme_swap_fee, memeOutExpected, constantProduct.meme_referrer_fee.fields);
|
|
802
|
+
return memeOutExpected - memeSwapFee;
|
|
803
|
+
}
|
|
804
|
+
MemezPump.pump = pump;
|
|
805
|
+
function dump(pumpState, memeAmount) {
|
|
806
|
+
const constantProduct = pumpState.constant_product.fields;
|
|
807
|
+
const memeSwapFee = BlastFunPackage.MemezFees.takeWithDiscount(constantProduct.meme_swap_fee, memeAmount, constantProduct.meme_referrer_fee.fields);
|
|
808
|
+
const inputAmount = memeAmount - memeSwapFee;
|
|
809
|
+
const memeBalance = BigInt(constantProduct.meme_balance);
|
|
810
|
+
const quoteBalance = BigInt(constantProduct.quote_balance);
|
|
811
|
+
const virtualLiquidity = BigInt(constantProduct.virtual_liquidity);
|
|
812
|
+
const burnBps = BlastFunPackage.MemezBurner.calculate(constantProduct.burner.fields, quoteBalance);
|
|
813
|
+
const burnAmount = package_Bps.calcUp(burnBps, inputAmount);
|
|
814
|
+
const inputAmountAfterFee = inputAmount - (package_Bps.value(burnBps) !== BigInt(0) ? burnAmount : BigInt(0));
|
|
815
|
+
let quoteOutBeforeFee = (virtualLiquidity + quoteBalance) * inputAmountAfterFee / (memeBalance + inputAmountAfterFee);
|
|
816
|
+
if (quoteOutBeforeFee > quoteBalance) quoteOutBeforeFee = quoteBalance;
|
|
817
|
+
const quoteSwapFee = BlastFunPackage.MemezFees.takeWithDiscount(constantProduct.quote_swap_fee, quoteOutBeforeFee, constantProduct.quote_referrer_fee.fields);
|
|
818
|
+
return quoteOutBeforeFee - quoteSwapFee;
|
|
819
|
+
}
|
|
820
|
+
MemezPump.dump = dump;
|
|
821
|
+
})(BlastFunPackage.MemezPump || (BlastFunPackage.MemezPump = {}));
|
|
822
|
+
})(package_BlastFunPackage || (package_BlastFunPackage = {}));
|
|
823
|
+
var package_MathUtils, package_Bps, package_BlastFunPackage;
|
|
824
|
+
(function(BlastFunCustomCalculation) {
|
|
825
|
+
async function getCurveAmountAfterBuy(params) {
|
|
826
|
+
let { suiAmount, poolId, frontendFee } = params;
|
|
827
|
+
const memezFun = await getMoveObjectContent(poolId);
|
|
828
|
+
const pumpState = await package_BlastFunPackage.MemezPump.getPumpState(memezFun);
|
|
829
|
+
suiAmount = getAmountAfterFee(suiAmount, frontendFee);
|
|
830
|
+
return package_BlastFunPackage.MemezPump.pump(pumpState, suiAmount);
|
|
831
|
+
}
|
|
832
|
+
BlastFunCustomCalculation.getCurveAmountAfterBuy = getCurveAmountAfterBuy;
|
|
833
|
+
async function getSuiAmountAfterSell(params) {
|
|
834
|
+
const { curveAmount, poolId, frontendFee } = params;
|
|
835
|
+
const memezFun = await getMoveObjectContent(poolId);
|
|
836
|
+
const pumpState = await package_BlastFunPackage.MemezPump.getPumpState(memezFun);
|
|
837
|
+
const suiAmount = package_BlastFunPackage.MemezPump.dump(pumpState, curveAmount);
|
|
838
|
+
return getAmountAfterFee(suiAmount, frontendFee);
|
|
839
|
+
}
|
|
840
|
+
BlastFunCustomCalculation.getSuiAmountAfterSell = getSuiAmountAfterSell;
|
|
841
|
+
})(custom_calculation_BlastFunCustomCalculation || (custom_calculation_BlastFunCustomCalculation = {}));
|
|
842
|
+
var custom_calculation_BlastFunCustomCalculation;
|
|
843
|
+
async function splitSuiCoinAfterFeeFromBuyTx(params) {
|
|
844
|
+
const { inputAmount, accountAddress, frontendFee, frontendFeeRecipient, extendTx, isSponsored } = params;
|
|
845
|
+
tiny_invariant(params.inCoinType === SUI_FULL_TYPE || params.inCoinType === SUI_TYPE, "Input must be SUI");
|
|
846
|
+
const { tx: _tx, coinIn } = extendTx || {};
|
|
847
|
+
const splitSui = getSplitCoinsAfterFee(inputAmount, frontendFee || "0");
|
|
848
|
+
const tx = _tx || new Transaction();
|
|
849
|
+
let coinData;
|
|
850
|
+
if (coinIn) {
|
|
851
|
+
coinData = tx.splitCoins(coinIn, splitSui);
|
|
852
|
+
bluefin_BluefinTx.transferOrDestroyZeroCoin(tx, SUI_FULL_TYPE, coinIn, accountAddress);
|
|
853
|
+
} else {
|
|
854
|
+
const { coinData: _data } = await getSplitCoinForTx({
|
|
855
|
+
account: accountAddress,
|
|
856
|
+
amount: inputAmount,
|
|
857
|
+
splits: splitSui,
|
|
858
|
+
coinType: SUI_FULL_TYPE,
|
|
859
|
+
inheritTx: tx,
|
|
860
|
+
isSponsored: isSponsored || false
|
|
861
|
+
});
|
|
862
|
+
coinData = _data;
|
|
863
|
+
}
|
|
864
|
+
let inputSuiCoin;
|
|
865
|
+
if (frontendFee && frontendFeeRecipient) {
|
|
866
|
+
const [_inputSuiCoin, frontendFeeCoin] = coinData;
|
|
867
|
+
inputSuiCoin = _inputSuiCoin;
|
|
868
|
+
tx.transferObjects([
|
|
869
|
+
frontendFeeCoin
|
|
870
|
+
], tx.pure.address(frontendFeeRecipient || accountAddress));
|
|
871
|
+
} else [inputSuiCoin] = coinData;
|
|
872
|
+
return {
|
|
873
|
+
tx,
|
|
874
|
+
coinOut: inputSuiCoin
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
async function splitSuiCoinAfterFeeFromSellTx(tx, params, suiCoin) {
|
|
878
|
+
tiny_invariant(params.outCoinType === SUI_FULL_TYPE || params.outCoinType === SUI_TYPE, "Output must be SUI");
|
|
879
|
+
if (Number(params.frontendFee || 0) <= 0) return {
|
|
880
|
+
tx,
|
|
881
|
+
coinOut: suiCoin
|
|
882
|
+
};
|
|
883
|
+
const suiCoinValue = tx.moveCall({
|
|
884
|
+
target: "0x2::coin::value",
|
|
885
|
+
typeArguments: [
|
|
886
|
+
params.outCoinType
|
|
887
|
+
],
|
|
888
|
+
arguments: [
|
|
889
|
+
suiCoin
|
|
890
|
+
]
|
|
891
|
+
});
|
|
892
|
+
const suiCoinFeeValue = tx.moveCall({
|
|
893
|
+
target: `${BLUEFIN_PACKAGE_ID}::math::mul_div_u64`,
|
|
894
|
+
typeArguments: [],
|
|
895
|
+
arguments: [
|
|
896
|
+
suiCoinValue,
|
|
897
|
+
tx.pure.u64(BigInt(Number(params.frontendFee || "0") * BASE_BPS)),
|
|
898
|
+
tx.pure.u64(BigInt(BASE_BPS))
|
|
899
|
+
]
|
|
900
|
+
});
|
|
901
|
+
const suiCoinFee = tx.moveCall({
|
|
902
|
+
target: "0x2::coin::split",
|
|
903
|
+
typeArguments: [
|
|
904
|
+
params.outCoinType
|
|
905
|
+
],
|
|
906
|
+
arguments: [
|
|
907
|
+
suiCoin,
|
|
908
|
+
suiCoinFeeValue
|
|
909
|
+
]
|
|
910
|
+
});
|
|
911
|
+
tx.transferObjects([
|
|
912
|
+
suiCoinFee
|
|
913
|
+
], tx.pure.address(params.frontendFeeRecipient || params.accountAddress));
|
|
914
|
+
return {
|
|
915
|
+
tx,
|
|
916
|
+
coinOut: suiCoin
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
var types_SupportedBondingCurve = /*#__PURE__*/ function(SupportedBondingCurve) {
|
|
920
|
+
SupportedBondingCurve["BLAST_FUN"] = "BLAST_FUN";
|
|
921
|
+
SupportedBondingCurve["MOONBAGS"] = "MOONBAGS";
|
|
922
|
+
return SupportedBondingCurve;
|
|
923
|
+
}({});
|
|
924
|
+
(function(BlastFunCustomTransaction) {
|
|
925
|
+
async function getAllowedVersions(_tx) {
|
|
926
|
+
const tx = _tx || new Transaction();
|
|
927
|
+
return tx.moveCall({
|
|
928
|
+
target: `${constants_BlastFunConstants.PACKAGE_ID_V4}::memez_allowed_versions::get_allowed_versions`,
|
|
929
|
+
arguments: [
|
|
930
|
+
tx.object(constants_BlastFunConstants.MEMEZ_AV_OBJECT_ID)
|
|
931
|
+
],
|
|
932
|
+
typeArguments: []
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
BlastFunCustomTransaction.getAllowedVersions = getAllowedVersions;
|
|
936
|
+
async function buildBuyTx(params) {
|
|
937
|
+
tiny_invariant(!await package_BlastFunPackage.MemezFun.isSniperProtectedMemezFun(params.poolId), "This coin is a sniper protected coin, please use the Blast Fun website to trade it.");
|
|
938
|
+
const { tx, coinOut: suiCoinIn } = await splitSuiCoinAfterFeeFromBuyTx(params);
|
|
939
|
+
tiny_invariant(suiCoinIn, "Coin out is required");
|
|
940
|
+
const curveCoin = tx.moveCall({
|
|
941
|
+
target: `${constants_BlastFunConstants.PACKAGE_ID_V4}::memez_pump::pump`,
|
|
942
|
+
typeArguments: [
|
|
943
|
+
params.outCoinType,
|
|
944
|
+
SUI_FULL_TYPE
|
|
945
|
+
],
|
|
946
|
+
arguments: [
|
|
947
|
+
tx.object(params.poolId),
|
|
948
|
+
suiCoinIn,
|
|
949
|
+
tx.pure.vector("address", []),
|
|
950
|
+
tx.pure.vector("vector<u8>", []),
|
|
951
|
+
tx.pure.u64(params.minAmountOut),
|
|
952
|
+
await getAllowedVersions(tx)
|
|
953
|
+
]
|
|
954
|
+
});
|
|
955
|
+
return {
|
|
956
|
+
tx,
|
|
957
|
+
coinOut: curveCoin
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
BlastFunCustomTransaction.buildBuyTx = buildBuyTx;
|
|
961
|
+
async function buildSellTx(params) {
|
|
962
|
+
tiny_invariant(params.outCoinType === SUI_FULL_TYPE || params.outCoinType === SUI_TYPE, "Output must be SUI");
|
|
963
|
+
const memezFun = await getMoveObjectContent(params.poolId);
|
|
964
|
+
const { tx, coinData } = await getSplitCoinForTx({
|
|
965
|
+
amount: params.inputAmount,
|
|
966
|
+
coinType: params.inCoinType,
|
|
967
|
+
inheritTx: params.extendTx?.tx,
|
|
968
|
+
account: params.accountAddress,
|
|
969
|
+
splits: [
|
|
970
|
+
params.inputAmount
|
|
971
|
+
]
|
|
972
|
+
});
|
|
973
|
+
const [inputCoin] = coinData;
|
|
974
|
+
const [suiCoin] = tx.moveCall({
|
|
975
|
+
target: `${constants_BlastFunConstants.PACKAGE_ID_V4}::memez_pump::dump`,
|
|
976
|
+
typeArguments: [
|
|
977
|
+
params.inCoinType,
|
|
978
|
+
params.outCoinType
|
|
979
|
+
],
|
|
980
|
+
arguments: [
|
|
981
|
+
tx.object(params.poolId),
|
|
982
|
+
tx.object(memezFun.ipx_meme_coin_treasury),
|
|
983
|
+
inputCoin,
|
|
984
|
+
tx.pure.vector("address", []),
|
|
985
|
+
tx.pure.u64(params.minAmountOut),
|
|
986
|
+
await getAllowedVersions(tx)
|
|
987
|
+
]
|
|
988
|
+
});
|
|
989
|
+
return await splitSuiCoinAfterFeeFromSellTx(tx, params, suiCoin);
|
|
990
|
+
}
|
|
991
|
+
BlastFunCustomTransaction.buildSellTx = buildSellTx;
|
|
992
|
+
})(custom_transaction_BlastFunCustomTransaction || (custom_transaction_BlastFunCustomTransaction = {}));
|
|
993
|
+
var custom_transaction_BlastFunCustomTransaction;
|
|
994
|
+
const payload = {
|
|
995
|
+
network: Network.MAINNET,
|
|
996
|
+
fullNodeUrl: getFullnodeUrl("mainnet")
|
|
997
|
+
};
|
|
998
|
+
const memezPump = new MemezPumpSDK(payload);
|
|
999
|
+
function getMemezPumpSDK() {
|
|
1000
|
+
return memezPump;
|
|
1001
|
+
}
|
|
1002
|
+
(function(BlastFunSDKCalculation) {
|
|
1003
|
+
async function getCurveAmountAfterBuy(params) {
|
|
1004
|
+
let { suiAmount, poolId, frontendFee } = params;
|
|
1005
|
+
suiAmount = getAmountAfterFee(suiAmount, frontendFee);
|
|
1006
|
+
const { memeAmountOut } = await getMemezPumpSDK().quotePump({
|
|
1007
|
+
pool: poolId,
|
|
1008
|
+
amount: suiAmount
|
|
1009
|
+
});
|
|
1010
|
+
return memeAmountOut;
|
|
1011
|
+
}
|
|
1012
|
+
BlastFunSDKCalculation.getCurveAmountAfterBuy = getCurveAmountAfterBuy;
|
|
1013
|
+
async function getSuiAmountAfterSell(params) {
|
|
1014
|
+
const { curveAmount, poolId, frontendFee } = params;
|
|
1015
|
+
const { quoteAmountOut } = await getMemezPumpSDK().quoteDump({
|
|
1016
|
+
pool: poolId,
|
|
1017
|
+
amount: curveAmount
|
|
1018
|
+
});
|
|
1019
|
+
return getAmountAfterFee(quoteAmountOut, frontendFee);
|
|
1020
|
+
}
|
|
1021
|
+
BlastFunSDKCalculation.getSuiAmountAfterSell = getSuiAmountAfterSell;
|
|
1022
|
+
})(sdk_calculation_BlastFunSDKCalculation || (sdk_calculation_BlastFunSDKCalculation = {}));
|
|
1023
|
+
var sdk_calculation_BlastFunSDKCalculation;
|
|
1024
|
+
(function(BlastFunSDKTransaction) {
|
|
1025
|
+
async function buildBuyTx(params) {
|
|
1026
|
+
const { tx, coinOut: suiCoinIn } = await splitSuiCoinAfterFeeFromBuyTx(params);
|
|
1027
|
+
tiny_invariant(suiCoinIn, "Coin out is required");
|
|
1028
|
+
const { memeCoin, tx: tx2 } = await getMemezPumpSDK().pump({
|
|
1029
|
+
pool: params.poolId,
|
|
1030
|
+
quoteCoin: suiCoinIn,
|
|
1031
|
+
minAmountOut: params.minAmountOut,
|
|
1032
|
+
tx
|
|
1033
|
+
});
|
|
1034
|
+
return {
|
|
1035
|
+
tx: tx2,
|
|
1036
|
+
coinOut: memeCoin
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
BlastFunSDKTransaction.buildBuyTx = buildBuyTx;
|
|
1040
|
+
async function buildSellTx(params) {
|
|
1041
|
+
tiny_invariant(params.outCoinType === SUI_FULL_TYPE || params.outCoinType === SUI_TYPE, "Output must be SUI");
|
|
1042
|
+
const { tx, coinData } = await getSplitCoinForTx({
|
|
1043
|
+
amount: params.inputAmount,
|
|
1044
|
+
coinType: params.inCoinType,
|
|
1045
|
+
inheritTx: params.extendTx?.tx,
|
|
1046
|
+
account: params.accountAddress,
|
|
1047
|
+
splits: [
|
|
1048
|
+
params.inputAmount
|
|
1049
|
+
]
|
|
1050
|
+
});
|
|
1051
|
+
const [inputCoin] = coinData;
|
|
1052
|
+
const { quoteCoin, tx: tx2 } = await getMemezPumpSDK().dump({
|
|
1053
|
+
pool: params.poolId,
|
|
1054
|
+
memeCoin: inputCoin,
|
|
1055
|
+
tx,
|
|
1056
|
+
minAmountOut: params.minAmountOut
|
|
1057
|
+
});
|
|
1058
|
+
return await splitSuiCoinAfterFeeFromSellTx(tx2, params, quoteCoin);
|
|
1059
|
+
}
|
|
1060
|
+
BlastFunSDKTransaction.buildSellTx = buildSellTx;
|
|
1061
|
+
async function getBuyTransaction({ coinInAmount, coinInType, coinOutType, poolId, slippage, walletAddress, tradeFee }, suiClient) {
|
|
1062
|
+
tiny_invariant(coinInType === SUI_TYPE, "coinInType must be SUI for buy transaction");
|
|
1063
|
+
tiny_invariant(coinOutType !== coinInType, "coinOutType must be different from coinInType");
|
|
1064
|
+
tiny_invariant(slippage >= 0 && slippage < 1, "slippage must be between 0 (inclusive) and 1 (exclusive)");
|
|
1065
|
+
tiny_invariant(coinInAmount > 0, "coinInAmount must be greater than 0");
|
|
1066
|
+
const inputAmount = coinInAmount;
|
|
1067
|
+
const outputAmount = await sdk_calculation_BlastFunSDKCalculation.getCurveAmountAfterBuy({
|
|
1068
|
+
poolId: poolId,
|
|
1069
|
+
suiAmount: inputAmount,
|
|
1070
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0
|
|
1071
|
+
});
|
|
1072
|
+
const minAmountOut = BigInt(new bignumber(outputAmount.toString()).multipliedBy(1 - slippage).toFixed(0, 3));
|
|
1073
|
+
let { tx, coinOut } = await buildBuyTx({
|
|
1074
|
+
accountAddress: walletAddress,
|
|
1075
|
+
inCoinType: coinInType,
|
|
1076
|
+
outCoinType: coinOutType,
|
|
1077
|
+
inputAmount: inputAmount.toString(),
|
|
1078
|
+
minAmountOut: minAmountOut.toString(),
|
|
1079
|
+
poolId: poolId,
|
|
1080
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0,
|
|
1081
|
+
frontendFeeRecipient: tradeFee ? tradeFee.tradeFeeRecipientAddress : void 0,
|
|
1082
|
+
extendTx: void 0,
|
|
1083
|
+
isSponsored: false
|
|
1084
|
+
});
|
|
1085
|
+
tiny_invariant(coinOut, "Coin out is required");
|
|
1086
|
+
tx.transferObjects([
|
|
1087
|
+
coinOut
|
|
1088
|
+
], tx.pure.address(walletAddress));
|
|
1089
|
+
tx.setSender(walletAddress);
|
|
1090
|
+
const feeAmount = await getNeededGasFee(tx, walletAddress, 0);
|
|
1091
|
+
tx = await addGasFee({
|
|
1092
|
+
inheritTx: tx,
|
|
1093
|
+
sender: walletAddress,
|
|
1094
|
+
feeAmount,
|
|
1095
|
+
suiInputAmount: inputAmount
|
|
1096
|
+
});
|
|
1097
|
+
return tx.build({
|
|
1098
|
+
client: suiClient
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
BlastFunSDKTransaction.getBuyTransaction = getBuyTransaction;
|
|
1102
|
+
async function getSellTransaction({ coinInAmount, coinInType, coinOutType, poolId, slippage, walletAddress, tradeFee }, suiClient) {
|
|
1103
|
+
tiny_invariant(coinOutType === SUI_TYPE, "coinOutType must be SUI for sell transaction");
|
|
1104
|
+
tiny_invariant(coinOutType !== coinInType, "coinOutType must be different from coinInType");
|
|
1105
|
+
tiny_invariant(slippage >= 0 && slippage < 1, "slippage must be between 0 (inclusive) and 1 (exclusive)");
|
|
1106
|
+
tiny_invariant(coinInAmount > 0, "coinInAmount must be greater than 0");
|
|
1107
|
+
const inputAmount = coinInAmount;
|
|
1108
|
+
const outputAmount = await sdk_calculation_BlastFunSDKCalculation.getSuiAmountAfterSell({
|
|
1109
|
+
poolId: poolId,
|
|
1110
|
+
curveAmount: inputAmount,
|
|
1111
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0
|
|
1112
|
+
});
|
|
1113
|
+
const minAmountOut = BigInt(new bignumber(outputAmount.toString()).multipliedBy(1 - slippage).toFixed(0, 3));
|
|
1114
|
+
let { tx, coinOut } = await buildSellTx({
|
|
1115
|
+
accountAddress: walletAddress,
|
|
1116
|
+
inCoinType: coinInType,
|
|
1117
|
+
outCoinType: coinOutType,
|
|
1118
|
+
inputAmount: inputAmount.toString(),
|
|
1119
|
+
minAmountOut: minAmountOut.toString(),
|
|
1120
|
+
poolId: poolId,
|
|
1121
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0,
|
|
1122
|
+
frontendFeeRecipient: tradeFee ? tradeFee.tradeFeeRecipientAddress : void 0,
|
|
1123
|
+
extendTx: void 0,
|
|
1124
|
+
isSponsored: false
|
|
1125
|
+
});
|
|
1126
|
+
tiny_invariant(coinOut, "Coin out is required");
|
|
1127
|
+
tx.transferObjects([
|
|
1128
|
+
coinOut
|
|
1129
|
+
], tx.pure.address(walletAddress));
|
|
1130
|
+
tx.setSender(walletAddress);
|
|
1131
|
+
const feeAmount = await getNeededGasFee(tx, walletAddress, 0);
|
|
1132
|
+
tx = await addGasFee({
|
|
1133
|
+
inheritTx: tx,
|
|
1134
|
+
sender: walletAddress,
|
|
1135
|
+
feeAmount
|
|
1136
|
+
});
|
|
1137
|
+
return tx.build({
|
|
1138
|
+
client: suiClient
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
BlastFunSDKTransaction.getSellTransaction = getSellTransaction;
|
|
1142
|
+
})(sdk_transaction_BlastFunSDKTransaction || (sdk_transaction_BlastFunSDKTransaction = {}));
|
|
1143
|
+
var sdk_transaction_BlastFunSDKTransaction;
|
|
1144
|
+
(function(MoonbagsConstants) {
|
|
1145
|
+
MoonbagsConstants.PACKAGE_ID = "0xb8df325010942634a4afb3db3901ee215546af43a4ec4af781e7213b0bba7290";
|
|
1146
|
+
MoonbagsConstants.CONFIG_OBJECT_ID = "0x74aecf86067c6913960ba4925333aefd2b1f929cafca7e21fd55a8f244b70499";
|
|
1147
|
+
MoonbagsConstants.LOCK_CONFIG_OBJECT_ID = "0xfb09822d9808980abd04c51321adb850701f5f55535c6206658ef4d910c3e9be";
|
|
1148
|
+
MoonbagsConstants.BURN_MANAGER_OBJECT_ID = "0x1d94aa32518d0cb00f9de6ed60d450c9a2090761f326752ffad06b2e9404f845";
|
|
1149
|
+
MoonbagsConstants.POOLS_OBJECT_ID = "0xf699e7f2276f5c9a75944b37a0c5b5d9ddfd2471bf6242483b03ab2887d198d0";
|
|
1150
|
+
MoonbagsConstants.GLOBAL_CONFIG_OBJECT_ID = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
|
|
1151
|
+
MoonbagsConstants.FEE_DENOMINATOR = BigInt(10000);
|
|
1152
|
+
})(constants_MoonbagsConstants || (constants_MoonbagsConstants = {}));
|
|
1153
|
+
var constants_MoonbagsConstants;
|
|
1154
|
+
(function(MoonbagsPackage) {
|
|
1155
|
+
(function(Curves) {
|
|
1156
|
+
function calculateAddLiquidityCost(tokenIn, tokenReserve, tokenAmount) {
|
|
1157
|
+
const availableReserve = tokenReserve - tokenAmount;
|
|
1158
|
+
const numerator = tokenIn * tokenReserve;
|
|
1159
|
+
const fraction = numerator / availableReserve;
|
|
1160
|
+
return fraction - tokenIn;
|
|
1161
|
+
}
|
|
1162
|
+
Curves.calculateAddLiquidityCost = calculateAddLiquidityCost;
|
|
1163
|
+
function calculateRemoveLiquidityReturn(tokenIn, tokenReserve, tokenRemoved) {
|
|
1164
|
+
const denominator = tokenIn + tokenRemoved;
|
|
1165
|
+
const fraction = tokenIn * tokenReserve / denominator;
|
|
1166
|
+
return tokenReserve - fraction;
|
|
1167
|
+
}
|
|
1168
|
+
Curves.calculateRemoveLiquidityReturn = calculateRemoveLiquidityReturn;
|
|
1169
|
+
function calculateTokenAmountReceived(tokenIn, tokenReserve, tokenOutReserve) {
|
|
1170
|
+
const denominator = tokenIn + tokenOutReserve;
|
|
1171
|
+
const fraction = tokenIn * tokenReserve / denominator;
|
|
1172
|
+
return tokenReserve - fraction;
|
|
1173
|
+
}
|
|
1174
|
+
Curves.calculateTokenAmountReceived = calculateTokenAmountReceived;
|
|
1175
|
+
})(MoonbagsPackage.Curves || (MoonbagsPackage.Curves = {}));
|
|
1176
|
+
(function(Moonbags) {
|
|
1177
|
+
async function getDynamicVirtualTokenReserves(poolId) {
|
|
1178
|
+
const result = await getSuiClient().getDynamicFieldObject({
|
|
1179
|
+
parentId: poolId,
|
|
1180
|
+
name: {
|
|
1181
|
+
type: "vector<u8>",
|
|
1182
|
+
value: "virtual_token_reserves"
|
|
1183
|
+
}
|
|
1184
|
+
});
|
|
1185
|
+
if (!result.data) return;
|
|
1186
|
+
return BigInt(result.data.content.fields.value);
|
|
1187
|
+
}
|
|
1188
|
+
Moonbags.getDynamicVirtualTokenReserves = getDynamicVirtualTokenReserves;
|
|
1189
|
+
function buyExactInReturnsWithLock(params) {
|
|
1190
|
+
const { inputAmount, amountIn, pool, config, dynamicVirtualTokenReserves } = params;
|
|
1191
|
+
const virtualSuiReserves = BigInt(pool.virtual_sui_reserves);
|
|
1192
|
+
const virtualTokenReserves = BigInt(pool.virtual_token_reserves);
|
|
1193
|
+
const remainTokenReserves = BigInt(pool.remain_token_reserves.fields.balance);
|
|
1194
|
+
const expectedOut = MoonbagsPackage.Curves.calculateRemoveLiquidityReturn(virtualSuiReserves, virtualTokenReserves, amountIn);
|
|
1195
|
+
const availableOut = virtualTokenReserves - (dynamicVirtualTokenReserves || remainTokenReserves);
|
|
1196
|
+
let finalOut = expectedOut;
|
|
1197
|
+
let usedSui = amountIn;
|
|
1198
|
+
if (expectedOut > availableOut) {
|
|
1199
|
+
finalOut = availableOut;
|
|
1200
|
+
usedSui = MoonbagsPackage.Curves.calculateAddLiquidityCost(virtualSuiReserves, virtualTokenReserves, availableOut) + BigInt(1);
|
|
1201
|
+
}
|
|
1202
|
+
const feeAmount = usedSui * BigInt(config.platform_fee) / constants_MoonbagsConstants.FEE_DENOMINATOR;
|
|
1203
|
+
tiny_invariant(inputAmount >= usedSui + feeAmount, "Input amount is less than used SUI + fee");
|
|
1204
|
+
return {
|
|
1205
|
+
receivedCurve: finalOut,
|
|
1206
|
+
receivedSui: inputAmount - usedSui - feeAmount
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
Moonbags.buyExactInReturnsWithLock = buyExactInReturnsWithLock;
|
|
1210
|
+
function sellReturns(params) {
|
|
1211
|
+
const { amountIn, pool, config } = params;
|
|
1212
|
+
const virtualSuiReserves = BigInt(pool.virtual_sui_reserves);
|
|
1213
|
+
const virtualTokenReserves = BigInt(pool.virtual_token_reserves);
|
|
1214
|
+
const realSuiReserves = BigInt(pool.real_sui_reserves.fields.balance);
|
|
1215
|
+
let rawSuiOut = MoonbagsPackage.Curves.calculateRemoveLiquidityReturn(virtualTokenReserves, virtualSuiReserves, amountIn);
|
|
1216
|
+
if (rawSuiOut > realSuiReserves) rawSuiOut = realSuiReserves;
|
|
1217
|
+
const feeAmount = rawSuiOut * BigInt(config.platform_fee) / constants_MoonbagsConstants.FEE_DENOMINATOR;
|
|
1218
|
+
const finalSuiOut = rawSuiOut - feeAmount;
|
|
1219
|
+
return {
|
|
1220
|
+
receivedCurve: BigInt(0),
|
|
1221
|
+
receivedSui: finalSuiOut
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1224
|
+
Moonbags.sellReturns = sellReturns;
|
|
1225
|
+
})(MoonbagsPackage.Moonbags || (MoonbagsPackage.Moonbags = {}));
|
|
1226
|
+
})(package_MoonbagsPackage || (package_MoonbagsPackage = {}));
|
|
1227
|
+
var package_MoonbagsPackage;
|
|
1228
|
+
(function(MoonbagsCalculation) {
|
|
1229
|
+
async function getCurveAmountAfterBuy(params) {
|
|
1230
|
+
let { suiAmount, poolId, frontendFee } = params;
|
|
1231
|
+
const [poolMoveStruct, configMoveStruct, dynamicVirtualTokenReserves] = await Promise.all([
|
|
1232
|
+
getMoveObjectContent(poolId),
|
|
1233
|
+
getMoveObjectContent(constants_MoonbagsConstants.CONFIG_OBJECT_ID),
|
|
1234
|
+
package_MoonbagsPackage.Moonbags.getDynamicVirtualTokenReserves(poolId)
|
|
1235
|
+
]);
|
|
1236
|
+
const pool = poolMoveStruct;
|
|
1237
|
+
const config = configMoveStruct;
|
|
1238
|
+
if (void 0 !== frontendFee) suiAmount = getAmountAfterFee(suiAmount, frontendFee);
|
|
1239
|
+
const usedSui = suiAmount * constants_MoonbagsConstants.FEE_DENOMINATOR / (constants_MoonbagsConstants.FEE_DENOMINATOR + BigInt(config.platform_fee));
|
|
1240
|
+
const { receivedCurve } = package_MoonbagsPackage.Moonbags.buyExactInReturnsWithLock({
|
|
1241
|
+
inputAmount: suiAmount,
|
|
1242
|
+
amountIn: usedSui,
|
|
1243
|
+
pool,
|
|
1244
|
+
config,
|
|
1245
|
+
dynamicVirtualTokenReserves
|
|
1246
|
+
});
|
|
1247
|
+
return receivedCurve;
|
|
1248
|
+
}
|
|
1249
|
+
MoonbagsCalculation.getCurveAmountAfterBuy = getCurveAmountAfterBuy;
|
|
1250
|
+
async function getSuiAmountAfterSell(params) {
|
|
1251
|
+
const { curveAmount, poolId, frontendFee } = params;
|
|
1252
|
+
const [poolMoveStruct, configMoveStruct] = await Promise.all([
|
|
1253
|
+
getMoveObjectContent(poolId),
|
|
1254
|
+
getMoveObjectContent(constants_MoonbagsConstants.CONFIG_OBJECT_ID)
|
|
1255
|
+
]);
|
|
1256
|
+
const pool = poolMoveStruct;
|
|
1257
|
+
const config = configMoveStruct;
|
|
1258
|
+
const { receivedSui } = package_MoonbagsPackage.Moonbags.sellReturns({
|
|
1259
|
+
amountIn: curveAmount,
|
|
1260
|
+
pool,
|
|
1261
|
+
config
|
|
1262
|
+
});
|
|
1263
|
+
if (void 0 !== frontendFee) return getAmountAfterFee(receivedSui, frontendFee);
|
|
1264
|
+
return receivedSui;
|
|
1265
|
+
}
|
|
1266
|
+
MoonbagsCalculation.getSuiAmountAfterSell = getSuiAmountAfterSell;
|
|
1267
|
+
async function getUsedSuiForTx(params) {
|
|
1268
|
+
let { suiAmount, frontendFee } = params;
|
|
1269
|
+
const configMoveStruct = await getMoveObjectContent(constants_MoonbagsConstants.CONFIG_OBJECT_ID);
|
|
1270
|
+
const config = configMoveStruct;
|
|
1271
|
+
if (void 0 !== frontendFee) suiAmount = getAmountAfterFee(suiAmount, frontendFee);
|
|
1272
|
+
return suiAmount * constants_MoonbagsConstants.FEE_DENOMINATOR / (constants_MoonbagsConstants.FEE_DENOMINATOR + BigInt(config.platform_fee));
|
|
1273
|
+
}
|
|
1274
|
+
MoonbagsCalculation.getUsedSuiForTx = getUsedSuiForTx;
|
|
1275
|
+
})(calculation_MoonbagsCalculation || (calculation_MoonbagsCalculation = {}));
|
|
1276
|
+
var calculation_MoonbagsCalculation;
|
|
1277
|
+
(function(MoonbagsTransaction) {
|
|
1278
|
+
async function buildBuyTx(params) {
|
|
1279
|
+
const { tx, coinOut } = await splitSuiCoinAfterFeeFromBuyTx(params);
|
|
1280
|
+
tiny_invariant(coinOut, "Coin out is required");
|
|
1281
|
+
const usedSui = await calculation_MoonbagsCalculation.getUsedSuiForTx({
|
|
1282
|
+
suiAmount: BigInt(params.inputAmount),
|
|
1283
|
+
frontendFee: params.frontendFee
|
|
1284
|
+
});
|
|
1285
|
+
const [suiCoin, curveCoin] = tx.moveCall({
|
|
1286
|
+
target: `${constants_MoonbagsConstants.PACKAGE_ID}::moonbags::buy_exact_in_returns_with_lock`,
|
|
1287
|
+
typeArguments: [
|
|
1288
|
+
params.outCoinType
|
|
1289
|
+
],
|
|
1290
|
+
arguments: [
|
|
1291
|
+
tx.object(constants_MoonbagsConstants.CONFIG_OBJECT_ID),
|
|
1292
|
+
tx.object(constants_MoonbagsConstants.LOCK_CONFIG_OBJECT_ID),
|
|
1293
|
+
coinOut,
|
|
1294
|
+
tx.pure.u64(usedSui),
|
|
1295
|
+
tx.pure.u64(params.minAmountOut),
|
|
1296
|
+
tx.object(constants_MoonbagsConstants.BURN_MANAGER_OBJECT_ID),
|
|
1297
|
+
tx.object(constants_MoonbagsConstants.POOLS_OBJECT_ID),
|
|
1298
|
+
tx.object(constants_MoonbagsConstants.GLOBAL_CONFIG_OBJECT_ID),
|
|
1299
|
+
tx.object(SUI_METADATA_OBJECT_ID),
|
|
1300
|
+
tx.object(CLOCK_OBJECT_ID)
|
|
1301
|
+
]
|
|
1302
|
+
});
|
|
1303
|
+
bluefin_BluefinTx.transferOrDestroyZeroCoin(tx, SUI_FULL_TYPE, suiCoin, params.accountAddress);
|
|
1304
|
+
return {
|
|
1305
|
+
tx,
|
|
1306
|
+
coinOut: curveCoin
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
MoonbagsTransaction.buildBuyTx = buildBuyTx;
|
|
1310
|
+
async function buildSellTx(params) {
|
|
1311
|
+
tiny_invariant(params.outCoinType === SUI_FULL_TYPE || params.outCoinType === SUI_TYPE, "Output must be SUI");
|
|
1312
|
+
const { tx, coinData } = await getSplitCoinForTx({
|
|
1313
|
+
amount: params.inputAmount,
|
|
1314
|
+
coinType: params.inCoinType,
|
|
1315
|
+
inheritTx: params.extendTx?.tx,
|
|
1316
|
+
account: params.accountAddress,
|
|
1317
|
+
splits: [
|
|
1318
|
+
params.inputAmount
|
|
1319
|
+
]
|
|
1320
|
+
});
|
|
1321
|
+
const [inputCoin] = coinData;
|
|
1322
|
+
const [suiCoin, curveCoin] = tx.moveCall({
|
|
1323
|
+
target: `${constants_MoonbagsConstants.PACKAGE_ID}::moonbags::sell_returns`,
|
|
1324
|
+
typeArguments: [
|
|
1325
|
+
params.inCoinType
|
|
1326
|
+
],
|
|
1327
|
+
arguments: [
|
|
1328
|
+
tx.object(constants_MoonbagsConstants.CONFIG_OBJECT_ID),
|
|
1329
|
+
inputCoin,
|
|
1330
|
+
tx.pure.u64(params.minAmountOut),
|
|
1331
|
+
tx.object(CLOCK_OBJECT_ID)
|
|
1332
|
+
]
|
|
1333
|
+
});
|
|
1334
|
+
bluefin_BluefinTx.transferOrDestroyZeroCoin(tx, params.inCoinType, curveCoin, params.accountAddress);
|
|
1335
|
+
return await splitSuiCoinAfterFeeFromSellTx(tx, params, suiCoin);
|
|
1336
|
+
}
|
|
1337
|
+
MoonbagsTransaction.buildSellTx = buildSellTx;
|
|
1338
|
+
async function getBuyTransaction({ coinInAmount, coinInType, coinOutType, poolId, slippage, walletAddress, tradeFee }, suiClient) {
|
|
1339
|
+
tiny_invariant(coinInType === SUI_TYPE, "coinInType must be SUI for buy transaction");
|
|
1340
|
+
tiny_invariant(coinOutType !== coinInType, "coinOutType must be different from coinInType");
|
|
1341
|
+
tiny_invariant(slippage >= 0 && slippage < 1, "slippage must be between 0 (inclusive) and 1 (exclusive)");
|
|
1342
|
+
tiny_invariant(coinInAmount > 0, "coinInAmount must be greater than 0");
|
|
1343
|
+
const inputAmount = coinInAmount;
|
|
1344
|
+
const outputAmount = await calculation_MoonbagsCalculation.getCurveAmountAfterBuy({
|
|
1345
|
+
poolId: poolId,
|
|
1346
|
+
suiAmount: inputAmount,
|
|
1347
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0
|
|
1348
|
+
});
|
|
1349
|
+
const minAmountOut = BigInt(new bignumber(outputAmount.toString()).multipliedBy(1 - slippage).toFixed(0, 3));
|
|
1350
|
+
let { tx, coinOut } = await buildBuyTx({
|
|
1351
|
+
accountAddress: walletAddress,
|
|
1352
|
+
inCoinType: coinInType,
|
|
1353
|
+
outCoinType: coinOutType,
|
|
1354
|
+
inputAmount: inputAmount.toString(),
|
|
1355
|
+
minAmountOut: minAmountOut.toString(),
|
|
1356
|
+
poolId: poolId,
|
|
1357
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0,
|
|
1358
|
+
frontendFeeRecipient: tradeFee ? tradeFee.tradeFeeRecipientAddress : void 0,
|
|
1359
|
+
extendTx: void 0,
|
|
1360
|
+
isSponsored: false
|
|
1361
|
+
});
|
|
1362
|
+
tiny_invariant(coinOut, "Coin out is required");
|
|
1363
|
+
tx.transferObjects([
|
|
1364
|
+
coinOut
|
|
1365
|
+
], tx.pure.address(walletAddress));
|
|
1366
|
+
tx.setSender(walletAddress);
|
|
1367
|
+
const feeAmount = await getNeededGasFee(tx, walletAddress, 0);
|
|
1368
|
+
tx = await addGasFee({
|
|
1369
|
+
inheritTx: tx,
|
|
1370
|
+
sender: walletAddress,
|
|
1371
|
+
feeAmount,
|
|
1372
|
+
suiInputAmount: inputAmount
|
|
1373
|
+
});
|
|
1374
|
+
return tx.build({
|
|
1375
|
+
client: suiClient
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
MoonbagsTransaction.getBuyTransaction = getBuyTransaction;
|
|
1379
|
+
async function getSellTransaction({ coinInAmount, coinInType, coinOutType, poolId, slippage, walletAddress, tradeFee }, suiClient) {
|
|
1380
|
+
tiny_invariant(coinOutType === SUI_TYPE, "coinOutType must be SUI for sell transaction");
|
|
1381
|
+
tiny_invariant(coinOutType !== coinInType, "coinOutType must be different from coinInType");
|
|
1382
|
+
tiny_invariant(slippage >= 0 && slippage < 1, "slippage must be between 0 (inclusive) and 1 (exclusive)");
|
|
1383
|
+
tiny_invariant(coinInAmount > 0, "coinInAmount must be greater than 0");
|
|
1384
|
+
const inputAmount = coinInAmount;
|
|
1385
|
+
const outputAmount = await calculation_MoonbagsCalculation.getSuiAmountAfterSell({
|
|
1386
|
+
poolId: poolId,
|
|
1387
|
+
curveAmount: inputAmount,
|
|
1388
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0
|
|
1389
|
+
});
|
|
1390
|
+
const minAmountOut = BigInt(new bignumber(outputAmount.toString()).multipliedBy(1 - slippage).toFixed(0, 3));
|
|
1391
|
+
let { tx, coinOut } = await buildSellTx({
|
|
1392
|
+
accountAddress: walletAddress,
|
|
1393
|
+
inCoinType: coinInType,
|
|
1394
|
+
outCoinType: coinOutType,
|
|
1395
|
+
inputAmount: inputAmount.toString(),
|
|
1396
|
+
minAmountOut: minAmountOut.toString(),
|
|
1397
|
+
poolId: poolId,
|
|
1398
|
+
frontendFee: tradeFee ? tradeFee.tradeFeePercent : void 0,
|
|
1399
|
+
frontendFeeRecipient: tradeFee ? tradeFee.tradeFeeRecipientAddress : void 0,
|
|
1400
|
+
extendTx: void 0,
|
|
1401
|
+
isSponsored: false
|
|
1402
|
+
});
|
|
1403
|
+
tiny_invariant(coinOut, "Coin out is required");
|
|
1404
|
+
tx.transferObjects([
|
|
1405
|
+
coinOut
|
|
1406
|
+
], tx.pure.address(walletAddress));
|
|
1407
|
+
tx.setSender(walletAddress);
|
|
1408
|
+
const feeAmount = await getNeededGasFee(tx, walletAddress, 0);
|
|
1409
|
+
tx = await addGasFee({
|
|
1410
|
+
inheritTx: tx,
|
|
1411
|
+
sender: walletAddress,
|
|
1412
|
+
feeAmount
|
|
1413
|
+
});
|
|
1414
|
+
return tx.build({
|
|
1415
|
+
client: suiClient
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
MoonbagsTransaction.getSellTransaction = getSellTransaction;
|
|
1419
|
+
})(transaction_MoonbagsTransaction || (transaction_MoonbagsTransaction = {}));
|
|
1420
|
+
var transaction_MoonbagsTransaction;
|
|
1421
|
+
export { aftermath_AftermathAggregator as AftermathAggregator, astros_AstrosAggregator as AstrosAggregator, BASE_BPS, BLUEFIN_PACKAGE_ID, constants_BlastFunConstants as BlastFunConstants, custom_calculation_BlastFunCustomCalculation as BlastFunCustomCalculation, custom_transaction_BlastFunCustomTransaction as BlastFunCustomTransaction, package_BlastFunPackage as BlastFunPackage, sdk_calculation_BlastFunSDKCalculation as BlastFunSDKCalculation, sdk_transaction_BlastFunSDKTransaction as BlastFunSDKTransaction, bluefin_BluefinTx as BluefinTx, package_Bps as Bps, CLOCK_OBJECT_ID, cetus_CetusAggregator as CetusAggregator, flowx_FlowXAggregator as FlowXAggregator, package_MathUtils as MathUtils, calculation_MoonbagsCalculation as MoonbagsCalculation, constants_MoonbagsConstants as MoonbagsConstants, package_MoonbagsPackage as MoonbagsPackage, transaction_MoonbagsTransaction as MoonbagsTransaction, NATIVE_USDC_TOKEN_TYPE, SUI_FULL_TYPE, SUI_METADATA_OBJECT_ID, SUI_TYPE, _7k_SevenKAggregator as SevenKAggregator, common_SupportedAggregator as SupportedAggregator, types_SupportedBondingCurve as SupportedBondingCurve, types_TradeFeeOptions as TradeFeeOptions, USDC_TOKEN_TYPE, adaptSuiClient, adaptTransaction, addGasFee, createCompatibleSuiClient, getAmountAfterFee, getCoinObjectIdsByAmount, getMemezPumpSDK, getMoveObject, getMoveObjectContent, getNeededGasFee, getSplitCoinForTx, getSplitCoinsAfterFee, getSuiClient, splitSuiCoinAfterFeeFromBuyTx, splitSuiCoinAfterFeeFromSellTx };
|