-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (63 loc) · 2.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
/**
* Script that buys a token for example - CAKE2 whenever liquidity is provided to the pool.
* Params (
* tokenAddress
* privateKey
* amountToBuy
* noOfBuys
* )
*/
require('dotenv').config()
const ethers = require("ethers");
const abi = require('./ABI/SmartRouter.json');
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const signer = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
/**
* Taking specific value for the above mentioned params to test the script
* tokenAddress = 0x8d008B313C1d6C7fE2982F62d32Da7507cF43551 (CAKE2)
* amountToBuy = 10000000000000 wei (0.00001 BnB)
* noOfBuys = 2
*/
// PANCAKE SWAP & TESTNET SMARTCHAIN ADDRESSES
const SMART_ROUTER = '0xD99D1c33F9fC3444f8101754aBC46c52416550D1';
const WBNB = '0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd';
const CAKE2 = '0x8d008B313C1d6C7fE2982F62d32Da7507cF43551';
const noOfBuys = 2;
// INITIALIZE THE NUMBER OF COUNTS
let count = 0;
// MAIN FUNCTION
async function main() {
if (count == noOfBuys) {
console.log('NUMBER OF BUYS EXCEEDED');
return;
}
count += 1;
try {
// INSTANTIATE ROUTER CONTRACT
const routerContract = new ethers.Contract(SMART_ROUTER, abi, signer);
// CHECK THE LIQUIDITY AVAILABILITY
const getAmountsOut = await routerContract.getAmountsOut(
'10000000000000',
[WBNB, CAKE2]
);
const cake2Amount = getAmountsOut[1];
if (cake2Amount == 0) {
console.log('LIQUIDITY NOT AVAILABLE');
return;
}
const valueToSend = ethers.parseEther('0.00001');
// SWAP IF ENOUGH LIQUIDITY
console.log('Executing the swap...');
const txResponse = await routerContract.swapExactETHForTokens(
0, // minOutAmount set to 0 for testing purposes
[WBNB, CAKE2], // path to be followed for the swap
process.env.ADDRESS, // wallet address to send CAKE2 tokens
Date.now() + 1000 * 60 * 10, // deadline
{ value: valueToSend } // amount of tBNB to SWAP
);
console.log('Transaction Response ', txResponse);
} catch (e) {
console.log(e);
}
}
module.exports = main;