subtract debits from cake balance

This commit is contained in:
João Geonizeli
2021-09-18 16:08:49 -03:00
parent fb957803cc
commit 698befd7aa
6 changed files with 27 additions and 9 deletions

View File

@@ -31,7 +31,7 @@ class FiatBalanceDashboard < Administrate::BaseDashboard
# FORM_ATTRIBUTES # FORM_ATTRIBUTES
# an array of attributes that will be displayed # an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages. # on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = [:user, :amount_cents].freeze FORM_ATTRIBUTES = [:amount_cents].freeze
# COLLECTION_FILTERS # COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search # a hash that defines filters that can be used while searching via the search

View File

@@ -1,4 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
default_scope { order(created_at: :desc) }
end end

View File

@@ -33,6 +33,9 @@ class StakeOrder < ApplicationRecord
super & ["pool_name", "amount"] super & ["pool_name", "amount"]
end end
scope :add, -> { where("amount > 0") }
scope :remove, -> { where("amount < 0") }
private private
def notification_message def notification_message

View File

@@ -29,6 +29,7 @@ class User < ApplicationRecord
has_many :documents, class_name: "UserDocument", dependent: :destroy has_many :documents, class_name: "UserDocument", dependent: :destroy
has_many :stake_orders, dependent: :restrict_with_error has_many :stake_orders, dependent: :restrict_with_error
has_many :sell_crypto_orders, dependent: :restrict_with_error
has_one :fiat_balance, dependent: :restrict_with_error has_one :fiat_balance, dependent: :restrict_with_error
validates :first_name, :last_name, :email, presence: true validates :first_name, :last_name, :email, presence: true

View File

@@ -10,12 +10,12 @@ class BscClient
} }
end end
def token_balance(token, token_decimals, wallet_address) def token_balance(contract:, digits:, wallet_address:)
result = self.class.get( result = self.class.get(
"https://api-eu1.tatum.io/v3/blockchain/token/balance/BSC/#{token}/#{wallet_address}", "https://api-eu1.tatum.io/v3/blockchain/token/balance/BSC/#{contract}/#{wallet_address}",
headers: @headers headers: @headers
).parsed_response["balance"] ).parsed_response["balance"]
result.to_f / (10**token_decimals) (result.to_f / (10**digits)).round(5)
end end
end end

View File

@@ -11,12 +11,24 @@ class Wallet
private private
def total_cake def total_cake
return "0" if address.blank? return 0 if address.blank?
BscClient.new.token_balance( BscClient.new.token_balance(
"0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82", contract: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
18, digits: 18,
address, wallet_address: address,
) ) - total_cake_debit
end
def total_cake_debit
total_cake_stake_debit + total_cake_sell_debit
end
def total_cake_stake_debit
user.stake_orders.processing.add.sum(&:amount).to_f
end
def total_cake_sell_debit
user.sell_crypto_orders.processing.sum(&:paid_amount).to_f
end end
end end