/
bot.js
76 lines (68 loc) · 2.3 KB
/
bot.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
const ethers = require('ethers');
const addresses = {
WETH: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
factory: '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f',
router: '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D',
recipient: '0xf11b2fc4f28150517af11c2c456cbe75e976f663'
}
const mnemonic = 'your mnemonic here';
const provider = new ethers.providers.WebSocketProvider('Infura websocket url to mainnet');
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
const account = wallet.connect(provider);
const factory = new ethers.Contract(
addresses.factory,
['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
account
);
const router = new ethers.Contract(
addresses.router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
account
);
factory.on('PairCreated', async (token0, token1, pairAddress) => {
console.log(`
New pair detected
=================
token0: ${token0}
token1: ${token1}
pairAddress: ${pairAddress}
`);
//The quote currency needs to be WETH (we will pay with WETH)
let tokenIn, tokenOut;
if(token0 === addresses.WETH) {
tokenIn = token0;
tokenOut = token1;
}
if(token1 == addresses.WETH) {
tokenIn = token1;
tokenOut = token0;
}
//The quote currency is not WETH
if(typeof tokenIn === 'undefined') {
return;
}
//We buy for 0.1 ETH of the new token
const amountIn = ethers.utils.parseUnits('0.1', 'ether');
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
//Our execution price will be a bit different, we need some flexbility
const amountOutMin = amounts[1].sub(amounts[1].div(10));
console.log(`
Buying new token
=================
tokenIn: ${amountIn.toString()} ${tokenIn} (WETH)
tokenOut: ${amounOutMin.toString()} ${tokenOut}
`);
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10 //10 minutes
);
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);
});