-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfetch.js
143 lines (115 loc) · 4.75 KB
/
fetch.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
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
'use strict';
const dev = true;
const Web3 = require('web3');
const ethers = require('ethers');
const {ChainId, Token, TokenAmount, Fetcher, Pair, Route, Trade, TradeType, Percent} = require('@pancakeswap-libs/sdk');
var url = "wss://bsc-ws-node.nariox.org:443";
const web3 = new Web3(url);
const abis = require('./pancakeswap-mainnet.json');
const BigNumber = require("bignumber.js");
const {JsonRpcProvider} = require("@ethersproject/providers");
const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
const axios = require("axios");
const fs = require('fs');
const apiKeyBscScan = "000000000000000000000";
const pancakePairAddress = "0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16";
const amountInBUSD = web3.utils.toBN(web3.utils.toWei('20000'));
const pancakeRouterAddress = "0x10ED43C718714eb63d5aA57B78B54704E256024E";
const getAbi = async (name,address) => {
fs.access('./'+name+'-abis.json', fs.F_OK, (err) => {
if (err) {
if(dev) console.log(err);
const options = {
method: 'GET',
url: 'https://api.bscscan.com/api?module=contract&action=getabi&address='+address+'&apikey='+apiKeyBscScan
};
axios.request(options).then(function (response) {
if(dev) console.log(response.data.result);
let data = response.data.result;
fs.writeFileSync(name+'-abis.json', data);
return JSON.parse(response.data.result);
}).catch(function (error) {
if(dev) console.error(error);
});
}else{
fs.readFile('./'+name+'-abis.json', (err, data) => {
if (err) throw err;
let datas = JSON.parse(data);
if(dev) console.log(datas);
return datas;
});
}
})
};
const tokens =
{
BUSD: {
name: 'BUSD',
address: '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'
},
WBNB: {
name: 'WBNB',
address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
},
RACA: {
name: 'RACA',
address: '0x12bb890508c125661e03b09ec06e404bc9289040'
},
DAI: {
name: 'DAI',
address: '0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3'
},
}
;
async function buy(One,Two,WalletAddress) {
const OneamountIn = ethers.utils.parseUnits('100', 18);
let amounts = await routerContract.getAmountsOut(OneamountIn, [One, Two]);
const TwoamountOutMin = amounts[1].sub(amounts[1].div(10));
if(dev) console.log(ethers.utils.formatEther(OneamountIn));
if(dev) console.log(ethers.utils.formatEther(TwoamountOutMin));
const approveTx = await busdContract.approve(
router,
OneamountIn
);
let returned = await approveTx.wait();
if(dev) console.log(returned);
const swapTx = await routerContract.swapExactTokensForTokens(
OneamountIn,
TwoamountOutMin,
[One, Two],
WalletAddress,
Date.now() + 1000 * 60 * 10,
{gasLimit: 250000}
)
returned = await swapTx.wait();
if(dev) console.log(returned);
}
const init = async () => {
const pancakeSwap = new web3.eth.Contract(
abis.router.ABI,
pancakeRouterAddress
);
web3.eth.subscribe('newBlockHeaders')
.on('data', async block => {
const exchangeAmount = await new BigNumber(1);
const shiftedExchangeAmount = await new BigNumber(exchangeAmount).shiftedBy(18);
// const [inputToken, outputToken] = await Promise.all( [tokens.BUSD.address, tokens.WBNB.address].map(tokenAddress => ( new Token(ChainId.MAINNET,tokenAddress,18))));
// const pair = await Fetcher.fetchPairData(inputToken, outputToken, provider);
// console.log("pair >>>>>>>>>>>>>>>>");
// console.log(inputToken);
// console.log(outputToken);
// console.log("pair >>>>>>>>>>>>>>>>");
//const shiftedPancakeValue = await new BigNumber(bakeOutput[1]).shiftedBy(-18);
const bakeOutput = await pancakeSwap.methods.getAmountsOut(shiftedExchangeAmount, [tokens.BUSD.address, tokens.WBNB.address]).call();
if(dev) console.log(`PancakeSwap BUSD-WBNB: ${web3.utils.fromWei(shiftedExchangeAmount.toString())} -> ${web3.utils.fromWei(bakeOutput[1].toString())}`);
const bakeOutput2 = await pancakeSwap.methods.getAmountsOut(shiftedExchangeAmount, [tokens.WBNB.address, tokens.RACA.address]).call();
if(dev) console.log(`PancakeSwap WBNB-RACA: ${web3.utils.fromWei(shiftedExchangeAmount.toString())} -> ${web3.utils.fromWei(bakeOutput2[1].toString())}`);
const bakeOutput3 = await pancakeSwap.methods.getAmountsOut(shiftedExchangeAmount, [tokens.RACA.address, tokens.BUSD.address]).call();
if(dev) console.log(`PancakeSwap RACA-BUSD: ${web3.utils.fromWei(shiftedExchangeAmount.toString())} -> ${web3.utils.fromWei(bakeOutput3[1].toString())}`);
})
.on('error', error => {
if(dev) console.log(error);
});
}
getAbi(tokens.DAI.name,tokens.DAI.address);
//init();