mantle-agent-kit-sdk 1.0.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/.env.example +18 -0
- package/LICENSE +21 -0
- package/README.md +405 -0
- package/dist/index.cjs +1479 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +782 -0
- package/dist/index.d.ts +782 -0
- package/dist/index.js +1443 -0
- package/dist/index.js.map +1 -0
- package/examples/complete-workflow.ts +57 -0
- package/examples/x402-initialization.ts +71 -0
- package/package.json +89 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1443 @@
|
|
|
1
|
+
import { parseEther, encodeFunctionData, createWalletClient, http, publicActions, erc20Abi } from 'viem';
|
|
2
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
3
|
+
import { mantle, mantleSepoliaTestnet } from 'viem/chains';
|
|
4
|
+
import { getTransactionReceipt } from 'viem/actions';
|
|
5
|
+
import axios6 from 'axios';
|
|
6
|
+
import CryptoJS from 'crypto-js';
|
|
7
|
+
|
|
8
|
+
var __defProp = Object.defineProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var sendTransaction = async (agent, to, amount) => {
|
|
14
|
+
const hash = await agent.client.sendTransaction({
|
|
15
|
+
to,
|
|
16
|
+
value: parseEther(amount)
|
|
17
|
+
});
|
|
18
|
+
const receipt = await getTransactionReceipt(agent.client, {
|
|
19
|
+
hash
|
|
20
|
+
});
|
|
21
|
+
return receipt;
|
|
22
|
+
};
|
|
23
|
+
async function checkAllowance(agent, tokenAddress, ownerAddress, spenderAddress) {
|
|
24
|
+
try {
|
|
25
|
+
const allowance = await agent.client.readContract({
|
|
26
|
+
address: tokenAddress,
|
|
27
|
+
abi: erc20Abi,
|
|
28
|
+
functionName: "allowance",
|
|
29
|
+
args: [ownerAddress, spenderAddress]
|
|
30
|
+
});
|
|
31
|
+
return allowance;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error("Failed to query allowance:", error);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/constants/okx/index.ts
|
|
39
|
+
var okx_exports = {};
|
|
40
|
+
__export(okx_exports, {
|
|
41
|
+
ETH_ADDRESS: () => ETH_ADDRESS,
|
|
42
|
+
baseUrl: () => baseUrl,
|
|
43
|
+
configs: () => configs
|
|
44
|
+
});
|
|
45
|
+
var configs = {
|
|
46
|
+
apiKey: process.env.OKX_API_KEY,
|
|
47
|
+
secretKey: process.env.OKX_SECRET_KEY,
|
|
48
|
+
apiPassphrase: process.env.OKX_API_PASSPHRASE,
|
|
49
|
+
projectId: process.env.OKX_PROJECT_ID
|
|
50
|
+
};
|
|
51
|
+
var baseUrl = "https://web3.okx.com/api/v6/";
|
|
52
|
+
var ETH_ADDRESS = "0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000";
|
|
53
|
+
function getHeaders(timestamp, method, requestPath, queryString = "", body = "") {
|
|
54
|
+
const { apiKey, secretKey, apiPassphrase, projectId } = configs;
|
|
55
|
+
if (!apiKey || !secretKey || !apiPassphrase || !projectId) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Missing required environment variables for API authentication"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const stringToSign = timestamp + method + requestPath + (queryString || body);
|
|
61
|
+
return {
|
|
62
|
+
"Content-Type": "application/json",
|
|
63
|
+
"OK-ACCESS-KEY": apiKey,
|
|
64
|
+
"OK-ACCESS-SIGN": CryptoJS.enc.Base64.stringify(
|
|
65
|
+
CryptoJS.HmacSHA256(stringToSign, secretKey)
|
|
66
|
+
),
|
|
67
|
+
"OK-ACCESS-TIMESTAMP": timestamp,
|
|
68
|
+
"OK-ACCESS-PASSPHRASE": apiPassphrase,
|
|
69
|
+
"OK-ACCESS-PROJECT": projectId
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/utils/okx/getApproveTx.ts
|
|
74
|
+
async function getApproveTx(tokenAddress, amount, chainIndex) {
|
|
75
|
+
try {
|
|
76
|
+
const path = "dex/aggregator/approve-transaction";
|
|
77
|
+
const url = `${baseUrl}${path}`;
|
|
78
|
+
const params = {
|
|
79
|
+
chainIndex,
|
|
80
|
+
tokenContractAddress: tokenAddress,
|
|
81
|
+
approveAmount: amount
|
|
82
|
+
};
|
|
83
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
84
|
+
const requestPath = `/api/v6/${path}`;
|
|
85
|
+
const queryString = "?" + new URLSearchParams(params).toString();
|
|
86
|
+
const headers = getHeaders(timestamp, "GET", requestPath, queryString);
|
|
87
|
+
const response = await axios6.get(url, { params, headers });
|
|
88
|
+
if (response.data.code === "0") {
|
|
89
|
+
return response.data.data[0];
|
|
90
|
+
} else {
|
|
91
|
+
throw new Error(`API Error: ${response.data.msg || "Unknown error"}`);
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error(
|
|
95
|
+
"Failed to get approval transaction data:",
|
|
96
|
+
error.message
|
|
97
|
+
);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async function getGasLimit(fromAddress, toAddress, chainIndex, txAmount = "0", inputData = "") {
|
|
102
|
+
try {
|
|
103
|
+
const path = "dex/pre-transaction/gas-limit";
|
|
104
|
+
const url = `https://web3.okx.com/api/v6/${path}`;
|
|
105
|
+
const body = {
|
|
106
|
+
chainIndex,
|
|
107
|
+
fromAddress,
|
|
108
|
+
toAddress,
|
|
109
|
+
txAmount,
|
|
110
|
+
extJson: {
|
|
111
|
+
inputData
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const bodyString = JSON.stringify(body);
|
|
115
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
116
|
+
const requestPath = `/api/v6/${path}`;
|
|
117
|
+
const headers = getHeaders(timestamp, "POST", requestPath, "", bodyString);
|
|
118
|
+
const response = await axios6.post(url, body, { headers });
|
|
119
|
+
if (response.data.code === "0") {
|
|
120
|
+
return response.data.data[0].gasLimit;
|
|
121
|
+
} else {
|
|
122
|
+
throw new Error(`API Error: ${response.data.msg || "Unknown error"}`);
|
|
123
|
+
}
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error("Failed to get gas limit:", error.message);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/utils/okx/approveToken.ts
|
|
131
|
+
async function approveToken(agent, tokenAddress, amount) {
|
|
132
|
+
const chainIndex = agent.chain === "mainnet" ? "5000" : "5003";
|
|
133
|
+
const walletAddress = agent.account.address;
|
|
134
|
+
const spenderAddress = "0x1f16A607a7f3F3044E477abFFc8BD33952cE306b";
|
|
135
|
+
const currentAllowance = await checkAllowance(
|
|
136
|
+
agent,
|
|
137
|
+
tokenAddress,
|
|
138
|
+
walletAddress,
|
|
139
|
+
spenderAddress
|
|
140
|
+
);
|
|
141
|
+
if (currentAllowance >= BigInt(amount)) {
|
|
142
|
+
console.log("Sufficient allowance already exists");
|
|
143
|
+
return { allowanceExists: true, data: null };
|
|
144
|
+
}
|
|
145
|
+
console.log("Insufficient allowance, approving tokens...");
|
|
146
|
+
const approveData = await getApproveTx(tokenAddress, amount, chainIndex);
|
|
147
|
+
const gasLimit = await getGasLimit(
|
|
148
|
+
walletAddress,
|
|
149
|
+
tokenAddress,
|
|
150
|
+
chainIndex,
|
|
151
|
+
"0",
|
|
152
|
+
approveData.data
|
|
153
|
+
);
|
|
154
|
+
const txObject = {
|
|
155
|
+
to: tokenAddress,
|
|
156
|
+
data: approveData.data,
|
|
157
|
+
gas: gasLimit
|
|
158
|
+
};
|
|
159
|
+
return { allowanceExists: false, data: txObject };
|
|
160
|
+
}
|
|
161
|
+
async function getSwapTransaction(fromTokenAddress, toTokenAddress, amount, userWalletAddress, chainIndex, slippagePercent = "0.5") {
|
|
162
|
+
try {
|
|
163
|
+
const path = "dex/aggregator/swap";
|
|
164
|
+
const url = `${baseUrl}${path}`;
|
|
165
|
+
const params = {
|
|
166
|
+
chainIndex,
|
|
167
|
+
fromTokenAddress,
|
|
168
|
+
toTokenAddress,
|
|
169
|
+
amount,
|
|
170
|
+
userWalletAddress,
|
|
171
|
+
slippage: slippagePercent
|
|
172
|
+
};
|
|
173
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
174
|
+
const requestPath = `/api/v6/${path}`;
|
|
175
|
+
const queryString = "?" + new URLSearchParams(params).toString();
|
|
176
|
+
const headers = getHeaders(timestamp, "GET", requestPath, queryString);
|
|
177
|
+
const response = await axios6.get(url, { params, headers });
|
|
178
|
+
if (response.data.code === "0") {
|
|
179
|
+
return response.data.data[0];
|
|
180
|
+
} else {
|
|
181
|
+
throw new Error(`API Error: ${response.data.msg || "Unknown error"}`);
|
|
182
|
+
}
|
|
183
|
+
} catch (error) {
|
|
184
|
+
console.error(
|
|
185
|
+
"Failed to get swap transaction data:",
|
|
186
|
+
error.message
|
|
187
|
+
);
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/tools/okx/swap.ts
|
|
193
|
+
async function executeSwap(agent, fromTokenAddress, toTokenAddress, amount, slippagePercent) {
|
|
194
|
+
const chainIndex = agent.chain === "mainnet" ? "5000" : "5003";
|
|
195
|
+
const walletAddress = agent.account.address;
|
|
196
|
+
if (fromTokenAddress !== ETH_ADDRESS) {
|
|
197
|
+
const approval = await approveToken(agent, fromTokenAddress, amount);
|
|
198
|
+
if (!approval.allowanceExists && approval.data) {
|
|
199
|
+
const approveTxHash = await agent.client.sendTransaction({
|
|
200
|
+
to: approval.data.to,
|
|
201
|
+
data: approval.data.data,
|
|
202
|
+
value: BigInt(0),
|
|
203
|
+
gas: BigInt(approval.data.gas)
|
|
204
|
+
});
|
|
205
|
+
console.log(`Approval tx sent: ${approveTxHash}`);
|
|
206
|
+
const approveReceipt = await agent.client.waitForTransactionReceipt({
|
|
207
|
+
hash: approveTxHash
|
|
208
|
+
});
|
|
209
|
+
console.log(`Approval confirmed: ${approveReceipt.status}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
const swapData = await getSwapTransaction(
|
|
213
|
+
fromTokenAddress,
|
|
214
|
+
toTokenAddress,
|
|
215
|
+
amount,
|
|
216
|
+
walletAddress,
|
|
217
|
+
chainIndex,
|
|
218
|
+
slippagePercent
|
|
219
|
+
);
|
|
220
|
+
const txData = swapData.tx;
|
|
221
|
+
console.log("Swap TX data received");
|
|
222
|
+
const gasLimit = await getGasLimit(
|
|
223
|
+
walletAddress,
|
|
224
|
+
txData.to,
|
|
225
|
+
chainIndex,
|
|
226
|
+
txData.value || "0",
|
|
227
|
+
txData.data
|
|
228
|
+
);
|
|
229
|
+
console.log("Gas limit received");
|
|
230
|
+
const txHash = await agent.client.sendTransaction({
|
|
231
|
+
to: txData.to,
|
|
232
|
+
data: txData.data,
|
|
233
|
+
value: BigInt(txData.value || "0"),
|
|
234
|
+
gas: BigInt(gasLimit)
|
|
235
|
+
});
|
|
236
|
+
console.log(`Swap tx sent: ${txHash}`);
|
|
237
|
+
const swapReceipt = await agent.client.waitForTransactionReceipt({
|
|
238
|
+
hash: txHash
|
|
239
|
+
});
|
|
240
|
+
console.log(`Swap confirmed: ${swapReceipt.status}`);
|
|
241
|
+
return {
|
|
242
|
+
data: txHash
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/constants/openocean/index.ts
|
|
247
|
+
var openocean_exports = {};
|
|
248
|
+
__export(openocean_exports, {
|
|
249
|
+
NATIVE_TOKEN_ADDRESS: () => NATIVE_TOKEN_ADDRESS,
|
|
250
|
+
OPENOCEAN_BASE_URL: () => OPENOCEAN_BASE_URL,
|
|
251
|
+
OPENOCEAN_CHAIN: () => OPENOCEAN_CHAIN,
|
|
252
|
+
OPENOCEAN_EXCHANGE_PROXY: () => OPENOCEAN_EXCHANGE_PROXY
|
|
253
|
+
});
|
|
254
|
+
var OPENOCEAN_BASE_URL = "https://open-api.openocean.finance/v4";
|
|
255
|
+
var OPENOCEAN_CHAIN = {
|
|
256
|
+
mainnet: "mantle",
|
|
257
|
+
testnet: "mantle"
|
|
258
|
+
// OpenOcean may not support testnet
|
|
259
|
+
};
|
|
260
|
+
var NATIVE_TOKEN_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
261
|
+
var OPENOCEAN_EXCHANGE_PROXY = "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64";
|
|
262
|
+
|
|
263
|
+
// src/helpers/openocean/index.ts
|
|
264
|
+
function getHeaders2(apiKey) {
|
|
265
|
+
const headers = {
|
|
266
|
+
"Content-Type": "application/json"
|
|
267
|
+
};
|
|
268
|
+
return headers;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/utils/openocean/getQuoteData.ts
|
|
272
|
+
async function getQuoteData(fromToken, toToken, amount, chain) {
|
|
273
|
+
try {
|
|
274
|
+
const chainName = OPENOCEAN_CHAIN[chain];
|
|
275
|
+
const url = `${OPENOCEAN_BASE_URL}/${chainName}/quote`;
|
|
276
|
+
const params = {
|
|
277
|
+
inTokenAddress: fromToken,
|
|
278
|
+
outTokenAddress: toToken,
|
|
279
|
+
amount,
|
|
280
|
+
gasPrice: "5"
|
|
281
|
+
// Default gas price in gwei
|
|
282
|
+
};
|
|
283
|
+
const response = await axios6.get(url, {
|
|
284
|
+
params,
|
|
285
|
+
headers: getHeaders2()
|
|
286
|
+
});
|
|
287
|
+
if (response.data.code === 200) {
|
|
288
|
+
return response.data.data;
|
|
289
|
+
} else {
|
|
290
|
+
throw new Error(`OpenOcean API Error: ${response.data.message || "Unknown error"}`);
|
|
291
|
+
}
|
|
292
|
+
} catch (error) {
|
|
293
|
+
console.error("Failed to get OpenOcean quote:", error.message);
|
|
294
|
+
throw error;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
async function getSwapData(fromToken, toToken, amount, userAddress, slippage, chain) {
|
|
298
|
+
try {
|
|
299
|
+
const chainName = OPENOCEAN_CHAIN[chain];
|
|
300
|
+
const url = `${OPENOCEAN_BASE_URL}/${chainName}/swap`;
|
|
301
|
+
const params = {
|
|
302
|
+
inTokenAddress: fromToken,
|
|
303
|
+
outTokenAddress: toToken,
|
|
304
|
+
amount,
|
|
305
|
+
gasPrice: "5",
|
|
306
|
+
// Default gas price in gwei
|
|
307
|
+
slippage,
|
|
308
|
+
account: userAddress
|
|
309
|
+
};
|
|
310
|
+
const response = await axios6.get(url, {
|
|
311
|
+
params,
|
|
312
|
+
headers: getHeaders2()
|
|
313
|
+
});
|
|
314
|
+
if (response.data.code === 200) {
|
|
315
|
+
return response.data.data;
|
|
316
|
+
} else {
|
|
317
|
+
throw new Error(`OpenOcean API Error: ${response.data.message || "Unknown error"}`);
|
|
318
|
+
}
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.error("Failed to get OpenOcean swap data:", error.message);
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/tools/openocean/getQuote.ts
|
|
326
|
+
async function getOpenOceanQuote(agent, fromToken, toToken, amount) {
|
|
327
|
+
return await getQuoteData(fromToken, toToken, amount, agent.chain);
|
|
328
|
+
}
|
|
329
|
+
async function checkAllowance2(agent, tokenAddress, ownerAddress, spenderAddress) {
|
|
330
|
+
try {
|
|
331
|
+
const allowance = await agent.client.readContract({
|
|
332
|
+
address: tokenAddress,
|
|
333
|
+
abi: erc20Abi,
|
|
334
|
+
functionName: "allowance",
|
|
335
|
+
args: [ownerAddress, spenderAddress]
|
|
336
|
+
});
|
|
337
|
+
return allowance;
|
|
338
|
+
} catch (error) {
|
|
339
|
+
console.error("Failed to query allowance:", error);
|
|
340
|
+
throw error;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
async function approveToken2(agent, tokenAddress, spenderAddress, amount) {
|
|
344
|
+
const walletAddress = agent.account.address;
|
|
345
|
+
const currentAllowance = await checkAllowance2(
|
|
346
|
+
agent,
|
|
347
|
+
tokenAddress,
|
|
348
|
+
walletAddress,
|
|
349
|
+
spenderAddress
|
|
350
|
+
);
|
|
351
|
+
if (currentAllowance >= BigInt(amount)) {
|
|
352
|
+
console.log("Sufficient allowance already exists");
|
|
353
|
+
return { approved: true, txHash: null };
|
|
354
|
+
}
|
|
355
|
+
console.log("Insufficient allowance, approving tokens...");
|
|
356
|
+
const { encodeFunctionData: encodeFunctionData8 } = await import('viem');
|
|
357
|
+
const approveData = encodeFunctionData8({
|
|
358
|
+
abi: erc20Abi,
|
|
359
|
+
functionName: "approve",
|
|
360
|
+
args: [spenderAddress, BigInt(amount)]
|
|
361
|
+
});
|
|
362
|
+
const gasEstimate = await agent.client.estimateGas({
|
|
363
|
+
account: agent.account,
|
|
364
|
+
to: tokenAddress,
|
|
365
|
+
data: approveData
|
|
366
|
+
});
|
|
367
|
+
const txHash = await agent.client.sendTransaction({
|
|
368
|
+
to: tokenAddress,
|
|
369
|
+
data: approveData,
|
|
370
|
+
value: BigInt(0),
|
|
371
|
+
gas: gasEstimate
|
|
372
|
+
});
|
|
373
|
+
console.log(`Approval tx sent: ${txHash}`);
|
|
374
|
+
const receipt = await agent.client.waitForTransactionReceipt({
|
|
375
|
+
hash: txHash
|
|
376
|
+
});
|
|
377
|
+
console.log(`Approval confirmed: ${receipt.status}`);
|
|
378
|
+
return { approved: true, txHash };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// src/tools/openocean/swap.ts
|
|
382
|
+
async function swapOnOpenOcean(agent, fromToken, toToken, amount, slippage = "1") {
|
|
383
|
+
const walletAddress = agent.account.address;
|
|
384
|
+
if (fromToken.toLowerCase() !== NATIVE_TOKEN_ADDRESS.toLowerCase()) {
|
|
385
|
+
await approveToken2(agent, fromToken, OPENOCEAN_EXCHANGE_PROXY, amount);
|
|
386
|
+
}
|
|
387
|
+
const swapData = await getSwapData(
|
|
388
|
+
fromToken,
|
|
389
|
+
toToken,
|
|
390
|
+
amount,
|
|
391
|
+
walletAddress,
|
|
392
|
+
slippage,
|
|
393
|
+
agent.chain
|
|
394
|
+
);
|
|
395
|
+
console.log("OpenOcean swap data received");
|
|
396
|
+
console.log(`Expected output: ${swapData.outAmount}`);
|
|
397
|
+
const txHash = await agent.client.sendTransaction({
|
|
398
|
+
to: swapData.to,
|
|
399
|
+
data: swapData.data,
|
|
400
|
+
value: BigInt(swapData.value || "0"),
|
|
401
|
+
gas: BigInt(swapData.estimatedGas)
|
|
402
|
+
});
|
|
403
|
+
console.log(`OpenOcean swap tx sent: ${txHash}`);
|
|
404
|
+
const receipt = await agent.client.waitForTransactionReceipt({
|
|
405
|
+
hash: txHash
|
|
406
|
+
});
|
|
407
|
+
console.log(`Swap confirmed: ${receipt.status}`);
|
|
408
|
+
return {
|
|
409
|
+
txHash,
|
|
410
|
+
outAmount: swapData.outAmount
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/constants/oneinch/index.ts
|
|
415
|
+
var oneinch_exports = {};
|
|
416
|
+
__export(oneinch_exports, {
|
|
417
|
+
NATIVE_TOKEN_ADDRESS: () => NATIVE_TOKEN_ADDRESS2,
|
|
418
|
+
ONEINCH_BASE_URL: () => ONEINCH_BASE_URL,
|
|
419
|
+
ONEINCH_CHAIN_ID: () => ONEINCH_CHAIN_ID,
|
|
420
|
+
ONEINCH_ROUTER_ADDRESS: () => ONEINCH_ROUTER_ADDRESS,
|
|
421
|
+
configs: () => configs2
|
|
422
|
+
});
|
|
423
|
+
var ONEINCH_BASE_URL = "https://api.1inch.dev/swap/v6.0";
|
|
424
|
+
var ONEINCH_CHAIN_ID = {
|
|
425
|
+
mainnet: "5000",
|
|
426
|
+
// Mantle mainnet
|
|
427
|
+
testnet: "5003"
|
|
428
|
+
// Mantle testnet (may not be supported)
|
|
429
|
+
};
|
|
430
|
+
var NATIVE_TOKEN_ADDRESS2 = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
431
|
+
var ONEINCH_ROUTER_ADDRESS = "0x111111125421cA6dc452d289314280a0f8842A65";
|
|
432
|
+
var configs2 = {
|
|
433
|
+
apiKey: process.env.ONEINCH_API_KEY
|
|
434
|
+
// Optional: for higher rate limits
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
// src/helpers/oneinch/index.ts
|
|
438
|
+
function getHeaders3() {
|
|
439
|
+
const headers = {
|
|
440
|
+
"Content-Type": "application/json"
|
|
441
|
+
};
|
|
442
|
+
if (configs2.apiKey) {
|
|
443
|
+
headers["Authorization"] = `Bearer ${configs2.apiKey}`;
|
|
444
|
+
}
|
|
445
|
+
return headers;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// src/utils/oneinch/getSwapData.ts
|
|
449
|
+
async function getQuoteData2(fromToken, toToken, amount, chain) {
|
|
450
|
+
try {
|
|
451
|
+
const chainId = ONEINCH_CHAIN_ID[chain];
|
|
452
|
+
const url = `${ONEINCH_BASE_URL}/${chainId}/quote`;
|
|
453
|
+
const params = {
|
|
454
|
+
src: fromToken,
|
|
455
|
+
dst: toToken,
|
|
456
|
+
amount
|
|
457
|
+
};
|
|
458
|
+
const response = await axios6.get(url, {
|
|
459
|
+
params,
|
|
460
|
+
headers: getHeaders3()
|
|
461
|
+
});
|
|
462
|
+
return response.data;
|
|
463
|
+
} catch (error) {
|
|
464
|
+
console.error("Failed to get 1inch quote:", error.message);
|
|
465
|
+
throw error;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function getSwapData2(fromToken, toToken, amount, userAddress, slippage, chain) {
|
|
469
|
+
try {
|
|
470
|
+
const chainId = ONEINCH_CHAIN_ID[chain];
|
|
471
|
+
const url = `${ONEINCH_BASE_URL}/${chainId}/swap`;
|
|
472
|
+
const params = {
|
|
473
|
+
src: fromToken,
|
|
474
|
+
dst: toToken,
|
|
475
|
+
amount,
|
|
476
|
+
from: userAddress,
|
|
477
|
+
slippage,
|
|
478
|
+
disableEstimate: "true"
|
|
479
|
+
// We'll estimate gas ourselves
|
|
480
|
+
};
|
|
481
|
+
const response = await axios6.get(url, {
|
|
482
|
+
params,
|
|
483
|
+
headers: getHeaders3()
|
|
484
|
+
});
|
|
485
|
+
return response.data;
|
|
486
|
+
} catch (error) {
|
|
487
|
+
console.error("Failed to get 1inch swap data:", error.message);
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// src/tools/oneinch/getQuote.ts
|
|
493
|
+
async function get1inchQuote(agent, fromToken, toToken, amount) {
|
|
494
|
+
return await getQuoteData2(fromToken, toToken, amount, agent.chain);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// src/tools/oneinch/swap.ts
|
|
498
|
+
async function swapOn1inch(agent, fromToken, toToken, amount, slippage = "1") {
|
|
499
|
+
const walletAddress = agent.account.address;
|
|
500
|
+
if (fromToken.toLowerCase() !== NATIVE_TOKEN_ADDRESS2.toLowerCase()) {
|
|
501
|
+
await approveToken2(agent, fromToken, ONEINCH_ROUTER_ADDRESS, amount);
|
|
502
|
+
}
|
|
503
|
+
const swapData = await getSwapData2(
|
|
504
|
+
fromToken,
|
|
505
|
+
toToken,
|
|
506
|
+
amount,
|
|
507
|
+
walletAddress,
|
|
508
|
+
slippage,
|
|
509
|
+
agent.chain
|
|
510
|
+
);
|
|
511
|
+
console.log("1inch swap data received");
|
|
512
|
+
console.log(`Expected output: ${swapData.dstAmount}`);
|
|
513
|
+
const gasEstimate = await agent.client.estimateGas({
|
|
514
|
+
account: agent.account,
|
|
515
|
+
to: swapData.tx.to,
|
|
516
|
+
data: swapData.tx.data,
|
|
517
|
+
value: BigInt(swapData.tx.value || "0")
|
|
518
|
+
});
|
|
519
|
+
const txHash = await agent.client.sendTransaction({
|
|
520
|
+
to: swapData.tx.to,
|
|
521
|
+
data: swapData.tx.data,
|
|
522
|
+
value: BigInt(swapData.tx.value || "0"),
|
|
523
|
+
gas: gasEstimate
|
|
524
|
+
});
|
|
525
|
+
console.log(`1inch swap tx sent: ${txHash}`);
|
|
526
|
+
const receipt = await agent.client.waitForTransactionReceipt({
|
|
527
|
+
hash: txHash
|
|
528
|
+
});
|
|
529
|
+
console.log(`Swap confirmed: ${receipt.status}`);
|
|
530
|
+
return {
|
|
531
|
+
txHash,
|
|
532
|
+
dstAmount: swapData.dstAmount
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// src/constants/uniswap/index.ts
|
|
537
|
+
var uniswap_exports = {};
|
|
538
|
+
__export(uniswap_exports, {
|
|
539
|
+
DEFAULT_POOL_FEE: () => DEFAULT_POOL_FEE,
|
|
540
|
+
FACTORY_ADDRESS: () => FACTORY_ADDRESS,
|
|
541
|
+
FEE_TIERS: () => FEE_TIERS,
|
|
542
|
+
NATIVE_TOKEN_ADDRESS: () => NATIVE_TOKEN_ADDRESS3,
|
|
543
|
+
QUOTER_V2_ADDRESS: () => QUOTER_V2_ADDRESS,
|
|
544
|
+
SWAP_ROUTER_ADDRESS: () => SWAP_ROUTER_ADDRESS,
|
|
545
|
+
WMNT_ADDRESS: () => WMNT_ADDRESS
|
|
546
|
+
});
|
|
547
|
+
var SWAP_ROUTER_ADDRESS = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45";
|
|
548
|
+
var QUOTER_V2_ADDRESS = "0x61fFE014bA17989E743c5F6cB21bF9697530B21e";
|
|
549
|
+
var FACTORY_ADDRESS = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
|
|
550
|
+
var WMNT_ADDRESS = "0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8";
|
|
551
|
+
var NATIVE_TOKEN_ADDRESS3 = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
552
|
+
var FEE_TIERS = {
|
|
553
|
+
LOWEST: 100,
|
|
554
|
+
// 0.01%
|
|
555
|
+
LOW: 500,
|
|
556
|
+
// 0.05%
|
|
557
|
+
MEDIUM: 3e3,
|
|
558
|
+
// 0.3%
|
|
559
|
+
HIGH: 1e4
|
|
560
|
+
// 1%
|
|
561
|
+
};
|
|
562
|
+
var DEFAULT_POOL_FEE = FEE_TIERS.MEDIUM;
|
|
563
|
+
var quoterV2Abi = [
|
|
564
|
+
{
|
|
565
|
+
inputs: [
|
|
566
|
+
{
|
|
567
|
+
components: [
|
|
568
|
+
{ name: "tokenIn", type: "address" },
|
|
569
|
+
{ name: "tokenOut", type: "address" },
|
|
570
|
+
{ name: "amountIn", type: "uint256" },
|
|
571
|
+
{ name: "fee", type: "uint24" },
|
|
572
|
+
{ name: "sqrtPriceLimitX96", type: "uint160" }
|
|
573
|
+
],
|
|
574
|
+
name: "params",
|
|
575
|
+
type: "tuple"
|
|
576
|
+
}
|
|
577
|
+
],
|
|
578
|
+
name: "quoteExactInputSingle",
|
|
579
|
+
outputs: [
|
|
580
|
+
{ name: "amountOut", type: "uint256" },
|
|
581
|
+
{ name: "sqrtPriceX96After", type: "uint160" },
|
|
582
|
+
{ name: "initializedTicksCrossed", type: "uint32" },
|
|
583
|
+
{ name: "gasEstimate", type: "uint256" }
|
|
584
|
+
],
|
|
585
|
+
stateMutability: "nonpayable",
|
|
586
|
+
type: "function"
|
|
587
|
+
}
|
|
588
|
+
];
|
|
589
|
+
var swapRouterAbi = [
|
|
590
|
+
{
|
|
591
|
+
inputs: [
|
|
592
|
+
{
|
|
593
|
+
components: [
|
|
594
|
+
{ name: "tokenIn", type: "address" },
|
|
595
|
+
{ name: "tokenOut", type: "address" },
|
|
596
|
+
{ name: "fee", type: "uint24" },
|
|
597
|
+
{ name: "recipient", type: "address" },
|
|
598
|
+
{ name: "amountIn", type: "uint256" },
|
|
599
|
+
{ name: "amountOutMinimum", type: "uint256" },
|
|
600
|
+
{ name: "sqrtPriceLimitX96", type: "uint160" }
|
|
601
|
+
],
|
|
602
|
+
name: "params",
|
|
603
|
+
type: "tuple"
|
|
604
|
+
}
|
|
605
|
+
],
|
|
606
|
+
name: "exactInputSingle",
|
|
607
|
+
outputs: [{ name: "amountOut", type: "uint256" }],
|
|
608
|
+
stateMutability: "payable",
|
|
609
|
+
type: "function"
|
|
610
|
+
}
|
|
611
|
+
];
|
|
612
|
+
function getTokenAddress(token) {
|
|
613
|
+
if (token.toLowerCase() === NATIVE_TOKEN_ADDRESS3.toLowerCase()) {
|
|
614
|
+
return WMNT_ADDRESS;
|
|
615
|
+
}
|
|
616
|
+
return token;
|
|
617
|
+
}
|
|
618
|
+
async function getUniswapQuoteData(agent, tokenIn, tokenOut, amountIn, fee = DEFAULT_POOL_FEE) {
|
|
619
|
+
try {
|
|
620
|
+
const result = await agent.client.simulateContract({
|
|
621
|
+
address: QUOTER_V2_ADDRESS,
|
|
622
|
+
abi: quoterV2Abi,
|
|
623
|
+
functionName: "quoteExactInputSingle",
|
|
624
|
+
args: [
|
|
625
|
+
{
|
|
626
|
+
tokenIn: getTokenAddress(tokenIn),
|
|
627
|
+
tokenOut: getTokenAddress(tokenOut),
|
|
628
|
+
amountIn: BigInt(amountIn),
|
|
629
|
+
fee,
|
|
630
|
+
sqrtPriceLimitX96: BigInt(0)
|
|
631
|
+
}
|
|
632
|
+
]
|
|
633
|
+
});
|
|
634
|
+
const [amountOut, sqrtPriceX96After, , gasEstimate] = result.result;
|
|
635
|
+
return {
|
|
636
|
+
amountOut,
|
|
637
|
+
sqrtPriceX96After,
|
|
638
|
+
gasEstimate
|
|
639
|
+
};
|
|
640
|
+
} catch (error) {
|
|
641
|
+
console.error("Failed to get Uniswap quote:", error);
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function buildSwapCalldata(params, fee = DEFAULT_POOL_FEE) {
|
|
646
|
+
return encodeFunctionData({
|
|
647
|
+
abi: swapRouterAbi,
|
|
648
|
+
functionName: "exactInputSingle",
|
|
649
|
+
args: [
|
|
650
|
+
{
|
|
651
|
+
tokenIn: getTokenAddress(params.tokenIn),
|
|
652
|
+
tokenOut: getTokenAddress(params.tokenOut),
|
|
653
|
+
fee,
|
|
654
|
+
recipient: params.recipient,
|
|
655
|
+
amountIn: BigInt(params.amountIn),
|
|
656
|
+
amountOutMinimum: BigInt(params.amountOutMinimum),
|
|
657
|
+
sqrtPriceLimitX96: BigInt(0)
|
|
658
|
+
}
|
|
659
|
+
]
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// src/tools/uniswap/getQuote.ts
|
|
664
|
+
async function getUniswapQuote(agent, fromToken, toToken, amount, fee = DEFAULT_POOL_FEE) {
|
|
665
|
+
return await getUniswapQuoteData(agent, fromToken, toToken, amount, fee);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// src/tools/uniswap/swap.ts
|
|
669
|
+
async function swapOnUniswap(agent, fromToken, toToken, amount, slippage = "0.5", fee = DEFAULT_POOL_FEE) {
|
|
670
|
+
const walletAddress = agent.account.address;
|
|
671
|
+
const isNativeIn = fromToken.toLowerCase() === NATIVE_TOKEN_ADDRESS3.toLowerCase();
|
|
672
|
+
const quote = await getUniswapQuoteData(agent, fromToken, toToken, amount, fee);
|
|
673
|
+
console.log(`Uniswap quote: ${quote.amountOut.toString()}`);
|
|
674
|
+
const slippageBps = Math.floor(parseFloat(slippage) * 100);
|
|
675
|
+
const amountOutMinimum = quote.amountOut * BigInt(1e4 - slippageBps) / BigInt(1e4);
|
|
676
|
+
if (!isNativeIn) {
|
|
677
|
+
await approveToken2(agent, fromToken, SWAP_ROUTER_ADDRESS, amount);
|
|
678
|
+
}
|
|
679
|
+
const calldata = buildSwapCalldata(
|
|
680
|
+
{
|
|
681
|
+
tokenIn: fromToken,
|
|
682
|
+
tokenOut: toToken,
|
|
683
|
+
amountIn: amount,
|
|
684
|
+
amountOutMinimum: amountOutMinimum.toString(),
|
|
685
|
+
recipient: walletAddress
|
|
686
|
+
},
|
|
687
|
+
fee
|
|
688
|
+
);
|
|
689
|
+
const gasEstimate = await agent.client.estimateGas({
|
|
690
|
+
account: agent.account,
|
|
691
|
+
to: SWAP_ROUTER_ADDRESS,
|
|
692
|
+
data: calldata,
|
|
693
|
+
value: isNativeIn ? BigInt(amount) : BigInt(0)
|
|
694
|
+
});
|
|
695
|
+
const txHash = await agent.client.sendTransaction({
|
|
696
|
+
to: SWAP_ROUTER_ADDRESS,
|
|
697
|
+
data: calldata,
|
|
698
|
+
value: isNativeIn ? BigInt(amount) : BigInt(0),
|
|
699
|
+
gas: gasEstimate
|
|
700
|
+
});
|
|
701
|
+
console.log(`Uniswap swap tx sent: ${txHash}`);
|
|
702
|
+
const receipt = await agent.client.waitForTransactionReceipt({
|
|
703
|
+
hash: txHash
|
|
704
|
+
});
|
|
705
|
+
console.log(`Swap confirmed: ${receipt.status}`);
|
|
706
|
+
return {
|
|
707
|
+
txHash,
|
|
708
|
+
amountOut: quote.amountOut.toString()
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// src/constants/squid/index.ts
|
|
713
|
+
var squid_exports = {};
|
|
714
|
+
__export(squid_exports, {
|
|
715
|
+
DESTINATION_CHAINS: () => DESTINATION_CHAINS,
|
|
716
|
+
NATIVE_TOKEN_ADDRESS: () => NATIVE_TOKEN_ADDRESS4,
|
|
717
|
+
SQUID_BASE_URL: () => SQUID_BASE_URL,
|
|
718
|
+
SQUID_CHAIN_ID: () => SQUID_CHAIN_ID
|
|
719
|
+
});
|
|
720
|
+
var SQUID_BASE_URL = "https://api.squidrouter.com/v2";
|
|
721
|
+
var SQUID_CHAIN_ID = {
|
|
722
|
+
mainnet: 5e3,
|
|
723
|
+
// Mantle mainnet
|
|
724
|
+
testnet: 5003
|
|
725
|
+
// Mantle testnet
|
|
726
|
+
};
|
|
727
|
+
var DESTINATION_CHAINS = {
|
|
728
|
+
ethereum: 101,
|
|
729
|
+
arbitrum: 110,
|
|
730
|
+
optimism: 111,
|
|
731
|
+
polygon: 109,
|
|
732
|
+
base: 184,
|
|
733
|
+
bsc: 102,
|
|
734
|
+
avalanche: 106,
|
|
735
|
+
mantle: 5e3
|
|
736
|
+
};
|
|
737
|
+
var NATIVE_TOKEN_ADDRESS4 = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
738
|
+
|
|
739
|
+
// src/helpers/squid/index.ts
|
|
740
|
+
function getHeaders4(integratorId) {
|
|
741
|
+
const headers = {
|
|
742
|
+
"Content-Type": "application/json"
|
|
743
|
+
};
|
|
744
|
+
return headers;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// src/utils/squid/getRouteData.ts
|
|
748
|
+
async function getRouteData(fromToken, toToken, fromChain, toChain, amount, fromAddress, slippage = 1) {
|
|
749
|
+
const params = {
|
|
750
|
+
fromChain: fromChain.toString(),
|
|
751
|
+
toChain: toChain.toString(),
|
|
752
|
+
fromToken: fromToken.toLowerCase() === NATIVE_TOKEN_ADDRESS4.toLowerCase() ? "native" : fromToken,
|
|
753
|
+
toToken: toToken.toLowerCase() === NATIVE_TOKEN_ADDRESS4.toLowerCase() ? "native" : toToken,
|
|
754
|
+
fromAmount: amount,
|
|
755
|
+
fromAddress,
|
|
756
|
+
slippage: slippage.toString(),
|
|
757
|
+
enableForecast: false
|
|
758
|
+
};
|
|
759
|
+
const response = await axios6.get(`${SQUID_BASE_URL}/route`, {
|
|
760
|
+
params,
|
|
761
|
+
headers: getHeaders4()
|
|
762
|
+
});
|
|
763
|
+
return response.data;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// src/tools/squid/crossChainSwap.ts
|
|
767
|
+
async function crossChainSwapViaSquid(agent, fromToken, toToken, fromChain, toChain, amount, slippage = 1) {
|
|
768
|
+
const routeData = await getRouteData(
|
|
769
|
+
fromToken,
|
|
770
|
+
toToken,
|
|
771
|
+
fromChain,
|
|
772
|
+
toChain,
|
|
773
|
+
amount,
|
|
774
|
+
agent.account.address,
|
|
775
|
+
slippage
|
|
776
|
+
);
|
|
777
|
+
const transactionRequest = routeData.route.transactionRequest;
|
|
778
|
+
if (fromToken.toLowerCase() !== NATIVE_TOKEN_ADDRESS4.toLowerCase() && transactionRequest.targetAddress) {
|
|
779
|
+
await approveToken2(
|
|
780
|
+
agent,
|
|
781
|
+
fromToken,
|
|
782
|
+
transactionRequest.targetAddress,
|
|
783
|
+
amount
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
const hash = await agent.client.sendTransaction({
|
|
787
|
+
to: transactionRequest.targetAddress,
|
|
788
|
+
data: transactionRequest.data,
|
|
789
|
+
value: BigInt(transactionRequest.value || "0"),
|
|
790
|
+
gas: BigInt(transactionRequest.gasLimit || "0")
|
|
791
|
+
});
|
|
792
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
793
|
+
return hash;
|
|
794
|
+
}
|
|
795
|
+
async function getSquidRoute(agent, fromToken, toToken, fromChain, toChain, amount, slippage = 1) {
|
|
796
|
+
return await getRouteData(
|
|
797
|
+
fromToken,
|
|
798
|
+
toToken,
|
|
799
|
+
fromChain,
|
|
800
|
+
toChain,
|
|
801
|
+
amount,
|
|
802
|
+
agent.account.address,
|
|
803
|
+
slippage
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// src/constants/lendle/index.ts
|
|
808
|
+
var lendle_exports = {};
|
|
809
|
+
__export(lendle_exports, {
|
|
810
|
+
INTEREST_RATE_MODE: () => INTEREST_RATE_MODE,
|
|
811
|
+
LENDING_POOL: () => LENDING_POOL,
|
|
812
|
+
LENDING_POOL_ABI: () => LENDING_POOL_ABI,
|
|
813
|
+
LENDING_POOL_ADDRESSES_PROVIDER: () => LENDING_POOL_ADDRESSES_PROVIDER,
|
|
814
|
+
ORACLE: () => ORACLE,
|
|
815
|
+
PROTOCOL_DATA_PROVIDER: () => PROTOCOL_DATA_PROVIDER,
|
|
816
|
+
WMNT_ADDRESS: () => WMNT_ADDRESS2
|
|
817
|
+
});
|
|
818
|
+
var LENDING_POOL = {
|
|
819
|
+
mainnet: "0xCFa5aE7c2CE8Fadc6426C1ff872cA45378Fb7cF3",
|
|
820
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
821
|
+
// Not deployed on testnet
|
|
822
|
+
};
|
|
823
|
+
var LENDING_POOL_ADDRESSES_PROVIDER = {
|
|
824
|
+
mainnet: "0xAb94Bedd21ae3411eB2698945dfCab1D5C19C3d4",
|
|
825
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
826
|
+
};
|
|
827
|
+
var PROTOCOL_DATA_PROVIDER = {
|
|
828
|
+
mainnet: "0x552b9e4bae485C4B7F540777d7D25614CdB84773",
|
|
829
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
830
|
+
};
|
|
831
|
+
var ORACLE = {
|
|
832
|
+
mainnet: "0x870c9692Ab04944C86ec6FEeF63F261226506EfC",
|
|
833
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
834
|
+
};
|
|
835
|
+
var WMNT_ADDRESS2 = "0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8";
|
|
836
|
+
var INTEREST_RATE_MODE = {
|
|
837
|
+
STABLE: 1,
|
|
838
|
+
VARIABLE: 2
|
|
839
|
+
};
|
|
840
|
+
var LENDING_POOL_ABI = [
|
|
841
|
+
{
|
|
842
|
+
inputs: [
|
|
843
|
+
{ name: "asset", type: "address" },
|
|
844
|
+
{ name: "amount", type: "uint256" },
|
|
845
|
+
{ name: "onBehalfOf", type: "address" },
|
|
846
|
+
{ name: "referralCode", type: "uint16" }
|
|
847
|
+
],
|
|
848
|
+
name: "deposit",
|
|
849
|
+
outputs: [],
|
|
850
|
+
stateMutability: "nonpayable",
|
|
851
|
+
type: "function"
|
|
852
|
+
},
|
|
853
|
+
{
|
|
854
|
+
inputs: [
|
|
855
|
+
{ name: "asset", type: "address" },
|
|
856
|
+
{ name: "amount", type: "uint256" },
|
|
857
|
+
{ name: "to", type: "address" }
|
|
858
|
+
],
|
|
859
|
+
name: "withdraw",
|
|
860
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
861
|
+
stateMutability: "nonpayable",
|
|
862
|
+
type: "function"
|
|
863
|
+
},
|
|
864
|
+
{
|
|
865
|
+
inputs: [
|
|
866
|
+
{ name: "asset", type: "address" },
|
|
867
|
+
{ name: "amount", type: "uint256" },
|
|
868
|
+
{ name: "interestRateMode", type: "uint256" },
|
|
869
|
+
{ name: "referralCode", type: "uint16" },
|
|
870
|
+
{ name: "onBehalfOf", type: "address" }
|
|
871
|
+
],
|
|
872
|
+
name: "borrow",
|
|
873
|
+
outputs: [],
|
|
874
|
+
stateMutability: "nonpayable",
|
|
875
|
+
type: "function"
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
inputs: [
|
|
879
|
+
{ name: "asset", type: "address" },
|
|
880
|
+
{ name: "amount", type: "uint256" },
|
|
881
|
+
{ name: "rateMode", type: "uint256" },
|
|
882
|
+
{ name: "onBehalfOf", type: "address" }
|
|
883
|
+
],
|
|
884
|
+
name: "repay",
|
|
885
|
+
outputs: [{ name: "", type: "uint256" }],
|
|
886
|
+
stateMutability: "nonpayable",
|
|
887
|
+
type: "function"
|
|
888
|
+
},
|
|
889
|
+
{
|
|
890
|
+
inputs: [{ name: "user", type: "address" }],
|
|
891
|
+
name: "getUserAccountData",
|
|
892
|
+
outputs: [
|
|
893
|
+
{ name: "totalCollateralETH", type: "uint256" },
|
|
894
|
+
{ name: "totalDebtETH", type: "uint256" },
|
|
895
|
+
{ name: "availableBorrowsETH", type: "uint256" },
|
|
896
|
+
{ name: "currentLiquidationThreshold", type: "uint256" },
|
|
897
|
+
{ name: "ltv", type: "uint256" },
|
|
898
|
+
{ name: "healthFactor", type: "uint256" }
|
|
899
|
+
],
|
|
900
|
+
stateMutability: "view",
|
|
901
|
+
type: "function"
|
|
902
|
+
},
|
|
903
|
+
{
|
|
904
|
+
inputs: [{ name: "asset", type: "address" }],
|
|
905
|
+
name: "getReserveData",
|
|
906
|
+
outputs: [
|
|
907
|
+
{
|
|
908
|
+
components: [
|
|
909
|
+
{ name: "configuration", type: "uint256" },
|
|
910
|
+
{ name: "liquidityIndex", type: "uint128" },
|
|
911
|
+
{ name: "variableBorrowIndex", type: "uint128" },
|
|
912
|
+
{ name: "currentLiquidityRate", type: "uint128" },
|
|
913
|
+
{ name: "currentVariableBorrowRate", type: "uint128" },
|
|
914
|
+
{ name: "currentStableBorrowRate", type: "uint128" },
|
|
915
|
+
{ name: "lastUpdateTimestamp", type: "uint40" },
|
|
916
|
+
{ name: "aTokenAddress", type: "address" },
|
|
917
|
+
{ name: "stableDebtTokenAddress", type: "address" },
|
|
918
|
+
{ name: "variableDebtTokenAddress", type: "address" },
|
|
919
|
+
{ name: "interestRateStrategyAddress", type: "address" },
|
|
920
|
+
{ name: "id", type: "uint8" }
|
|
921
|
+
],
|
|
922
|
+
name: "",
|
|
923
|
+
type: "tuple"
|
|
924
|
+
}
|
|
925
|
+
],
|
|
926
|
+
stateMutability: "view",
|
|
927
|
+
type: "function"
|
|
928
|
+
}
|
|
929
|
+
];
|
|
930
|
+
|
|
931
|
+
// src/tools/lendle/supply.ts
|
|
932
|
+
async function lendleSupply(agent, tokenAddress, amount) {
|
|
933
|
+
const lendingPoolAddress = LENDING_POOL[agent.chain];
|
|
934
|
+
if (lendingPoolAddress === "0x0000000000000000000000000000000000000000") {
|
|
935
|
+
throw new Error(
|
|
936
|
+
`Lendle LendingPool not configured for ${agent.chain}. Only available on mainnet.`
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
const amountBigInt = BigInt(amount);
|
|
940
|
+
const isNative = tokenAddress.toLowerCase() === WMNT_ADDRESS2.toLowerCase();
|
|
941
|
+
if (!isNative) {
|
|
942
|
+
await approveToken2(agent, tokenAddress, lendingPoolAddress, amount);
|
|
943
|
+
}
|
|
944
|
+
const data = encodeFunctionData({
|
|
945
|
+
abi: LENDING_POOL_ABI,
|
|
946
|
+
functionName: "deposit",
|
|
947
|
+
args: [tokenAddress, amountBigInt, agent.account.address, 0]
|
|
948
|
+
// referralCode = 0 (uint16)
|
|
949
|
+
});
|
|
950
|
+
const hash = await agent.client.sendTransaction({
|
|
951
|
+
to: lendingPoolAddress,
|
|
952
|
+
data,
|
|
953
|
+
value: isNative ? amountBigInt : 0n
|
|
954
|
+
});
|
|
955
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
956
|
+
return hash;
|
|
957
|
+
}
|
|
958
|
+
async function lendleWithdraw(agent, tokenAddress, amount, to) {
|
|
959
|
+
const lendingPoolAddress = LENDING_POOL[agent.chain];
|
|
960
|
+
if (lendingPoolAddress === "0x0000000000000000000000000000000000000000") {
|
|
961
|
+
throw new Error(
|
|
962
|
+
`Lendle LendingPool not configured for ${agent.chain}. Only available on mainnet.`
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
const amountBigInt = BigInt(amount);
|
|
966
|
+
const toAddress = to || agent.account.address;
|
|
967
|
+
const data = encodeFunctionData({
|
|
968
|
+
abi: LENDING_POOL_ABI,
|
|
969
|
+
functionName: "withdraw",
|
|
970
|
+
args: [tokenAddress, amountBigInt, toAddress]
|
|
971
|
+
});
|
|
972
|
+
const hash = await agent.client.sendTransaction({
|
|
973
|
+
to: lendingPoolAddress,
|
|
974
|
+
data
|
|
975
|
+
});
|
|
976
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
977
|
+
return hash;
|
|
978
|
+
}
|
|
979
|
+
async function lendleBorrow(agent, tokenAddress, amount, interestRateMode = INTEREST_RATE_MODE.VARIABLE, onBehalfOf) {
|
|
980
|
+
const lendingPoolAddress = LENDING_POOL[agent.chain];
|
|
981
|
+
if (lendingPoolAddress === "0x0000000000000000000000000000000000000000") {
|
|
982
|
+
throw new Error(
|
|
983
|
+
`Lendle LendingPool not configured for ${agent.chain}. Only available on mainnet.`
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
const amountBigInt = BigInt(amount);
|
|
987
|
+
const onBehalfOfAddress = onBehalfOf || agent.account.address;
|
|
988
|
+
const data = encodeFunctionData({
|
|
989
|
+
abi: LENDING_POOL_ABI,
|
|
990
|
+
functionName: "borrow",
|
|
991
|
+
args: [tokenAddress, amountBigInt, BigInt(interestRateMode), 0, onBehalfOfAddress]
|
|
992
|
+
// referralCode = 0 (uint16)
|
|
993
|
+
});
|
|
994
|
+
const hash = await agent.client.sendTransaction({
|
|
995
|
+
to: lendingPoolAddress,
|
|
996
|
+
data
|
|
997
|
+
});
|
|
998
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
999
|
+
return hash;
|
|
1000
|
+
}
|
|
1001
|
+
async function lendleRepay(agent, tokenAddress, amount, rateMode = INTEREST_RATE_MODE.VARIABLE, onBehalfOf) {
|
|
1002
|
+
const lendingPoolAddress = LENDING_POOL[agent.chain];
|
|
1003
|
+
if (lendingPoolAddress === "0x0000000000000000000000000000000000000000") {
|
|
1004
|
+
throw new Error(
|
|
1005
|
+
`Lendle LendingPool not configured for ${agent.chain}. Only available on mainnet.`
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
const amountBigInt = BigInt(amount);
|
|
1009
|
+
const onBehalfOfAddress = onBehalfOf || agent.account.address;
|
|
1010
|
+
await approveToken2(agent, tokenAddress, lendingPoolAddress, amount);
|
|
1011
|
+
const data = encodeFunctionData({
|
|
1012
|
+
abi: LENDING_POOL_ABI,
|
|
1013
|
+
functionName: "repay",
|
|
1014
|
+
args: [tokenAddress, amountBigInt, BigInt(rateMode), onBehalfOfAddress]
|
|
1015
|
+
});
|
|
1016
|
+
const hash = await agent.client.sendTransaction({
|
|
1017
|
+
to: lendingPoolAddress,
|
|
1018
|
+
data
|
|
1019
|
+
});
|
|
1020
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
1021
|
+
return hash;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// src/constants/agni/index.ts
|
|
1025
|
+
var agni_exports = {};
|
|
1026
|
+
__export(agni_exports, {
|
|
1027
|
+
FACTORY: () => FACTORY,
|
|
1028
|
+
FEE_TIERS: () => FEE_TIERS2,
|
|
1029
|
+
POSITION_MANAGER: () => POSITION_MANAGER,
|
|
1030
|
+
SWAP_ROUTER: () => SWAP_ROUTER,
|
|
1031
|
+
SWAP_ROUTER_ABI: () => SWAP_ROUTER_ABI
|
|
1032
|
+
});
|
|
1033
|
+
var FACTORY = {
|
|
1034
|
+
mainnet: "0x25780dc8Fc3cfBD75F33bFDAB65e969b603b2035",
|
|
1035
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1036
|
+
};
|
|
1037
|
+
var SWAP_ROUTER = {
|
|
1038
|
+
mainnet: "0x319B69888b0d11cEC22caA5034e25FfFBDc88421",
|
|
1039
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1040
|
+
};
|
|
1041
|
+
var POSITION_MANAGER = {
|
|
1042
|
+
mainnet: "0x218bf598d1453383e2f4aa7b14ffb9bfb102d637",
|
|
1043
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1044
|
+
};
|
|
1045
|
+
var SWAP_ROUTER_ABI = [
|
|
1046
|
+
{
|
|
1047
|
+
inputs: [
|
|
1048
|
+
{
|
|
1049
|
+
components: [
|
|
1050
|
+
{ name: "tokenIn", type: "address" },
|
|
1051
|
+
{ name: "tokenOut", type: "address" },
|
|
1052
|
+
{ name: "fee", type: "uint24" },
|
|
1053
|
+
{ name: "recipient", type: "address" },
|
|
1054
|
+
{ name: "deadline", type: "uint256" },
|
|
1055
|
+
{ name: "amountIn", type: "uint256" },
|
|
1056
|
+
{ name: "amountOutMinimum", type: "uint256" },
|
|
1057
|
+
{ name: "sqrtPriceLimitX96", type: "uint160" }
|
|
1058
|
+
],
|
|
1059
|
+
name: "params",
|
|
1060
|
+
type: "tuple"
|
|
1061
|
+
}
|
|
1062
|
+
],
|
|
1063
|
+
name: "exactInputSingle",
|
|
1064
|
+
outputs: [{ name: "amountOut", type: "uint256" }],
|
|
1065
|
+
stateMutability: "payable",
|
|
1066
|
+
type: "function"
|
|
1067
|
+
}
|
|
1068
|
+
];
|
|
1069
|
+
var FEE_TIERS2 = {
|
|
1070
|
+
LOWEST: 100,
|
|
1071
|
+
// 0.01%
|
|
1072
|
+
LOW: 500,
|
|
1073
|
+
// 0.05%
|
|
1074
|
+
MEDIUM: 3e3,
|
|
1075
|
+
// 0.3%
|
|
1076
|
+
HIGH: 1e4
|
|
1077
|
+
// 1%
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
// src/tools/agni/swap.ts
|
|
1081
|
+
async function agniSwap(agent, tokenIn, tokenOut, amountIn, slippagePercent = 0.5, feeTier = FEE_TIERS2.MEDIUM) {
|
|
1082
|
+
const swapRouterAddress = SWAP_ROUTER[agent.chain];
|
|
1083
|
+
if (swapRouterAddress === "0x0000000000000000000000000000000000000000") {
|
|
1084
|
+
throw new Error(`Agni SwapRouter not available on ${agent.chain}`);
|
|
1085
|
+
}
|
|
1086
|
+
const amountInBigInt = BigInt(amountIn);
|
|
1087
|
+
const deadline = BigInt(Math.floor(Date.now() / 1e3) + 1200);
|
|
1088
|
+
const amountOutMinimum = amountInBigInt * BigInt(1e4 - Math.floor(slippagePercent * 100)) / 10000n;
|
|
1089
|
+
await approveToken2(agent, tokenIn, swapRouterAddress, amountIn);
|
|
1090
|
+
const data = encodeFunctionData({
|
|
1091
|
+
abi: SWAP_ROUTER_ABI,
|
|
1092
|
+
functionName: "exactInputSingle",
|
|
1093
|
+
args: [
|
|
1094
|
+
{
|
|
1095
|
+
tokenIn,
|
|
1096
|
+
tokenOut,
|
|
1097
|
+
fee: feeTier,
|
|
1098
|
+
recipient: agent.account.address,
|
|
1099
|
+
deadline,
|
|
1100
|
+
amountIn: amountInBigInt,
|
|
1101
|
+
amountOutMinimum,
|
|
1102
|
+
sqrtPriceLimitX96: 0n
|
|
1103
|
+
}
|
|
1104
|
+
]
|
|
1105
|
+
});
|
|
1106
|
+
const hash = await agent.client.sendTransaction({
|
|
1107
|
+
to: swapRouterAddress,
|
|
1108
|
+
data
|
|
1109
|
+
});
|
|
1110
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
1111
|
+
return hash;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
// src/constants/merchantmoe/index.ts
|
|
1115
|
+
var merchantmoe_exports = {};
|
|
1116
|
+
__export(merchantmoe_exports, {
|
|
1117
|
+
LB_FACTORY: () => LB_FACTORY,
|
|
1118
|
+
LB_QUOTER: () => LB_QUOTER,
|
|
1119
|
+
LB_ROUTER: () => LB_ROUTER,
|
|
1120
|
+
LB_ROUTER_ABI: () => LB_ROUTER_ABI,
|
|
1121
|
+
MOE_TOKEN: () => MOE_TOKEN
|
|
1122
|
+
});
|
|
1123
|
+
var LB_ROUTER = {
|
|
1124
|
+
mainnet: "0x013e138EF6008ae5FDFDE29700e3f2Bc61d21E3a",
|
|
1125
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1126
|
+
};
|
|
1127
|
+
var LB_FACTORY = {
|
|
1128
|
+
mainnet: "0xa6630671775c4EA2743840F9A5016dCf2A104054",
|
|
1129
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1130
|
+
};
|
|
1131
|
+
var LB_QUOTER = {
|
|
1132
|
+
mainnet: "0x501b8AFd35df20f531fF45F6f695793AC3316c85",
|
|
1133
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1134
|
+
};
|
|
1135
|
+
var MOE_TOKEN = "0x4515a45337f461a11ff0fe8abf3c606ae5dc00c9";
|
|
1136
|
+
var LB_ROUTER_ABI = [
|
|
1137
|
+
{
|
|
1138
|
+
inputs: [
|
|
1139
|
+
{ name: "amountIn", type: "uint256" },
|
|
1140
|
+
{ name: "amountOutMin", type: "uint256" },
|
|
1141
|
+
{ name: "path", type: "uint256[]" },
|
|
1142
|
+
{ name: "to", type: "address" },
|
|
1143
|
+
{ name: "deadline", type: "uint256" }
|
|
1144
|
+
],
|
|
1145
|
+
name: "swapExactTokensForTokens",
|
|
1146
|
+
outputs: [{ name: "amountOut", type: "uint256" }],
|
|
1147
|
+
stateMutability: "nonpayable",
|
|
1148
|
+
type: "function"
|
|
1149
|
+
}
|
|
1150
|
+
];
|
|
1151
|
+
|
|
1152
|
+
// src/tools/merchantmoe/swap.ts
|
|
1153
|
+
async function merchantMoeSwap(agent, tokenIn, tokenOut, amountIn, slippagePercent = 0.5) {
|
|
1154
|
+
const routerAddress = LB_ROUTER[agent.chain];
|
|
1155
|
+
if (routerAddress === "0x0000000000000000000000000000000000000000") {
|
|
1156
|
+
throw new Error(`Merchant Moe LB Router not available on ${agent.chain}`);
|
|
1157
|
+
}
|
|
1158
|
+
const amountInBigInt = BigInt(amountIn);
|
|
1159
|
+
const deadline = BigInt(Math.floor(Date.now() / 1e3) + 1200);
|
|
1160
|
+
const amountOutMin = amountInBigInt * BigInt(1e4 - Math.floor(slippagePercent * 100)) / 10000n;
|
|
1161
|
+
await approveToken2(agent, tokenIn, routerAddress, amountIn);
|
|
1162
|
+
const path = [BigInt(tokenIn), BigInt(tokenOut)];
|
|
1163
|
+
const data = encodeFunctionData({
|
|
1164
|
+
abi: LB_ROUTER_ABI,
|
|
1165
|
+
functionName: "swapExactTokensForTokens",
|
|
1166
|
+
args: [amountInBigInt, amountOutMin, path, agent.account.address, deadline]
|
|
1167
|
+
});
|
|
1168
|
+
const hash = await agent.client.sendTransaction({
|
|
1169
|
+
to: routerAddress,
|
|
1170
|
+
data
|
|
1171
|
+
});
|
|
1172
|
+
await agent.client.waitForTransactionReceipt({ hash });
|
|
1173
|
+
return hash;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// src/constants/meth/index.ts
|
|
1177
|
+
var meth_exports = {};
|
|
1178
|
+
__export(meth_exports, {
|
|
1179
|
+
METH_TOKEN: () => METH_TOKEN
|
|
1180
|
+
});
|
|
1181
|
+
var METH_TOKEN = {
|
|
1182
|
+
mainnet: "0xcDA86A272531e8640cD7F1a92c01839911B90bb0",
|
|
1183
|
+
testnet: "0x0000000000000000000000000000000000000000"
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
// src/tools/okx/getSwapQuote.ts
|
|
1187
|
+
var getSwapQuote = async (agent, from, to, amount, slippagePercentage) => {
|
|
1188
|
+
const chainIndex = agent.chain === "mainnet" ? "5000" : "5003";
|
|
1189
|
+
return getSwapTransaction(from, to, amount, chainIndex, slippagePercentage);
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1192
|
+
// src/utils/x402/index.ts
|
|
1193
|
+
var DEFAULT_PLATFORM_URL = "https://mantle-x402.vercel.app";
|
|
1194
|
+
var cachedConfig = null;
|
|
1195
|
+
var validationPromise = null;
|
|
1196
|
+
function getPlatformBaseUrl() {
|
|
1197
|
+
if (typeof process !== "undefined" && process.env) {
|
|
1198
|
+
return process.env.PLATFORM_URL || process.env.NEXT_PUBLIC_PLATFORM_URL || DEFAULT_PLATFORM_URL;
|
|
1199
|
+
}
|
|
1200
|
+
return DEFAULT_PLATFORM_URL;
|
|
1201
|
+
}
|
|
1202
|
+
async function validateAppId(appId) {
|
|
1203
|
+
const baseUrl2 = getPlatformBaseUrl();
|
|
1204
|
+
const url = `${baseUrl2}/api/v1/validate?appId=${encodeURIComponent(appId)}`;
|
|
1205
|
+
const response = await fetch(url, {
|
|
1206
|
+
method: "GET",
|
|
1207
|
+
headers: { "Content-Type": "application/json" }
|
|
1208
|
+
});
|
|
1209
|
+
if (!response.ok) {
|
|
1210
|
+
if (response.status === 404) {
|
|
1211
|
+
throw new Error(
|
|
1212
|
+
"Platform: Project not found. Invalid APP_ID. Please check your APP_ID configuration."
|
|
1213
|
+
);
|
|
1214
|
+
}
|
|
1215
|
+
if (response.status === 401) {
|
|
1216
|
+
throw new Error(
|
|
1217
|
+
"Platform: Unauthorized. Invalid APP_ID. Please verify your APP_ID."
|
|
1218
|
+
);
|
|
1219
|
+
}
|
|
1220
|
+
throw new Error(
|
|
1221
|
+
`Platform: Validation failed: ${response.status} ${response.statusText}`
|
|
1222
|
+
);
|
|
1223
|
+
}
|
|
1224
|
+
const data = await response.json();
|
|
1225
|
+
if (!data.appId || !data.payTo || !data.network) {
|
|
1226
|
+
throw new Error("Platform: Invalid response - missing required fields");
|
|
1227
|
+
}
|
|
1228
|
+
if (data.status !== "ACTIVE") {
|
|
1229
|
+
throw new Error(
|
|
1230
|
+
`Platform: Project is not active. Current status: ${data.status}`
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
return {
|
|
1234
|
+
appId: data.appId,
|
|
1235
|
+
name: data.name,
|
|
1236
|
+
payTo: data.payTo,
|
|
1237
|
+
network: data.network,
|
|
1238
|
+
status: data.status
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
async function initializePlatform() {
|
|
1242
|
+
if (cachedConfig) {
|
|
1243
|
+
return cachedConfig;
|
|
1244
|
+
}
|
|
1245
|
+
if (validationPromise) {
|
|
1246
|
+
return validationPromise;
|
|
1247
|
+
}
|
|
1248
|
+
let appId;
|
|
1249
|
+
if (typeof process !== "undefined" && process.env) {
|
|
1250
|
+
appId = process.env.APP_ID || process.env.NEXT_PUBLIC_APP_ID;
|
|
1251
|
+
}
|
|
1252
|
+
if (!appId || typeof appId !== "string" || appId.trim().length === 0) {
|
|
1253
|
+
throw new Error(
|
|
1254
|
+
"APP_ID is required. Set it in your .env file:\nAPP_ID=your_app_id_here"
|
|
1255
|
+
);
|
|
1256
|
+
}
|
|
1257
|
+
validationPromise = validateAppId(appId.trim());
|
|
1258
|
+
try {
|
|
1259
|
+
cachedConfig = await validationPromise;
|
|
1260
|
+
return cachedConfig;
|
|
1261
|
+
} catch (error) {
|
|
1262
|
+
validationPromise = null;
|
|
1263
|
+
throw error;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
function getProjectConfig() {
|
|
1267
|
+
if (!cachedConfig) {
|
|
1268
|
+
throw new Error(
|
|
1269
|
+
"Platform not initialized. Call initializePlatform() first."
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
return cachedConfig;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
// src/agent.ts
|
|
1276
|
+
var MNTAgentKit = class {
|
|
1277
|
+
account;
|
|
1278
|
+
client;
|
|
1279
|
+
chain;
|
|
1280
|
+
projectConfig;
|
|
1281
|
+
constructor(privateKey, chain) {
|
|
1282
|
+
this.account = privateKeyToAccount(privateKey);
|
|
1283
|
+
this.chain = chain;
|
|
1284
|
+
this.client = createWalletClient({
|
|
1285
|
+
chain: chain == "mainnet" ? mantle : mantleSepoliaTestnet,
|
|
1286
|
+
transport: http(),
|
|
1287
|
+
account: this.account
|
|
1288
|
+
}).extend(publicActions);
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* Initialize the agent with platform validation
|
|
1292
|
+
*
|
|
1293
|
+
* Validates APP_ID with the platform API.
|
|
1294
|
+
* Must be called after creating the agent instance.
|
|
1295
|
+
*
|
|
1296
|
+
* @returns The initialized agent instance
|
|
1297
|
+
* @throws Error if APP_ID is not set or validation fails
|
|
1298
|
+
*
|
|
1299
|
+
* @example
|
|
1300
|
+
* ```typescript
|
|
1301
|
+
* const agent = new MNTAgentKit(privateKey, "mainnet");
|
|
1302
|
+
* await agent.initialize(); // Validates APP_ID
|
|
1303
|
+
* ```
|
|
1304
|
+
*/
|
|
1305
|
+
async initialize() {
|
|
1306
|
+
this.projectConfig = await initializePlatform();
|
|
1307
|
+
return this;
|
|
1308
|
+
}
|
|
1309
|
+
async sendTransaction(to, amount) {
|
|
1310
|
+
return await sendTransaction(this, to, amount);
|
|
1311
|
+
}
|
|
1312
|
+
// OKX DEX Aggregator
|
|
1313
|
+
async getSwapQuote(fromTokenAddress, toTokenAddress, amount, slippagePercentage = "0.5") {
|
|
1314
|
+
return await getSwapQuote(
|
|
1315
|
+
this,
|
|
1316
|
+
fromTokenAddress,
|
|
1317
|
+
toTokenAddress,
|
|
1318
|
+
amount,
|
|
1319
|
+
slippagePercentage
|
|
1320
|
+
);
|
|
1321
|
+
}
|
|
1322
|
+
async executeSwap(fromTokenAddress, toTokenAddress, amount, slippagePercentage = "0.5") {
|
|
1323
|
+
if (this.chain === "mainnet") {
|
|
1324
|
+
return await executeSwap(
|
|
1325
|
+
this,
|
|
1326
|
+
fromTokenAddress,
|
|
1327
|
+
toTokenAddress,
|
|
1328
|
+
amount,
|
|
1329
|
+
slippagePercentage
|
|
1330
|
+
);
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
// OpenOcean DEX Aggregator
|
|
1334
|
+
async getOpenOceanQuote(fromToken, toToken, amount) {
|
|
1335
|
+
return await getOpenOceanQuote(this, fromToken, toToken, amount);
|
|
1336
|
+
}
|
|
1337
|
+
async swapOnOpenOcean(fromToken, toToken, amount, slippage = 0.5) {
|
|
1338
|
+
return await swapOnOpenOcean(
|
|
1339
|
+
this,
|
|
1340
|
+
fromToken,
|
|
1341
|
+
toToken,
|
|
1342
|
+
amount,
|
|
1343
|
+
slippage.toString()
|
|
1344
|
+
);
|
|
1345
|
+
}
|
|
1346
|
+
// 1inch DEX Aggregator
|
|
1347
|
+
async get1inchQuote(fromToken, toToken, amount) {
|
|
1348
|
+
return await get1inchQuote(this, fromToken, toToken, amount);
|
|
1349
|
+
}
|
|
1350
|
+
async swapOn1inch(fromToken, toToken, amount, slippage = 0.5) {
|
|
1351
|
+
return await swapOn1inch(
|
|
1352
|
+
this,
|
|
1353
|
+
fromToken,
|
|
1354
|
+
toToken,
|
|
1355
|
+
amount,
|
|
1356
|
+
slippage.toString()
|
|
1357
|
+
);
|
|
1358
|
+
}
|
|
1359
|
+
// Uniswap V3 DEX
|
|
1360
|
+
async getUniswapQuote(fromToken, toToken, amount) {
|
|
1361
|
+
return await getUniswapQuote(this, fromToken, toToken, amount);
|
|
1362
|
+
}
|
|
1363
|
+
async swapOnUniswap(fromToken, toToken, amount, slippage = 0.5) {
|
|
1364
|
+
return await swapOnUniswap(
|
|
1365
|
+
this,
|
|
1366
|
+
fromToken,
|
|
1367
|
+
toToken,
|
|
1368
|
+
amount,
|
|
1369
|
+
slippage.toString()
|
|
1370
|
+
);
|
|
1371
|
+
}
|
|
1372
|
+
// Lendle Lending Protocol
|
|
1373
|
+
async lendleSupply(tokenAddress, amount) {
|
|
1374
|
+
return await lendleSupply(this, tokenAddress, amount);
|
|
1375
|
+
}
|
|
1376
|
+
async lendleWithdraw(tokenAddress, amount, to) {
|
|
1377
|
+
return await lendleWithdraw(this, tokenAddress, amount, to);
|
|
1378
|
+
}
|
|
1379
|
+
async lendleBorrow(tokenAddress, amount, interestRateMode = 2, onBehalfOf) {
|
|
1380
|
+
return await lendleBorrow(
|
|
1381
|
+
this,
|
|
1382
|
+
tokenAddress,
|
|
1383
|
+
amount,
|
|
1384
|
+
interestRateMode,
|
|
1385
|
+
onBehalfOf
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
async lendleRepay(tokenAddress, amount, rateMode = 2, onBehalfOf) {
|
|
1389
|
+
return await lendleRepay(this, tokenAddress, amount, rateMode, onBehalfOf);
|
|
1390
|
+
}
|
|
1391
|
+
// Agni Finance DEX (#1 on Mantle)
|
|
1392
|
+
async agniSwap(tokenIn, tokenOut, amountIn, slippagePercent = 0.5, feeTier) {
|
|
1393
|
+
return await agniSwap(
|
|
1394
|
+
this,
|
|
1395
|
+
tokenIn,
|
|
1396
|
+
tokenOut,
|
|
1397
|
+
amountIn,
|
|
1398
|
+
slippagePercent,
|
|
1399
|
+
feeTier
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
// Merchant Moe DEX (#2 on Mantle)
|
|
1403
|
+
async merchantMoeSwap(tokenIn, tokenOut, amountIn, slippagePercent = 0.5) {
|
|
1404
|
+
return await merchantMoeSwap(
|
|
1405
|
+
this,
|
|
1406
|
+
tokenIn,
|
|
1407
|
+
tokenOut,
|
|
1408
|
+
amountIn,
|
|
1409
|
+
slippagePercent
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1412
|
+
// mETH Protocol - Liquid Staking Token
|
|
1413
|
+
getMethTokenAddress() {
|
|
1414
|
+
return METH_TOKEN[this.chain];
|
|
1415
|
+
}
|
|
1416
|
+
// Squid Router Cross-chain
|
|
1417
|
+
async getSquidRoute(fromToken, toToken, fromChain, toChain, amount, slippage = 1) {
|
|
1418
|
+
return await getSquidRoute(
|
|
1419
|
+
this,
|
|
1420
|
+
fromToken,
|
|
1421
|
+
toToken,
|
|
1422
|
+
fromChain,
|
|
1423
|
+
toChain,
|
|
1424
|
+
amount,
|
|
1425
|
+
slippage
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
async crossChainSwapViaSquid(fromToken, toToken, fromChain, toChain, amount, slippage = 1) {
|
|
1429
|
+
return await crossChainSwapViaSquid(
|
|
1430
|
+
this,
|
|
1431
|
+
fromToken,
|
|
1432
|
+
toToken,
|
|
1433
|
+
fromChain,
|
|
1434
|
+
toChain,
|
|
1435
|
+
amount,
|
|
1436
|
+
slippage
|
|
1437
|
+
);
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
export { agni_exports as AgniConstants, lendle_exports as LendleConstants, METH_TOKEN, MNTAgentKit, merchantmoe_exports as MerchantMoeConstants, meth_exports as MethConstants, okx_exports as OKXConstants, oneinch_exports as OneInchConstants, openocean_exports as OpenOceanConstants, squid_exports as SquidConstants, uniswap_exports as UniswapConstants, agniSwap, approveToken, crossChainSwapViaSquid, executeSwap, get1inchQuote, getOpenOceanQuote, getProjectConfig, getSquidRoute, getUniswapQuote, initializePlatform, lendleBorrow, lendleRepay, lendleSupply, lendleWithdraw, merchantMoeSwap, sendTransaction, swapOn1inch, swapOnOpenOcean, swapOnUniswap };
|
|
1442
|
+
//# sourceMappingURL=index.js.map
|
|
1443
|
+
//# sourceMappingURL=index.js.map
|