<commit_before>require 'bitcoin'

include Bitcoin::Builder

class WalletsController < ApplicationController
  before_action :set_wallet, only: [:show, :edit, :update, :destroy]

  # GET /wallets
  # GET /wallets.json
  def index
    @wallets = Wallet.all
  end

  # GET /wallets/1
  # GET /wallets/1.json
  def show
  end

  # GET /wallets/new
  def new
    @wallet = Wallet.new
  end

  # GET /wallets/1/edit
  def edit
  end

  # POST /wallets
  # POST /wallets.json
  def create
    @wallet = Wallet.new(wallet_params)

    respond_to do |format|
      if @wallet.save
        format.html { redirect_to @wallet, notice: 'Wallet was successfully created.' }
        format.json { render :show, status: :created, location: @wallet }
      else
        format.html { render :new }
        format.json { render json: @wallet.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /wallets/1
  # PATCH/PUT /wallets/1.json
  def update
    respond_to do |format|
      if @wallet.update(wallet_params)
        format.html { redirect_to @wallet, notice: 'Wallet was successfully updated.' }
        format.json { render :show, status: :ok, location: @wallet }
      else
        format.html { render :edit }
        format.json { render json: @wallet.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /wallets/1
  # DELETE /wallets/1.json
  def destroy
    @wallet.destroy
    respond_to do |format|
      format.html { redirect_to wallets_url, notice: 'Wallet was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  # Send money
  def send_bitcoins
    Bitcoin.network = :testnet3 # TODO FIX for config
    # "send_address"=>"ABC", "amount"=>"0.1", "round"=>"5"
    @wallet = Wallet.find(params[:id])

    key = @wallet.bitcoin_key

    # Work out all amounts
    @send_amount = (params[:amount].to_f * 1000000000 ) # Amount to send to address
    @round_up_amount = (params[:rounded_amount].to_f * 1000000000 ) - @send_amount # amount of round_up (send to donation bucket)
    @fee = params[:tx_fee].to_f * 1000000000 # Amount to leave as fee
    @total_amount = @send_amount + @fee + @round_up_amount # The amount we will be deducting from users unspent outputs

    # Addres
    @target_address = params[:send_address]
    
    # Get inputs to spend
    inputs = Output.find_for_amount(@total_amount, @wallet)

    puts "NO. of inputs #{inputs}"

    if inputs

      @change = Output.calculate_change(inputs, @total_amount)
      puts "CHANGE: #{@change}, #{@total_amount}"

      outputs  = []
      outputs << create_output(@send_amount, @target_address)
      outputs << create_output(@round_up_amount, "msGQ5M46fTMJ3KWg325P1RBeZPt1Jc6ZDK")
      outputs << create_output(@change, @wallet.addresses.first.val)

      new_tx = build_new_tx(key, inputs, outputs)
      puts "NEW TX: #{new_tx.to_json}"
      puts "HEX #{bin_to_hex(new_tx.to_payload)}"
      
      trans = Transaction.new
      trans.inputs << inputs
      trans.original_json = new_tx.to_json
      trans.unique_hash = new_tx.hash
      # We don't need to record outputs as they will appear from server
      trans.save

      respond_to do |format|
        format.html { redirect_to @wallet, notice: 'Bitcoins have been sent.' }
        format.json { head :no_content }
      end

    else
      respond_to do |format|
        format.html { redirect_to @wallet, error: 'No available outputs.' }
        format.json { head :no_content }
      end
    end

    
    
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_wallet<commit_msg>added logging and new call to create trans<commit_after>require 'bitcoin'

include Bitcoin::Builder

class WalletsController < ApplicationController
  before_action :set_wallet, only: [:show, :edit, :update, :destroy]

  # GET /wallets
  # GET /wallets.json
  def index
    @wallets = Wallet.all
  end

  # GET /wallets/1
  # GET /wallets/1.json
  def show
  end

  # GET /wallets/new
  def new
    @wallet = Wallet.new
  end

  # GET /wallets/1/edit
  def edit
  end

  # POST /wallets
  # POST /wallets.json
  def create
    @wallet = Wallet.new(wallet_params)

    respond_to do |format|
      if @wallet.save
        format.html { redirect_to @wallet, notice: 'Wallet was successfully created.' }
        format.json { render :show, status: :created, location: @wallet }
      else
        format.html { render :new }
        format.json { render json: @wallet.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /wallets/1
  # PATCH/PUT /wallets/1.json
  def update
    respond_to do |format|
      if @wallet.update(wallet_params)
        format.html { redirect_to @wallet, notice: 'Wallet was successfully updated.' }
        format.json { render :show, status: :ok, location: @wallet }
      else
        format.html { render :edit }
        format.json { render json: @wallet.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /wallets/1
  # DELETE /wallets/1.json
  def destroy
    @wallet.destroy
    respond_to do |format|
      format.html { redirect_to wallets_url, notice: 'Wallet was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  # Send money
  def send_bitcoins
    Bitcoin.network = :testnet3 # TODO FIX for config
    # "send_address"=>"ABC", "amount"=>"0.1", "round"=>"5"
    @wallet = Wallet.find(params[:id])

    key = @wallet.bitcoin_key

    # Work out all amounts
    @send_amount = (params[:amount].to_f * 1000000000 ) # Amount to send to address
    @round_up_amount = (params[:rounded_amount].to_f * 1000000000 ) - @send_amount # amount of round_up (send to donation bucket)
    @fee = params[:tx_fee].to_f * 1000000000 # Amount to leave as fee
    @total_amount = @send_amount + @fee + @round_up_amount # The amount we will be deducting from users unspent outputs

    # Addres
    @target_address = params[:send_address]
    
    # Get inputs to spend
    inputs = Output.find_for_amount(@total_amount, @wallet)

    if inputs

      @change = Output.calculate_change(inputs, @total_amount)
      logger.info("Total:#{@total_amount}, Sending #{@send_amount}, Rounded #{@round_amount}, Fee #{@fee}")

      outputs  = []
      outputs << create_output(@send_amount, @target_address)
      outputs << create_output(@round_up_amount, "msGQ5M46fTMJ3KWg325P1RBeZPt1Jc6ZDK")
      outputs << create_output(@change, @wallet.addresses.first.val)

      new_tx = build_new_tx(key, inputs, outputs)
      logger.debug("NEW TX: #{new_tx.to_json}")
      logger.debug("HEX #{bin_to_hex(new_tx.to_payload)}")
      
      Transaction.create_from_tx(inputs, new_tx)

      respond_to do |format|
        format.html { redirect_to @wallet, notice: 'Bitcoins have been sent.' }
        format.json { head :no_content }
      end

    else
      respond_to do |format|
        format.html { redirect_to @wallet, error: 'No available outputs.' }
        format.json { head :no_content }
      end
    end

    
    
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_wallet
{'fraction_non_alphanumeric': 0.10453593859036986, 'fraction_numerical': 0.014235868806699232, 'mean_word_length': 2.8423592493297587, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '5883903', 'n_tokens_mistral': 2398, 'n_tokens_neox': 2334, 'n_words': 619}