add createStakeOrder mutation
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
module Inputs
|
||||
class CreateStakeOrderAttributesInput < Types::BaseInputObject
|
||||
argument :currency_id, ID, required: true
|
||||
argument :pool_name, String, required: true
|
||||
argument :amount, String, "Amount to be paid", required: true
|
||||
end
|
||||
end
|
||||
31
app/graphql/mutations/create_stake_order.rb
Normal file
31
app/graphql/mutations/create_stake_order.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
# frozen_string_literal: true
|
||||
module Mutations
|
||||
class CreateStakeOrder < BaseMutation
|
||||
field :order, Types::StakeOrderType, null: true
|
||||
|
||||
argument :order, Inputs::CreateStakeOrderAttributesInput, required: true
|
||||
|
||||
def resolve(order:)
|
||||
currency_id = decode_id(order[:currency_id])
|
||||
amount = BigDecimal(order[:amount])
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
current_user
|
||||
.balances
|
||||
.find_by!(currency_id: currency_id)
|
||||
.withdrawal!(amount)
|
||||
|
||||
record = StakeOrder.create!(
|
||||
amount: amount,
|
||||
currency_id: currency_id,
|
||||
pool_name: order[:pool_name],
|
||||
user_id: current_user.id,
|
||||
)
|
||||
|
||||
{ order: record }
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
{ errors: Resolvers::ModelErrors.from_active_record_model(e.record) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
module Types
|
||||
class MutationType < Types::BaseObject
|
||||
field :create_stake_order, mutation: Mutations::CreateStakeOrder
|
||||
field :create_sell_crypto_order, mutation: Mutations::CreateSellCryptoOrder
|
||||
field :create_buy_crypto_order, mutation: Mutations::CreateBuyCryptoOrder
|
||||
end
|
||||
|
||||
@@ -4,26 +4,30 @@
|
||||
#
|
||||
# Table name: stake_orders
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# amount :decimal(20, 10) default(0.0), not null
|
||||
# pool_name :string not null
|
||||
# status :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# user_id :bigint not null
|
||||
# id :bigint not null, primary key
|
||||
# amount :decimal(20, 10) default(0.0), not null
|
||||
# pool_name :string not null
|
||||
# status :string not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# currency_id :bigint
|
||||
# user_id :bigint not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_stake_orders_on_user_id (user_id)
|
||||
# index_stake_orders_on_currency_id (currency_id)
|
||||
# index_stake_orders_on_user_id (user_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (currency_id => currencies.id)
|
||||
# fk_rails_... (user_id => users.id)
|
||||
#
|
||||
class StakeOrder < ApplicationRecord
|
||||
include Processable
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :currency
|
||||
|
||||
validates :pool_name, presence: true
|
||||
validates :amount, presence: true
|
||||
|
||||
Reference in New Issue
Block a user