forked from jamesbachini/Market-Maker-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.js
76 lines (67 loc) · 3.15 KB
/
mm.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');
require("dotenv").config();
const wethAddress = '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6'; // goerli weth
//const wethAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // mainnet weth
const routerAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564'; // Uniswap Router
const quoterAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'; // Uniswap Quoter
const tokenAddress = '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984'; // goerli uni
const fee = 3000; // Uniswap pool fee bps 500, 3000, 10000
const buyAmount = ethers.parseUnits('0.001', 'ether');
const targetPrice = BigInt(35); // target exchange rate
const targetAmountOut = buyAmount * targetPrice;
const sellAmount = buyAmount / targetPrice;
const tradeFrequency = 3600 * 1000; // ms (once per hour)
// `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`
const provider = new ethers.JsonRpcProvider(`https://eth-goerli.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY);
const account = wallet.connect(provider);
const token = new ethers.Contract(
tokenAddress,
[
'function approve(address spender, uint256 amount) external returns (bool)',
'function allowance(address owner, address spender) public view returns (uint256)',
],
account
);
const router = new ethers.Contract(
routerAddress,
['function exactInputSingle((address tokenIn, address tokenOut, uint24 fee, address recipient, uint256 deadline, uint256 amountIn, uint256 amountOutMinimum, uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountOut)'],
account
);
const quoter = new ethers.Contract(
quoterAddress,
['function quoteExactInputSingle(address tokenIn, address tokenOut, uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96) public view returns (uint256 amountOut)'],
account
);
const buyTokens = async () => {
console.log('Buying Tokens')
const deadline = Math.floor(Date.now() / 1000) + 600;
const tx = await router.exactInputSingle([wethAddress, tokenAddress, fee, wallet.address, deadline, buyAmount, 0, 0], {value: buyAmount});
await tx.wait();
console.log(tx.hash);
}
const sellTokens = async () => {
console.log('Selling Tokens')
const allowance = await token.allowance(wallet.address, routerAddress);
console.log(`Current allowance: ${allowance}`);
if (allowance < sellAmount) {
console.log('Approving Spend (bulk approve in production)');
const atx = await token.approve(routerAddress, sellAmount);
await atx.wait();
}
const deadline = Math.floor(Date.now() / 1000) + 600;
const tx = await router.exactInputSingle([tokenAddress, wethAddress, fee, wallet.address, deadline, sellAmount, 0, 0]);
await tx.wait();
console.log(tx.hash);
}
const checkPrice = async () => {
const amountOut = await quoter.quoteExactInputSingle(wethAddress, tokenAddress, fee, buyAmount, 0);
console.log(`Current Exchange Rate: ${amountOut.toString()}`);
console.log(`Target Exchange Rate: ${targetAmountOut.toString()}`);
if (amountOut < targetAmountOut) buyTokens();
if (amountOut > targetAmountOut) sellTokens();
}
checkPrice();
setInterval(() => {
checkPrice();
}, tradeFrequency);