// Main  main Arb bot that is checking for an Arb opportunity between the two UniSwap pools. If an opportunity is found FlashLoanArb function calls the FlashLoan.
// Also a Web3 only method that executes trades directly.
const Web3 = require("web3");
const fs = require('fs');
const ERC20Token =  JSON.parse(fs.readFileSync("./client/src/contracts/ERC20Token.json"));
const UniSwapFactory = JSON.parse(fs.readFileSync("./client/src/contracts/uniswap_factory_custom.json"));
const UniSwapExchange = JSON.parse(fs.readFileSync("./client/src/contracts/uniswap_exchange_custom.json"));
const LendingPoolAddressesProvider = JSON.parse(fs.readFileSync("client/src/contracts/LendingPoolAddressesProvider.json"));
const LendingPool = JSON.parse(fs.readFileSync("client/src/contracts/LendingPool.json"));
const BigNumber = require('bignumber.js');
const Util = require("./client/src/utils/utils");
const {table} = require('table');
require('dotenv').config();

let data, output;

TRADELIVE = false;                                                        // Change this to true to execute trades

BigNumber.set({ DECIMAL_PLACES: 18});

const TRADER_ACCOUNT = '0xeE398666cA860DFb7390b5D73EE927e9Fb41a60A';
const DAI_ADDRESS = '0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD';

let id, DaiTokenInstance, UniSwapFactoryInstance, UniSwapExchangeInstance, leaderExchangeAddr, followerExchangeAddr;

const web3 = new Web3(new Web3.providers.HttpProvider(process.env.INFURAKOVAN));

async function run(){
  // Main function that will run and check for Arb op
  var id = await web3.eth.net.getId();

  DaiTokenInstance = new web3.eth.Contract(ERC20Token.abi, DAI_ADDRESS);
  UniSwapFactoryInstance = new web3.eth.Contract(UniSwapFactory.abi, UniSwapFactory.networks[id].address);
  UniSwapExchangeInstance = new web3.eth.Contract(UniSwapExchange.abi, UniSwapExchange.networks[id].address);

  leaderExchangeAddr = await UniSwapFactoryInstance.methods.getExchange(0).call();                                      // Using custom UniSwap contracts
  followerExchangeAddr = await UniSwapFactoryInstance.methods.getExchange(1).call();

  console.log(leaderExchangeAddr);
  console.log(followerExchangeAddr);

  var followerBalanceWei = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();

  console.log('Your DAI Balance: ' + web3.utils.fromWei(followerBalanceWei.toString(10), 'ether'));

  while(true){
    console.log('\nChecking For Arb Op...');
    var ethBalance = await web3.eth.getBalance(TRADER_ACCOUNT);
    console.log('Balance Eth: ' + web3.utils.fromWei(ethBalance.toString(10), 'ether'));
    await PoolInfo();

    var titles = ['Eth Sell', 'Eff Price', 'Token Buy Qty', 'Tokens To Sell', 'Eff Price', 'Ether Buy Qty', 'Profit', 'Result']
    var trades = [];

    var maxProfit = new BigNumber(0);
    var maxProfitSpend = 0;
    var tokensToBuy = 0;
    var ethToBuy = 0;

    var leaderExEthBalanceWei = await web3.eth.getBalance(leaderExchangeAddr);
    var leaderExTokenBalanceWei = await DaiTokenInstance.methods.balanceOf(leaderExchangeAddr).call();
    var followerExEthBalanceWei = await web3.eth.getBalance(followerExchangeAddr);
    var followerExTokenBalanceWei = await DaiTokenInstance.methods.balanceOf(followerExchangeAddr).call();

    var ethSpend = new BigNumber(0);
    var spendCheck = new BigNumber(1);
    var maxCheck = new BigNumber(10);
    var diviser = new BigNumber(10);
    var divisionCount = 0;
    var divisionMax = 10;    // Each division divides ethSpend by 10. So this would be 0.00001.

    while(true){

      var trade = [];
      ethSpend = ethSpend.plus(spendCheck);

      if(ethSpend.isGreaterThan(maxCheck)){
        console.log('Max Spend Checked.')
        break;
      }

      let ethSpendWei = web3.utils.toWei(ethSpend.toString(10), 'ether');                                    // Amount of Eth that will be spent on trade

      //profit = await CalculateProfit(ethSpendWei, followerExTokenBalanceWei, followerExEthBalanceWei, leaderExEthBalanceWei, leaderExTokenBalanceWei, trade)
      profit = await CalculateProfit(ethSpendWei, leaderExEthBalanceWei, leaderExTokenBalanceWei, followerExTokenBalanceWei, followerExEthBalanceWei, trade)    // Checks if it's possible to make a profit

      if(profit.profit.isGreaterThan(maxProfit)){
        trade.push('New max profit');
        trades.push(trade);
        maxProfit = profit.profit;
        maxProfitSpend = ethSpend;
        tokensToBuy = profit.firstEffectivePrice.tokensToBuyWeiBN;
        ethToBuy = profit.secondEffectivePrice.tokensToBuyWeiBN;
        continue;
      }

      if(profit.profit.isNegative()){
        if(divisionCount < divisionMax){
          trade.push('No Profit');
          trades.push(trade);
          divisionCount++;
          spendCheck = spendCheck.dividedBy(diviser);
          maxCheck = maxCheck.dividedBy(diviser);
          ethSpend = BigNumber(0);
          continue;
        }
        trade.push("No Profit\nCan't Arb");
        trades.push(trade);
        continue;
      }

      if(profit.firstEffectivePrice.effectivePriceBN.gt(profit.secondEffectivePrice.effectivePriceBN)){
        trade.push("Profits");
        continue;
        // console.log('????')
        //console.log('Max Spend Found: ' + (ethSpend - spendCheck).toString());
        //console.log('Max Profit at: ' + maxProfitSpend + ' ' + web3.utils.fromWei(maxprofit.profit.toString(10), 'ether'))
        //return;
      }
      trade.push("Profit");
      trades.push(trade);
    }

    trades.unshift(titles);
    // console.log(trades)
    output = table(trades);
    console.log(output);

    if(maxProfitSpend == 0){
      console.log('No Arb Op.');
      console.log('--------------------------------------------');
      await sleep(6000);
      continue;
    }

    console.log('\n******** Arb Op ***********');
    // Web3Arb(maxProfit, maxProfitSpend, tokensToBuy);                         // This executes UniSwap swaps using web3.
    await FlashLoanArb(maxProfit, maxProfitSpend)                               // This executes via FlashLoan contract.

    console.log('*****************************')
    console.log('--------------------------------------------');

  }
}

async function FlashLoanArb(maxProfit, maxProfitSpend){
  // Uses an Aave flash loan to execute trades. See ./contracts/FlashLoanReceiverArb.sol and also README for more details.
  console.log(maxProfit.toString(10))
  console.log('Max Profit at: ' + maxProfitSpend + ' ' + web3.utils.fromWei(maxProfit.toString(10), 'ether'));

  /// Retrieve the LendingPool address
  const LendingPoolAddressesProviderInstance = new web3.eth.Contract(LendingPoolAddressesProvider.abi, '0x9C6C63aA0cD4557d7aE6D9306C06C093A2e35408');
  const lendingPool = await LendingPoolAddressesProviderInstance.methods.getLendingPool().call();

  const LendingPoolInstance = new web3.eth.Contract(LendingPool.abi, lendingPool);

  var receiverContract = '0x1ED5840AB41D578584232C13314de1d73B2F5CC3';    // Kovan deployed of FlashLoanReceiverArb.sol
  var reserveAddr = '0xFf795577d9AC8bD7D90Ee22b6C1703490b6512FD';         // This is the DAI address and it is confirmed as working
  var loanAmountWei = web3.utils.toWei(maxProfitSpend.toString(), 'ether');

  const reserveData = await LendingPoolInstance.methods.getReserveData(reserveAddr).call();
  console.log('Reserve Data: ');
  console.log(reserveData);                                             // This shows amount in provider.

  console.log('FlashLoan Arb');
  console.log('Creating tx');
  const tx =  LendingPoolInstance.methods.flashLoan(receiverContract, reserveAddr, loanAmountWei);
  console.log('Sending tx');
  if(TRADELIVE){
    var rx = await Util.sendTransaction(web3, tx, TRADER_ACCOUNT_ADDR, process.env.PRIVATEKEY, lendingPool);
    console.log(rx)
  }else{
    await sleep(6000);
  }
}

async function Web3Arb(maxProfit, maxProfitSpend, tokensToBuy){
  // Uses Web3 to interact with UniSwap directly.
  var followerTokenBalanceStartWei = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var leaderTokenBalanceStartWei = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var ethBalanceStartWei = await web3.eth.getBalance(TRADER_ACCOUNT);
  console.log(maxProfit.toString(10))
  console.log('Max Profit at: ' + maxProfitSpend + ' ' + web3.utils.fromWei(maxProfit.toString(10), 'ether'));

  let ethSpendWei = web3.utils.toWei(maxProfitSpend.toString(), 'ether');

  var block = await web3.eth.getBlock("latest");
  const DEADLINE = block.timestamp + 300;
  var slippage = new BigNumber('0.997');
  // var minTokensWeiBNCheck = prices.tokensToBuyWeiBN.multipliedBy(slippage).precision(18);

  var minTokensWeiBN = tokensToBuy.multipliedBy(slippage).precision(18);
  var wei = new BigNumber(1e18);
  var minTokensEthBN = minTokensWeiBN.dividedBy(wei);
  var minTokensWei = web3.utils.toWei(minTokensEthBN.toString(10), 'ether');
  // console.log(minTokensEthBN.toString(10))
  // console.log(tokensToBuy.toString(10))
  // console.log(minTokensWei.toString(10))
  const followerContract = new web3.eth.Contract(UniSwapExchange.abi, followerExchangeAddr);

  var traderFollowerBalanceStart = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var traderFollowerBalanceStartBN = new BigNumber(traderFollowerBalanceStart.toString(10));

  var tx = await followerContract.methods.ethToTokenSwapInput(minTokensWei, DEADLINE);
  if(TRADELIVE)
    await Util.sendTransactionWithValue(web3, tx, TRADER_ACCOUNT, process.env.PRIVATEKEY, followerExchangeAddr, ethSpendWei);    // Would be good to get return value here as its should be actual amount of tokens bought

  var traderFollowerBalanceEnd = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var traderFollowerBalanceEndBN = new BigNumber(traderFollowerBalanceEnd.toString(10));
  var bought = traderFollowerBalanceEndBN.minus(traderFollowerBalanceStartBN);
  console.log('!!BOUGHT: ' + bought.toString(10));

  // Sell tokens for eth
  var tokensToSellWei = web3.utils.toWei(tokensToBuy.toString(10), 'wei');

  const leaderContract = new web3.eth.Contract(UniSwapExchange.abi, leaderExchangeAddr);
  tx = await leaderContract.methods.tokenToEthSwapInput(tokensToSellWei, ethSpendWei, DEADLINE);
  if(TRADELIVE)
    await Util.sendTransaction(web3, tx, TRADER_ACCOUNT, process.env.PRIVATEKEY, leaderExchangeAddr);

  var followerBalanceWei = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var leaderBalanceWei = await DaiTokenInstance.methods.balanceOf(TRADER_ACCOUNT).call();
  var ethBalanceFinish = await web3.eth.getBalance(TRADER_ACCOUNT);
  var realisedProfit = BigNumber(ethBalanceFinish).minus(BigNumber(ethBalanceStartWei));
  console.log('Profit: ' + web3.utils.fromWei(realisedProfit.toString(10), 'ether'));
}

async function CalculateProfit(ethSpendWei, followerExTokenBalanceWei, followerExEthBalanceWei, leaderExEthBalanceWei, leaderExTokenBalanceWei, trade){
  // Calculates profit for Arb opportunity.
  let ethSpendWeiBN = new BigNumber(ethSpendWei);
  // var followerEffectivePrice = await Util.getEffectivePrices(web3, ethSpendWei, followerExEthBalanceWei, followerExTokenBalanceWei, false);
  var followerEffectivePrice = await Util.getEffectivePrices(web3, ethSpendWei, followerExTokenBalanceWei, followerExEthBalanceWei, false);

  trade.push(web3.utils.fromWei(followerEffectivePrice.ethSpendWei, 'ether'));
  trade.push(followerEffectivePrice.effectivePriceBN.toString(10));
  trade.push(web3.utils.fromWei(followerEffectivePrice.tokensToBuyWeiBN.toString(10), 'ether'));

  var tokensToSellWei = web3.utils.toWei(followerEffectivePrice.tokensToBuyWeiBN.toString(10), 'wei');
  // var leaderEffectivePrice = await Util.getEffectivePrices(web3, tokensToSellWei, leaderExTokenBalanceWei, leaderExEthBalanceWei, false);
  var leaderEffectivePrice = await Util.getEffectivePrices(web3, tokensToSellWei, leaderExEthBalanceWei, leaderExTokenBalanceWei, false);

  trade.push(web3.utils.fromWei(leaderEffectivePrice.ethSpendWei, 'ether'));
  trade.push(leaderEffectivePrice.effectivePriceBN.toString(10));
  trade.push(web3.utils.fromWei(leaderEffectivePrice.tokensToBuyWeiBN.toString(10), 'ether'));

  var profit = leaderEffectivePrice.tokensToBuyWeiBN.minus(ethSpendWeiBN);
  trade.push(web3.utils.fromWei(profit.toString(10), 'ether'));

  return { profit: profit, firstEffectivePrice: followerEffectivePrice, secondEffectivePrice: leaderEffectivePrice};
}

async function PoolInfo(){
  // Displays UniSwap pool information.
  var leaderExEthBalanceWei = await web3.eth.getBalance(leaderExchangeAddr);
  var leaderExTokenBalanceWei = await DaiTokenInstance.methods.balanceOf(leaderExchangeAddr).call();
  var followerExEthBalanceWei = await web3.eth.getBalance(followerExchangeAddr);
  var followerExTokenBalanceWei = await DaiTokenInstance.methods.balanceOf(followerExchangeAddr).call();

  var leaderSpotPrices = await Util.getSpotPrices(leaderExEthBalanceWei, leaderExTokenBalanceWei, false);
  var followerSpotPrices = await Util.getSpotPrices(followerExEthBalanceWei, followerExTokenBalanceWei, false);

  data = [
      ['', 'Leader Exchange', 'Follower Exchange'],
      ['Eth Pool', web3.utils.fromWei(leaderExEthBalanceWei, 'ether'), web3.utils.fromWei(followerExEthBalanceWei, 'ether')],
      ['Token Pool', web3.utils.fromWei(leaderExTokenBalanceWei, 'ether'), web3.utils.fromWei(followerExTokenBalanceWei, 'ether')],
      ['Eth Spot Price', leaderSpotPrices.ethPrice.toString(10), followerSpotPrices.ethPrice.toString(10)],
      ['Token Spot Price', leaderSpotPrices.tokenPrice.toString(10), followerSpotPrices.tokenPrice.toString(10)]
  ];

  output = table(data);
  console.log(output);
  return;
}

sleep = (x) => {
	return new Promise(resolve => {
		setTimeout(() => { resolve(true) }, x )
	})
}

run();
 

Javascript Online Compiler

Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.

About Javascript

Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.

Key Features

  • Open-source
  • Just-in-time compiled language
  • Embedded along with HTML and makes web pages alive
  • Originally named as LiveScript.
  • Executable in both browser and server which has Javascript engines like V8(chrome), SpiderMonkey(Firefox) etc.

Syntax help

STDIN Example

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    console.log("Hello, " + line);
});

variable declaration

KeywordDescriptionScope
varVar is used to declare variables(old way of declaring variables)Function or global scope
letlet is also used to declare variables(new way)Global or block Scope
constconst is used to declare const values. Once the value is assigned, it can not be modifiedGlobal or block Scope

Backtick Strings

Interpolation

let greetings = `Hello ${name}`

Multi line Strings

const msg = `
hello
world!
`

Arrays

An array is a collection of items or values.

Syntax:

let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);

Example:

let mobiles = ["iPhone", "Samsung", "Pixel"];

// accessing an array
console.log(mobiles[0]);

// changing an array element
mobiles[3] = "Nokia";

Arrow functions

Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.

Syntax:

() => expression

Example:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
                                    .map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);

De-structuring

Arrays

let [firstName, lastName] = ['Foo', 'Bar']

Objects

let {firstName, lastName} = {
  firstName: 'Foo',
  lastName: 'Bar'
}

rest(...) operator

 const {
    title,
    firstName,
    lastName,
    ...rest
  } = record;

Spread(...) operator

//Object spread
const post = {
  ...options,
  type: "new"
}
//array spread
const users = [
  ...adminUsers,
  ...normalUsers
]

Functions

function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
  console.log(`Hello ${name}!`);
}
 
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar

Loops

1. If:

IF is used to execute a block of code based on a condition.

Syntax

if(condition){
    // code
}

2. If-Else:

Else part is used to execute the block of code when the condition fails.

Syntax

if(condition){
    // code
} else {
    // code
}

3. Switch:

Switch is used to replace nested If-Else statements.

Syntax

switch(condition){
    case 'value1' :
        //code
        [break;]
    case 'value2' :
        //code
        [break;]
    .......
    default :
        //code
        [break;]
}

4. For

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
//code  
} 

5. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
  // code 
}  

6. Do-While

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
  // code 
} while (condition); 

Classes

ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.

Syntax:

class className {
  constructor() { ... } //Mandatory Class method
  method1() { ... }
  method2() { ... }
  ...
}

Example:

class Mobile {
  constructor(model) {
    this.name = model;
  }
}

mbl = new Mobile("iPhone");