Skip to content

Commit

Permalink
Add paging parameter(page, per)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Nov 13, 2023
1 parent 59aafc8 commit e63b484
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions glueby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency 'tapyrus', '>= 0.3.1'
spec.add_runtime_dependency 'activerecord', '~> 7.0.0'
spec.add_runtime_dependency 'kaminari'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'mysql2'
spec.add_development_dependency 'rails', '~> 7.0.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/glueby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Glueby

module GluebyLogger
def logger
if defined?(Rails)
if defined?(Rails) && Rails.logger
Rails.logger
else
Logger.new(STDOUT)
Expand Down
4 changes: 2 additions & 2 deletions lib/glueby/internal/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def balance(only_finalized = true)
wallet_adapter.balance(id, only_finalized)
end

def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
wallet_adapter.tokens(id, color_id, only_finalized)
def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true, page = 1, per = 25)
wallet_adapter.tokens(id, color_id, only_finalized, page, per)
end

# @param only_finalized [Boolean] The flag to get a UTXO with status only finalized
Expand Down
4 changes: 3 additions & 1 deletion lib/glueby/internal/wallet/abstract_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ def balance(wallet_id, only_finalized = true)
# @param [String] color_id - The color id
# @param [Boolean] only_finalized - includes only finalized UTXO value if it
# is true. Default is true.
# @param [Integer] page - The page parameter is responsible for specifying the current page being viewed within the paginated results. default is 1.
# @param [Integer] per - The per parameter is used to determine the number of items to display per page. default is 25.
# @return [Array<Utxo>] The array of the utxos with specified color_id
def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true, page = 1, per = 25)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

Expand Down
4 changes: 3 additions & 1 deletion lib/glueby/internal/wallet/active_record_wallet_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'securerandom'
require 'kaminari'

module Glueby
module Internal
Expand Down Expand Up @@ -213,10 +214,11 @@ def has_address?(wallet_id, address)
wallet.keys.exists?(script_pubkey: script_pubkey.to_hex)
end

def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true, page = 1, per = 25)
wallet = AR::Wallet.find_by(wallet_id: wallet_id)
utxos = wallet.tokens(color_id)
utxos = utxos.where(status: :finalized) if only_finalized
utxos = utxos.order(:id).page(page).per(per)
utxos
end

Expand Down
11 changes: 9 additions & 2 deletions lib/glueby/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ def balances(only_finalized = true)
end
end

def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
@internal_wallet.tokens(color_id, only_finalized)
def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true, page = 1, per = 25)
@internal_wallet.tokens(color_id, only_finalized, page, per).map do |utxo|
{
txid: utxo.txid,
index: utxo.index,
address: Tapyrus::Script.parse_from_payload(utxo.script_pubkey.htb).to_addr,
amount: utxo.value
}
end
end

private
Expand Down
12 changes: 11 additions & 1 deletion spec/glueby/internal/wallet/active_record_wallet_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,16 @@
end

describe '#tokens' do
subject { adapter.tokens(wallet_id, color_id, only_finalized) }
subject { adapter.tokens(wallet_id, color_id, only_finalized, page, per) }

let(:wallet_id) { wallet.wallet_id }
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c185856a84c483fb108b1cdf79ff53aa7d54d1a137a5178684bd89ca31f906b2bd'.htb) }
let(:color_id2) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c11863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'.htb) }
let(:key1) { wallet.keys.create(purpose: :receive) }
let(:key2) { wallet.keys.create(purpose: :receive) }
let(:only_finalized) { true }
let(:page) { 1 }
let(:per) { 25 }

let(:utxo1) do
Glueby::Internal::Wallet::AR::Utxo.create(
Expand Down Expand Up @@ -592,6 +594,14 @@

it { expect(subject.to_a).to eq [utxo3, utxo5] }
end

context 'page, per specified' do
let(:only_finalized) { false }
let(:page) { 2 }
let(:per) { 1 }

it { expect(subject.to_a).to eq [utxo5] }
end
end

describe '#create_pay_to_contract_private_key' do
Expand Down
2 changes: 1 addition & 1 deletion spec/glueby/internal/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def load_wallet(wallet_id); end

subject

expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:tokens).with(wallet.id, color_id, true)
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:tokens).with(wallet.id, color_id, true, 1, 25)
end
end
end
4 changes: 2 additions & 2 deletions spec/glueby/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def tokens(wallet_id, color_id, only_finalized)
let(:only_finalized) {true}
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload("c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3".htb) }
it do
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:tokens)
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:tokens).and_return([])
subject
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:tokens).with("wallet_id:1", color_id, only_finalized)
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:tokens).with("wallet_id:1", color_id, only_finalized, 1, 25)
end
end
end

0 comments on commit e63b484

Please sign in to comment.