<commit_before>﻿var etherDeltaAddress;
var ethUtils = ethereumjsutil;
var privateKey;
var gasPrice; // in gwei
var sent;

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

function StartWatching() {
    Init();
}

function Init() {
    etherDeltaAddress = "0x8d12a197cb00d4747a1fe03395095ce2a5cc6819";
    privateKey = ethUtils.toBuffer("0x");
    $.getScript("abi-decoder.js");
    gasPrice = new web3.BigNumber(120); // in gwei
    
    sent = [];
    web3.eth.filter("pending", function (error, result) {
        if (!error) {
            web3.eth.getTransaction(result, ReadTransaction)
        }
    });
    WriteLog("Started Monitoring");
    //CheckConnection();
}

function CheckConnection() {
    if (!web3.isConnected())
        WriteLog("Disconnected :(");
    $("#currentblock").html('Current Block: ' + web3.eth.syncing.currentBlock + ', Highest Block: ' + web3.eth.syncing.highestBlock);
    setTimeout(function () { CheckConnection(); }, 3000);
}

function ReadTransaction(error, tx) {
    if (tx == null || tx.to == null) return;
    if (error) console.error(tx);
    if (tx.to == etherDeltaAddress) {
        //console.log(tx);
        abiDecoder.addABI(EtherDeltaABI);
        var abiVal = abiDecoder.decodeMethod(tx.input);
        if (abiVal == undefined) {
            WriteLog("Unable to read abi");
            return;
        }
        if (abiVal.name == "trade") {
            //console.log(abival);
            var transDetails = new TransactionDetails(abiVal.params);
            if (!transDetails.tokenGet.hasOwnProperty("name") | !transDetails.tokenGive.hasOwnProperty("name"))
                return;
            if (_.contains(sent, tx.input)) {
                WriteLog("Duplicate, already sent - " + tx.input);
                return;
            }
            var worthBuying = CheckTransactionValue(transDetails, tx);
            if (worthBuying)
            {
                var rawTrans = CreateOwnTransaction(tx);
                console.log(rawTrans);
                web3.eth.sendRawTransaction(rawTrans);
                $.post('https://api.etherscan.io/api', 'module=proxy&action=eth_sendRawTransaction&hex=' + rawTrans);
                sent.push(tx.input);
            }
        }
    }
}

function CheckTransactionValue(transDetails, tx) {
    var price, action;
    if (transDetails.tokenGive.name == "ETH") {
        action = "buy";
        price = transDetails.tokenGive.normalnum.div(transDetails.tokenGet.normalnum);
        WriteLog(action + "ing:" + transDetails.tokenGet.name + " " + transDetails.amount.normal + "/" + transDetails.tokenGet.normal + " at " + price.toString(10) + " - " + web3.fromWei(tx.gasPrice, "gwei").toString(10) + ' - ' + tx.hash);
    }
    else if (transDetails.tokenGet.name == "ETH") {
        action = "sell"
        price = transDetails.tokenGet.normalnum.div(transDetails.tokenGive.normalnum);
        WriteLog(action + "ing:" + transDetails.tokenGive.name + " " + transDetails.amount.normal + "/" + transDetails.tokenGet.normal + " at " + price.toString(10) + " - " + web3.fromWei(tx.gasPrice, "gwei").toString(10) + ' - ' + tx.hash);
    }
    else {
        action = "inter token"
        price = "don't care"
    }

    var txAction = false;
    if (price != "don't care" & transDetails.tokenGet.buyprice != 0) {
        if (action == "buy" & price.gte(transDetails.tokenGet.sellprice)) {
            txAction = true;
            WriteLog("buy !!!!!!!!!!!!!!!!!!!!");
        }
        else if (action == "sell" & price.lte(transDetails.tokenGive.buyprice)) {
            txAction = true;
            WriteLog("sell !!!!!!!!!!!!!!!!!!!!");
        }
    }

    return txAction;
}

function TransactionDetails(abiVals) {
    this.tokenGet = new Token(abiVals[0].value, abiVals[1].value);
    this.tokenGive = new Token(abiVals[2].value, abiVals[3].value);
    this.amount = new Token(abiVals[0].value, abiVals[10].value);
}
//if(myObj.hasOwnProperty("<property name>")){
function Token(taddr, wei) {
    _token = _.where(tokens, { addr: taddr })[0];
    if (_token == null) {
        WriteLog("Unkown Addr: " + taddr);
        return null;
    }
    this.wei = new web3.BigNumber(wei);
    this.normal = web3.fromWei(this.wei.times(_token.scalem), _token.scale).toString(10);
    this.normalnum = web3.fromWei(this.wei.times(_token.scalem), _token.scale)
    this.name = _token.name;
    this.buyprice = _token.buyprice;
    this.sellprice = _token.sellprice;
}

function CreateOwnTransaction(tx) {

    var currentGasPrice = web3.toWei(gasPrice, "gwei");
    if (currentGasPrice.lt(tx.gasPrice))
        currentGasPrice = tx.gasPrice.plus(10);

    WriteLog("current price: " + currentGasPrice.toString(10) + " and tx gasPrice: " + tx.gasPrice.toString(10));
<commit_msg>using EDs decimals value instead of names<commit_after>﻿var etherDeltaAddress;
var ethUtils = ethereumjsutil;
var privateKey;
var gasPrice; // in gwei
var sent;

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

function StartWatching() {
    Init();
}

function Init() {
    etherDeltaAddress = "0x8d12a197cb00d4747a1fe03395095ce2a5cc6819";
    privateKey = ethUtils.toBuffer("0x");
    $.getScript("abi-decoder.js");
    gasPrice = new web3.BigNumber(120); // in gwei
    
    sent = [];
    web3.eth.filter("pending", function (error, result) {
        if (!error) {
            web3.eth.getTransaction(result, ReadTransaction)
        }
    });
    WriteLog("Started Monitoring");
    //CheckConnection();
}

function CheckConnection() {
    if (!web3.isConnected())
        WriteLog("Disconnected :(");
    $("#currentblock").html('Current Block: ' + web3.eth.syncing.currentBlock + ', Highest Block: ' + web3.eth.syncing.highestBlock);
    setTimeout(function () { CheckConnection(); }, 3000);
}

function ReadTransaction(error, tx) {
    if (tx == null || tx.to == null) return;
    if (error) console.error(tx);
    if (tx.to == etherDeltaAddress) {
        //console.log(tx);
        abiDecoder.addABI(EtherDeltaABI);
        var abiVal = abiDecoder.decodeMethod(tx.input);
        if (abiVal == undefined) {
            WriteLog("Unable to read abi");
            return;
        }
        if (abiVal.name == "trade") {
            //console.log(abival);
            var transDetails = new TransactionDetails(abiVal.params);
            if (!transDetails.tokenGet.hasOwnProperty("name") | !transDetails.tokenGive.hasOwnProperty("name"))
                return;
            if (_.contains(sent, tx.input)) {
                WriteLog("Duplicate, already sent - " + tx.input);
                return;
            }
            CheckTransactionValue(transDetails, tx);
            //var worthBuying = CheckTransactionValue(transDetails, tx);
            //if (worthBuying)
            //{
            //    var rawTrans = CreateOwnTransaction(tx);
            //    console.log(rawTrans);
            //    web3.eth.sendRawTransaction(rawTrans);
            //    $.post('https://api.etherscan.io/api', 'module=proxy&action=eth_sendRawTransaction&hex=' + rawTrans);
            //    sent.push(tx.input);
            //}
        }
    }
}

function CheckTransactionValue(transDetails, tx) {
    var price, action;
    if (transDetails.tokenGive.name == "ETH") {
        action = "buy";
        price = transDetails.tokenGive.normalnum.div(transDetails.tokenGet.normalnum);
        WriteLog(action + "ing:" + transDetails.tokenGet.name + " " + transDetails.amount.normal + "/" + transDetails.tokenGet.normal + " at " + price.toString(10) + " - " + web3.fromWei(tx.gasPrice, "gwei").toString(10) + ' - ' + tx.hash);
    }
    else if (transDetails.tokenGet.name == "ETH") {
        action = "sell"
        price = transDetails.tokenGet.normalnum.div(transDetails.tokenGive.normalnum);
        WriteLog(action + "ing:" + transDetails.tokenGive.name + " " + transDetails.amount.normal + "/" + transDetails.tokenGet.normal + " at " + price.toString(10) + " - " + web3.fromWei(tx.gasPrice, "gwei").toString(10) + ' - ' + tx.hash);
    }
    else {
        action = "inter token"
        price = "don't care"
    }
    /*
    var txAction = false;
    if (price != "don't care" & transDetails.tokenGet.buyprice != 0) {
        if (action == "buy" & price.gte(transDetails.tokenGet.sellprice)) {
            txAction = true;
            WriteLog("buy !!!!!!!!!!!!!!!!!!!!");
        }
        else if (action == "sell" & price.lte(transDetails.tokenGive.buyprice)) {
            txAction = true;
            WriteLog("sell !!!!!!!!!!!!!!!!!!!!");
        }
    }
    */
    return txAction;
}

function TransactionDetails(abiVals) {
    this.tokenGet = new Token(abiVals[0].value, abiVals[1].value);
    this.tokenGive = new Token(abiVals[2].value, abiVals[3].value);
    this.amount = new Token(abiVals[0].value, abiVals[10].value);
}
//if(myObj.hasOwnProperty("<property name>")){
function Token(taddr, wei) {
    _token = _.where(tokens, { addr: taddr })[0];
    if (_token == null) {
        WriteLog("Unkown Addr: " + taddr);
        return null;
    }
    this.wei = new web3.BigNumber(wei);
    this.normal = wei.div(Math.pow(10,_token.decimals));
    this.normalnum = wei.div(Math.pow(10,_token.decimals));
    this.name = _token.name;
    this.buyprice = _token.buyprice;
    this.sellprice = _token.sellprice;
}

function CreateOwnTransaction(tx) {

    var currentGasPrice = web3.toWei(gasPrice, "gwei");
    if (currentGasPrice.lt(tx.gasPrice))
        currentGasPrice = tx.gasPrice.plus(10);

    WriteLog("current price: " + currentGasPrice.toString(10) + " and tx gasPrice: " + tx.gasPrice.toString(10));

{'fraction_non_alphanumeric': 0.15683902832375768, 'fraction_numerical': 0.01733696199979841, 'mean_word_length': 2.691220238095238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2504525', 'n_tokens_mistral': 3285, 'n_tokens_neox': 3133, 'n_words': 554}