<commit_before>import React from 'react'
import ReactCountdownClock from 'react-countdown-clock'
import { getWeb3, attachToContract } from './web3'
import { getQueryVariable, setFlatFileContentToState } from './utils'

const startEvent = new Date(2017, 6, 19)
const endEvent = new Date(2017, 6, 24)

export class Invest extends React.Component {
  constructor(props) {
      super(props);
      console.log(props);
      if (this.tokensToInvestOnChange.bind) this.tokensToInvestOnChange = this.tokensToInvestOnChange.bind(this);
      if (this.investToTokens.bind) this.investToTokens = this.investToTokens.bind(this);
      this.state = {
        seconds: (endEvent - startEvent)/2 ,
      };
      this.state.contracts = {"crowdsale": {}, "token": {}};
      this.state.crowdsale = {};
      this.state.token = {};
  }

  componentDidMount () {
    const timeInterval = setInterval(() => this.setState({ seconds: this.state.seconds - 1}), 1000);
    this.setState({ timeInterval });

    var crowdsaleAddr = getQueryVariable("addr");
    this.state.contracts.crowdsale.addr = crowdsaleAddr;

    var derivativesLength = 4;
    var derivativesIterator = 0;
    var $this = this;
    setFlatFileContentToState("./contracts/SampleCrowdsale_flat.bin", function(_bin) {
      derivativesIterator++;
      $this.state.contracts.crowdsale.bin = _bin;

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsale_flat.abi", function(_abi) {
      derivativesIterator++;
      $this.state.contracts.crowdsale.abi = JSON.parse(_abi);

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsaleToken_flat.bin", function(_bin) {
      derivativesIterator++;
      $this.state.contracts.token.bin = _bin;

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsaleToken_flat.abi", function(_abi) {
      derivativesIterator++;
      $this.state.contracts.token.abi = JSON.parse(_abi);

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
  }

  extractContractsData($this) {
    console.log($this.state);
    getWeb3(function(web3, isOraclesNetwork) {
      console.log("getWeb3");
      var state = $this.state;
      state.curAddr = web3.eth.accounts[0];
      $this.setState(state);

      if (!$this.state.contracts.crowdsale.addr) return;
      attachToContract(web3, $this.state.contracts.crowdsale.abi, $this.state.contracts.crowdsale.addr, function(err, crowdsaleContract) {
        console.log("attach to crowdsale contract");
        if (err) return console.log(err);
        if (!crowdsaleContract) return console.log("There is no contract at this address");

        console.log(crowdsaleContract);

        crowdsaleContract.weiRaised.call(function(err, weiRaised) {
          if (err) return console.log(err);
          
          console.log("weiRaised: " + web3.fromWei(parseInt(weiRaised), "ether"));
          let state = $this.state;
          state.crowdsale.weiRaised = web3.fromWei(parseInt(weiRaised), "ether");
          $this.setState(state);
        });

        crowdsaleContract.rate.call(function(err, rate) {
          if (err) return console.log(err);
          
          console.log("rate: " + web3.fromWei(parseInt(rate), "ether"));
          let state = $this.state;
          state.crowdsale.rate = web3.fromWei(parseInt(rate), "ether");
          $this.setState(state);
        });

        crowdsaleContract.supply.call(function(err, supply) {
          if (err) return console.log(err);
          
          console.log("supply: " + supply);
          let state = $this.state;
          state.crowdsale.supply = supply;
          $this.setState(state);
        });

        crowdsaleContract.token.call(function(err, tokenAddr) {
          if (err) return console.log(err);
          
          console.log("token: " + tokenAddr);
          let state = $this.state;
          state.contracts.token.addr = tokenAddr;
          $this.setState(state);

          if (!tokenAddr || tokenAddr == "0x") return;
          attachToContract(web3, $this.state.contracts.token.abi, $this.state.contracts.token.addr, function(err, tokenContract) {
            console.log("attach to token contract");
            if (err) return console.log(err);
            if (!tokenContract) return console.log("There is no contract at this address");

            console.log(tokenContract);

            tokenContract.name.call(function(err, name) {
              if (err) return console.log(err);
              
              console.log("token name: " + name);
              let state = $this.state;
              state.token.name = name;
              $this.setState(state);
            });
            tokenContract.symbol.call(function(err, ticker) {
              if (err) console.log(err);
              console.log("token ticker: " + ticker);
              let state = $this.state;
              state.token.ticker = ticker;
              $this.setState(state);
            });
            tokenContract.supply.call(function(err, supply) {
              if (err) console.log(err);
              let state = $this.state;
              console.log("token supply: " + supply);
              state.token.supply = supply;
              $this.setState(state);
            });
          });
        });
      });
    });
  }

  investToTokens() {
    var $this = this;
    getWeb3(function(web3, isOraclesNetwork) {
      console.log(web3);
      console.log(isOraclesNetwork);
      var weiToSend = web3.toWei($this.state.tokensToInvest/$this.state.crowdsale.rate, "ether");
      var opts = {
        from: web3.eth.defaultAccount,
        value: weiToSend
      };

      console.log(opts);

      attachToContract(web3, $this.state.contracts.crowdsale.abi, $this.state.contracts.crowdsale.addr, function(err, crowdsaleContract) {
        console.log("attach to crowdsale contract");
        if (err) return console.log(err);
        if (!crowdsaleContract) return console.log("There is no contract at this address");

        console.log(crowdsaleContract);
        console.log(web3.eth.defaultAccount);
        console.log(opts);

        crowdsaleContract.buyTokens.sendTransaction(web3.eth.defaultAccount, opts, function(err, txHash) {
          if (err) return console.log(err);
          
          console.log("txHash: " + txHash);
          window.location.reload();
        });
      });
    });
  }

  tokensToInvestOnChange(event) {
    var state = this.state;<commit_msg>buy tokens fix<commit_after>import React from 'react'
import ReactCountdownClock from 'react-countdown-clock'
import { getWeb3, attachToContract } from './web3'
import { getQueryVariable, setFlatFileContentToState } from './utils'

const startEvent = new Date(2017, 6, 19)
const endEvent = new Date(2017, 6, 24)

export class Invest extends React.Component {
  constructor(props) {
      super(props);
      console.log(props);
      if (this.tokensToInvestOnChange.bind) this.tokensToInvestOnChange = this.tokensToInvestOnChange.bind(this);
      if (this.investToTokens.bind) this.investToTokens = this.investToTokens.bind(this);
      this.state = {
        seconds: (endEvent - startEvent)/2 ,
      };
      this.state.contracts = {"crowdsale": {}, "token": {}};
      this.state.crowdsale = {};
      this.state.token = {};
  }

  componentDidMount () {
    const timeInterval = setInterval(() => this.setState({ seconds: this.state.seconds - 1}), 1000);
    this.setState({ timeInterval });

    var crowdsaleAddr = getQueryVariable("addr");
    this.state.contracts.crowdsale.addr = crowdsaleAddr;

    var derivativesLength = 4;
    var derivativesIterator = 0;
    var $this = this;
    setFlatFileContentToState("./contracts/SampleCrowdsale_flat.bin", function(_bin) {
      derivativesIterator++;
      $this.state.contracts.crowdsale.bin = _bin;

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsale_flat.abi", function(_abi) {
      derivativesIterator++;
      $this.state.contracts.crowdsale.abi = JSON.parse(_abi);

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsaleToken_flat.bin", function(_bin) {
      derivativesIterator++;
      $this.state.contracts.token.bin = _bin;

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
    setFlatFileContentToState("./contracts/SampleCrowdsaleToken_flat.abi", function(_abi) {
      derivativesIterator++;
      $this.state.contracts.token.abi = JSON.parse(_abi);

      if (derivativesIterator == derivativesLength) {
        $this.extractContractsData($this);
      }
    });
  }

  extractContractsData($this) {
    console.log($this.state);
    getWeb3(function(web3, isOraclesNetwork) {
      console.log("getWeb3");
      var state = $this.state;
      state.curAddr = web3.eth.accounts[0];
      state.web3 = web3;
      $this.setState(state);

      if (!$this.state.contracts.crowdsale.addr) return;
      attachToContract(web3, $this.state.contracts.crowdsale.abi, $this.state.contracts.crowdsale.addr, function(err, crowdsaleContract) {
        console.log("attach to crowdsale contract");
        if (err) return console.log(err);
        if (!crowdsaleContract) return console.log("There is no contract at this address");

        console.log(crowdsaleContract);

        crowdsaleContract.weiRaised.call(function(err, weiRaised) {
          if (err) return console.log(err);
          
          console.log("weiRaised: " + web3.fromWei(parseInt(weiRaised), "ether"));
          let state = $this.state;
          state.crowdsale.weiRaised = web3.fromWei(parseInt(weiRaised), "ether");
          $this.setState(state);
        });

        crowdsaleContract.rate.call(function(err, rate) {
          if (err) return console.log(err);
          
          console.log("rate: " + web3.fromWei(parseInt(rate), "ether"));
          let state = $this.state;
          state.crowdsale.rate = web3.fromWei(parseInt(rate), "ether");
          $this.setState(state);
        });

        crowdsaleContract.supply.call(function(err, supply) {
          if (err) return console.log(err);
          
          console.log("supply: " + supply);
          let state = $this.state;
          state.crowdsale.supply = supply;
          $this.setState(state);
        });

        crowdsaleContract.token.call(function(err, tokenAddr) {
          if (err) return console.log(err);
          
          console.log("token: " + tokenAddr);
          let state = $this.state;
          state.contracts.token.addr = tokenAddr;
          $this.setState(state);

          if (!tokenAddr || tokenAddr == "0x") return;
          attachToContract(web3, $this.state.contracts.token.abi, $this.state.contracts.token.addr, function(err, tokenContract) {
            console.log("attach to token contract");
            if (err) return console.log(err);
            if (!tokenContract) return console.log("There is no contract at this address");

            console.log(tokenContract);

            tokenContract.name.call(function(err, name) {
              if (err) return console.log(err);
              
              console.log("token name: " + name);
              let state = $this.state;
              state.token.name = name;
              $this.setState(state);
            });
            tokenContract.symbol.call(function(err, ticker) {
              if (err) console.log(err);
              console.log("token ticker: " + ticker);
              let state = $this.state;
              state.token.ticker = ticker;
              $this.setState(state);
            });
            tokenContract.supply.call(function(err, supply) {
              if (err) console.log(err);
              let state = $this.state;
              console.log("token supply: " + supply);
              state.token.supply = supply;
              $this.setState(state);
            });
          });
        });
      });
    });
  }

  investToTokens() {
    var $this = this;
    let web3 = this.state.web3;
    var weiToSend = web3.toWei($this.state.tokensToInvest/$this.state.crowdsale.rate, "ether");
    var opts = {
      from: web3.eth.accounts[0],
      value: weiToSend
    };

    console.log(opts);

    attachToContract(web3, $this.state.contracts.crowdsale.abi, $this.state.contracts.crowdsale.addr, function(err, crowdsaleContract) {
      console.log("attach to crowdsale contract");
      if (err) return console.log(err);
      if (!crowdsaleContract) return console.log("There is no contract at this address");

      console.log(crowdsaleContract);
      console.log(web3.eth.defaultAccount);
      console.log(opts);

      crowdsaleContract.buyTokens.sendTransaction(web3.eth.accounts[0], opts, function(err, txHash) {
        if (err) return console.log(err);
        
        console.log("txHash: " + txHash);
        window.location.reload();
      });
    });
  }

  tokensToInvestOnChange(event) {
    var state = this.state;
{'fraction_non_alphanumeric': 0.1325962751354159, 'fraction_numerical': 0.006752244564814128, 'mean_word_length': 2.5328964613368283, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '1518509', 'n_tokens_mistral': 4123, 'n_tokens_neox': 4000, 'n_words': 694}