-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathswapOn1inch.js
More file actions
131 lines (104 loc) · 4.2 KB
/
swapOn1inch.js
File metadata and controls
131 lines (104 loc) · 4.2 KB
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
import { ethers } from "ethers"
import fetch from 'node-fetch';
import Web3 from 'web3';
import 'dotenv/config';
import addresses from './addresses.json' assert {type: "json"};
// import sleep from './utils.js'
// chain info -- matic
const chainId = 137;
const web3RpcUrl = 'https://rpc-cometh-mainnet.maticvigil.com/v1/0937c004ab133135c86586b55ca212a6c9ecd224';
// set api url and connect to web3
const broadcastApiUrl = 'https://tx-gateway.1inch.io/v1.1/' + chainId + '/broadcast';
const apiBaseUrl = 'https://api.1inch.io/v4.0/' + chainId;
const web3 = new Web3(web3RpcUrl);
function apiRequestUrl(methodName, queryParams) {
return apiBaseUrl + methodName + '?' + (new URLSearchParams(queryParams)).toString();
};
async function checkAllowance(tokenAddress, walletAddress) {
return fetch(apiRequestUrl('/approve/allowance', { tokenAddress, walletAddress }))
.then(res => res.json())
.then(res => res.allowance);
};
async function broadCastRawTransaction(rawTransaction) {
return fetch(broadcastApiUrl, {
method: 'post',
body: JSON.stringify({ rawTransaction }),
headers: { 'Content-Type': 'application/json' }
})
.then(res => res.json())
.then(res => {
return res.transactionHash;
});
}
async function signAndSendTransaction(transaction, privateKey) {
const { rawTransaction } = await web3.eth.accounts.signTransaction(transaction, privateKey);
return await broadCastRawTransaction(rawTransaction);
}
async function buildTxForApproveTradeWithRouter(walletAddress, tokenAddress, amount) {
const url = apiRequestUrl(
'/approve/transaction',
amount ? { tokenAddress, amount } : { tokenAddress }
);
const transaction = await fetch(url).then(res => res.json());
const gasLimit = await web3.eth.estimateGas({
...transaction,
from: walletAddress
});
return {
...transaction,
gas: gasLimit
};
}
async function buildTxForSwap(swapParams) {
const url = apiRequestUrl('/swap', swapParams);
return fetch(url).then(res => res.json()).then(res => res.tx);
}
async function swapOn1inch(walletAddress, privateKey) {
// swapParams
const swapParams = {
fromTokenAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC 6 decimals
toTokenAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619', // WETH, 18 decimals
amount: '399000000', // 400 usdc, depend on the decimals
fromAddress: walletAddress,
slippage: 0.5,
disableEstimate: false,
allowPartialFill: false,
};
// check token allowance
let allowance = await checkAllowance(swapParams.fromTokenAddress, walletAddress);
console.log('Allowance: ', allowance);
if (allowance === "0") {
// approve token allowance
console.log("Start approving token spending");
const transactionForSign = await buildTxForApproveTradeWithRouter(walletAddress, swapParams.fromTokenAddress);
const approveTxHash = await signAndSendTransaction(transactionForSign, privateKey);
console.log('Approve tx hash: ', approveTxHash);
allowance = await checkAllowance(swapParams.fromTokenAddress, walletAddress);
};
// swap
if (allowance !== "0") {
console.log("Spending approved, and start swap!");
const swapTransaction = await buildTxForSwap(swapParams);
console.log('Transaction for swap: ', swapTransaction);
// Send a transaction and get its hash
const swapTxHash = await signAndSendTransaction(swapTransaction, privateKey);
console.log('Swap transaction hash: ', swapTxHash);
const receipt = web3.eth.getTransactionReceipt(swapTxHash);
console.log('Swaped!');
};
}
async function main() {
for (var index = 0; index < addresses.length; index++) {
let walletAddress = addresses[index]["address"];
let privateKey = addresses[index]["private_key"];
console.log("------------");
console.log(`Start swap. walletAddress: ${walletAddress}`);
await swapOn1inch(walletAddress, privateKey);
};
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});