2022-11-12 02:27:46 +01:00

83 lines
2.8 KiB
Ruby

# == Schema Information
#
# Table name: bets
#
# id :uuid not null, primary key
# tip_provider_id :string not null
# tip_provider_bet_id :string not null
# tip_provider_percent :float not null
# exchange_id :string not null
# exchange_event_name :string not null
# exchange_event_id :string
# tip_provider_odds :string
# expected_value :float
# exchange_bet_id :string
# stake :float default(0.0), not null
# outcome :string default("processing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# exchange_account_id :string
# tip_provider_event_id :string
# exchange_odds :string
# team1 :string not null
# team2 :string
# tip_provider_market_details :jsonb
# exchange_market_details :jsonb
# log :jsonb
# original_json :jsonb
# executed_odds :string
# placement_attempts :integer default(0)
# period :string
#
class Bet < ApplicationRecord
enum outcome: { processing: 'processing', won: 'won', lost: 'lost', open: 'open', expired: 'expired', skipped: 'skipped', ignored: 'ignored', errored: 'errored', voided: 'voided', cancelled: 'cancelled'}
scope :placed_bets, -> { where('outcome in (?)', %w[won lost open voided cancelled]) }
scope :retryable_bets, -> { where("outcome in ('expired', 'skipped')") }
default_scope { where(exchange_account_id: ENV['EXCHANGE_ACCOUNT']) }
belongs_to :exchange_account
include Loggable
def json_payload
{
id: id,
exchange: exchange_id,
exchange_event_name: exchange_event_name,
created_at: updated_at,
exchange_odds: exchange_odds,
tip_provider_odds: tip_provider_odds,
expected_value: expected_value&.round(1),
stake: stake,
outcome: outcome,
market_identifier: tip_provider_market_details.to_s,
exchange_market_details: exchange_market_details.to_s,
bet_placement_type: bet_placement_type,
executed_odds: executed_odds,
ev_percent: tip_provider_percent.round(2),
event_scheduled_time: original_json['started_at'] ? Time.at(original_json['started_at']) : "",
log: show_log
}
end
def self.calculate_expected_value(stake, odds)
(stake * odds) - stake
end
private
def placed_bet?
open? || lost? || won? || voided?
end
def bet_placement_type
if placed_bet?
return 'Simulated' unless exchange_bet_id
return "Placed [#{exchange_bet_id}]"
end
''
end
end