forked from w1r2p1/Arbitrage-with-AAVE-Flashloan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arbaave.js
110 lines (93 loc) · 3.42 KB
/
arbaave.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
require("dotenv").config()
const Web3 = require('web3');
const { mainnet: addresses } = require('./addresses');
const ContractWithFlashLoanArtifact = require('./build/contracts/ContractWithFlashLoan.json');
const oneSplitABI = require('./abis/onesplit.json');
const oneSplitAddress = "0xC586BeF4a0992C495Cf22e1aeEE4E446CECDee0E";
const web3 = new Web3(
new Web3.providers.WebsocketProvider('ws://127.0.0.1:8545')
);
const { address: admin } = web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
const onesplitContract = new web3.eth.Contract(
oneSplitABI,
oneSplitAddress,
);
const txCost = '100'
const AMOUNT_DAI_WEI = web3.utils.toBN(web3.utils.toWei('40000', 'ether'));
const AMOUNT_DAI = web3.utils.fromWei(AMOUNT_DAI_WEI);
const init = async () => {
const networkId = await web3.eth.net.getId();
const contractWithFlashLoanAddress =
ContractWithFlashLoanArtifact.networks[networkId].address;
const flashloan = new web3.eth.Contract(
ContractWithFlashLoanArtifact.abi,
contractWithFlashLoanAddress,
);
web3.eth.subscribe('newBlockHeaders')
.on('data', async block => {
console.log(`New block received. Block # ${block.number}`);
const amountsEthresults = await onesplitContract
.methods
.getExpectedReturn(
addresses.tokens.dai,
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
AMOUNT_DAI_WEI,
100,
0
)
.call();
amountsEth = amountsEthresults.returnAmount;
const ethFromOnesplit = web3.utils.fromWei(amountsEth.toString());
const ethPricetoBuy = AMOUNT_DAI / ethFromOnesplit;
const amountsDairesults = await onesplitContract
.methods
.getExpectedReturn(
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
addresses.tokens.dai,
amountsEth,
100,
0
)
.call();
amountsDai = amountsDairesults.returnAmount;
const daiFromOnesplit = web3.utils.fromWei(amountsDai.toString());
const ethPriceToSell = daiFromOnesplit / ethFromOnesplit;
console.log(`Total number of ETH received from purchase: ${ethFromOnesplit}`);
console.log(`Total number of DAI received from sale of ETH: ${daiFromOnesplit}`);
console.log(`Price to Buy: ${ethPricetoBuy}`);
console.log(`Price to Sell: ${ethPriceToSell}`);
{
const profit = daiFromOnesplit - AMOUNT_DAI - txCost;
console.log(`Profit in Dai: ${profit}`);
console.log();
if(profit > 100) {
console.log('Arb opportunity found DAI -> ETH!');
console.log(`Expected profit: ${profit} Dai`);
const params = 0;
const tx = flashloan.methods.initateFlashLoan(
contractWithFlashLoanAddress,
addresses.tokens.dai,
AMOUNT_DAI_WEI,
params
);
const gasprice = await web3.eth.getGasPrice();
const gasPrice = Math.round(gasprice * 1.3);
const gasLimit = 4000000;
const data = tx.encodeABI();
const txData = {
from: admin,
to: flashloan.options.address,
data,
gasPrice,
gasLimit
};
const receipt = await web3.eth.sendTransaction(txData);
console.log(`Transaction hash: ${receipt.transactionHash}`);
};
}
})
.on('error', error => {
console.log(error);
});
}
init();