Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
New seed: initial user funds, exchange pairs, transaction requests an…
Browse files Browse the repository at this point in the history
…d consumptions (#325)

* Add account_user linking when seeding users

* Add transaction request and consumption seed

* Give some tokens to all seeded users

* Add exchange_pair seed
  • Loading branch information
unnawut authored Jul 12, 2018
1 parent bc02c4a commit 7079c1f
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 15 deletions.
15 changes: 12 additions & 3 deletions apps/ewallet_db/lib/ewallet_db/transaction_request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ defmodule EWalletDB.TransactionRequest do

def get(_id, _opts), do: nil

@doc """
Retrieves a transaction request using one or more fields.
"""
@spec get_by(map() | keyword(), keyword()) :: %__MODULE__{} | nil | no_return()
def get_by(fields, opts \\ []) do
TransactionRequest
|> Repo.get_by(fields)
|> preload_option(opts)
end

@spec query_all_for_account_and_user_uuids([String.t()], [String.t()]) :: Ecto.Queryable.t()
def query_all_for_account_and_user_uuids(account_uuids, user_uuids) do
from(
t in TransactionRequest,
Expand Down Expand Up @@ -234,9 +245,7 @@ defmodule EWalletDB.TransactionRequest do
@doc """
Touches a request by updating the `updated_at` field.
"""
@spec touch(%TransactionRequest{}) ::
{:ok, %TransactionRequest{}}
| {:error, Map.t()}
@spec touch(%TransactionRequest{}) :: {:ok, %TransactionRequest{}} | {:error, Map.t()}
def touch(request) do
request
|> touch_changeset(%{updated_at: NaiveDateTime.utc_now()})
Expand Down
8 changes: 7 additions & 1 deletion apps/ewallet_db/priv/repo/seeds/01_user.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule EWalletDB.Repo.Seeds.UserSeed do
alias EWalletDB.Helpers.Crypto
alias EWalletDB.User
alias EWalletDB.{Account, AccountUser, User}

@argsline_desc """
This email and password combination is required for logging into the admin panel.
Expand All @@ -25,12 +25,15 @@ defmodule EWalletDB.Repo.Seeds.UserSeed do
email: args[:admin_email],
password: args[:admin_password],
metadata: %{},
account_uuid: Account.get_master_account().uuid
}

case User.get_by_email(data.email) do
nil ->
case User.insert(data) do
{:ok, user} ->
{:ok, _} = AccountUser.link(data.account_uuid, user.uuid)

writer.success("""
ID : #{user.id}
Email : #{user.email}
Expand All @@ -42,13 +45,16 @@ defmodule EWalletDB.Repo.Seeds.UserSeed do
{:seeded_admin_user_email, user.email},
{:seeded_admin_user_password, user.password},
]

{:error, changeset} ->
writer.error(" Admin Panel user #{data.email} could not be inserted:")
writer.print_errors(changeset)

_ ->
writer.error(" Admin Panel user #{data.email} could not be inserted:")
writer.error(" Unknown error.")
end

%User{} = user ->
writer.warn("""
ID : #{user.id}
Expand Down
16 changes: 8 additions & 8 deletions apps/ewallet_db/priv/repo/seeds_sample/00_token.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ defmodule EWalletDB.Repo.Seeds.TokenSampleSeed do
%{
symbol: "OMG",
name: "OmiseGO",
subunit_to_unit: 10_000,
genesis_amount: 10_000_000_000, # 1,000,000 OMG
subunit_to_unit: 1_000_000_000_000_000_000,
genesis_amount: 1_000_000_000_000_000_000_000_000, # 1,000,000 OMG
account_name: "master_account"
},
%{
symbol: "KNC",
name: "Kyber",
subunit_to_unit: 1_000,
genesis_amount: 1_000_000_000, # 1,000,000 KNC
subunit_to_unit: 1_000_000_000_000_000_000,
genesis_amount: 1_000_000_000_000_000_000_000_000, # 1,000,000 KNC
account_name: "master_account"
},
%{
symbol: "BTC",
name: "Bitcoin",
subunit_to_unit: 10_000,
genesis_amount: 10_000_000_000, # 1,000,000 BTC
subunit_to_unit: 1_000_000_000_000_000_000,
genesis_amount: 1_000_000_000_000_000_000_000_000, # 1,000,000 BTC
account_name: "master_account"
},
%{
symbol: "OEM",
name: "One EM",
subunit_to_unit: 100,
genesis_amount: 100_000_000, # 1,000,000 OEM
subunit_to_unit: 1_000_000_000_000_000_000,
genesis_amount: 1_000_000_000_000_000_000_000_000, # 1,000,000 OEM
account_name: "master_account"
},
%{
Expand Down
33 changes: 31 additions & 2 deletions apps/ewallet_db/priv/repo/seeds_sample/00_user.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
defmodule EWalletDB.Repo.Seeds.UserSampleSeed do
alias EWalletDB.User
alias Ecto.UUID
alias EWallet.TransactionGate
alias EWalletDB.{Account, AccountUser, Token, User}

@users_count 5
@username_prefix "user"
@provider_prefix "provider_user_id"
@minimum_token_amount 1_000

def seed do
[
Expand All @@ -24,25 +27,32 @@ defmodule EWalletDB.Repo.Seeds.UserSampleSeed do
data = %{
provider_user_id: @provider_prefix <> running_string,
username: @username_prefix <> running_string,
metadata: %{}
metadata: %{},
account_uuid: Account.get_master_account().uuid
}

case User.get_by_provider_user_id(data.provider_user_id) do
nil ->
case User.insert(data) do
{:ok, user} ->
:ok = give_token(user, Token.all(), @minimum_token_amount)
{:ok, _} = AccountUser.link(data.account_uuid, user.uuid)

writer.success("""
User ID : #{user.id}
Provider user ID : #{user.provider_user_id}
Username : #{user.username}
""")

{:error, changeset} ->
writer.error(" eWallet user #{data.email} could not be inserted:")
writer.print_errors(changeset)

_ ->
writer.error(" eWallet user #{data.email} could not be inserted:")
writer.error(" Unknown error.")
end

%User{} = user ->
writer.warn("""
User ID : #{user.id}
Expand All @@ -51,4 +61,23 @@ defmodule EWalletDB.Repo.Seeds.UserSampleSeed do
""")
end
end

defp give_token(user, tokens, minimum_amount) when is_list(tokens) do
Enum.each(tokens, fn token ->
give_token(user, token, minimum_amount)
end)
end

defp give_token(user, token, minimum_amount) do
master_account = Account.get_master_account()

TransactionGate.create(%{
"from_address" => Account.get_primary_wallet(master_account).address,
"to_address" => User.get_primary_wallet(user).address,
"token_id" => token.id,
"amount" => :rand.uniform(10) * minimum_amount * token.subunit_to_unit,
"metadata" => %{},
"idempotency_token" => UUID.generate()
})
end
end
63 changes: 63 additions & 0 deletions apps/ewallet_db/priv/repo/seeds_sample/01_exchange_pair.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule EWalletDB.Repo.Seeds.ExchangePairSeed do
alias EWallet.Web.Preloader
alias EWalletDB.{Token, ExchangePair}

@pairs [
%{from_token_symbol: "BTC", to_token_symbol: "OEM", rate: 1_000_000},
%{from_token_symbol: "ETH", to_token_symbol: "OMG", rate: 400},

%{from_token_symbol: "OEM", to_token_symbol: "BTC", rate: 0.000001},
%{from_token_symbol: "OEM", to_token_symbol: "OMG", rate: 0.001},

%{from_token_symbol: "OMG", to_token_symbol: "OEM", rate: 1_000},
%{from_token_symbol: "OMG", to_token_symbol: "BTC", rate: 0.0010},
%{from_token_symbol: "OMG", to_token_symbol: "ETH", rate: 0.0025}
]

def seed do
[
run_banner: "Seeding sample exchange pairs:",
argsline: [],
]
end

def run(writer, _args) do
Enum.each @pairs, fn args ->
run_with(writer, args)
end
end

def run_with(writer, args) do
from_token = Token.get_by(symbol: args.from_token_symbol)
to_token = Token.get_by(symbol: args.to_token_symbol)

case ExchangePair.get_by([from_token_uuid: from_token.uuid, to_token_uuid: to_token.uuid]) do
nil ->
{:ok, pair} =
ExchangePair.insert(%{
from_token_uuid: from_token.uuid,
to_token_uuid: to_token.uuid,
rate: args.rate
})

{:ok, pair} = Preloader.preload_one(pair, [:from_token, :to_token])

writer.success("""
Exchange Pair ID : #{pair.id}
From Token ID : #{pair.from_token.id}
To Token ID : #{pair.to_token.id}
Rate : #{pair.rate}
""")

%ExchangePair{} = pair ->
{:ok, pair} = Preloader.preload_one(pair, [:from_token, :to_token])

writer.warn("""
Exchange Pair ID : #{pair.id}
From Token ID : #{pair.from_token.id}
To Token ID : #{pair.to_token.id}
Rate : #{pair.rate}
""")
end
end
end
Loading

0 comments on commit 7079c1f

Please sign in to comment.