forked from hpyhacking/peatio
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing Management API Alpha version, provide the ability to manage deposits and withdrawal with third-party RSA signed payload, Now Barong and AppLogic application can participate in fiat transfer logic.
- Loading branch information
Showing
89 changed files
with
2,464 additions
and
723 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
module ManagementAPIv1 | ||
class Deposits < Grape::API | ||
|
||
desc 'Returns deposits as paginated collection.' do | ||
@settings[:scope] = :read_deposits | ||
success ManagementAPIv1::Entities::Deposit | ||
end | ||
params do | ||
optional :uid, type: String, desc: 'The shared user ID.' | ||
optional :currency, type: String, values: -> { Currency.codes(bothcase: true) }, desc: 'The currency code.' | ||
optional :page, type: Integer, default: 1, integer_gt_zero: true, desc: 'The page number (defaults to 1).' | ||
optional :limit, type: Integer, default: 100, range: 1..1000, desc: 'The number of deposits per page (defaults to 100, maximum is 1000).' | ||
optional :state, type: String, values: -> { Deposit::STATES.map(&:to_s) }, desc: 'The state to filter by.' | ||
end | ||
post '/deposits' do | ||
if params[:currency].present? | ||
currency = Currency.find_by!(code: params[:currency]) | ||
end | ||
|
||
if params[:uid].present? | ||
member = Authentication.find_by!(provider: :barong, uid: params[:uid]).member | ||
end | ||
|
||
Deposit | ||
.order(id: :desc) | ||
.tap { |q| q.where!(currency: currency) if currency } | ||
.tap { |q| q.where!(member: member) if member } | ||
.tap { |q| q.where!(aasm_state: params[:state]) if params[:state] } | ||
.page(params[:page]) | ||
.per(params[:limit]) | ||
.tap { |q| present q, with: ManagementAPIv1::Entities::Deposit } | ||
status 200 | ||
end | ||
|
||
desc 'Returns deposit by TID.' do | ||
@settings[:scope] = :read_deposits | ||
success ManagementAPIv1::Entities::Deposit | ||
end | ||
params do | ||
requires :tid, type: String, desc: 'The transaction ID.' | ||
end | ||
post '/deposits/get' do | ||
present Deposit.find_by!(params.slice(:tid)), with: ManagementAPIv1::Entities::Deposit | ||
end | ||
|
||
desc 'Creates new fiat deposit with state set to «submitted». ' \ | ||
'Optionally pass field «state» set to «accepted» if want to load money instantly. ' \ | ||
'You can also use PUT /fiat_deposits/:id later to load money or cancel deposit.' do | ||
@settings[:scope] = :write_deposits | ||
success ManagementAPIv1::Entities::Deposit | ||
end | ||
params do | ||
requires :uid, type: String, desc: 'The shared user ID.' | ||
optional :tid, type: String, desc: 'The shared transaction ID. Must not exceed 64 characters. Peatio will generate one automatically unless supplied.' | ||
requires :currency, type: String, values: -> { Currency.fiats.codes(bothcase: true) }, desc: 'The currency code.' | ||
requires :amount, type: BigDecimal, desc: 'The deposit amount.' | ||
optional :state, type: String, desc: 'The state of deposit.', values: %w[accepted] | ||
end | ||
post '/deposits/new' do | ||
member = Authentication.find_by(provider: :barong, uid: params[:uid])&.member | ||
currency = Currency.find_by(code: params[:currency]) | ||
account = member&.ac(currency) | ||
data = { member: member, currency: currency, account: account }.merge!(params.slice(:amount, :tid)) | ||
deposit = ::Deposits::Fiat.new(data) | ||
if deposit.save | ||
deposit.with_lock do | ||
deposit.accept! | ||
deposit.touch(:done_at) | ||
end if params[:state] == 'accepted' | ||
present deposit, with: ManagementAPIv1::Entities::Deposit | ||
else | ||
body errors: deposit.errors.full_messages | ||
status 422 | ||
end | ||
end | ||
|
||
desc 'Allows to load money or cancel deposit.' do | ||
@settings[:scope] = :write_deposits | ||
success ManagementAPIv1::Entities::Deposit | ||
end | ||
params do | ||
requires :tid, type: String, desc: 'The shared transaction ID.' | ||
requires :state, type: String, desc: 'The new state to apply.', values: %w[canceled accepted] | ||
end | ||
put '/deposits/state' do | ||
deposit = ::Deposits::Fiat.find_by!(params.slice(:tid)) | ||
if deposit.submitted? | ||
deposit.with_lock do | ||
params[:state] == 'canceled' ? deposit.cancel! : deposit.accept! | ||
deposit.touch(:done_at) | ||
end | ||
present deposit, with: ManagementAPIv1::Entities::Deposit | ||
status 200 | ||
else | ||
status 422 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module ManagementAPIv1 | ||
module Entities | ||
class Base < Grape::Entity | ||
format_with(:iso8601) { |t| t.iso8601 if t } | ||
format_with(:decimal) { |d| d.to_s('F') if d } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module ManagementAPIv1 | ||
module Entities | ||
class Deposit < Base | ||
expose :tid, documentation: { type: Integer, desc: 'The shared transaction ID.' } | ||
expose(:currency, documentation: { type: String, desc: 'The currency code.' }) { |d| d.currency.code } | ||
expose(:uid, documentation: { type: String, desc: 'The shared user ID.' }) { |w| w.member.authentications.barong.first.uid } | ||
expose(:type, documentation: { type: String, desc: 'The deposit type (fiat or coin).' }) { |d| d.class.name.demodulize.underscore } | ||
expose :amount, documentation: { type: String, desc: 'The deposit amount.' }, format_with: :decimal | ||
states = [ | ||
'«submitted» – initial state.', | ||
'«canceled» – deposit has been canceled by outer service.', | ||
'«rejected» – deposit has been rejected by outer service..', | ||
'«accepted» – deposit has been accepted by outer service, money are loaded.' | ||
] | ||
expose :aasm_state, as: :state, documentation: { type: String, desc: 'The deposit state. ' + states.join(' ') } | ||
expose :created_at, format_with: :iso8601, documentation: { type: String, desc: 'The datetime when deposit was created.' } | ||
expose :done_at, as: :completed_at, format_with: :iso8601, documentation: { type: String, desc: 'The datetime when deposit was completed.' } | ||
expose :txid, as: :blockchain_txid, if: -> (d, _) { d.coin? }, documentation: { type: String, desc: 'The transaction ID on the Blockchain (coin only).' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module ManagementAPIv1 | ||
module Entities | ||
class Withdraw < Base | ||
expose :tid, documentation: { type: Integer, desc: 'The shared transaction ID.' } | ||
expose(:uid, documentation: { type: String, desc: 'The shared user ID.' }) { |w| w.member.authentications.barong.first.uid } | ||
expose(:currency, documentation: { type: String, desc: 'The currency code.' }) { |w| w.currency.code } | ||
expose(:type, documentation: { type: String, desc: 'The withdraw type (fiat or coin).' }) { |w| w.class.name.demodulize.underscore } | ||
expose :amount, documentation: { type: String, desc: 'The withdraw amount excluding fee.' }, format_with: :decimal | ||
expose :fee, documentation: { type: String, desc: 'The exchange fee.' }, format_with: :decimal | ||
expose :rid, documentation: { type: String, desc: 'The beneficiary ID or wallet address on the Blockchain.' } | ||
states = [ | ||
'«prepared» – initial state, money are not locked.', | ||
'«submitted» – withdraw has been allowed by outer service for further validation, money are locked.', | ||
'«canceled» – withdraw has been canceled by outer service, money are unlocked.', | ||
'«accepted» – system has validated withdraw and queued it for processing by worker, money are locked.', | ||
'«rejected» – system has validated withdraw and found errors, money are unlocked.', | ||
'«suspected» – system detected suspicious activity, money are unlocked.', | ||
'«processing» – worker is processing withdraw as the current moment, money are locked.', | ||
'«succeed» – worker has successfully processed withdraw, money are subtracted from the account.', | ||
'«failed» – worker has encountered an unhandled error while processing withdraw, money are unlocked.' | ||
] | ||
expose :aasm_state, as: :state, documentation: { type: String, desc: 'The withdraw state. ' + states.join(' ') } | ||
expose :created_at, format_with: :iso8601, documentation: { type: String, desc: 'The datetime when withdraw was created.' } | ||
expose :txid, as: :blockchain_txid, documentation: { type: String, desc: 'The transaction ID on the Blockchain (coin only).' }, if: -> (w, _) { w.coin? } | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
app/api/management_api_v1/entities/withdraw_destination.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module ManagementAPIv1 | ||
module Entities | ||
class WithdrawDestination < Base | ||
expose :id, documentation: { type: Integer, desc: 'The withdraw destination ID.' } | ||
expose(:currency, documentation: { type: String, desc: 'The currency code.' }) { |w| w.currency.code } | ||
expose(:uid, documentation: { type: String, desc: 'The shared user ID.' }) { |w| w.member.authentications.barong.first.uid } | ||
expose :label, documentation: { type: String, desc: 'The associated label.' } | ||
expose(:type, documentation: { type: String, desc: 'The withdraw destination type (fiat or coin).' }) { |w| w.class.name.demodulize.underscore } | ||
%w[ fiat coin ].each do |type| | ||
"withdraw_destination/#{type}".camelize.constantize.fields.each do |field, desc| | ||
expose(field, documentation: { type: String, desc: desc }, if: -> (w, _) { w.respond_to?(field) }) { |w| w.public_send(field) } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module ManagementAPIv1 | ||
module Exceptions | ||
class Authentication < Base | ||
def status | ||
@options.fetch(:status, 401) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ManagementAPIv1 | ||
module Exceptions | ||
class Base < StandardError | ||
def initialize(message:, **options) | ||
@options = options | ||
super(message) | ||
end | ||
|
||
def headers | ||
@options.fetch(:headers, {}) | ||
end | ||
|
||
def status | ||
@options.fetch(:status) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module ManagementAPIv1 | ||
module Helpers | ||
|
||
end | ||
end |
Oops, something went wrong.