Sha256: c5d6c80b9eaf82efdf31e5aba2a8a404ad5677cc97096edd06e6f3d1520f0025

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class TransactionToBlockWorker < ApplicationWorker
  sidekiq_options retry: false

  def perform(receiver_key, sender_key, amount, block_id)
    @receiver_key = receiver_key
    @sender_key = sender_key
    @amount = amount
    @block_id = block_id
    transaction_accepted?
  end

  private
    attr_reader :receiver_key, :sender_key, :amount, :block_id

    def transaction_accepted?
      raise WalletNotFoundError unless wallets_present?
      raise BlockNotFoundError unless block_exists?
      raise AmountNotEnoughError unless amount_check
      subtract_amount
      if add_trasaction_to_block
        @success = true
      end
    end

    def fee
      (amount.to_f * 0.10).round(2)
    end

    def subtract_amount
      @sender_wallet.balance -= amount.to_f + fee
      @sender_wallet.save
    end

    def wallets_present?
      wallet_exists?("pr_key", receiver_key) && wallet_exists?("pv_key", sender_key)
    end

    def amount_check
      @sender_wallet = Wallet.find_by(pv_key: sender_key)
      @sender_wallet.balance >= amount.to_f + fee
    end

    def add_trasaction_to_block
      TransactionToBlockService.new(receiver_key, sender_key, amount, block_id).call
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
osbc-0.2.0 app/workers/transaction_to_block_worker.rb
osbc-0.1.9 app/workers/transaction_to_block_worker.rb
osbc-0.1.8 app/workers/transaction_to_block_worker.rb
osbc-0.1.7 app/workers/transaction_to_block_worker.rb
osbc-0.1.6 app/workers/transaction_to_block_worker.rb
osbc-0.1.5 app/workers/transaction_to_block_worker.rb