Skip to content

Commit

Permalink
Merge branch 'master' into add-bullish-bearish-sentiment-ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
tspenov authored Nov 21, 2023
2 parents b945563 + 3d908ce commit eef5b0d
Show file tree
Hide file tree
Showing 81 changed files with 1,713 additions and 460 deletions.
16 changes: 4 additions & 12 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
[
inputs: [
"config/*.{ex,exs}",
"lib/*.{ex,exs}",
"lib/**/*.{ex,exs}",
"test/*.{ex,exs}",
"test/**/*.{ex,exs}",
"priv/**/*.{ex,exs}",
"mix.exs",
".formatter.exs"
],
# plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"]
import_deps: [:ecto, :ecto_sql, :phoenix],
plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,ex,exs}", "priv/*/seeds.exs"],
subdirectories: ["priv/*/migrations"]
]
1 change: 0 additions & 1 deletion .iex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ alias Sanbase.Model.{
MarketSegment,
LatestCoinmarketcapData,
ModelUtils,

Currency,
Ico,
IcoCurrency
Expand Down
1 change: 1 addition & 0 deletions assets/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
}
},
},
safelist: [ ],
plugins: [
require("@tailwindcss/forms"),
// Allows prefixing tailwind classes with LiveView classes to add rules
Expand Down
22 changes: 13 additions & 9 deletions lib/sanbase/balances/balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,22 @@ defmodule Sanbase.Balance do
def current_balance_top_addresses(slug, opts) do
with {:ok, {decimals, infrastructure, blockchain}} <- info_by_slug(slug),
{:ok, table} <- realtime_balances_table(slug, infrastructure) do
query_struct = top_addresses_query(slug, decimals, blockchain, table, opts)

ClickhouseRepo.query_transform(query_struct, fn [address, balance] ->
%{
address: address,
infrastructure: infrastructure,
balance: balance
}
end)
current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts)
end
end

def current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts) do
query_struct = top_addresses_query(slug, decimals, blockchain, table, opts)

ClickhouseRepo.query_transform(query_struct, fn [address, balance] ->
%{
address: address,
infrastructure: infrastructure,
balance: balance
}
end)
end

def realtime_balances_table_or_nil(slug, infr) do
case realtime_balances_table(slug, infr) do
{:ok, table} -> {:ok, table}
Expand Down
30 changes: 20 additions & 10 deletions lib/sanbase/balances/balance_sql_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,16 +359,23 @@ defmodule Sanbase.Balance.SqlQuery do
sql = """
SELECT address, balance
FROM (
SELECT address, argMax(balance, dt) / pow(10, {{decimals}}) AS balance
FROM #{table}
PREWHERE
addressType = 'normal'
GROUP BY address
SELECT
ebr.address,
argMax(ebr.balance, ebr.dt) / pow(10, {{decimals}}) AS balance
FROM #{table} AS ebr
WHERE (ebr.address GLOBAL IN (
SELECT address
FROM eth_top_holders_daily
WHERE value > 1e10 AND(dt = toStartOfDay(today() - toIntervalDay(1))) AND (rank > 0)
ORDER BY value #{direction}
LIMIT {{limit}}*2
)) AND (ebr.addressType = 'normal')
GROUP BY ebr.address
)
#{labels_join_str}
WHERE balance > 1e-10
ORDER BY balance #{direction}
LIMIT {{limit}} OFFSET {{offset}}
LIMIT {{limit}}
OFFSET {{offset}}
"""

Sanbase.Clickhouse.Query.new(sql, params)
Expand Down Expand Up @@ -397,7 +404,7 @@ defmodule Sanbase.Balance.SqlQuery do
FROM #{table}
PREWHERE
assetRefId = (SELECT asset_ref_id FROM asset_metadata FINAL WHERE name = {{slug}} LIMIT 1) AND
addressType = 'normal'
addressType = 'normal' AND (dt > (now() - toIntervalDay(1)))
GROUP BY address
)
#{labels_join_str}
Expand All @@ -420,8 +427,11 @@ defmodule Sanbase.Balance.SqlQuery do
GLOBAL ANY INNER JOIN
(
SELECT address
FROM blockchain_address_labels
PREWHERE blockchain = {{#{blockchain_key}}} AND label IN ({{#{labels_key}}})})
FROM current_label_addresses
WHERE blockchain = {{#{blockchain_key}}} AND
label_id IN (
SELECT label_id FROM label_metadata WHERE key IN ({{#{labels_key}}})
)
) USING (address)
"""

Expand Down
5 changes: 5 additions & 0 deletions lib/sanbase/billing/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ defmodule Sanbase.Billing do
|> Enum.empty?()
end

def eligible_for_api_trial?(user_id) do
Subscription.all_user_subscriptions_for_product(user_id, Product.product_api())
|> Enum.empty?()
end

@doc ~s"""
Sync the locally defined Products and Plans with stripe.
Expand Down
48 changes: 29 additions & 19 deletions lib/sanbase/billing/subscription/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ defmodule Sanbase.Billing.Subscription do
Please, contact administrator of the site for more information.
"""
@product_sanbase Product.product_sanbase()
@product_api Product.product_api()
@sanbase_basic_plan_id 205
@preload_fields [:user, plan: [:product]]
@trial_days 14

# Unused due to disabling of annual discounts
# @one_month_discount_days 30
# @during_trial_discount_percent_off 50
# @trial_days 14

# @one_month_trial_discount_percent_off 35
# @annual_discount_plan_ids [202]

Expand Down Expand Up @@ -139,7 +141,7 @@ defmodule Sanbase.Billing.Subscription do
{:ok, stripe_subscription} <- create_stripe_subscription(user, plan, coupon),
{:ok, db_subscription} <- create_subscription_db(stripe_subscription, user, plan) do
if db_subscription.status == :active do
maybe_delete_trialing_subscriptions(user.id)
maybe_delete_trialing_subscriptions(user.id, plan)
end

{:ok, default_preload(db_subscription, force: true)}
Expand All @@ -155,16 +157,26 @@ defmodule Sanbase.Billing.Subscription do
{:ok, stripe_subscription} <- create_stripe_subscription(user, plan, coupon),
{:ok, db_subscription} <- create_subscription_db(stripe_subscription, user, plan) do
if db_subscription.status == :active do
maybe_delete_trialing_subscriptions(user.id)
maybe_delete_trialing_subscriptions(user.id, plan)
end

{:ok, default_preload(db_subscription, force: true)}
end
end

def maybe_delete_trialing_subscriptions(user_id) do
def maybe_delete_trialing_subscriptions(user_id, %Plan{product_id: product_id})
when product_id == @product_sanbase do
__MODULE__
|> __MODULE__.Query.user_has_any_subscriptions_for_product(user_id, @product_sanbase)
|> Repo.all()
|> Enum.filter(fn subscription -> subscription.status == :trialing end)
|> Enum.each(fn subscription -> StripeApi.delete_subscription(subscription.stripe_id) end)
end

def maybe_delete_trialing_subscriptions(user_id, %Plan{product_id: product_id})
when product_id == @product_api do
__MODULE__
|> __MODULE__.Query.user_has_any_subscriptions_for_product(user_id, Product.product_sanbase())
|> __MODULE__.Query.user_has_any_subscriptions_for_product(user_id, @product_api)
|> Repo.all()
|> Enum.filter(fn subscription -> subscription.status == :trialing end)
|> Enum.each(fn subscription -> StripeApi.delete_subscription(subscription.stripe_id) end)
Expand Down Expand Up @@ -499,26 +511,24 @@ defmodule Sanbase.Billing.Subscription do
end

defp subscription_defaults(user, %Plan{product_id: product_id} = plan)
when product_id == @product_sanbase do
when product_id in [@product_sanbase, @product_api] do
defaults = %{
customer: user.stripe_customer_id,
items: [%{plan: plan.stripe_id}]
}

defaults =
case Billing.eligible_for_sanbase_trial?(user.id) do
true ->
Map.put(
defaults,
:trial_end,
Sanbase.DateTimeUtils.days_after(14) |> DateTime.to_unix()
)

false ->
defaults
end
trial_end_unix = Sanbase.DateTimeUtils.days_after(@trial_days) |> DateTime.to_unix()

cond do
product_id == @product_sanbase and Billing.eligible_for_sanbase_trial?(user.id) ->
Map.put(defaults, :trial_end, trial_end_unix)

defaults
product_id == @product_api and Billing.eligible_for_api_trial?(user.id) ->
Map.put(defaults, :trial_end, trial_end_unix)

true ->
defaults
end
end

defp subscription_defaults(user, plan) do
Expand Down
Loading

0 comments on commit eef5b0d

Please sign in to comment.