Skip to content

Commit

Permalink
Rename method name token_utxos -> list_unspent_with_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Nov 30, 2023
1 parent aff6091 commit 213e661
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 55 deletions.
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 token_utxos(color_id = nil, only_finalized = true, page = 1, per = 25)
wallet_adapter.token_utxos(id, color_id, only_finalized, page, per)
def list_unspent_with_count(color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
wallet_adapter.list_unspent_with_count(id, color_id, only_finalized, label, page, per)
end

# @param color_id [Tapyrus::Color::ColorIdentifier] The color identifier associated with UTXO.
Expand Down
2 changes: 1 addition & 1 deletion lib/glueby/internal/wallet/abstract_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def balance(wallet_id, only_finalized = 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 token_utxos(wallet_id, color_id = nil, only_finalized = true, page = 1, per = 25)
def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

Expand Down
69 changes: 40 additions & 29 deletions lib/glueby/internal/wallet/active_record_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,18 @@ def balance(wallet_id, only_finalized = true)
utxos.sum(&:value)
end

def token_utxos(wallet_id, color_id = nil, only_finalized = true, page = 1, per = 25)
wallet = AR::Wallet.find_by(wallet_id: wallet_id)
utxos = wallet.utxos.where(locked_at: nil)
utxos = utxos.where(color_id: color_id.to_hex) if color_id && !color_id.default?
utxos = utxos.where(color_id: nil) if color_id && color_id.default?
utxos = utxos.where(status: :finalized) if only_finalized
utxos = utxos.order(:id)
def list_unspent_with_count(wallet_id, color_id = nil, only_finalized = true, label = nil, page = 1, per = 25)
utxos = list_unspent_internal(wallet_id, color_id, only_finalized, label)
utxos = utxos.page(page).per(per) if per > 0
utxos
end
{
count: utxos.total_count,
outputs: utxos_to_h(utxos)
}
end

def list_unspent(wallet_id, color_id = nil, only_finalized = true, label = nil)
utxos = token_utxos(wallet_id, color_id, only_finalized, 1, 0)
if [:unlabeled, nil].include?(label)
utxos = utxos.where(label: nil)
elsif label && (label != :all)
utxos = utxos.where(label: label)
else
utxos
end

utxos.map do |utxo|
{
txid: utxo.txid,
vout: utxo.index,
script_pubkey: utxo.script_pubkey,
color_id: utxo.color_id,
amount: utxo.value,
finalized: utxo.status == 'finalized',
label: utxo.label
}
end
utxos = list_unspent_internal(wallet_id, color_id, only_finalized, label)
utxos_to_h(utxos)
end

def sign_tx(wallet_id, tx, prevtxs = [], sighashtype: Tapyrus::SIGHASH_TYPE[:all])
Expand Down Expand Up @@ -255,6 +235,37 @@ def create_pay_to_contract_private_key(wallet_id, payment_base, contents)
key_type: Tapyrus::Key::TYPES[:compressed]
)
end

def list_unspent_internal(wallet_id, color_id = nil, only_finalized = true, label = nil)
wallet = AR::Wallet.find_by(wallet_id: wallet_id)
utxos = wallet.utxos.where(locked_at: nil)
utxos = utxos.where(color_id: color_id.to_hex) if color_id && !color_id.default?
utxos = utxos.where(color_id: nil) if color_id && color_id.default?
utxos = utxos.where(status: :finalized) if only_finalized
utxos = utxos.order(:id)
utxos = if [:unlabeled, nil].include?(label)
utxos = utxos.where(label: nil)
elsif label && (label != :all)
utxos = utxos.where(label: label)
else
utxos
end
utxos
end

def utxos_to_h(utxos)
utxos.map do |utxo|
{
txid: utxo.txid,
vout: utxo.index,
script_pubkey: utxo.script_pubkey,
color_id: utxo.color_id,
amount: utxo.value,
finalized: utxo.status == 'finalized',
label: utxo.label
}
end
end
end
end
end
Expand Down
10 changes: 1 addition & 9 deletions lib/glueby/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ def balances(only_finalized = true)
end

def token_utxos(color_id = nil, only_finalized = true, page = 1, per = 25)
@internal_wallet.token_utxos(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,
status: utxo.status == "finalized" ? :confirmed : :unconfirmed
}
end
@internal_wallet.list_unspent_with_count(color_id, only_finalized, nil, page, per)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
end
end

describe '#token_utxos' do
describe '#list_unspent_with_count' do
it 'does not implemented' do
expect { adapter.token_utxos("wallet_id") }.to raise_error(NotImplementedError)
expect { adapter.list_unspent_with_count("wallet_id") }.to raise_error(NotImplementedError)
end
end

Expand Down
52 changes: 47 additions & 5 deletions spec/glueby/internal/wallet/active_record_wallet_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,16 @@
end
end

describe '#token_utxos' do
subject { adapter.token_utxos(wallet_id, color_id, only_finalized, page, per) }
describe '#list_unspent_with_count' do
subject { adapter.list_unspent_with_count(wallet_id, color_id, only_finalized, label, 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(:label) { nil }
let(:page) { 1 }
let(:per) { 25 }

Expand Down Expand Up @@ -601,20 +602,61 @@
utxo5
end

it { expect(subject.to_a).to eq [utxo3]}
it do
expect(subject[:count]).to eq 1
expect(subject[:outputs]).to eq [{
txid: '2222222222222222222222222222222222222222222222222222222222222222',
vout: 0,
color_id: color_id.to_hex,
label: nil,
script_pubkey: utxo3.script_pubkey,
amount: 1,
finalized: true
}]
end

context 'only_finalized = false' do
let(:only_finalized) { false }

it { expect(subject.to_a).to eq [utxo3, utxo5] }
it do
expect(subject[:count]).to eq 2
expect(subject[:outputs]).to eq [{
txid: '2222222222222222222222222222222222222222222222222222222222222222',
vout: 0,
color_id: color_id.to_hex,
label: nil,
script_pubkey: utxo3.script_pubkey,
amount: 1,
finalized: true
},{
txid: '2222222222222222222222222222222222222222222222222222222222222222',
vout: 4,
color_id: color_id.to_hex,
label: nil,
script_pubkey: utxo5.script_pubkey,
amount: 1,
finalized: false
}]
end
end

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

it { expect(subject.to_a).to eq [utxo5] }
it do
expect(subject[:count]).to eq 2
expect({
txid: '2222222222222222222222222222222222222222222222222222222222222222',
vout: 4,
color_id: color_id.to_hex,
label: nil,
script_pubkey: utxo5.script_pubkey,
amount: 1,
finalized: false
})
end
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/glueby/internal/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,17 @@ def load_wallet(wallet_id); end
end
end

describe "#token_utxos" do
subject { wallet.token_utxos(color_id) }
describe "#list_unspent_with_count" do
subject { wallet.list_unspent_with_count(color_id) }

let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3'.htb) }

it "call WalletAdapter#token_utxos" do
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:token_utxos)
it "call WalletAdapter#list_unspent_with_count" do
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:list_unspent_with_count)

subject

expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:token_utxos).with(wallet.id, color_id, true, 1, 25)
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:list_unspent_with_count).with(wallet.id, color_id, true, nil, 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 token_utxos(wallet_id, color_id, only_finalized, per, page)
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(:token_utxos).and_return([])
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:list_unspent_with_count).and_return([])
subject
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:token_utxos).with("wallet_id:1", color_id, only_finalized, 1, 25)
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:list_unspent_with_count).with("wallet_id:1", color_id, only_finalized, nil, 1, 25)
end
end
end

0 comments on commit 213e661

Please sign in to comment.