openassets-ruby 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/openassets/api.rb +14 -1
- data/lib/openassets/protocol/asset_definition.rb +3 -1
- data/lib/openassets/transaction/transaction_builder.rb +55 -7
- data/lib/openassets/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 373d3c6333cae6c3f7b59ab0c5b655016d97a246
|
4
|
+
data.tar.gz: f2893fc9d18904df4ea8120d6d9cf88a955021e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1eded75af7ea679718ce74292b4cd2c4a74fa0e84788ec760061d587d2d729d00fe43ca0dfd046991cf56c225384d3ecb5010ba4a53540dd9e6f612fe81bf952
|
7
|
+
data.tar.gz: 24180dbf44cdadd4ccf2daf36bdce9e3fcf9abcab72654a677dbaa58e6cf02af1335a0f4b4c6f9273c7dca4827d1409961ca4aab2bb333742291c2af3465be95
|
data/README.md
CHANGED
@@ -40,7 +40,7 @@ The configuration options are as follows:
|
|
40
40
|
|**provider**|The RPC server. Specify possible now only "bitcoind".|bitcoind|
|
41
41
|
|**cache**|The path to the database file. If you want to use in-memory database, specify ':memory:'.|cache.db|
|
42
42
|
|**dust_limit**|The amount of Bitcoin, which is set to the each output of the Open Assets Protocol(issue or transfer).|600 (satoshi)|
|
43
|
-
|**default_fees**|The transaction fee. (used by issue_asset and send_asset, send_bitcoin )|10000 (satoshi)|
|
43
|
+
|**default_fees**|The default transaction fee in satoshi. If you want to use auto fee settings, specify ':auto'. (used by issue_asset and send_asset, send_bitcoin )|10000 (satoshi)|
|
44
44
|
|**min_confirmation**|The minimum number of confirmations the transaction containing an output that used to get UTXO.|1|
|
45
45
|
|**max_confirmation**|The maximum number of confirmations the transaction containing an output that used to get UTXO.|9999999|
|
46
46
|
|**rpc**|The access information to the RPC server of Bitcoin Core.|N/A|
|
data/lib/openassets/api.rb
CHANGED
@@ -364,7 +364,20 @@ module OpenAssets
|
|
364
364
|
end
|
365
365
|
|
366
366
|
def create_tx_builder
|
367
|
-
|
367
|
+
if @config[:default_fees] == :auto
|
368
|
+
# Estimate a transaction fee rate (satoshis/KB) if fee is specified by :auto
|
369
|
+
efr = coin_to_satoshi(provider.estimatefee(1).to_s).to_i
|
370
|
+
if efr < 0
|
371
|
+
# Negative efr means "estimatefee" of bitcoin-api returns false
|
372
|
+
# In this case, use default minimum fees rate (10_000 satoshis/KB)
|
373
|
+
efr = 10_000
|
374
|
+
end
|
375
|
+
OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit], efr)
|
376
|
+
else
|
377
|
+
# If fee is specified by a fixed value (or the default value)
|
378
|
+
OpenAssets::Transaction::TransactionBuilder.new(@config[:dust_limit])
|
379
|
+
end
|
380
|
+
|
368
381
|
end
|
369
382
|
|
370
383
|
def load_tx(txid)
|
@@ -7,8 +7,12 @@ module OpenAssets
|
|
7
7
|
# The minimum allowed output value.
|
8
8
|
attr_accessor :amount
|
9
9
|
|
10
|
-
|
10
|
+
# The estimated transaction fee rate (satoshis/KB).
|
11
|
+
attr_accessor :efr
|
12
|
+
|
13
|
+
def initialize(amount = 600, efr = 10000)
|
11
14
|
@amount = amount
|
15
|
+
@efr = efr
|
12
16
|
end
|
13
17
|
|
14
18
|
# Creates a transaction for issuing an asset.
|
@@ -17,6 +21,12 @@ module OpenAssets
|
|
17
21
|
# @param [Integer] fees The fees to include in the transaction.
|
18
22
|
# @return[Bitcoin:Protocol:Tx] An unsigned transaction for issuing asset.
|
19
23
|
def issue_asset(issue_spec, metadata, fees)
|
24
|
+
|
25
|
+
if fees == :auto
|
26
|
+
# Calculate fees (assume that one vin and four vouts are wrote)
|
27
|
+
fees = calc_fee(1, 4)
|
28
|
+
end
|
29
|
+
|
20
30
|
inputs, total_amount =
|
21
31
|
TransactionBuilder.collect_uncolored_outputs(issue_spec.unspent_outputs, 2 * @amount + fees)
|
22
32
|
tx = Bitcoin::Protocol::Tx.new
|
@@ -50,7 +60,12 @@ module OpenAssets
|
|
50
60
|
asset_transfer_spec.unspent_outputs, nil, oa_address_to_address(btc_change_script), 0)
|
51
61
|
transfer([[asset_id, asset_transfer_spec]], [btc_transfer_spec], fees)
|
52
62
|
end
|
53
|
-
|
63
|
+
|
64
|
+
# Creates a transaction for sending assets to many.
|
65
|
+
# @param[Array[OpenAssets::Transaction::TransferParameters]] asset_transfer_spec The parameters of the asset being transferred.
|
66
|
+
# @param[String] btc_change_script The script where to send bitcoin change, if any.
|
67
|
+
# @param[Integer] fees The fees to include in the transaction.
|
68
|
+
# @return[Bitcoin::Protocol:Tx] The resulting unsigned transaction.
|
54
69
|
def transfer_assets(transfer_specs, btc_change_script, fees)
|
55
70
|
btc_transfer_spec = OpenAssets::Transaction::TransferParameters.new(
|
56
71
|
transfer_specs[0][1].unspent_outputs, nil, oa_address_to_address(btc_change_script), 0)
|
@@ -72,12 +87,18 @@ module OpenAssets
|
|
72
87
|
def transfer_btcs(btc_transfer_specs, fees)
|
73
88
|
transfer([], btc_transfer_specs, fees)
|
74
89
|
end
|
75
|
-
|
76
|
-
|
90
|
+
|
77
91
|
# Create a transaction for burn asset
|
78
92
|
def burn_asset(unspents, asset_id, fee)
|
93
|
+
|
79
94
|
tx = Bitcoin::Protocol::Tx.new
|
80
95
|
targets = unspents.select{|o|o.output.asset_id == asset_id}
|
96
|
+
|
97
|
+
if fee == :auto
|
98
|
+
# Calculate fees and otsuri (assume that one vout exists)
|
99
|
+
fee = calc_fee(targets.size, 1)
|
100
|
+
end
|
101
|
+
|
81
102
|
raise TransactionBuildError.new('There is no asset.') if targets.length == 0
|
82
103
|
total_amount = targets.inject(0){|sum, o|o.output.value + sum}
|
83
104
|
otsuri = total_amount - fee
|
@@ -207,18 +228,32 @@ module OpenAssets
|
|
207
228
|
# Calculate total amount of bitcoins to send
|
208
229
|
btc_transfer_total_amount = btc_transfer_specs.inject(0) {|sum, b| sum + b.amount}
|
209
230
|
|
210
|
-
if
|
231
|
+
if fees == :auto
|
232
|
+
fixed_fees = 0
|
233
|
+
else
|
234
|
+
fixed_fees = fees
|
235
|
+
end
|
236
|
+
|
237
|
+
if btc_excess < btc_transfer_total_amount + fixed_fees
|
211
238
|
# When there does not exist enough bitcoins to send in the inputs
|
212
239
|
# assign new address (utxo) to the inputs (does not include output coins)
|
213
240
|
# CREATING INPUT (if needed)
|
214
241
|
uncolored_outputs, uncolored_amount =
|
215
|
-
|
242
|
+
TransactionBuilder.collect_uncolored_outputs(utxo, btc_transfer_total_amount + fixed_fees - btc_excess)
|
216
243
|
utxo = utxo - uncolored_outputs
|
217
244
|
inputs << uncolored_outputs
|
218
245
|
btc_excess += uncolored_amount
|
219
246
|
end
|
220
247
|
|
248
|
+
if fees == :auto
|
249
|
+
# Calculate fees and otsuri (the numbers of vins and vouts are known)
|
250
|
+
# +1 in the second term means "otsuri" vout,
|
251
|
+
# and outputs size means the number of vout witn asset_id
|
252
|
+
fees = calc_fee(inputs.size, outputs.size + btc_transfer_specs.size + 1)
|
253
|
+
end
|
254
|
+
|
221
255
|
otsuri = btc_excess - btc_transfer_total_amount - fees
|
256
|
+
|
222
257
|
if otsuri > 0 && otsuri < @amount
|
223
258
|
# When there exists otsuri, but it is smaller than @amount (default is 600 satoshis)
|
224
259
|
# assign new address (utxo) to the input (does not include @amount - otsuri)
|
@@ -261,9 +296,22 @@ module OpenAssets
|
|
261
296
|
}
|
262
297
|
|
263
298
|
outputs.each{|o|tx.add_out(o)}
|
264
|
-
|
299
|
+
|
265
300
|
tx
|
266
301
|
end
|
302
|
+
|
303
|
+
# Calculate a transaction fee
|
304
|
+
# @param [Integer] inputs_num: The number of vin fields.
|
305
|
+
# @param [Integer] outputs_num: The number of vout fields.
|
306
|
+
# @return [Integer] A transaction fee.
|
307
|
+
def calc_fee(inputs_num, outputs_num)
|
308
|
+
# See http://bitcoinfees.com/
|
309
|
+
tx_size = 148 * inputs_num + 34 * outputs_num + 10
|
310
|
+
# See Bitcoin::tx.calculate_minimum_fee
|
311
|
+
fee = (1 + tx_size / 1_000) * @efr
|
312
|
+
|
313
|
+
fee
|
314
|
+
end
|
267
315
|
|
268
316
|
end
|
269
317
|
|
data/lib/openassets/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openassets-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- azuchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bitcoin-ruby
|