-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.ts
149 lines (130 loc) · 3.58 KB
/
util.ts
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
import { Contract, providers, utils, Wallet } from 'ethers';
import readline from 'readline';
import {
bnbAmount,
gasGwei,
gasLimit,
token,
mnemonic,
tokenAddress,
walletAddress,
} from './config.json';
export const fetchChar = async () => {
process.stdin.setRawMode(true);
return new Promise<string>((resolve) =>
process.stdin.once('data', (data) => {
process.stdin.setRawMode(false);
resolve(String(data));
})
);
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
export const fetchInput = async (question: string) => {
return new Promise<string>((resolve) => {
rl.question(question, resolve);
});
};
export type TokenConfig = {
wbnbAddress: string;
routerAddress: string;
factoryAddress: string;
websocketUrl: string;
scanUrl: string;
buyFunction: string;
chainId: number;
name: string;
};
export const getTokenConfig = async (): Promise<TokenConfig> => {
return await import(`./config.${token}.json`);
};
let _account: Wallet;
export const getAccount = ({ websocketUrl, name, chainId }: TokenConfig) => {
if (_account) return _account;
const provider = new providers.WebSocketProvider(websocketUrl, {
name,
chainId,
});
const wallet = Wallet.fromMnemonic(mnemonic);
_account = wallet.connect(provider);
return _account;
};
export const getFactory = (config: TokenConfig) => {
const account = getAccount(config);
return new Contract(
config.factoryAddress,
[
'event PairCreated(address indexed token0, address indexed token1, address pair, uint)',
],
account
);
};
export const getRouter = (config: TokenConfig) => {
const { routerAddress, buyFunction } = config;
const account = getAccount(config);
return new Contract(
routerAddress,
[
`function ${buyFunction}(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)`,
],
account
);
};
export const getPayment = () => {
const value = utils.parseEther(bnbAmount).toHexString();
const gasPrice = utils.parseUnits(gasGwei, 'gwei');
return {
gasPrice,
gasLimit,
value,
bnbAmount,
};
};
let inputTokenAddress: string;
export const fetchBuy = async (
router: Contract,
{ wbnbAddress, scanUrl, buyFunction }: TokenConfig,
waitForUser = true
) => {
const { bnbAmount, ...payment } = getPayment();
let char = '';
inputTokenAddress = inputTokenAddress || tokenAddress;
let userInputtedToken = false;
if (!inputTokenAddress) {
inputTokenAddress = await fetchInput('No token contract, paste it here: ');
userInputtedToken = true;
}
if (!userInputtedToken && waitForUser) {
console.log(
`Sniper loaded with ${bnbAmount} ${token} rounds. Press enter to buy, or any other key to quit`
);
char = await fetchChar();
} else {
console.log(`Sniper firing with ${bnbAmount} ${token} rounds.`);
}
if (userInputtedToken || !waitForUser || char === '\r') {
console.log(`Swapping ${token} for tokens...`);
const tx = await router[buyFunction](
0,
[wbnbAddress, inputTokenAddress],
walletAddress,
Math.floor(Date.now() / 1000) + 60 * 10, // 10 minutes from now
payment
);
const receipt = await tx.wait();
console.log(`Transaction hash: ${scanUrl}/tx/${receipt.transactionHash}`);
return true;
} else {
process.exit(0);
}
};
/* export const fetchSell = async (
router: Contract,
{ wbnbAddress, scanUrl, buyFunction }: TokenConfig,
waitForUser = true
) => {
const { bnbAmount, ...payment } = getPayment();
};
*/