glueby 0.6.0 → 0.6.4

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: 6dfda968fd96c80d2e47dafcaba138721e4250a45193e3091e05f37816a2da5c
4
- data.tar.gz: 53cb96baec24e35afb9a271496009b034bea26c013b1cc4aab6ee306acbfe3de
3
+ metadata.gz: 017fd6425f25bc8b48e36ff46456ff42a88dedd43c9e0359c7b39bc06e1fc8df
4
+ data.tar.gz: e617c0e9e5d931f795a4ead6d929aac46f2b70f3da5eefbd74434814d85884b2
5
5
  SHA512:
6
- metadata.gz: eb7e3fd70fdf180ef3442f269fba947a542986a588b12dac8ad42a5fb2e54b3ee97955c63ae16a6739105475834ad61de27fe6989e0a7fb2d045dcd0a9ad746e
7
- data.tar.gz: 3ddc3b8eaab504a46337222577613082e44f6a8d047aa80b7084ea9030c39432fc9b763bf7cde25b3f2d122a49441411e327912215bd070d073349631bc9f421
6
+ metadata.gz: 5f93eee936244581432347212864661160c5c9f282009ae1bcb4d0870f344f951d93b2a06df2f680e1ab3227b0be87face08eba17c6539fc286c24b719d8f68e
7
+ data.tar.gz: 18df860bc145726f1cbf133f9f3df9f099c01bcba6f3f8c2817e19686c906938df455c2346dd55d21a5ebed99e30e9204ae4dd50ea0bf18043f0ee6a83caec08
@@ -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
@@ -2,7 +2,10 @@ module Glueby
2
2
  module Contract
3
3
  module AR
4
4
  class Timestamp < ::ActiveRecord::Base
5
+ include Glueby::GluebyLogger
5
6
  include Glueby::Contract::Timestamp::Util
7
+ include Glueby::Contract::TxBuilder
8
+
6
9
  enum status: { init: 0, unconfirmed: 1, confirmed: 2 }
7
10
  enum timestamp_type: { simple: 0, trackable: 1 }
8
11
 
@@ -16,6 +19,38 @@ module Glueby
16
19
  super(wallet_id: attributes[:wallet_id], content_hash: @content_hash,
17
20
  prefix: attributes[:prefix] ? attributes[:prefix] : '', status: :init, timestamp_type: attributes[:timestamp_type] || :simple)
18
21
  end
22
+
23
+ # Return true if timestamp type is 'trackable' and output in timestamp transaction has not been spent yet, otherwise return false.
24
+ def latest
25
+ trackable?
26
+ end
27
+
28
+ # Broadcast and save timestamp
29
+ # @param [Glueby::Contract::FixedFeeEstimator] fee_estimator
30
+ # @return true if tapyrus transactions were broadcasted and the timestamp was updated successfully, otherwise false.
31
+ def save_with_broadcast(fee_estimator: Glueby::Contract::FixedFeeEstimator.new)
32
+ utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
33
+ wallet = Glueby::Wallet.load(wallet_id)
34
+ funding_tx, tx, p2c_address, payment_base = create_txs(wallet, prefix, content_hash, fee_estimator, utxo_provider, type: timestamp_type.to_sym)
35
+ if funding_tx
36
+ ::ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
37
+ wallet.internal_wallet.broadcast(funding_tx)
38
+ logger.info("funding tx was broadcasted(id=#{id}, funding_tx.txid=#{funding_tx.txid})")
39
+ end
40
+ end
41
+ ::ActiveRecord::Base.transaction(joinable: false, requires_new: true) do
42
+ wallet.internal_wallet.broadcast(tx) do |tx|
43
+ assign_attributes(txid: tx.txid, status: :unconfirmed, p2c_address: p2c_address, payment_base: payment_base)
44
+ save!
45
+ end
46
+ logger.info("timestamp tx was broadcasted (id=#{id}, txid=#{tx.txid})")
47
+ end
48
+ true
49
+ rescue => e
50
+ logger.error("failed to broadcast (id=#{id}, reason=#{e.message})")
51
+ errors.add(:base, "failed to broadcast (id=#{id}, reason=#{e.message})")
52
+ false
53
+ end
19
54
  end
20
55
  end
21
56
  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
@@ -19,9 +19,8 @@ module Glueby
19
19
  module_function
20
20
 
21
21
  # @param [Glueby::Wallet] wallet
22
- # @param [Array] data The data to be used for generating pay-to-contract address
23
- # @return [Array(String, String)]
24
- # Return (pay-to-contract address, public key used for generating address)
22
+ # @param [Array] contents The data to be used for generating pay-to-contract address
23
+ # @return [pay-to-contract address, public key used for generating address]
25
24
  def create_pay_to_contract_address(wallet, contents: nil)
26
25
  pubkey = wallet.internal_wallet.create_pubkey.pubkey
27
26
  # Calculate P + H(P || contents)G
@@ -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
@@ -44,7 +44,7 @@ module Glueby
44
44
 
45
45
  fee = fee_estimator.fee(dummy_tx(txb.build))
46
46
  # The outputs need to be shuffled so that no utxos are spent twice as possible.
47
- sum, outputs = wallet.collect_uncolored_outputs(fee + value, nil, true, true)
47
+ sum, outputs = collect_uncolored_outputs(wallet, fee + value)
48
48
 
49
49
  outputs.each do |utxo|
50
50
  txb.add_utxo({
@@ -73,6 +73,20 @@ module Glueby
73
73
  end
74
74
  end
75
75
 
76
+ def collect_uncolored_outputs(wallet, amount)
77
+ utxos = wallet.list_unspent.select { |o| !o[:color_id] && o[:amount] == @default_value }
78
+ utxos.shuffle!
79
+
80
+ utxos.inject([0, []]) do |sum, output|
81
+ new_sum = sum[0] + output[:amount]
82
+ new_outputs = sum[1] << output
83
+ return [new_sum, new_outputs] if new_sum >= amount
84
+
85
+ [new_sum, new_outputs]
86
+ end
87
+ raise Glueby::Contract::Errors::InsufficientFunds
88
+ end
89
+
76
90
  def validate_config!
77
91
  if UtxoProvider.config
78
92
  utxo_pool_size = UtxoProvider.config[:utxo_pool_size]
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.4"
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
@@ -16,6 +17,16 @@ module Glueby
16
17
  require 'glueby/railtie'
17
18
  end
18
19
 
20
+ module GluebyLogger
21
+ def logger
22
+ if defined?(Rails)
23
+ Rails.logger
24
+ else
25
+ Logger.new(STDOUT)
26
+ end
27
+ end
28
+ end
29
+
19
30
  # Add prefix to activerecord table names
20
31
  def self.table_name_prefix
21
32
  'glueby_'
@@ -8,27 +8,8 @@ module Glueby
8
8
 
9
9
  def create
10
10
  timestamps = Glueby::Contract::AR::Timestamp.where(status: :init)
11
- utxo_provider = Glueby::UtxoProvider.new if Glueby.configuration.use_utxo_provider?
12
- timestamps.each do |t|
13
- begin
14
- wallet = Glueby::Wallet.load(t.wallet_id)
15
- funding_tx, tx, p2c_address, payment_base = create_txs(wallet, t.prefix, t.content_hash, Glueby::Contract::FixedFeeEstimator.new, utxo_provider, type: t.timestamp_type.to_sym)
16
- if funding_tx
17
- ::ActiveRecord::Base.transaction do
18
- wallet.internal_wallet.broadcast(funding_tx)
19
- puts "funding tx was broadcasted(id=#{t.id}, funding_tx.txid=#{funding_tx.txid})"
20
- end
21
- end
22
- ::ActiveRecord::Base.transaction do
23
- wallet.internal_wallet.broadcast(tx) do |tx|
24
- t.update(txid: tx.txid, status: :unconfirmed, p2c_address: p2c_address, payment_base: payment_base)
25
- end
26
- puts "timestamp tx was broadcasted (id=#{t.id}, txid=#{tx.txid})"
27
- end
28
- rescue => e
29
- puts "failed to broadcast (id=#{t.id}, reason=#{e.message})"
30
- end
31
- end
11
+ fee_estimator = Glueby::Contract::FixedFeeEstimator.new
12
+ timestamps.each { |t| t.save_with_broadcast(fee_estimator: fee_estimator) }
32
13
  end
33
14
  end
34
15
  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.6.0
4
+ version: 0.6.4
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-08 00:00:00.000000000 Z
11
+ date: 2022-01-20 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