-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
331 lines (281 loc) · 10.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
require('dotenv').config();
const { FusionSDK, NetworkEnum } = require('@1inch/fusion-sdk');
const { ethers } = require('ethers');
const axios = require('axios'); // Import axios for API calls
// Initialize the environment variables
const PRIVATE_KEY = process.env.PRIVATE_KEY;
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL); // Your RPC URL
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
// Initialize the SDK
const sdk = new FusionSDK({
url: 'https://api.1inch.dev/fusion',
network: NetworkEnum.BINANCE,
blockchainProvider: provider,
authKey: process.env.ONE_INCH_API_KEY,
});
// wBNB contract Address
const WBNB_ADDRESS = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
// Placeholder address for native BNB in the 1inch ecosystem
const BNB_PLACEHOLDER_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
// Function to manually sign the order
async function signTypedOrder(order, network) {
const typedData = order.getTypedData(network); // Get EIP-712 typed data from order
const domain = {
name: typedData.domain.name,
version: typedData.domain.version,
chainId: typedData.domain.chainId,
verifyingContract: typedData.domain.verifyingContract,
};
// Remove EIP712Domain from types
const types = { ...typedData.types };
delete types.EIP712Domain;
const value = typedData.message; // Use the correct message structure
// Manually sign the typed data using ethers.js
const signature = await wallet._signTypedData(domain, types, value);
return signature;
}
// Function to wrap BNB into WBNB
async function wrapBNB(amountInWei) {
const WBNB_ABI = ['function deposit() payable'];
const wbnbContract = new ethers.Contract(WBNB_ADDRESS, WBNB_ABI, wallet);
// Estimate gas
const gasEstimate = await wbnbContract.estimateGas.deposit({
value: amountInWei,
});
console.log(`Estimated gas for wrapping: ${gasEstimate.toString()}`);
const tx = await wbnbContract.deposit({
value: amountInWei,
gasLimit: gasEstimate,
});
await tx.wait();
console.log(`Wrapped ${ethers.utils.formatEther(amountInWei)} BNB into WBNB`);
}
// Function to unwrap WBNB into BNB
async function unwrapWBNB() {
const WBNB_ABI = [
'function withdraw(uint256)',
'function balanceOf(address) view returns (uint256)',
];
const wbnbContract = new ethers.Contract(WBNB_ADDRESS, WBNB_ABI, wallet);
// Get the WBNB balance
const wbnbBalance = await wbnbContract.balanceOf(wallet.address);
if (wbnbBalance.isZero()) {
console.log('No WBNB balance to unwrap.');
return;
}
// Estimate gas
const gasEstimate = await wbnbContract.estimateGas.withdraw(wbnbBalance);
console.log(`Estimated gas for unwrapping: ${gasEstimate.toString()}`);
const tx = await wbnbContract.withdraw(wbnbBalance, {
gasLimit: gasEstimate,
});
await tx.wait();
console.log(
`Unwrapped ${ethers.utils.formatEther(wbnbBalance)} WBNB into BNB`
);
}
// Function to approve any ERC20 token for Fusion contract using 1inch API
async function approveToken(tokenAddress, amountInWei) {
const chainId = 56; // BSC chain ID
const apiBase = `https://api.1inch.dev/swap/v6.0/${chainId}`;
const headers = {
Authorization: `Bearer ${process.env.ONE_INCH_API_KEY}`,
};
// Check current allowance using 1inch API
const allowanceUrl = `${apiBase}/approve/allowance`;
const allowanceParams = {
tokenAddress: tokenAddress,
walletAddress: wallet.address,
};
try {
const allowanceResponse = await axios.get(allowanceUrl, {
params: allowanceParams,
headers: headers,
});
const currentAllowance = ethers.BigNumber.from(
allowanceResponse.data.allowance
);
if (currentAllowance.gte(amountInWei)) {
console.log('Sufficient allowance already set for token:', tokenAddress);
return;
}
} catch (error) {
console.error(
'Error fetching allowance:',
error.response ? error.response.data : error.message
);
throw error;
}
// Get approval transaction data from 1inch API
const approveTxUrl = `${apiBase}/approve/transaction`;
const approveTxParams = {
tokenAddress: tokenAddress,
amount: amountInWei.toString(),
};
try {
const approveTxResponse = await axios.get(approveTxUrl, {
params: approveTxParams,
headers: headers,
});
const txData = approveTxResponse.data;
// Send the approval transaction
const tx = await wallet.sendTransaction({
to: txData.to,
data: txData.data,
value: txData.value
? ethers.BigNumber.from(txData.value)
: ethers.constants.Zero,
});
await tx.wait();
console.log(
`Approved ${ethers.utils.formatEther(
amountInWei
)} tokens to Fusion contract for ${tokenAddress}`
);
} catch (error) {
console.error(
'Error approving token:',
error.response ? error.response.data : error.message
);
throw error;
}
}
// Function to create and submit the order using SDK
async function createAndSubmitOrder(quote, fromTokenAddress) {
const orderParams = {
fromTokenAddress: fromTokenAddress, // The token you are swapping from
toTokenAddress: quote.params.toTokenAddress.val, // The token you are swapping to (from the quote)
amount: quote.fromTokenAmount.toString(), // Convert BigInt to string
walletAddress: wallet.address, // Your wallet address
receiver: '0x0000000000000000000000000000000000000000', // Optional, defaults to wallet address
preset: 'medium', // Use the recommended preset
allowPartialFills: false, // Disallow partial fills for simplicity
allowMultipleFills: false, // Disallow multiple fills for simplicity
};
// Generate the order using the SDK
const { order, quoteId } = await sdk.createOrder(orderParams);
// Manually sign the order
const signature = await signTypedOrder(order, NetworkEnum.BINANCE);
// Build the order struct and obtain the Order UID
const orderStruct = order.build();
const orderUid = order.getOrderHash(NetworkEnum.BINANCE); // This is the Order UID
// Ensure the orderUid is a valid 66-character hex string
if (!/^0x[0-9a-fA-F]{64}$/.test(orderUid)) {
throw new Error('Invalid order hash generated');
}
console.log('Order UID:', orderUid);
// Create the relayer request
const relayerRequest = {
order: orderStruct,
signature,
quoteId,
extension: order.extension.encode(),
};
try {
// Submit the order using the API directly
await sdk.api.submitOrder(relayerRequest);
console.log('Order submitted successfully');
} catch (error) {
console.error(
'Error submitting order:',
error.response ? error.response.data : error.message
);
throw error;
}
// Return the Order UID for further use
return orderUid;
}
// Function to get order status using Order UID
async function getOrderStatus(orderUid) {
try {
// Use the Fusion SDK to get the order status
const orderStatus = await sdk.getOrderStatus(orderUid);
console.log('Order Status:', orderStatus.status);
return orderStatus;
} catch (error) {
console.error(
'Error fetching order status:',
error.response ? error.response.data : error.message
);
}
}
// Simple delay function
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Main function to get quote, wrap BNB, approve token, create order, and fetch order status
async function main(fromTokenAddress, toTokenAddress, amountInWei) {
try {
let needToWrapBNB = false;
let needToUnwrapWBNB = false;
// Check if we are swapping from BNB
if (
fromTokenAddress === ethers.constants.AddressZero ||
fromTokenAddress.toLowerCase() === BNB_PLACEHOLDER_ADDRESS.toLowerCase()
) {
console.log('Swapping from BNB');
needToWrapBNB = true;
// Use WBNB address when getting the quote
fromTokenAddress = WBNB_ADDRESS;
}
// Check if we are swapping to BNB
if (
toTokenAddress === ethers.constants.AddressZero ||
toTokenAddress.toLowerCase() === BNB_PLACEHOLDER_ADDRESS.toLowerCase()
) {
console.log('Swapping to BNB');
needToUnwrapWBNB = true;
// Use WBNB address for swapping
toTokenAddress = WBNB_ADDRESS;
}
// Get quote using the SDK with WBNB as the fromTokenAddress
const quoteParams = {
fromTokenAddress: fromTokenAddress, // WBNB address
toTokenAddress: toTokenAddress, // Token to swap to
amount: amountInWei.toString(), // Amount to swap in wei
walletAddress: wallet.address, // Your wallet address
};
const quote = await sdk.getQuote(quoteParams);
console.log('Quote response:', quote);
// Wrap BNB if needed
if (needToWrapBNB) {
console.log('Wrapping BNB to WBNB...');
await wrapBNB(amountInWei); // Wrap BNB into WBNB
}
// Approve the `fromTokenAddress` for Fusion contract
await approveToken(fromTokenAddress, amountInWei);
// Wait for 2 seconds to comply with rate limit
await delay(2000);
// Create, sign, and submit the order, and get the Order UID
const orderUid = await createAndSubmitOrder(quote, fromTokenAddress);
// Wait for the order to be filled
console.log('Waiting for the order to be filled...');
let orderStatus;
while (true) {
await delay(10000); // Wait for 10 seconds
orderStatus = await getOrderStatus(orderUid);
if (orderStatus.status === 'filled') {
console.log('Order has been filled.');
console.log(orderStatus);
break;
} else if (orderStatus.status === 'cancelled') {
console.log('Order was cancelled.');
return;
} else {
console.log('Order not yet filled. Waiting...');
}
}
// Unwrap WBNB to BNB if swapping to BNB
if (needToUnwrapWBNB) {
console.log('Unwrapping WBNB to BNB...');
await unwrapWBNB(); // Unwrap WBNB to BNB
}
} catch (error) {
console.error('Error:', error);
}
}
// Execute the main function with dynamic tokens and amount
const amountToSwap = ethers.utils.parseUnits('0.005', 18); // Example amount: 0.005 BNB
const BNB_ADDRESS = ethers.constants.AddressZero; // BNB uses address zero
const USDC_ADDRESS = '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d'; // USDC token address on BSC
main(BNB_ADDRESS, USDC_ADDRESS, amountToSwap);