@7kprotocol/sdk-ts 3.5.1 → 3.5.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/config/index.js +9 -0
- package/lib/cjs/features/metaAg/common.js +117 -0
- package/lib/cjs/features/metaAg/index.js +88 -93
- package/lib/cjs/features/metaAg/providers/astro.js +42 -0
- package/lib/cjs/features/metaAg/providers/bluefin.js +6 -6
- package/lib/cjs/features/metaAg/providers/bluefinx.js +87 -0
- package/lib/cjs/features/metaAg/providers/cetus.js +4 -4
- package/lib/cjs/features/metaAg/providers/flowx.js +4 -4
- package/lib/cjs/features/metaAg/providers/okx.js +181 -0
- package/lib/cjs/features/swap/buildTx.js +3 -0
- package/lib/cjs/features/swap/buildTxV2.js +10 -6
- package/lib/cjs/features/swap/config.js +2 -1
- package/lib/cjs/features/swap/getQuote.js +3 -2
- package/lib/cjs/libs/protocols/bluefinx/client.js +1 -1
- package/lib/cjs/types/config/index.d.ts +4 -0
- package/lib/cjs/types/config/index.d.ts.map +1 -1
- package/lib/cjs/types/features/metaAg/common.d.ts +33 -0
- package/lib/cjs/types/features/metaAg/common.d.ts.map +1 -0
- package/lib/cjs/types/features/metaAg/index.d.ts +11 -2
- package/lib/cjs/types/features/metaAg/index.d.ts.map +1 -1
- package/lib/cjs/types/features/metaAg/providers/astro.d.ts +9 -0
- package/lib/cjs/types/features/metaAg/providers/astro.d.ts.map +1 -0
- package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts +3 -3
- package/lib/cjs/types/features/metaAg/providers/bluefin.d.ts.map +1 -1
- package/lib/cjs/types/features/metaAg/providers/bluefinx.d.ts +10 -0
- package/lib/cjs/types/features/metaAg/providers/bluefinx.d.ts.map +1 -0
- package/lib/cjs/types/features/metaAg/providers/cetus.d.ts +4 -5
- package/lib/cjs/types/features/metaAg/providers/cetus.d.ts.map +1 -1
- package/lib/cjs/types/features/metaAg/providers/flowx.d.ts +3 -3
- package/lib/cjs/types/features/metaAg/providers/flowx.d.ts.map +1 -1
- package/lib/cjs/types/features/metaAg/providers/okx.d.ts +21 -0
- package/lib/cjs/types/features/metaAg/providers/okx.d.ts.map +1 -0
- package/lib/cjs/types/features/swap/buildTx.d.ts.map +1 -1
- package/lib/cjs/types/features/swap/buildTxV2.d.ts +3 -3
- package/lib/cjs/types/features/swap/buildTxV2.d.ts.map +1 -1
- package/lib/cjs/types/features/swap/config.d.ts.map +1 -1
- package/lib/cjs/types/features/swap/getQuote.d.ts +1 -3
- package/lib/cjs/types/features/swap/getQuote.d.ts.map +1 -1
- package/lib/cjs/types/index.d.ts +3 -1
- package/lib/cjs/types/index.d.ts.map +1 -1
- package/lib/cjs/types/metaAg.js +11 -4
- package/lib/cjs/types/okx.js +6 -0
- package/lib/cjs/types/types/metaAg.d.ts +60 -8
- package/lib/cjs/types/types/metaAg.d.ts.map +1 -1
- package/lib/cjs/types/types/okx.d.ts +193 -0
- package/lib/cjs/types/types/okx.d.ts.map +1 -0
- package/lib/cjs/types/utils/condition.d.ts +1 -1
- package/lib/cjs/types/utils/condition.d.ts.map +1 -1
- package/lib/esm/config/index.js +9 -0
- package/lib/esm/features/metaAg/common.js +109 -0
- package/lib/esm/features/metaAg/index.js +89 -94
- package/lib/esm/features/metaAg/providers/astro.js +39 -0
- package/lib/esm/features/metaAg/providers/bluefin.js +6 -6
- package/lib/esm/features/metaAg/providers/bluefinx.js +84 -0
- package/lib/esm/features/metaAg/providers/cetus.js +4 -4
- package/lib/esm/features/metaAg/providers/flowx.js +4 -4
- package/lib/esm/features/metaAg/providers/okx.js +146 -0
- package/lib/esm/features/swap/buildTx.js +3 -0
- package/lib/esm/features/swap/buildTxV2.js +6 -2
- package/lib/esm/features/swap/config.js +2 -1
- package/lib/esm/features/swap/getQuote.js +3 -2
- package/lib/esm/libs/protocols/bluefinx/client.js +1 -1
- package/lib/esm/types/config/index.d.ts +4 -0
- package/lib/esm/types/config/index.d.ts.map +1 -1
- package/lib/esm/types/features/metaAg/common.d.ts +33 -0
- package/lib/esm/types/features/metaAg/common.d.ts.map +1 -0
- package/lib/esm/types/features/metaAg/index.d.ts +11 -2
- package/lib/esm/types/features/metaAg/index.d.ts.map +1 -1
- package/lib/esm/types/features/metaAg/providers/astro.d.ts +9 -0
- package/lib/esm/types/features/metaAg/providers/astro.d.ts.map +1 -0
- package/lib/esm/types/features/metaAg/providers/bluefin.d.ts +3 -3
- package/lib/esm/types/features/metaAg/providers/bluefin.d.ts.map +1 -1
- package/lib/esm/types/features/metaAg/providers/bluefinx.d.ts +10 -0
- package/lib/esm/types/features/metaAg/providers/bluefinx.d.ts.map +1 -0
- package/lib/esm/types/features/metaAg/providers/cetus.d.ts +4 -5
- package/lib/esm/types/features/metaAg/providers/cetus.d.ts.map +1 -1
- package/lib/esm/types/features/metaAg/providers/flowx.d.ts +3 -3
- package/lib/esm/types/features/metaAg/providers/flowx.d.ts.map +1 -1
- package/lib/esm/types/features/metaAg/providers/okx.d.ts +21 -0
- package/lib/esm/types/features/metaAg/providers/okx.d.ts.map +1 -0
- package/lib/esm/types/features/swap/buildTx.d.ts.map +1 -1
- package/lib/esm/types/features/swap/buildTxV2.d.ts +3 -3
- package/lib/esm/types/features/swap/buildTxV2.d.ts.map +1 -1
- package/lib/esm/types/features/swap/config.d.ts.map +1 -1
- package/lib/esm/types/features/swap/getQuote.d.ts +1 -3
- package/lib/esm/types/features/swap/getQuote.d.ts.map +1 -1
- package/lib/esm/types/index.d.ts +3 -1
- package/lib/esm/types/index.d.ts.map +1 -1
- package/lib/esm/types/metaAg.js +8 -2
- package/lib/esm/types/okx.js +5 -0
- package/lib/esm/types/types/metaAg.d.ts +60 -8
- package/lib/esm/types/types/metaAg.d.ts.map +1 -1
- package/lib/esm/types/types/okx.d.ts +193 -0
- package/lib/esm/types/types/okx.d.ts.map +1 -0
- package/lib/esm/types/utils/condition.d.ts +1 -1
- package/lib/esm/types/utils/condition.d.ts.map +1 -1
- package/package.json +3 -2
package/lib/cjs/config/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
|
|
|
6
6
|
const HERMES_API = "https://hermes.pyth.network";
|
|
7
7
|
const WORMHOLE_STATE_ID = "0xaeab97f96cf9877fee2883315d459552b2b921edc16d7ceac6eab944dd88919c";
|
|
8
8
|
const PYTH_STATE_ID = "0x1f9310238ee9298fb703c3419030b35b22bb1cc37113e3bb5007c99aec79e5b8";
|
|
9
|
+
let api = "";
|
|
9
10
|
let apiKey = "";
|
|
10
11
|
let bluefinXApiKey = "";
|
|
11
12
|
let suiClient = new client_1.SuiClient({
|
|
@@ -13,6 +14,12 @@ let suiClient = new client_1.SuiClient({
|
|
|
13
14
|
});
|
|
14
15
|
let pythClient = new pyth_sui_js_1.SuiPythClient(suiClient, PYTH_STATE_ID, WORMHOLE_STATE_ID);
|
|
15
16
|
let pythConnection = new pyth_sui_js_1.SuiPriceServiceConnection(HERMES_API);
|
|
17
|
+
function setApi(url) {
|
|
18
|
+
api = url;
|
|
19
|
+
}
|
|
20
|
+
function getApi() {
|
|
21
|
+
return api;
|
|
22
|
+
}
|
|
16
23
|
function setApiKey(key) {
|
|
17
24
|
apiKey = key;
|
|
18
25
|
}
|
|
@@ -44,6 +51,8 @@ function getPythConnection() {
|
|
|
44
51
|
return pythConnection;
|
|
45
52
|
}
|
|
46
53
|
const Config = {
|
|
54
|
+
setApi,
|
|
55
|
+
getApi,
|
|
47
56
|
setApiKey,
|
|
48
57
|
getApiKey,
|
|
49
58
|
setBluefinXApiKey,
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.timeout = exports.metaSettle = exports.simulateAggregator = exports.simulateSwapTx = exports.simulateBluefinX = void 0;
|
|
4
|
+
const transactions_1 = require("@mysten/sui/transactions");
|
|
5
|
+
const _7k_1 = require("../../constants/_7k");
|
|
6
|
+
const buildTx_1 = require("../swap/buildTx");
|
|
7
|
+
const DEFAULT_GAS_USED = {
|
|
8
|
+
computationCost: "0",
|
|
9
|
+
nonRefundableStorageFee: "0",
|
|
10
|
+
storageCost: "0",
|
|
11
|
+
storageRebate: "0",
|
|
12
|
+
};
|
|
13
|
+
const simulateBluefinX = (quote) => {
|
|
14
|
+
return {
|
|
15
|
+
id: quote.id,
|
|
16
|
+
simulatedAmountOut: quote.amountOut,
|
|
17
|
+
gasUsed: DEFAULT_GAS_USED,
|
|
18
|
+
provider: quote.provider,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.simulateBluefinX = simulateBluefinX;
|
|
22
|
+
const simulateSwapTx = async (tx, inspector, simulation) => {
|
|
23
|
+
const res = await (0, exports.timeout)(() => inspector.devInspectTransactionBlock({
|
|
24
|
+
sender: simulation.sender,
|
|
25
|
+
transactionBlock: tx,
|
|
26
|
+
}), simulation.timeout ?? 2000);
|
|
27
|
+
if (res.effects.status.status === "failure") {
|
|
28
|
+
throw new Error(res.error ?? "Simulation failed");
|
|
29
|
+
}
|
|
30
|
+
const amountOut = extractAmountOutWrapper(res.events);
|
|
31
|
+
return {
|
|
32
|
+
simulatedAmountOut: amountOut,
|
|
33
|
+
gasUsed: res.effects.gasUsed,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
exports.simulateSwapTx = simulateSwapTx;
|
|
37
|
+
const simulateAggregator = async (provider, quote, simulation, inspector, options) => {
|
|
38
|
+
const tx = new transactions_1.Transaction();
|
|
39
|
+
const coinOut = await provider.swap({
|
|
40
|
+
quote,
|
|
41
|
+
coinIn: (0, transactions_1.coinWithBalance)({
|
|
42
|
+
balance: BigInt(quote.amountIn),
|
|
43
|
+
type: quote.coinTypeIn,
|
|
44
|
+
useGasCoin: false,
|
|
45
|
+
}),
|
|
46
|
+
signer: simulation.sender,
|
|
47
|
+
tx,
|
|
48
|
+
});
|
|
49
|
+
tx.add((0, exports.metaSettle)(quote, coinOut, 10000, options.tipBps, options.partner, options.partnerCommissionBps));
|
|
50
|
+
tx.transferObjects([coinOut], simulation.sender);
|
|
51
|
+
const res = await (0, exports.simulateSwapTx)(tx, inspector, simulation);
|
|
52
|
+
return {
|
|
53
|
+
id: quote.id,
|
|
54
|
+
provider: provider.kind,
|
|
55
|
+
...res,
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
exports.simulateAggregator = simulateAggregator;
|
|
59
|
+
/**
|
|
60
|
+
* this settlement does not charge commission fee for partner, since all integrated aggregators already charge commission fee for partner
|
|
61
|
+
* @param quote Meta Aggregator Quote
|
|
62
|
+
* @param coinOut Coin Out Object
|
|
63
|
+
* @param slippageBps Slippage Bps
|
|
64
|
+
* @param tipBps Tip Bps default = 0
|
|
65
|
+
* @param partner address of partner for analytic default is zero address
|
|
66
|
+
*/
|
|
67
|
+
const metaSettle = (quote, coinOut, slippageBps = 100, tipBps = 0, partner, commissionBps = 0) => {
|
|
68
|
+
return (tx) => {
|
|
69
|
+
const { minAmount, expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, slippageBps, commissionBps, tipBps);
|
|
70
|
+
if (tipBps > 0) {
|
|
71
|
+
tx.moveCall({
|
|
72
|
+
target: `${_7k_1._7K_META_PUBLISHED_AT}::vault::collect_tip`,
|
|
73
|
+
typeArguments: [quote.coinTypeOut],
|
|
74
|
+
arguments: [
|
|
75
|
+
tx.object(_7k_1._7K_META_VAULT),
|
|
76
|
+
tx.object(_7k_1._7K_META_CONFIG),
|
|
77
|
+
coinOut,
|
|
78
|
+
tx.pure.u64(tipBps),
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
tx.moveCall({
|
|
83
|
+
target: `${_7k_1._7K_META_PUBLISHED_AT}::settle::settle`,
|
|
84
|
+
typeArguments: [quote.coinTypeIn, quote.coinTypeOut],
|
|
85
|
+
arguments: [
|
|
86
|
+
tx.object(_7k_1._7K_META_CONFIG),
|
|
87
|
+
tx.object(_7k_1._7K_META_VAULT),
|
|
88
|
+
tx.pure.u64(quote.amountIn),
|
|
89
|
+
coinOut,
|
|
90
|
+
tx.pure.u64(minAmount),
|
|
91
|
+
tx.pure.u64(expectedAmount),
|
|
92
|
+
tx.pure.option("address", partner),
|
|
93
|
+
tx.pure.u64(commissionBps),
|
|
94
|
+
tx.pure.u64(0), // ps
|
|
95
|
+
],
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
exports.metaSettle = metaSettle;
|
|
100
|
+
const extractAmountOutWrapper = (events) => {
|
|
101
|
+
const swapEvent = events
|
|
102
|
+
.filter((event) => event.type === `${_7k_1._7K_META_PACKAGE_ID}::settle::Swap`)
|
|
103
|
+
?.pop();
|
|
104
|
+
return swapEvent?.parsedJson?.amount_out;
|
|
105
|
+
};
|
|
106
|
+
const timeout = async (fn, timeout, msg) => {
|
|
107
|
+
if (timeout <= 0)
|
|
108
|
+
return fn();
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
const timer = setTimeout(() => reject(new Error(`Timeout ${msg ?? "operation"}`)), timeout);
|
|
111
|
+
fn()
|
|
112
|
+
.then(resolve)
|
|
113
|
+
.catch(reject)
|
|
114
|
+
.finally(() => clearTimeout(timer));
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
exports.timeout = timeout;
|
|
@@ -37,18 +37,22 @@ exports.MetaAg = void 0;
|
|
|
37
37
|
const client_1 = require("@mysten/sui/client");
|
|
38
38
|
const transactions_1 = require("@mysten/sui/transactions");
|
|
39
39
|
const utils_1 = require("@mysten/sui/utils");
|
|
40
|
-
const _7k_1 = require("../../constants/_7k");
|
|
41
40
|
const sui_1 = require("../../constants/sui");
|
|
42
41
|
const metaAg_1 = require("../../types/metaAg");
|
|
43
42
|
const condition_1 = require("../../utils/condition");
|
|
44
43
|
const SuiClientUtils_1 = require("../../utils/SuiClientUtils");
|
|
45
44
|
const buildTx_1 = require("../swap/buildTx");
|
|
45
|
+
const common_1 = require("./common");
|
|
46
46
|
const bluefin_1 = require("./providers/bluefin");
|
|
47
|
+
const bluefinx_1 = require("./providers/bluefinx");
|
|
48
|
+
const okx_1 = require("./providers/okx");
|
|
47
49
|
const HERMES_API = "https://hermes.pyth.network";
|
|
48
50
|
const DEFAULT_PROVIDERS = {
|
|
49
51
|
[metaAg_1.EProvider.BLUEFIN7K]: {},
|
|
50
52
|
[metaAg_1.EProvider.FLOWX]: {},
|
|
51
53
|
[metaAg_1.EProvider.CETUS]: {},
|
|
54
|
+
[metaAg_1.EProvider.ASTRO]: {},
|
|
55
|
+
[metaAg_1.EProvider.BLUEFINX]: {},
|
|
52
56
|
};
|
|
53
57
|
class MetaAg {
|
|
54
58
|
constructor(options) {
|
|
@@ -85,6 +89,16 @@ class MetaAg {
|
|
|
85
89
|
const { CetusProvider } = await Promise.resolve().then(() => __importStar(require("./providers/cetus"))).catch(catchImportError(metaAg_1.EProvider.CETUS));
|
|
86
90
|
this.providers[metaAg_1.EProvider.CETUS] = new CetusProvider(providerOptions, this.options, this.client);
|
|
87
91
|
break;
|
|
92
|
+
case metaAg_1.EProvider.OKX:
|
|
93
|
+
this.providers[metaAg_1.EProvider.OKX] = new okx_1.OkxProvider(providerOptions, this.options, this.client);
|
|
94
|
+
break;
|
|
95
|
+
case metaAg_1.EProvider.BLUEFINX:
|
|
96
|
+
this.providers[metaAg_1.EProvider.BLUEFINX] = new bluefinx_1.BluefinXProvider(providerOptions);
|
|
97
|
+
break;
|
|
98
|
+
case metaAg_1.EProvider.ASTRO:
|
|
99
|
+
const { AstroProvider } = await Promise.resolve().then(() => __importStar(require("./providers/astro"))).catch(catchImportError(metaAg_1.EProvider.ASTRO));
|
|
100
|
+
this.providers[metaAg_1.EProvider.ASTRO] = new AstroProvider(providerOptions);
|
|
101
|
+
break;
|
|
88
102
|
default:
|
|
89
103
|
throw new Error(`Provider not supported: ${provider}`);
|
|
90
104
|
}
|
|
@@ -92,51 +106,40 @@ class MetaAg {
|
|
|
92
106
|
}
|
|
93
107
|
async _simulate(provider, quote, simulation) {
|
|
94
108
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
tx,
|
|
106
|
-
});
|
|
107
|
-
tx.add(metaSettle(quote, coinOut, 10000, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
|
|
108
|
-
tx.transferObjects([coinOut], simulation.sender);
|
|
109
|
-
const res = await timeout(() => this.inspector.devInspectTransactionBlock({
|
|
110
|
-
sender: simulation.sender,
|
|
111
|
-
transactionBlock: tx,
|
|
112
|
-
}), simulation.timeout ?? 2000, `simulation for ${provider.kind} provider with id ${id}`);
|
|
113
|
-
if (res.effects.status.status === "failure") {
|
|
114
|
-
throw new Error(res.error ?? "Simulation failed");
|
|
109
|
+
if ((0, metaAg_1.isAggregatorProvider)(provider)) {
|
|
110
|
+
return (0, common_1.simulateAggregator)(provider, quote, simulation, this.inspector, this.options);
|
|
111
|
+
}
|
|
112
|
+
switch (quote.provider) {
|
|
113
|
+
case metaAg_1.EProvider.BLUEFINX:
|
|
114
|
+
return (0, common_1.simulateBluefinX)(quote);
|
|
115
|
+
case metaAg_1.EProvider.OKX:
|
|
116
|
+
return (0, okx_1.simulateOKXSwap)(quote, this.inspector, simulation, this.options);
|
|
117
|
+
default:
|
|
118
|
+
throw new Error(`Provider not supported: ${provider.kind}`);
|
|
115
119
|
}
|
|
116
|
-
const amountOut = extractAmountOutWrapper(res.events);
|
|
117
|
-
return {
|
|
118
|
-
id,
|
|
119
|
-
simulatedAmountOut: amountOut,
|
|
120
|
-
gasUsed: res.effects.gasUsed,
|
|
121
|
-
provider: provider.kind,
|
|
122
|
-
};
|
|
123
120
|
}
|
|
124
121
|
catch (error) {
|
|
125
|
-
console.warn(
|
|
122
|
+
console.warn(error, { provider: provider.kind, quote: quote.id });
|
|
126
123
|
}
|
|
127
124
|
}
|
|
128
125
|
async _quote(provider, options, simulation) {
|
|
129
|
-
const quote = await timeout(async () => {
|
|
126
|
+
const quote = await (0, common_1.timeout)(async () => {
|
|
130
127
|
const quote = await provider.quote(options);
|
|
128
|
+
if (!quote)
|
|
129
|
+
return null;
|
|
131
130
|
const { expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, 0, this.options.partnerCommissionBps, this.options.tipBps);
|
|
132
131
|
quote.amountOut = expectedAmount;
|
|
133
132
|
return quote;
|
|
134
|
-
}, options.timeout ?? 2000, `quote for ${provider.kind} provider from ${options.
|
|
135
|
-
if (simulation) {
|
|
133
|
+
}, options.timeout ?? 2000, `quote for ${provider.kind} provider from ${options.coinTypeIn} to ${options.coinTypeOut}`);
|
|
134
|
+
if (quote && simulation) {
|
|
136
135
|
if (simulation.onSimulated) {
|
|
137
136
|
this._simulate(provider, quote, simulation).then((payload) => {
|
|
138
137
|
if (payload) {
|
|
139
|
-
|
|
138
|
+
setTimeout(() => {
|
|
139
|
+
quote.simulatedAmountOut = payload.simulatedAmountOut;
|
|
140
|
+
quote.gasUsed = payload.gasUsed;
|
|
141
|
+
simulation.onSimulated?.({ ...quote });
|
|
142
|
+
});
|
|
140
143
|
}
|
|
141
144
|
});
|
|
142
145
|
}
|
|
@@ -148,6 +151,29 @@ class MetaAg {
|
|
|
148
151
|
}
|
|
149
152
|
return quote;
|
|
150
153
|
}
|
|
154
|
+
async _fastSwap({ quote, signer, useGasCoin, signTransaction }, getTransactionBlockParams) {
|
|
155
|
+
const tx = new transactions_1.Transaction();
|
|
156
|
+
const coin = await this.swap({
|
|
157
|
+
quote,
|
|
158
|
+
signer,
|
|
159
|
+
tx,
|
|
160
|
+
coinIn: (0, transactions_1.coinWithBalance)({
|
|
161
|
+
type: quote.coinTypeIn,
|
|
162
|
+
balance: BigInt(quote.amountIn),
|
|
163
|
+
useGasCoin,
|
|
164
|
+
}),
|
|
165
|
+
});
|
|
166
|
+
tx.transferObjects([coin], signer);
|
|
167
|
+
tx.setSenderIfNotSet(signer);
|
|
168
|
+
const txBytes = await tx.build({ client: this.client });
|
|
169
|
+
const { signature, bytes } = await signTransaction((0, utils_1.toBase64)(txBytes));
|
|
170
|
+
return this.client.executeTransactionBlock({
|
|
171
|
+
transactionBlock: bytes,
|
|
172
|
+
signature,
|
|
173
|
+
options: getTransactionBlockParams?.options,
|
|
174
|
+
signal: getTransactionBlockParams?.signal,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
151
177
|
/**
|
|
152
178
|
* Get quotes from all providers
|
|
153
179
|
* @param options - quote options
|
|
@@ -157,8 +183,8 @@ class MetaAg {
|
|
|
157
183
|
async quote(options, simulation) {
|
|
158
184
|
const opts = {
|
|
159
185
|
...options,
|
|
160
|
-
|
|
161
|
-
|
|
186
|
+
coinTypeIn: (0, utils_1.normalizeStructTag)(options.coinTypeIn),
|
|
187
|
+
coinTypeOut: (0, utils_1.normalizeStructTag)(options.coinTypeOut),
|
|
162
188
|
};
|
|
163
189
|
const quotes = await Promise.allSettled(Object.entries(this.options.providers)
|
|
164
190
|
.filter(([_k, v]) => !v.disabled)
|
|
@@ -174,6 +200,8 @@ class MetaAg {
|
|
|
174
200
|
}
|
|
175
201
|
/**
|
|
176
202
|
* Build transaction from quote
|
|
203
|
+
* @info Use this function to build composable transaction (ie: add more commands after the swap, consume the coin out object)
|
|
204
|
+
* @warning Providers that build transaction on the fly (typically RFQ, Swap-API providers ie: BluefinX, Okx, ...) are not supported, please use `fastSwap` instead
|
|
177
205
|
* @param options - build tx options
|
|
178
206
|
* @param slippageBps - slippage bps if not specified, fallback to global slippage bps, if none of them specified, default to 100
|
|
179
207
|
* @returns coin out object, you must consume it by transferObjects, or other sub sequence commands
|
|
@@ -181,11 +209,32 @@ class MetaAg {
|
|
|
181
209
|
async swap(options, slippageBps) {
|
|
182
210
|
const provider = await this._getProvider(options.quote.provider);
|
|
183
211
|
(0, condition_1.assert)(!!provider, `Provider not found: ${options.quote.provider}`);
|
|
212
|
+
(0, condition_1.assert)((0, metaAg_1.isAggregatorProvider)(provider), `Provider does not support swap: ${provider.kind}`);
|
|
184
213
|
const coinOut = await provider.swap(options);
|
|
185
|
-
options.tx.add(metaSettle(options.quote, coinOut, slippageBps ?? this.options.slippageBps ?? 100, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
|
|
214
|
+
options.tx.add((0, common_1.metaSettle)(options.quote, coinOut, slippageBps ?? this.options.slippageBps ?? 100, this.options.tipBps, this.options.partner, this.options.partnerCommissionBps));
|
|
186
215
|
options.tx.setSenderIfNotSet(options.signer);
|
|
187
216
|
return coinOut;
|
|
188
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Build, Sign, and Execute transaction in one step
|
|
220
|
+
* @param options - fast swap options
|
|
221
|
+
* @returns - txDigest of the transaction
|
|
222
|
+
*/
|
|
223
|
+
async fastSwap(options, getTransactionBlockParams) {
|
|
224
|
+
const provider = await this._getProvider(options.quote.provider);
|
|
225
|
+
if ((0, metaAg_1.isAggregatorProvider)(provider)) {
|
|
226
|
+
return this._fastSwap(options, getTransactionBlockParams);
|
|
227
|
+
}
|
|
228
|
+
else if ((0, metaAg_1.isSwapAPIProvider)(provider)) {
|
|
229
|
+
return this.client.waitForTransaction({
|
|
230
|
+
...getTransactionBlockParams,
|
|
231
|
+
digest: await provider.fastSwap(options),
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
throw new Error(`Provider not supported: ${provider.kind}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
189
238
|
/**
|
|
190
239
|
* Update meta aggregator options
|
|
191
240
|
* @param options - update options payload
|
|
@@ -219,71 +268,17 @@ class MetaAg {
|
|
|
219
268
|
}
|
|
220
269
|
}
|
|
221
270
|
exports.MetaAg = MetaAg;
|
|
222
|
-
/**
|
|
223
|
-
* this settlement does not charge commission fee for partner, since all integrated aggregators already charge commission fee for partner
|
|
224
|
-
* @param quote Meta Aggregator Quote
|
|
225
|
-
* @param coinOut Coin Out Object
|
|
226
|
-
* @param slippageBps Slippage Bps
|
|
227
|
-
* @param tipBps Tip Bps default = 0
|
|
228
|
-
* @param partner address of partner for analytic default is zero address
|
|
229
|
-
*/
|
|
230
|
-
const metaSettle = (quote, coinOut, slippageBps = 100, tipBps = 0, partner, commissionBps = 0) => {
|
|
231
|
-
return (tx) => {
|
|
232
|
-
const { minAmount, expectedAmount } = (0, buildTx_1.getExpectedReturn)(quote.rawAmountOut, slippageBps, commissionBps, tipBps);
|
|
233
|
-
if (tipBps > 0) {
|
|
234
|
-
tx.moveCall({
|
|
235
|
-
target: `${_7k_1._7K_META_PUBLISHED_AT}::vault::collect_tip`,
|
|
236
|
-
typeArguments: [quote.coinTypeOut],
|
|
237
|
-
arguments: [
|
|
238
|
-
tx.object(_7k_1._7K_META_VAULT),
|
|
239
|
-
tx.object(_7k_1._7K_META_CONFIG),
|
|
240
|
-
coinOut,
|
|
241
|
-
tx.pure.u64(tipBps),
|
|
242
|
-
],
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
tx.moveCall({
|
|
246
|
-
target: `${_7k_1._7K_META_PUBLISHED_AT}::settle::settle`,
|
|
247
|
-
typeArguments: [quote.coinTypeIn, quote.coinTypeOut],
|
|
248
|
-
arguments: [
|
|
249
|
-
tx.object(_7k_1._7K_META_CONFIG),
|
|
250
|
-
tx.object(_7k_1._7K_META_VAULT),
|
|
251
|
-
tx.pure.u64(quote.amountIn),
|
|
252
|
-
coinOut,
|
|
253
|
-
tx.pure.u64(minAmount),
|
|
254
|
-
tx.pure.u64(expectedAmount),
|
|
255
|
-
tx.pure.option("address", partner),
|
|
256
|
-
tx.pure.u64(commissionBps),
|
|
257
|
-
tx.pure.u64(0), // ps
|
|
258
|
-
],
|
|
259
|
-
});
|
|
260
|
-
};
|
|
261
|
-
};
|
|
262
|
-
const extractAmountOutWrapper = (events) => {
|
|
263
|
-
const swapEvent = events
|
|
264
|
-
.filter((event) => event.type === `${_7k_1._7K_META_PACKAGE_ID}::settle::Swap`)
|
|
265
|
-
?.pop();
|
|
266
|
-
return swapEvent?.parsedJson?.amount_out;
|
|
267
|
-
};
|
|
268
271
|
const catchImportError = (provider) => {
|
|
269
272
|
return (e) => {
|
|
270
273
|
const map = {
|
|
271
274
|
[metaAg_1.EProvider.CETUS]: "@cetusprotocol/aggregator-sdk",
|
|
272
275
|
[metaAg_1.EProvider.FLOWX]: "@flowx-finance/sdk",
|
|
273
276
|
[metaAg_1.EProvider.BLUEFIN7K]: "@7kprotocol/sdk-ts",
|
|
277
|
+
[metaAg_1.EProvider.ASTRO]: "@naviprotocol/astros-aggregator-sdk",
|
|
278
|
+
[metaAg_1.EProvider.OKX]: "",
|
|
279
|
+
[metaAg_1.EProvider.BLUEFINX]: "",
|
|
274
280
|
};
|
|
275
281
|
console.warn(`Please install ${map[provider]} to use ${provider} provider`);
|
|
276
282
|
throw e;
|
|
277
283
|
};
|
|
278
284
|
};
|
|
279
|
-
const timeout = async (fn, timeout, msg) => {
|
|
280
|
-
if (timeout <= 0)
|
|
281
|
-
return fn();
|
|
282
|
-
return new Promise((resolve, reject) => {
|
|
283
|
-
const timer = setTimeout(() => reject(new Error(`Timeout ${msg ?? "operation"}`)), timeout);
|
|
284
|
-
fn()
|
|
285
|
-
.then(resolve)
|
|
286
|
-
.catch(reject)
|
|
287
|
-
.finally(() => clearTimeout(timer));
|
|
288
|
-
});
|
|
289
|
-
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AstroProvider = void 0;
|
|
4
|
+
const astros_aggregator_sdk_1 = require("@naviprotocol/astros-aggregator-sdk");
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const _7k_1 = require("../../../constants/_7k");
|
|
7
|
+
const metaAg_1 = require("../../../types/metaAg");
|
|
8
|
+
const condition_1 = require("../../../utils/condition");
|
|
9
|
+
class AstroProvider {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.kind = metaAg_1.EProvider.ASTRO;
|
|
13
|
+
}
|
|
14
|
+
async quote({ amountIn, coinTypeIn, coinTypeOut, }) {
|
|
15
|
+
const quote = await (0, astros_aggregator_sdk_1.getQuote)(coinTypeIn, coinTypeOut, amountIn, this.options.api, {
|
|
16
|
+
baseUrl: this.options.api,
|
|
17
|
+
byAmountIn: true,
|
|
18
|
+
depth: this.options.depth,
|
|
19
|
+
dexList: this.options.dexList,
|
|
20
|
+
serviceFee: {
|
|
21
|
+
fee: 0,
|
|
22
|
+
receiverAddress: _7k_1._7K_PARTNER_ADDRESS,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
id: (0, uuid_1.v4)(),
|
|
27
|
+
provider: this.kind,
|
|
28
|
+
quote,
|
|
29
|
+
amountIn,
|
|
30
|
+
rawAmountOut: quote.amount_out.toString(),
|
|
31
|
+
amountOut: quote.amount_out.toString(),
|
|
32
|
+
coinTypeIn,
|
|
33
|
+
coinTypeOut,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async swap({ signer, quote, coinIn, tx }) {
|
|
37
|
+
(0, condition_1.assert)(quote.provider === metaAg_1.EProvider.ASTRO, "Invalid quote");
|
|
38
|
+
const coin = await (0, astros_aggregator_sdk_1.buildSwapPTBFromQuote)(signer, tx, 0, coinIn, quote.quote);
|
|
39
|
+
return coin;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.AstroProvider = AstroProvider;
|
|
@@ -5,7 +5,6 @@ const pyth_sui_js_1 = require("@pythnetwork/pyth-sui-js");
|
|
|
5
5
|
const uuid_1 = require("uuid");
|
|
6
6
|
const config_1 = require("../../../config");
|
|
7
7
|
const _7k_1 = require("../../../constants/_7k");
|
|
8
|
-
const apiEndpoints_1 = require("../../../constants/apiEndpoints");
|
|
9
8
|
const metaAg_1 = require("../../../types/metaAg");
|
|
10
9
|
const condition_1 = require("../../../utils/condition");
|
|
11
10
|
const buildTxV2_1 = require("../../swap/buildTxV2");
|
|
@@ -21,6 +20,8 @@ class BluefinProvider {
|
|
|
21
20
|
const pythConnection = new pyth_sui_js_1.SuiPriceServiceConnection(this.metaOptions.hermesApi);
|
|
22
21
|
if (options.apiKey)
|
|
23
22
|
config_1.Config.setApiKey(options.apiKey);
|
|
23
|
+
if (options.api)
|
|
24
|
+
config_1.Config.setApi(options.api);
|
|
24
25
|
config_1.Config.setSuiClient(client);
|
|
25
26
|
config_1.Config.setPythClient(pythClient);
|
|
26
27
|
config_1.Config.setPythConnection(pythConnection);
|
|
@@ -28,9 +29,8 @@ class BluefinProvider {
|
|
|
28
29
|
async quote(options) {
|
|
29
30
|
const quote = await (0, getQuote_1.getQuote)({
|
|
30
31
|
amountIn: options.amountIn,
|
|
31
|
-
tokenIn: options.
|
|
32
|
-
tokenOut: options.
|
|
33
|
-
api: this.options.api || apiEndpoints_1.API_ENDPOINTS.MAIN,
|
|
32
|
+
tokenIn: options.coinTypeIn,
|
|
33
|
+
tokenOut: options.coinTypeOut,
|
|
34
34
|
sources: this.options.sources,
|
|
35
35
|
maxPaths: this.options.maxPaths,
|
|
36
36
|
excludedPools: this.options.excludedPools,
|
|
@@ -43,8 +43,8 @@ class BluefinProvider {
|
|
|
43
43
|
amountIn: quote.swapAmountWithDecimal,
|
|
44
44
|
rawAmountOut: quote.returnAmountWithDecimal,
|
|
45
45
|
amountOut: quote.returnAmountWithDecimal,
|
|
46
|
-
coinTypeIn: options.
|
|
47
|
-
coinTypeOut: options.
|
|
46
|
+
coinTypeIn: options.coinTypeIn,
|
|
47
|
+
coinTypeOut: options.coinTypeOut,
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
50
|
async swap({ quote, signer, tx, coinIn }) {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BluefinXProvider = void 0;
|
|
4
|
+
const transactions_1 = require("@mysten/sui/transactions");
|
|
5
|
+
const utils_1 = require("@mysten/sui/utils");
|
|
6
|
+
const uuid_1 = require("uuid");
|
|
7
|
+
const config_1 = require("../../../config");
|
|
8
|
+
const _7k_1 = require("../../../constants/_7k");
|
|
9
|
+
const client_1 = require("../../../libs/protocols/bluefinx/client");
|
|
10
|
+
const aggregator_1 = require("../../../types/aggregator");
|
|
11
|
+
const metaAg_1 = require("../../../types/metaAg");
|
|
12
|
+
const condition_1 = require("../../../utils/condition");
|
|
13
|
+
const buildTxV2_1 = require("../../swap/buildTxV2");
|
|
14
|
+
const getQuote_1 = require("../../swap/getQuote");
|
|
15
|
+
const common_1 = require("../common");
|
|
16
|
+
const SUPPORT_COINS = [
|
|
17
|
+
"0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI",
|
|
18
|
+
"0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
|
|
19
|
+
];
|
|
20
|
+
class BluefinXProvider {
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.kind = metaAg_1.EProvider.BLUEFINX;
|
|
24
|
+
if (options.apiKey)
|
|
25
|
+
config_1.Config.setApiKey(options.apiKey);
|
|
26
|
+
if (options.api)
|
|
27
|
+
config_1.Config.setApi(options.api);
|
|
28
|
+
}
|
|
29
|
+
async quote(options) {
|
|
30
|
+
if (!this.canQuote(options))
|
|
31
|
+
return null;
|
|
32
|
+
const quote = await (0, getQuote_1.getQuote)({
|
|
33
|
+
amountIn: options.amountIn,
|
|
34
|
+
tokenIn: options.coinTypeIn,
|
|
35
|
+
tokenOut: options.coinTypeOut,
|
|
36
|
+
sources: ["bluefinx"],
|
|
37
|
+
taker: options.signer,
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
id: (0, uuid_1.v4)(),
|
|
41
|
+
provider: this.kind,
|
|
42
|
+
quote,
|
|
43
|
+
amountIn: quote.swapAmountWithDecimal,
|
|
44
|
+
rawAmountOut: quote.returnAmountWithDecimal,
|
|
45
|
+
amountOut: quote.returnAmountWithDecimal,
|
|
46
|
+
coinTypeIn: options.coinTypeIn,
|
|
47
|
+
coinTypeOut: options.coinTypeOut,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
canQuote(options) {
|
|
51
|
+
return (!!options.signer &&
|
|
52
|
+
(0, utils_1.isValidSuiAddress)(options.signer) &&
|
|
53
|
+
SUPPORT_COINS.includes(options.coinTypeIn) &&
|
|
54
|
+
SUPPORT_COINS.includes(options.coinTypeOut));
|
|
55
|
+
}
|
|
56
|
+
async fastSwap(options) {
|
|
57
|
+
(0, condition_1.assert)(options.quote.provider === this.kind, "Invalid BluefinX quote");
|
|
58
|
+
const quote = options.quote.quote;
|
|
59
|
+
const tx = new transactions_1.Transaction();
|
|
60
|
+
const { tx: bluefinTx } = await (0, buildTxV2_1.buildTxV2Int)({
|
|
61
|
+
quoteResponse: quote,
|
|
62
|
+
accountAddress: options.signer,
|
|
63
|
+
commission: { commissionBps: 0, partner: _7k_1._7K_PARTNER_ADDRESS },
|
|
64
|
+
slippage: 0,
|
|
65
|
+
extendTx: {
|
|
66
|
+
tx,
|
|
67
|
+
coinIn: (0, transactions_1.coinWithBalance)({
|
|
68
|
+
type: options.quote.coinTypeIn,
|
|
69
|
+
balance: BigInt(options.quote.amountIn),
|
|
70
|
+
useGasCoin: false,
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
}, (tx, coinOut) => {
|
|
74
|
+
if (coinOut) {
|
|
75
|
+
tx.add((0, common_1.metaSettle)(options.quote, coinOut, 0, 0));
|
|
76
|
+
tx.transferObjects([coinOut], options.signer);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
(0, condition_1.assert)(bluefinTx instanceof aggregator_1.BluefinXTx, "BluefinX transaction not found");
|
|
80
|
+
const { signature } = await options.signTransaction(bluefinTx.txBytes);
|
|
81
|
+
const res = await (0, client_1.executeBluefinTx)(bluefinTx, signature);
|
|
82
|
+
(0, condition_1.assert)(res.approved, "BluefinX transaction not approved");
|
|
83
|
+
(0, condition_1.assert)(res.txDigest, "BluefinX transaction digest not found");
|
|
84
|
+
return res.txDigest;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.BluefinXProvider = BluefinXProvider;
|
|
@@ -24,8 +24,8 @@ class CetusProvider {
|
|
|
24
24
|
const quote = await this.cetusClient.findRouters({
|
|
25
25
|
amount: quoteOptions.amountIn,
|
|
26
26
|
byAmountIn: true,
|
|
27
|
-
from: quoteOptions.
|
|
28
|
-
target: quoteOptions.
|
|
27
|
+
from: quoteOptions.coinTypeIn,
|
|
28
|
+
target: quoteOptions.coinTypeOut,
|
|
29
29
|
providers: this.options.sources,
|
|
30
30
|
splitCount: this.options.splitCount,
|
|
31
31
|
splitAlgorithm: this.options.splitAlgorithm,
|
|
@@ -41,8 +41,8 @@ class CetusProvider {
|
|
|
41
41
|
amountIn: quote.amountIn.toString() || "0",
|
|
42
42
|
rawAmountOut: quote.amountOut.toString() || "0",
|
|
43
43
|
amountOut: quote.amountOut.toString() || "0",
|
|
44
|
-
coinTypeIn: quoteOptions.
|
|
45
|
-
coinTypeOut: quoteOptions.
|
|
44
|
+
coinTypeIn: quoteOptions.coinTypeIn,
|
|
45
|
+
coinTypeOut: quoteOptions.coinTypeOut,
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
48
|
async swap(options) {
|
|
@@ -16,8 +16,8 @@ class FlowxProvider {
|
|
|
16
16
|
async quote(quoteOptions) {
|
|
17
17
|
const quote = await this.quoter.getRoutes({
|
|
18
18
|
amountIn: quoteOptions.amountIn,
|
|
19
|
-
tokenIn: quoteOptions.
|
|
20
|
-
tokenOut: quoteOptions.
|
|
19
|
+
tokenIn: quoteOptions.coinTypeIn,
|
|
20
|
+
tokenOut: quoteOptions.coinTypeOut,
|
|
21
21
|
includeSources: this.options.sources,
|
|
22
22
|
excludePools: this.options.excludePools,
|
|
23
23
|
excludeSources: this.options.excludeSources,
|
|
@@ -31,8 +31,8 @@ class FlowxProvider {
|
|
|
31
31
|
amountIn: quote.amountIn?.toString() ?? "0",
|
|
32
32
|
rawAmountOut: quote.amountOut?.toString() ?? "0",
|
|
33
33
|
amountOut: quote.amountOut?.toString() ?? "0",
|
|
34
|
-
coinTypeIn: quoteOptions.
|
|
35
|
-
coinTypeOut: quoteOptions.
|
|
34
|
+
coinTypeIn: quoteOptions.coinTypeIn,
|
|
35
|
+
coinTypeOut: quoteOptions.coinTypeOut,
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
async swap(options) {
|