-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add portfolio request filter (#1609)
* fix: add portfolio request filter * test: adjust test
- Loading branch information
Showing
14 changed files
with
200 additions
and
119 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
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,64 @@ | ||
module Users | ||
class CkbTransactions < ActiveInteraction::Base | ||
include Api::V2::Exceptions | ||
|
||
object :user | ||
object :request, class: ActionDispatch::Request | ||
string :address_hash, default: nil | ||
string :tx_hash, default: nil | ||
string :sort, default: "ckb_transaction_id.desc" | ||
integer :page, default: 1 | ||
integer :page_size, default: CkbTransaction.default_per_page | ||
|
||
def execute | ||
account_books = sort_account_books(filter_account_books).page(page).per(page_size).fast_page | ||
transactions = CkbTransaction.where(id: account_books.map(&:ckb_transaction_id)). | ||
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, | ||
:is_cellbase, :updated_at, :capacity_involved). | ||
order(id: :desc) | ||
|
||
options = FastJsonapi::PaginationMetaGenerator.new( | ||
records: transactions, | ||
records_counter: account_books, | ||
request:, | ||
page:, | ||
page_size:, | ||
).call | ||
options[:params] = { previews: true, address: user.addresses } | ||
|
||
transactions_serializer = CkbTransactionsSerializer.new(transactions, options) | ||
transactions_serializer.serialized_json | ||
end | ||
|
||
private | ||
|
||
def filter_account_books | ||
address_ids = user.address_ids | ||
if address_hash.present? | ||
address = Address.find_address!(address_hash) | ||
address_ids = Array[address.id] | ||
end | ||
|
||
scope = AccountBook.joins(:ckb_transaction).where( | ||
account_books: { address_id: address_ids }, | ||
ckb_transactions: { tx_status: "committed" }, | ||
) | ||
scope = scope.where(ckb_transactions: { tx_hash: }) if tx_hash.present? | ||
|
||
scope | ||
rescue StandardError | ||
raise AddressNotFoundError.new | ||
end | ||
|
||
def sort_account_books(records) | ||
sorting, ordering = sort.split(".", 2) | ||
sorting = "ckb_transactions.block_timestamp" if sorting == "time" | ||
|
||
if ordering.nil? || !ordering.match?(/^(asc|desc)$/i) | ||
ordering = "asc" | ||
end | ||
|
||
records.order("#{sorting} #{ordering}") | ||
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,32 @@ | ||
module Users | ||
class SignIn < ActiveInteraction::Base | ||
include Api::V2::Exceptions | ||
|
||
string :address, :message, :signature | ||
string :pub_key, default: nil | ||
|
||
validates :address, :message, :signature, presence: true | ||
validate :validate_params_format! | ||
validate :validate_signature! | ||
|
||
def execute | ||
user = User.find_or_create_by(identifier: address) | ||
jwt = PortfolioUtils.generate_jwt({ uuid: user.uuid }) | ||
|
||
{ name: user.name, jwt: } | ||
end | ||
|
||
private | ||
|
||
def validate_params_format! | ||
raise AddressNotMatchEnvironmentError.new(ENV["CKB_NET_MODE"]) unless QueryKeyUtils.valid_address?(address) | ||
raise InvalidPortfolioMessageError.new unless QueryKeyUtils.hex_string?(message) | ||
raise InvalidPortfolioSignatureError.new unless QueryKeyUtils.hex_string?(signature) | ||
end | ||
|
||
def validate_signature! | ||
verified = PortfolioSignatureVerifier.new(address, message, signature, pub_key).verified? | ||
raise InvalidPortfolioSignatureError.new unless verified | ||
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,38 @@ | ||
module Users | ||
class Statistics < ActiveInteraction::Base | ||
include Api::V2::Exceptions | ||
|
||
object :user | ||
string :latest_address | ||
|
||
validates :latest_address, presence: true | ||
validate :check_addresses_consistent! | ||
|
||
def execute | ||
addresses = user.addresses | ||
balance = addresses.pluck(:balance).sum | ||
balance_occupied = addresses.pluck(:balance_occupied).sum | ||
dao_deposit = addresses.pluck(:dao_deposit).sum | ||
interest = addresses.pluck(:interest).sum | ||
unclaimed_compensation = addresses.pluck(:unclaimed_compensation).sum | ||
dao_compensation = interest.to_i + unclaimed_compensation.to_i | ||
|
||
CkbUtils.hash_value_to_s(balance:, balance_occupied:, dao_deposit:, | ||
interest:, dao_compensation:) | ||
end | ||
|
||
private | ||
|
||
def check_addresses_consistent! | ||
unless QueryKeyUtils.valid_address?(latest_address) | ||
raise AddressNotMatchEnvironmentError.new(ENV["CKB_NET_MODE"]) | ||
end | ||
|
||
address = Address.find_by_address_hash(latest_address) | ||
unless user.portfolios.exists?(address:) | ||
address = user.portfolios.last&.address | ||
raise PortfolioLatestDiscrepancyError.new(address&.address_hash) | ||
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,12 @@ | ||
module Users | ||
class Update < ActiveInteraction::Base | ||
object :user | ||
string :name | ||
|
||
validates :name, presence: true | ||
|
||
def execute | ||
user.update(name:) | ||
end | ||
end | ||
end |
Oops, something went wrong.