glueby 0.10.2 → 1.0.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: 71dfd0cca2e922f07b29b4839be2f7cff8c71f587ff043bb05eee6d8cac3435e
4
- data.tar.gz: f3b4b8864d75be7eccad66c402bb56693949ade3c7dfa1b21e3f5abc5c6391b5
3
+ metadata.gz: 660935ed3cb303d6c9f73b27fffd978f048365e60b701b598b0fa0384ceff8ad
4
+ data.tar.gz: 02d01a50fb301290ee693eb02b9592d193f6a8a10b5fb4cbb5419bc1ab01a41a
5
5
  SHA512:
6
- metadata.gz: 831aaf50ea027c47450ad167721b441a6253b9c439d1608179f7c0bf9244eaa5cc8bc5a1fc916b31411616389e4750ecdda9cda0497479b6810e8fba134a0fd8
7
- data.tar.gz: 4f078178c9874c00e1ce36e11c13b7813972880ecfc3f7c8ee686040012b6e8ce0038376f2ec808027a99c3beb377e7a5e7493c2f78577533ed11403cd2928e9
6
+ metadata.gz: efc0bed86713965cc63cd747fc4477a825ff9e715ef9d95c5dfed1c42b2622e090cc3d3d85f1feef715bc497081269c2315c683bf8b0c9d90b975b04b310f69a
7
+ data.tar.gz: 3c2c5295abd8782850eaa1a901b44d901eaf088d3666eab918ce43992e5c5a9c5091583147511d8cfd5286351de8095eae2f4ae1163386088578717b2a199da7
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,7 +11,6 @@ module Glueby
11
11
 
12
12
  belongs_to :prev, class_name: 'Glueby::Contract::AR::Timestamp', optional: true
13
13
 
14
- validates :prev, uniqueness: true, allow_nil: true
15
14
  validate :validate_prev
16
15
 
17
16
  class << self
@@ -72,24 +71,29 @@ module Glueby
72
71
  end
73
72
 
74
73
  # Broadcast and save timestamp
75
- # @param [Glueby::Contract::FixedFeeEstimator] fee_estimator
74
+ # @param [Glueby::Contract::FeeEstimator] fee_estimator
76
75
  # @param [Glueby::UtxoProvider] utxo_provider
77
76
  # @return true if tapyrus transactions were broadcasted and the timestamp was updated successfully, otherwise false.
78
- 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)
79
78
  save_with_broadcast!(fee_estimator: fee_estimator, utxo_provider: utxo_provider)
80
- rescue Errors::FailedToBroadcast, Errors::PrevTimestampNotFound, Errors::PrevTimestampIsNotTrackable => e
79
+ rescue Errors::FailedToBroadcast => e
81
80
  logger.error("failed to broadcast (id=#{id}, reason=#{e.message})")
82
81
  false
82
+ rescue Errors::ArgumentError => e
83
+ logger.info("failed to broadcast by argument error (id=#{id}, reason=#{e.message})")
84
+ false
83
85
  end
84
86
 
85
87
  # Broadcast and save timestamp, and it raises errors
86
- # @param [Glueby::Contract::FixedFeeEstimator] fee_estimator
88
+ # @param [Glueby::Contract::FeeEstimator] fee_estimator
87
89
  # @param [Glueby::UtxoProvider] utxo_provider
88
90
  # @return true if tapyrus transactions were broadcasted and the timestamp was updated successfully
89
91
  # @raise [Glueby::Contract::Errors::FailedToBroadcast] If the broadcasting is failure
90
92
  # @raise [Glueby::Contract::Errors::PrevTimestampNotFound] If it is not available that the timestamp record which correspond with the prev_id attribute
91
93
  # @raise [Glueby::Contract::Errors::PrevTimestampIsNotTrackable] If the timestamp record by prev_id is not trackable
92
- 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!
93
97
  utxo_provider = Glueby::UtxoProvider.new if !utxo_provider && Glueby.configuration.use_utxo_provider?
94
98
 
95
99
  funding_tx, tx, p2c_address, payment_base = create_txs(fee_estimator, utxo_provider)
@@ -104,7 +108,7 @@ module Glueby
104
108
  wallet.internal_wallet.broadcast(tx) do |tx|
105
109
  assign_attributes(txid: tx.txid, status: :unconfirmed, p2c_address: p2c_address, payment_base: payment_base)
106
110
  @tx = tx
107
- save!
111
+ save!(validate: false) # The validation is already executed at head of #save_with_broadcast!
108
112
 
109
113
  if update_trackable?
110
114
  prev.latest = false
@@ -114,8 +118,7 @@ module Glueby
114
118
  end
115
119
  logger.info("timestamp tx was broadcasted (id=#{id}, txid=#{tx.txid})")
116
120
  true
117
- rescue ActiveRecord::RecordInvalid,
118
- Tapyrus::RPC::Error,
121
+ rescue Tapyrus::RPC::Error,
119
122
  Internal::Wallet::Errors::WalletAlreadyLoaded,
120
123
  Internal::Wallet::Errors::WalletNotFound,
121
124
  Errors::InsufficientFunds => e
@@ -133,8 +136,6 @@ module Glueby
133
136
  builder = builder_class.new(wallet, fee_estimator)
134
137
 
135
138
  if builder.instance_of?(Contract::Timestamp::TxBuilder::UpdatingTrackable)
136
- validate_prev!
137
-
138
139
  builder.set_prev_timestamp_info(
139
140
  timestamp_utxo: prev.utxo,
140
141
  payment_base: prev.payment_base,
@@ -174,9 +175,7 @@ module Glueby
174
175
  def validate_prev
175
176
  validate_prev!
176
177
  true
177
- rescue Errors::PrevTimestampNotFound,
178
- Errors::PrevTimestampIsNotTrackable,
179
- Errors::UnnecessaryPrevTimestamp
178
+ rescue Errors::ArgumentError
180
179
  false
181
180
  end
182
181
 
@@ -200,6 +199,12 @@ module Glueby
200
199
  errors.add(:prev_id, message)
201
200
  raise Errors::PrevTimestampIsNotTrackable, message
202
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
203
208
  end
204
209
  end
205
210
  end
@@ -18,6 +18,7 @@ module Glueby
18
18
  class PrevTimestampNotFound < ArgumentError; end
19
19
  class PrevTimestampIsNotTrackable < ArgumentError; end
20
20
  class UnnecessaryPrevTimestamp < ArgumentError; end
21
+ class PrevTimestampAlreadyUpdated < ArgumentError; end
21
22
  end
22
23
  end
23
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)
@@ -25,7 +25,11 @@ module Glueby
25
25
  @prefix = prefix
26
26
  @data = data
27
27
 
28
- @txb.data(prefix + data)
28
+ contents = [prefix, data].map do |content|
29
+ content.bytes.map { |i| i.to_s(16) }.join
30
+ end
31
+
32
+ @txb.data(*contents)
29
33
  self
30
34
  end
31
35
 
@@ -55,7 +59,7 @@ module Glueby
55
59
  private
56
60
 
57
61
  def fee
58
- @fee ||= @fee_estimator.fee(dummy_tx(@txb.build))
62
+ @fee ||= @fee_estimator.fee(FeeEstimator.dummy_tx(@txb.build))
59
63
  end
60
64
 
61
65
  def sign_tx
@@ -18,10 +18,10 @@ module Glueby
18
18
  attr_reader :p2c_address, :payment_base
19
19
 
20
20
  # @param [Gleuby::Wallet] wallet The wallet that is sender of the timestamp transaction.
21
- # @param [String] content Data to be hashed and stored in blockchain.
21
+ # @param [String] content The data to be hashed(it is due to digest argument) and stored in blockchain.
22
22
  # @param [String] prefix prefix of op_return data
23
23
  # @param [Glueby::Contract::FeeEstimator] fee_estimator
24
- # @param [Symbol] digest type which select of:
24
+ # @param [Symbol] digest The types which will be used for process the content argument. Select of:
25
25
  # - :sha256
26
26
  # - :double_sha256
27
27
  # - :none
@@ -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
@@ -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.2"
2
+ VERSION = "1.0.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.2
4
+ version: 1.0.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-02 00:00:00.000000000 Z
11
+ date: 2022-04-07 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