glueby 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bd80045038551e7ffc41a613fb67401f3675c79098222e335359209070ff280a
4
- data.tar.gz: f966a44bb7de60429e91d39681f63c6b827dbcb8806700cbfe6b5e961f013b2d
3
+ metadata.gz: ea6c9da8fdd46b8e2d82e7d403580d160f8eded28723f7ee33c14e002be5428f
4
+ data.tar.gz: d74cc716d2ce35f19eb0ed5d9eb6d46cd464fa3a19a87073dd61a1e2180ef5fe
5
5
  SHA512:
6
- metadata.gz: 4e443a845c4d65eeba06c83d40617497b984f8a0c158e54debb1ac82d762af53ebba8073e8fc18855f93816370dc8969657bec8d8e95d5990df3fe50c9c7471a
7
- data.tar.gz: 2c5f2670b48f6a1149052b239d89c626c323e21f222bd2deb201074b96d54ac935080624cb92a2399414aed03de44258a41da5b0fc4ecc5221d854ef201c06de
6
+ metadata.gz: 6c6c75c349edfd4c5ee5fc0e752ce9c65f1ad2f5b323d38f2a51b2ab5d91de8786daf67f1bfb1fdd221a7cc10cf891b6f6551fd58afe8059593a81536027d25c
7
+ data.tar.gz: 38c2b4a8e2a085bf238ca37fd1a39930e2b337d0f80a9695adb45116dc8e2cc4478c3c0041c08b0def00cdf15235664bee9d4c6beb9e7a285c1320f8f1b768c2
@@ -0,0 +1,4 @@
1
+ module Glueby
2
+ # The minimum limit of transaction output value to avoid to be recognized as a dust output.
3
+ DUST_LIMIT = 600
4
+ end
@@ -22,6 +22,8 @@ module Glueby
22
22
  class FixedFeeEstimator
23
23
  include FeeEstimator
24
24
 
25
+ attr_reader :fixed_fee
26
+
25
27
  class << self
26
28
  attr_accessor :default_fixed_fee
27
29
  end
@@ -80,8 +80,7 @@ module Glueby
80
80
  private
81
81
 
82
82
  def issue_reissuable_token(issuer:, amount:)
83
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
84
- funding_tx = create_funding_tx(wallet: issuer, utxo_provider: utxo_provider)
83
+ funding_tx = create_funding_tx(wallet: issuer)
85
84
  script_pubkey = funding_tx.outputs.first.script_pubkey
86
85
  color_id = Tapyrus::Color::ColorIdentifier.reissuable(script_pubkey)
87
86
 
@@ -97,8 +96,7 @@ module Glueby
97
96
  end
98
97
 
99
98
  def issue_non_reissuable_token(issuer:, amount:)
100
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
101
- funding_tx = create_funding_tx(wallet: issuer, utxo_provider: utxo_provider) if utxo_provider
99
+ funding_tx = create_funding_tx(wallet: issuer) if Glueby.configuration.use_utxo_provider?
102
100
  funding_tx = issuer.internal_wallet.broadcast(funding_tx) if funding_tx
103
101
 
104
102
  tx = create_issue_tx_for_non_reissuable_token(funding_tx: funding_tx, issuer: issuer, amount: amount)
@@ -114,8 +112,7 @@ module Glueby
114
112
  end
115
113
 
116
114
  def issue_nft_token(issuer:)
117
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
118
- funding_tx = create_funding_tx(wallet: issuer, utxo_provider: utxo_provider) if utxo_provider
115
+ funding_tx = create_funding_tx(wallet: issuer) if Glueby.configuration.use_utxo_provider?
119
116
  funding_tx = issuer.internal_wallet.broadcast(funding_tx) if funding_tx
120
117
 
121
118
  tx = create_issue_tx_for_nft_token(funding_tx: funding_tx, issuer: issuer)
@@ -145,10 +142,9 @@ module Glueby
145
142
  def reissue!(issuer:, amount:)
146
143
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
147
144
  raise Glueby::Contract::Errors::InvalidTokenType unless token_type == Tapyrus::Color::TokenTypes::REISSUABLE
148
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
149
145
 
150
146
  if validate_reissuer(wallet: issuer)
151
- funding_tx = create_funding_tx(wallet: issuer, script: @script_pubkey, utxo_provider: utxo_provider)
147
+ funding_tx = create_funding_tx(wallet: issuer, script: @script_pubkey)
152
148
  funding_tx = issuer.internal_wallet.broadcast(funding_tx)
153
149
  tx = create_reissue_tx(funding_tx: funding_tx, issuer: issuer, amount: amount, color_id: color_id)
154
150
  tx = issuer.internal_wallet.broadcast(tx)
@@ -171,8 +167,7 @@ module Glueby
171
167
  def transfer!(sender:, receiver_address:, amount: 1)
172
168
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
173
169
 
174
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
175
- funding_tx = create_funding_tx(wallet: sender, utxo_provider: utxo_provider) if utxo_provider
170
+ funding_tx = create_funding_tx(wallet: sender) if Glueby.configuration.use_utxo_provider?
176
171
  funding_tx = sender.internal_wallet.broadcast(funding_tx) if funding_tx
177
172
 
178
173
  tx = create_transfer_tx(funding_tx: funding_tx, color_id: color_id, sender: sender, receiver_address: receiver_address, amount: amount)
@@ -190,9 +185,24 @@ module Glueby
190
185
  # @raise [InvalidAmount] if amount is not positive integer.
191
186
  def burn!(sender:, amount: 0)
192
187
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
188
+ balance = sender.balances[color_id.to_hex]
189
+ raise Glueby::Contract::Errors::InsufficientTokens unless balance
190
+ raise Glueby::Contract::Errors::InsufficientTokens if balance < amount
191
+
192
+ burn_all_amount_flag = true if balance - amount == 0
193
193
 
194
194
  utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
195
- funding_tx = create_funding_tx(wallet: sender, utxo_provider: utxo_provider) if utxo_provider
195
+ if utxo_provider
196
+ funding_tx = create_funding_tx(
197
+ wallet: sender,
198
+ # When it burns all the amount of the color id, burn tx is not going to be have any output
199
+ # because change outputs is not necessary. Transactions needs one output at least.
200
+ # At that time, set true to this option to get more value to be created change output to
201
+ # the tx.
202
+ need_value_for_change_output: burn_all_amount_flag
203
+ )
204
+ end
205
+
196
206
  funding_tx = sender.internal_wallet.broadcast(funding_tx) if funding_tx
197
207
 
198
208
  tx = create_burn_tx(funding_tx: funding_tx, color_id: color_id, sender: sender, amount: amount)
@@ -3,24 +3,22 @@
3
3
  module Glueby
4
4
  module Contract
5
5
  module TxBuilder
6
- # The amount of output in funding tx for Reissuable token.
7
- FUNDING_TX_AMOUNT = 10_000
8
-
9
6
  def receive_address(wallet:)
10
7
  wallet.receive_address
11
8
  end
12
9
 
13
10
  # Create new public key, and new transaction that sends TPC to it
14
- def create_funding_tx(wallet:, script: nil, fee_estimator: FixedFeeEstimator.new, utxo_provider: nil)
15
- if utxo_provider
11
+ def create_funding_tx(wallet:, script: nil, fee_estimator: FixedFeeEstimator.new, need_value_for_change_output: false)
12
+ if Glueby.configuration.use_utxo_provider?
13
+ utxo_provider = UtxoProvider.new
16
14
  script_pubkey = script ? script : Tapyrus::Script.parse_from_addr(wallet.internal_wallet.receive_address)
17
- funding_tx, _index = utxo_provider.get_utxo(script_pubkey, FUNDING_TX_AMOUNT)
15
+ funding_tx, _index = utxo_provider.get_utxo(script_pubkey, funding_tx_amount(need_value_for_change_output: need_value_for_change_output))
18
16
  utxo_provider.wallet.sign_tx(funding_tx)
19
17
  else
20
18
  txb = Tapyrus::TxBuilder.new
21
19
  fee = fee_estimator.fee(dummy_tx(txb.build))
22
20
 
23
- sum, outputs = wallet.internal_wallet.collect_uncolored_outputs(fee + FUNDING_TX_AMOUNT)
21
+ sum, outputs = wallet.internal_wallet.collect_uncolored_outputs(fee + funding_tx_amount(need_value_for_change_output: need_value_for_change_output))
24
22
  outputs.each do |utxo|
25
23
  txb.add_utxo({
26
24
  script_pubkey: Tapyrus::Script.parse_from_payload(utxo[:script_pubkey].htb),
@@ -31,7 +29,7 @@ module Glueby
31
29
  end
32
30
 
33
31
  receiver_address = script ? script.addresses.first : wallet.internal_wallet.receive_address
34
- tx = txb.pay(receiver_address, FUNDING_TX_AMOUNT)
32
+ tx = txb.pay(receiver_address, funding_tx_amount)
35
33
  .change_address(wallet.internal_wallet.change_address)
36
34
  .fee(fee)
37
35
  .build
@@ -189,8 +187,7 @@ module Glueby
189
187
  tx.inputs << Tapyrus::TxIn.new(out_point: out_point)
190
188
  funding_tx.outputs.first.value
191
189
  else
192
- dust = 600 # in case that the wallet has output which has just fee amount.
193
- sum_tpc, outputs = sender.internal_wallet.collect_uncolored_outputs(fee + dust)
190
+ sum_tpc, outputs = sender.internal_wallet.collect_uncolored_outputs(fee + DUST_LIMIT)
194
191
  fill_input(tx, outputs)
195
192
  sum_tpc
196
193
  end
@@ -220,7 +217,12 @@ module Glueby
220
217
  def fill_change_tpc(tx, wallet, change)
221
218
  return unless change.positive?
222
219
 
223
- change_script = Tapyrus::Script.parse_from_addr(wallet.internal_wallet.change_address)
220
+ if Glueby.configuration.use_utxo_provider?
221
+ change_script = Tapyrus::Script.parse_from_addr(UtxoProvider.new.wallet.change_address)
222
+ else
223
+ change_script = Tapyrus::Script.parse_from_addr(wallet.internal_wallet.change_address)
224
+ end
225
+
224
226
  tx.outputs << Tapyrus::TxOut.new(value: change, script_pubkey: change_script)
225
227
  end
226
228
 
@@ -278,6 +280,21 @@ module Glueby
278
280
  tx.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: receiver_colored_script)
279
281
  dummy_tx(tx)
280
282
  end
283
+
284
+ private
285
+
286
+ # The amount of output in funding tx
287
+ # It returns same amount with FixedFeeEstimator's fixed fee. Because it is enough for paying fee for consumer
288
+ # transactions of the funding transactions.
289
+ #
290
+ # @option [Boolean] need_value_for_change_output If it is true, adds more value than the fee for producing change output.
291
+ def funding_tx_amount(need_value_for_change_output: false)
292
+ if need_value_for_change_output
293
+ FixedFeeEstimator.new.fixed_fee + DUST_LIMIT
294
+ else
295
+ FixedFeeEstimator.new.fixed_fee
296
+ end
297
+ end
281
298
  end
282
299
  end
283
300
  end
@@ -29,7 +29,6 @@ module Glueby
29
29
  return if utxos.empty?
30
30
 
31
31
  utxos.each { |utxo| txb.add_utxo(utxo) }
32
- address = wallet.receive_address
33
32
 
34
33
  shortage = [utxo_provider.utxo_pool_size - current_utxo_pool_size, 0].max
35
34
  return if shortage == 0
@@ -65,7 +64,7 @@ module Glueby
65
64
  message = <<~MESSAGE
66
65
  1. Please replenishment TPC which is for paying tpc to UtxoProvider.
67
66
  UtxoProvider needs #{value_to_fill_utxo_pool} tapyrus in UTXO pool.
68
- UtxoProvider wallet's address is '#{wallet.receive_address}'
67
+ UtxoProvider wallet's address is '#{address}'
69
68
  2. Then create UTXOs for paying in UTXO pool with 'rake glueby:utxo_provider:manage_utxo_pool'
70
69
  MESSAGE
71
70
  else
@@ -88,7 +87,7 @@ module Glueby
88
87
 
89
88
  # Show the address of Utxo Provider
90
89
  def print_address
91
- puts wallet.receive_address
90
+ puts address
92
91
  end
93
92
 
94
93
  private
@@ -130,6 +129,10 @@ module Glueby
130
129
  def delimit(num)
131
130
  num.to_s.reverse.scan(/.{1,3}/).join('_').reverse
132
131
  end
132
+
133
+ def address
134
+ @address ||= wallet.get_addresses.first || wallet.receive_address
135
+ end
133
136
  end
134
137
  end
135
138
  end
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
data/lib/glueby.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "glueby/version"
2
+ require "glueby/constants"
2
3
  require 'tapyrus'
3
4
 
4
5
  module Glueby
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glueby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-15 00:00:00.000000000 Z
11
+ date: 2021-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tapyrus
@@ -103,6 +103,7 @@ files:
103
103
  - lib/glueby/active_record/system_information.rb
104
104
  - lib/glueby/block_syncer.rb
105
105
  - lib/glueby/configuration.rb
106
+ - lib/glueby/constants.rb
106
107
  - lib/glueby/contract.rb
107
108
  - lib/glueby/contract/active_record.rb
108
109
  - lib/glueby/contract/active_record/reissuable_token.rb