glueby 0.10.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 374ec643c65f210f1ecc1f35951ead50b86beaf9298a404a3cd21d0c3b0f1872
4
- data.tar.gz: 3b4c3a5780e06ca072b4b8b08839210a927a67af42a00ad9557926820ffefada
3
+ metadata.gz: 959a9d1dfbdea6b4b18e969b07399a9228fce31e80c7defcb9c393b71df1607b
4
+ data.tar.gz: efe95c4c0ef08692d30fb1ff9dfeeea13d777f809d71f19d329b9b50d4406a31
5
5
  SHA512:
6
- metadata.gz: b101685ebba2023302b266abe49603842d05af674e71e5b6c993539a01587c313033ef927f015de619c28f7e60986e51379cf32aac25a69b80dc903881ae51b3
7
- data.tar.gz: 2e437e7403b573d7018727766da166d64cac78147d95bb355f46284d1cb65a2ba36115d578c7fa29c6d6e488e2fdd9914f504e06c3fe346f962cbb1938700145
6
+ metadata.gz: 66f61489f313fcaf4c3fe22486cd2e13307a9b9ab78e9a47a1f61e60928d65455b0691c52f3e3461930f052d41e53c7c5a5f57ff7eab12ed52b95025c89e52ba
7
+ data.tar.gz: 744f83c4d6211e7643f21f53d823490cc9e0235bd318f603c1b1a08e8cfda70d1e23a328287457b3591dc57ed9079e8b47ac150f546b5c56576c3228bf98d58a
data/README.md CHANGED
@@ -445,9 +445,9 @@ Configuration:
445
445
 
446
446
  ## Other configurations
447
447
 
448
- ### Default fixed fee for the FixedFeeEstimator
448
+ ### Default fixed fee for the FeeEstimator::Fixed
449
449
 
450
- The architecture of Glueby accepts any fee estimation strategies for paying transactions fee. However, we officially support only one strategy: the fixed fee strategy.
450
+ The architecture of Glueby accepts any fee estimation strategies for paying transactions fee. However, we officially support only one strategy: the fixed fee strategy to contract transactions.
451
451
  It just returns a fixed fee value without any estimation.
452
452
  Here provides a configuration to modify the default fixed fee value it returns like this:
453
453
 
@@ -457,6 +457,19 @@ Glueby.configure do |config|
457
457
  end
458
458
  ```
459
459
 
460
+ ### Default fee rate for the FeeEstimator::Auto
461
+
462
+ Glueby provide automate fee estimator `Glueby::Contract::FeeEstimator::Auto` that calculate minimum fee possible to broadcast from fee rate and tx size.
463
+ This method can be used in UTXO provider's management task so far. In other place where creates txs is not tested with this yet.
464
+
465
+ Here provides a configuration to modify the default fee rate it returns like this:
466
+
467
+ ```ruby
468
+ Glueby.configure do |config|
469
+ config.default_fee_rate = 1_000
470
+ end
471
+ ```
472
+
460
473
  ## Error handling
461
474
 
462
475
  Glueby has base error classes like `Glueby::Error` and `Glueby::ArgumentError`.
@@ -83,10 +83,16 @@ module Glueby
83
83
  UtxoProvider.configure(config)
84
84
  end
85
85
 
86
- # Set default fixed fee to the FixedFeeEstimator
87
- # @param [Integer] fee The default fee value in tapyrus to the FixedFeeEstimator
86
+ # Set default fixed fee to the FeeEstimator::Fixed
87
+ # @param [Integer] fee The default fee value in tapyrus to the FeeEstimator::Fixed
88
88
  def default_fixed_fee=(fee)
89
- Contract::FixedFeeEstimator.default_fixed_fee = fee
89
+ Contract::FeeEstimator::Fixed.default_fixed_fee = fee
90
+ end
91
+
92
+ # Set default fee rate to the FeeEstimator::Auto
93
+ # @param [Integer] fee_rate The default fee rate in tapyrus/kB to the FeeEstimator::Auto
94
+ def default_fee_rate=(fee_rate)
95
+ Contract::FeeEstimator::Auto.default_fee_rate = fee_rate
90
96
  end
91
97
  end
92
98
  end
@@ -11,6 +11,8 @@ module Glueby
11
11
 
12
12
  belongs_to :prev, class_name: 'Glueby::Contract::AR::Timestamp', optional: true
13
13
 
14
+ validate :validate_prev
15
+
14
16
  class << self
15
17
  def digest_content(content, digest)
16
18
  case digest&.downcase
@@ -69,24 +71,29 @@ module Glueby
69
71
  end
70
72
 
71
73
  # Broadcast and save timestamp
72
- # @param [Glueby::Contract::FixedFeeEstimator] fee_estimator
74
+ # @param [Glueby::Contract::FeeEstimator] fee_estimator
73
75
  # @param [Glueby::UtxoProvider] utxo_provider
74
76
  # @return true if tapyrus transactions were broadcasted and the timestamp was updated successfully, otherwise false.
75
- def save_with_broadcast(fee_estimator: Glueby::Contract::FixedFeeEstimator.new, utxo_provider: nil)
77
+ def save_with_broadcast(fee_estimator: Glueby::Contract::FeeEstimator::Fixed.new, utxo_provider: nil)
76
78
  save_with_broadcast!(fee_estimator: fee_estimator, utxo_provider: utxo_provider)
77
- rescue Errors::FailedToBroadcast, Errors::PrevTimestampNotFound, Errors::PrevTimestampIsNotTrackable => e
79
+ rescue Errors::FailedToBroadcast => e
78
80
  logger.error("failed to broadcast (id=#{id}, reason=#{e.message})")
79
81
  false
82
+ rescue Errors::ArgumentError => e
83
+ logger.info("failed to broadcast by argument error (id=#{id}, reason=#{e.message})")
84
+ false
80
85
  end
81
86
 
82
87
  # Broadcast and save timestamp, and it raises errors
83
- # @param [Glueby::Contract::FixedFeeEstimator] fee_estimator
88
+ # @param [Glueby::Contract::FeeEstimator] fee_estimator
84
89
  # @param [Glueby::UtxoProvider] utxo_provider
85
90
  # @return true if tapyrus transactions were broadcasted and the timestamp was updated successfully
86
91
  # @raise [Glueby::Contract::Errors::FailedToBroadcast] If the broadcasting is failure
87
92
  # @raise [Glueby::Contract::Errors::PrevTimestampNotFound] If it is not available that the timestamp record which correspond with the prev_id attribute
88
93
  # @raise [Glueby::Contract::Errors::PrevTimestampIsNotTrackable] If the timestamp record by prev_id is not trackable
89
- def save_with_broadcast!(fee_estimator: Glueby::Contract::FixedFeeEstimator.new, utxo_provider: nil)
94
+ # @raise [Glueby::Contract::Errors::PrevTimestampAlreadyUpdated] If the previous timestamp was already updated
95
+ def save_with_broadcast!(fee_estimator: Glueby::Contract::FeeEstimator::Fixed.new, utxo_provider: nil)
96
+ validate_prev!
90
97
  utxo_provider = Glueby::UtxoProvider.new if !utxo_provider && Glueby.configuration.use_utxo_provider?
91
98
 
92
99
  funding_tx, tx, p2c_address, payment_base = create_txs(fee_estimator, utxo_provider)
@@ -101,7 +108,7 @@ module Glueby
101
108
  wallet.internal_wallet.broadcast(tx) do |tx|
102
109
  assign_attributes(txid: tx.txid, status: :unconfirmed, p2c_address: p2c_address, payment_base: payment_base)
103
110
  @tx = tx
104
- save!
111
+ save!(validate: false) # The validation is already executed at head of #save_with_broadcast!
105
112
 
106
113
  if update_trackable?
107
114
  prev.latest = false
@@ -111,8 +118,7 @@ module Glueby
111
118
  end
112
119
  logger.info("timestamp tx was broadcasted (id=#{id}, txid=#{tx.txid})")
113
120
  true
114
- rescue ActiveRecord::RecordInvalid,
115
- Tapyrus::RPC::Error,
121
+ rescue Tapyrus::RPC::Error,
116
122
  Internal::Wallet::Errors::WalletAlreadyLoaded,
117
123
  Internal::Wallet::Errors::WalletNotFound,
118
124
  Errors::InsufficientFunds => e
@@ -130,18 +136,6 @@ module Glueby
130
136
  builder = builder_class.new(wallet, fee_estimator)
131
137
 
132
138
  if builder.instance_of?(Contract::Timestamp::TxBuilder::UpdatingTrackable)
133
- unless prev
134
- message = "The previous timestamp(id: #{prev_id}) not found."
135
- errors.add(:prev_id, message)
136
- raise Errors::PrevTimestampNotFound, message
137
- end
138
-
139
- unless prev.trackable?
140
- message = "The previous timestamp(id: #{prev_id}) type must be trackable"
141
- errors.add(:prev_id, message)
142
- raise Errors::PrevTimestampIsNotTrackable, message
143
- end
144
-
145
139
  builder.set_prev_timestamp_info(
146
140
  timestamp_utxo: prev.utxo,
147
141
  payment_base: prev.payment_base,
@@ -177,6 +171,41 @@ module Glueby
177
171
  def update_trackable?
178
172
  trackable? && prev_id
179
173
  end
174
+
175
+ def validate_prev
176
+ validate_prev!
177
+ true
178
+ rescue Errors::ArgumentError
179
+ false
180
+ end
181
+
182
+ def validate_prev!
183
+ if simple? && prev_id
184
+ message = "The previous timestamp(id: #{prev_id}) must be nil in simple timestamp"
185
+ errors.add(:prev_id, message)
186
+ raise Errors::UnnecessaryPrevTimestamp, message
187
+ end
188
+
189
+ return unless update_trackable?
190
+
191
+ unless prev
192
+ message = "The previous timestamp(id: #{prev_id}) not found."
193
+ errors.add(:prev_id, message)
194
+ raise Errors::PrevTimestampNotFound, message
195
+ end
196
+
197
+ unless prev.trackable?
198
+ message = "The previous timestamp(id: #{prev_id}) type must be trackable"
199
+ errors.add(:prev_id, message)
200
+ raise Errors::PrevTimestampIsNotTrackable, message
201
+ end
202
+
203
+ if new_record? && self.class.where(prev_id: prev_id).exists?
204
+ message = "The previous timestamp(id: #{prev_id}) was already updated"
205
+ errors.add(:prev_id, message)
206
+ raise Errors::PrevTimestampAlreadyUpdated, message
207
+ end
208
+ end
180
209
  end
181
210
  end
182
211
  end
@@ -17,6 +17,8 @@ module Glueby
17
17
  class UnsupportedDigestType < ArgumentError; end
18
18
  class PrevTimestampNotFound < ArgumentError; end
19
19
  class PrevTimestampIsNotTrackable < ArgumentError; end
20
+ class UnnecessaryPrevTimestamp < ArgumentError; end
21
+ class PrevTimestampAlreadyUpdated < ArgumentError; end
20
22
  end
21
23
  end
22
24
  end
@@ -0,0 +1,34 @@
1
+ module Glueby
2
+ module Contract
3
+ module FeeEstimator
4
+ # It calculates actual minimum fee to broadcast txs.
5
+ class Auto
6
+ include FeeEstimator
7
+
8
+ # This is same with Tapyrus Core's default min_relay_fee value.(tapyrus/kB)
9
+ DEFAULT_FEE_RATE = 1_000
10
+
11
+ # @!attribute [r] fee_rate
12
+ # @return [Integer] the fee rate(tapyrus/kB) that is used actual fee calculation
13
+ attr_reader :fee_rate
14
+
15
+ class << self
16
+ # @!attribute [rw] default_fee_rate
17
+ # @return [Integer] The global fee rate configuration. All instances use this value as the default.
18
+ attr_accessor :default_fee_rate
19
+ end
20
+
21
+ # @param [Integer] fee_rate
22
+ def initialize(fee_rate: Auto.default_fee_rate || DEFAULT_FEE_RATE)
23
+ @fee_rate = fee_rate
24
+ end
25
+
26
+ private
27
+
28
+ def estimate_fee(tx)
29
+ ((tx.to_payload.bytesize / 1000.0) * fee_rate).ceil
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module Glueby
2
+ module Contract
3
+ module FeeEstimator
4
+ class Fixed
5
+ include FeeEstimator
6
+
7
+ # Default fee in tapyrus that is used if it is not configured
8
+ DEFAULT_FEE = 10_000
9
+
10
+ attr_reader :fixed_fee
11
+
12
+ class << self
13
+ attr_accessor :default_fixed_fee
14
+ end
15
+
16
+ def initialize(fixed_fee: Fixed.default_fixed_fee || DEFAULT_FEE)
17
+ @fixed_fee = fixed_fee
18
+ end
19
+
20
+ private
21
+
22
+ # @private
23
+ # @return fee by tapyrus(not TPC).
24
+ def estimate_fee(_tx)
25
+ @fixed_fee
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,6 +1,9 @@
1
1
  module Glueby
2
2
  module Contract
3
3
  module FeeEstimator
4
+ autoload :Fixed, 'glueby/contract/fee_estimator/fixed'
5
+ autoload :Auto, 'glueby/contract/fee_estimator/auto'
6
+
4
7
  # @param [Tapyrus::Tx] tx - The target tx
5
8
  # @return fee by tapyrus(not TPC).
6
9
  def fee(tx)
@@ -8,37 +11,38 @@ module Glueby
8
11
  estimate_fee(tx)
9
12
  end
10
13
 
11
- private
12
-
13
- # @private
14
- # @abstract Override in subclasses. This is would be implemented an actual estimation logic.
15
- # @param [Tapyrus::Tx] tx - The target tx
16
- # @return fee by tapyrus(not TPC).
17
- def estimate_fee(tx)
18
- raise NotImplementedError
19
- end
20
- end
21
-
22
- class FixedFeeEstimator
23
- include FeeEstimator
14
+ # Add dummy inputs and outputs to tx
15
+ def dummy_tx(tx)
16
+ dummy = Tapyrus::Tx.parse_from_payload(tx.to_payload)
24
17
 
25
- attr_reader :fixed_fee
18
+ # dummy input for tpc
19
+ out_point = Tapyrus::OutPoint.new('00' * 32, 0)
20
+ dummy.inputs << Tapyrus::TxIn.new(out_point: out_point)
26
21
 
27
- class << self
28
- attr_accessor :default_fixed_fee
29
- end
22
+ # Add script_sig to all intpus
23
+ dummy.inputs.each do |input|
24
+ input.script_sig = Tapyrus::Script.parse_from_payload('000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.htb)
25
+ end
30
26
 
31
- def initialize(fixed_fee: FixedFeeEstimator.default_fixed_fee || 10_000)
32
- @fixed_fee = fixed_fee
27
+ # dummy output to return change
28
+ change_script = Tapyrus::Script.to_p2pkh('0000000000000000000000000000000000000000')
29
+ dummy.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: change_script)
30
+ dummy
33
31
  end
32
+ module_function :dummy_tx
34
33
 
35
34
  private
36
35
 
37
36
  # @private
37
+ # @abstract Override in subclasses. This is would be implemented an actual estimation logic.
38
+ # @param [Tapyrus::Tx] tx - The target tx
38
39
  # @return fee by tapyrus(not TPC).
39
- def estimate_fee(_tx)
40
- @fixed_fee
40
+ def estimate_fee(tx)
41
+ raise NotImplementedError
41
42
  end
42
43
  end
44
+
45
+ # Create alias name of FeeEstimator::Fixed class for backward compatibility
46
+ FixedFeeEstimator = FeeEstimator::Fixed
43
47
  end
44
48
  end
@@ -32,11 +32,11 @@ module Glueby
32
32
  extend Glueby::Contract::TxBuilder
33
33
 
34
34
  class << self
35
- def transfer(sender:, receiver_address:, amount:, fee_estimator: FixedFeeEstimator.new)
35
+ def transfer(sender:, receiver_address:, amount:, fee_estimator: FeeEstimator::Fixed.new)
36
36
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
37
37
 
38
38
  tx = Tapyrus::Tx.new
39
- dummy_fee = fee_estimator.fee(dummy_tx(tx))
39
+ dummy_fee = fee_estimator.fee(FeeEstimator.dummy_tx(tx))
40
40
 
41
41
  sum, outputs = sender.internal_wallet.collect_uncolored_outputs(dummy_fee + amount)
42
42
  fill_input(tx, outputs)
@@ -55,7 +55,7 @@ module Glueby
55
55
  private
56
56
 
57
57
  def fee
58
- @fee ||= @fee_estimator.fee(dummy_tx(@txb.build))
58
+ @fee ||= @fee_estimator.fee(FeeEstimator.dummy_tx(@txb.build))
59
59
  end
60
60
 
61
61
  def sign_tx
@@ -35,7 +35,7 @@ module Glueby
35
35
  wallet:,
36
36
  content:,
37
37
  prefix: '',
38
- fee_estimator: Glueby::Contract::FixedFeeEstimator.new,
38
+ fee_estimator: Glueby::Contract::FeeEstimator::Fixed.new,
39
39
  digest: :sha256,
40
40
  utxo_provider: nil,
41
41
  timestamp_type: :simple,
@@ -8,7 +8,7 @@ module Glueby
8
8
  end
9
9
 
10
10
  # Create new public key, and new transaction that sends TPC to it
11
- def create_funding_tx(wallet:, script: nil, fee_estimator: FixedFeeEstimator.new, need_value_for_change_output: false, only_finalized: true)
11
+ def create_funding_tx(wallet:, script: nil, fee_estimator: FeeEstimator::Fixed.new, need_value_for_change_output: false, only_finalized: true)
12
12
  if Glueby.configuration.use_utxo_provider?
13
13
  utxo_provider = UtxoProvider.new
14
14
  script_pubkey = script ? script : Tapyrus::Script.parse_from_addr(wallet.internal_wallet.receive_address)
@@ -16,7 +16,7 @@ module Glueby
16
16
  utxo_provider.wallet.sign_tx(funding_tx)
17
17
  else
18
18
  txb = Tapyrus::TxBuilder.new
19
- fee = fee_estimator.fee(dummy_tx(txb.build))
19
+ fee = fee_estimator.fee(FeeEstimator.dummy_tx(txb.build))
20
20
  amount = fee + funding_tx_amount(need_value_for_change_output: need_value_for_change_output)
21
21
  sum, outputs = wallet.internal_wallet.collect_uncolored_outputs(amount, nil, only_finalized)
22
22
  outputs.each do |utxo|
@@ -37,7 +37,7 @@ module Glueby
37
37
  end
38
38
  end
39
39
 
40
- def create_issue_tx_for_reissuable_token(funding_tx:, issuer:, amount:, split: 1, fee_estimator: FixedFeeEstimator.new)
40
+ def create_issue_tx_for_reissuable_token(funding_tx:, issuer:, amount:, split: 1, fee_estimator: FeeEstimator::Fixed.new)
41
41
  tx = Tapyrus::Tx.new
42
42
 
43
43
  out_point = Tapyrus::OutPoint.from_txid(funding_tx.txid, 0)
@@ -50,7 +50,7 @@ module Glueby
50
50
 
51
51
  add_split_output(tx, amount, split, receiver_colored_script)
52
52
 
53
- fee = fee_estimator.fee(dummy_tx(tx))
53
+ fee = fee_estimator.fee(FeeEstimator.dummy_tx(tx))
54
54
  fill_change_tpc(tx, issuer, output.value - fee)
55
55
  prev_txs = [{
56
56
  txid: funding_tx.txid,
@@ -61,15 +61,15 @@ module Glueby
61
61
  issuer.internal_wallet.sign_tx(tx, prev_txs)
62
62
  end
63
63
 
64
- def create_issue_tx_for_non_reissuable_token(funding_tx: nil, issuer:, amount:, split: 1, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
64
+ def create_issue_tx_for_non_reissuable_token(funding_tx: nil, issuer:, amount:, split: 1, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
65
65
  create_issue_tx_from_out_point(funding_tx: funding_tx, token_type: Tapyrus::Color::TokenTypes::NON_REISSUABLE, issuer: issuer, amount: amount, split: split, fee_estimator: fee_estimator, only_finalized: only_finalized)
66
66
  end
67
67
 
68
- def create_issue_tx_for_nft_token(funding_tx: nil, issuer:, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
68
+ def create_issue_tx_for_nft_token(funding_tx: nil, issuer:, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
69
69
  create_issue_tx_from_out_point(funding_tx: funding_tx, token_type: Tapyrus::Color::TokenTypes::NFT, issuer: issuer, amount: 1, fee_estimator: fee_estimator, only_finalized: only_finalized)
70
70
  end
71
71
 
72
- def create_issue_tx_from_out_point(funding_tx: nil, token_type:, issuer:, amount:, split: 1, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
72
+ def create_issue_tx_from_out_point(funding_tx: nil, token_type:, issuer:, amount:, split: 1, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
73
73
  tx = Tapyrus::Tx.new
74
74
 
75
75
  fee = fee_estimator.fee(dummy_issue_tx_from_out_point)
@@ -111,7 +111,7 @@ module Glueby
111
111
  issuer.internal_wallet.sign_tx(tx, prev_txs)
112
112
  end
113
113
 
114
- def create_reissue_tx(funding_tx:, issuer:, amount:, color_id:, split: 1, fee_estimator: FixedFeeEstimator.new)
114
+ def create_reissue_tx(funding_tx:, issuer:, amount:, color_id:, split: 1, fee_estimator: FeeEstimator::Fixed.new)
115
115
  tx = Tapyrus::Tx.new
116
116
 
117
117
  out_point = Tapyrus::OutPoint.from_txid(funding_tx.txid, 0)
@@ -122,7 +122,7 @@ module Glueby
122
122
  receiver_colored_script = receiver_script.add_color(color_id)
123
123
  add_split_output(tx, amount, split, receiver_colored_script)
124
124
 
125
- fee = fee_estimator.fee(dummy_tx(tx))
125
+ fee = fee_estimator.fee(FeeEstimator.dummy_tx(tx))
126
126
  fill_change_tpc(tx, issuer, output.value - fee)
127
127
  prev_txs = [{
128
128
  txid: funding_tx.txid,
@@ -133,7 +133,7 @@ module Glueby
133
133
  issuer.internal_wallet.sign_tx(tx, prev_txs)
134
134
  end
135
135
 
136
- def create_transfer_tx(funding_tx:nil, color_id:, sender:, receiver_address:, amount:, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
136
+ def create_transfer_tx(funding_tx:nil, color_id:, sender:, receiver_address:, amount:, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
137
137
  receivers = [{ address: receiver_address, amount: amount }]
138
138
  create_multi_transfer_tx(
139
139
  funding_tx: funding_tx,
@@ -145,7 +145,7 @@ module Glueby
145
145
  )
146
146
  end
147
147
 
148
- def create_multi_transfer_tx(funding_tx:nil, color_id:, sender:, receivers:, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
148
+ def create_multi_transfer_tx(funding_tx:nil, color_id:, sender:, receivers:, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
149
149
  tx = Tapyrus::Tx.new
150
150
 
151
151
  amount = receivers.reduce(0) { |sum, r| sum + r[:amount].to_i }
@@ -161,7 +161,7 @@ module Glueby
161
161
 
162
162
  fill_change_token(tx, sender, sum_token - amount, color_id)
163
163
 
164
- fee = fee_estimator.fee(dummy_tx(tx))
164
+ fee = fee_estimator.fee(FeeEstimator.dummy_tx(tx))
165
165
  sum_tpc = if funding_tx
166
166
  out_point = Tapyrus::OutPoint.from_txid(funding_tx.txid, 0)
167
167
  tx.inputs << Tapyrus::TxIn.new(out_point: out_point)
@@ -187,7 +187,7 @@ module Glueby
187
187
  sender.internal_wallet.sign_tx(tx, prev_txs)
188
188
  end
189
189
 
190
- def create_burn_tx(funding_tx:nil, color_id:, sender:, amount: 0, fee_estimator: FixedFeeEstimator.new, only_finalized: true)
190
+ def create_burn_tx(funding_tx:nil, color_id:, sender:, amount: 0, fee_estimator: FeeEstimator::Fixed.new, only_finalized: true)
191
191
  tx = Tapyrus::Tx.new
192
192
 
193
193
  utxos = sender.internal_wallet.list_unspent(only_finalized)
@@ -196,7 +196,7 @@ module Glueby
196
196
 
197
197
  fill_change_token(tx, sender, sum_token - amount, color_id) if amount.positive?
198
198
 
199
- fee = fee_estimator.fee(dummy_tx(tx))
199
+ fee = fee_estimator.fee(FeeEstimator.dummy_tx(tx))
200
200
 
201
201
  sum_tpc = if funding_tx
202
202
  out_point = Tapyrus::OutPoint.from_txid(funding_tx.txid, 0)
@@ -281,45 +281,26 @@ module Glueby
281
281
  results
282
282
  end
283
283
 
284
- # Add dummy inputs and outputs to tx
285
- def dummy_tx(tx)
286
- dummy = Tapyrus::Tx.parse_from_payload(tx.to_payload)
287
-
288
- # dummy input for tpc
289
- out_point = Tapyrus::OutPoint.new('00' * 32, 0)
290
- dummy.inputs << Tapyrus::TxIn.new(out_point: out_point)
291
-
292
- # Add script_sig to all intpus
293
- dummy.inputs.each do |input|
294
- input.script_sig = Tapyrus::Script.parse_from_payload('000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'.htb)
295
- end
296
-
297
- # dummy output to return change
298
- change_script = Tapyrus::Script.to_p2pkh('0000000000000000000000000000000000000000')
299
- dummy.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: change_script)
300
- dummy
301
- end
302
-
303
284
  # Add dummy inputs and outputs to tx for issue non-reissuable transaction and nft transaction
304
285
  def dummy_issue_tx_from_out_point
305
286
  tx = Tapyrus::Tx.new
306
287
  receiver_colored_script = Tapyrus::Script.parse_from_payload('21c20000000000000000000000000000000000000000000000000000000000000000bc76a914000000000000000000000000000000000000000088ac'.htb)
307
288
  tx.outputs << Tapyrus::TxOut.new(value: 0, script_pubkey: receiver_colored_script)
308
- dummy_tx(tx)
289
+ FeeEstimator.dummy_tx(tx)
309
290
  end
310
291
 
311
292
  private
312
293
 
313
294
  # The amount of output in funding tx
314
- # It returns same amount with FixedFeeEstimator's fixed fee. Because it is enough for paying fee for consumer
295
+ # It returns same amount with FeeEstimator::Fixed's fixed fee. Because it is enough for paying fee for consumer
315
296
  # transactions of the funding transactions.
316
297
  #
317
298
  # @option [Boolean] need_value_for_change_output If it is true, adds more value than the fee for producing change output.
318
299
  def funding_tx_amount(need_value_for_change_output: false)
319
300
  if need_value_for_change_output
320
- FixedFeeEstimator.new.fixed_fee + DUST_LIMIT
301
+ FeeEstimator::Fixed.new.fixed_fee + DUST_LIMIT
321
302
  else
322
- FixedFeeEstimator.new.fixed_fee
303
+ FeeEstimator::Fixed.new.fixed_fee
323
304
  end
324
305
  end
325
306
  end
@@ -20,7 +20,7 @@ module Glueby
20
20
  tx.inputs.each.with_index do |input, index|
21
21
  script_pubkey = script_for_input(input, prevtxs)
22
22
  next unless script_pubkey
23
- key = Key.key_for_script(script_pubkey)
23
+ key = keys.key_for_script(script_pubkey)
24
24
  next unless key
25
25
  sign_tx_for_p2pkh(tx, index, key, script_pubkey, sighashtype)
26
26
  end
@@ -24,6 +24,7 @@ module Glueby
24
24
  # value is configurable by :default_value. This method do the management to the pool.
25
25
  def manage_utxo_pool
26
26
  txb = Tapyrus::TxBuilder.new
27
+ fee_estimator = utxo_provider.fee_estimator_for_manage
27
28
 
28
29
  sum, utxos = collect_outputs
29
30
  return if utxos.empty?
@@ -35,7 +36,7 @@ module Glueby
35
36
 
36
37
  added_outputs = 0
37
38
  shortage.times do
38
- fee = utxo_provider.fee_estimator.fee(dummy_tx(txb.build))
39
+ fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
39
40
  break if (sum - fee) < utxo_provider.default_value
40
41
  txb.pay(utxo_provider.address, utxo_provider.default_value)
41
42
  sum -= utxo_provider.default_value
@@ -44,7 +45,7 @@ module Glueby
44
45
 
45
46
  return if added_outputs == 0
46
47
 
47
- fee = utxo_provider.fee_estimator.fee(dummy_tx(txb.build))
48
+ fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
48
49
  tx = txb.change_address(utxo_provider.address)
49
50
  .fee(fee)
50
51
  .build
@@ -16,6 +16,8 @@ module Glueby
16
16
  # @option config [Integer] :default_value
17
17
  # @option opts [Integer] :utxo_pool_size
18
18
  # @option opts [Glueby::Contract::FeeEstimator] :fee_estimator
19
+ # @option opts [Glueby::Contract::FeeEstimator] :fee_estimator_for_manage It uses this estimator in glueby:utxo_provider:manage_utxo_pool rake task.
20
+ # If it is nil, it uses same as :fee_estimator instead.
19
21
  def configure(config)
20
22
  @config = config
21
23
  end
@@ -24,10 +26,11 @@ module Glueby
24
26
  def initialize
25
27
  @wallet = load_wallet
26
28
  validate_config!
27
- @fee_estimator = (UtxoProvider.config && UtxoProvider.config[:fee_estimator]) || Glueby::Contract::FixedFeeEstimator.new
29
+ @fee_estimator = (UtxoProvider.config && UtxoProvider.config[:fee_estimator]) || Glueby::Contract::FeeEstimator::Fixed.new
30
+ @fee_estimator_for_manage = UtxoProvider.config && UtxoProvider.config[:fee_estimator_for_manage] || @fee_estimator
28
31
  end
29
32
 
30
- attr_reader :wallet, :fee_estimator, :address
33
+ attr_reader :wallet, :fee_estimator, :fee_estimator_for_manage, :address
31
34
 
32
35
  # Provide a UTXO
33
36
  # @param [Tapyrus::Script] script_pubkey The script to be provided
@@ -40,7 +43,7 @@ module Glueby
40
43
  txb = Tapyrus::TxBuilder.new
41
44
  txb.pay(script_pubkey.addresses.first, value)
42
45
 
43
- fee = fee_estimator.fee(dummy_tx(txb.build))
46
+ fee = fee_estimator.fee(Contract::FeeEstimator.dummy_tx(txb.build))
44
47
  # The outputs need to be shuffled so that no utxos are spent twice as possible.
45
48
  sum, outputs = collect_uncolored_outputs(wallet, fee + value)
46
49
 
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "0.10.1"
2
+ VERSION = "0.12.0"
3
3
  end
@@ -7,7 +7,7 @@ module Glueby
7
7
 
8
8
  def create
9
9
  timestamps = Glueby::Contract::AR::Timestamp.where(status: :init)
10
- fee_estimator = Glueby::Contract::FixedFeeEstimator.new
10
+ fee_estimator = Glueby::Contract::FeeEstimator::Fixed.new
11
11
  timestamps.each { |t| t.save_with_broadcast(fee_estimator: fee_estimator) }
12
12
  end
13
13
  end
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.10.1
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-01 00:00:00.000000000 Z
11
+ date: 2022-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tapyrus
@@ -110,6 +110,8 @@ files:
110
110
  - lib/glueby/contract/active_record/timestamp.rb
111
111
  - lib/glueby/contract/errors.rb
112
112
  - lib/glueby/contract/fee_estimator.rb
113
+ - lib/glueby/contract/fee_estimator/auto.rb
114
+ - lib/glueby/contract/fee_estimator/fixed.rb
113
115
  - lib/glueby/contract/payment.rb
114
116
  - lib/glueby/contract/timestamp.rb
115
117
  - lib/glueby/contract/timestamp/syncer.rb