block_io 1.2.1 → 2.0.0
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 +4 -4
- data/.appveyor.yml +26 -0
- data/.gitignore +1 -1
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/README.md +13 -7
- data/block_io.gemspec +9 -7
- data/examples/basic.rb +10 -12
- data/examples/dtrust.rb +36 -38
- data/examples/max_withdrawal.rb +29 -0
- data/examples/proxy.rb +36 -0
- data/examples/sweeper.rb +11 -6
- data/lib/block_io.rb +15 -411
- data/lib/block_io/client.rb +179 -0
- data/lib/block_io/constants.rb +10 -0
- data/lib/block_io/helper.rb +164 -0
- data/lib/block_io/key.rb +151 -0
- data/lib/block_io/version.rb +1 -1
- data/spec/client_spec.rb +223 -0
- data/spec/data/sign_and_finalize_dtrust_withdrawal_request.json +1 -0
- data/spec/data/sign_and_finalize_sweep_request.json +1 -0
- data/spec/data/sign_and_finalize_withdrawal_request.json +4 -0
- data/spec/data/sweep_from_address_response.json +1 -0
- data/spec/data/withdraw_from_dtrust_address_response.json +1 -0
- data/spec/data/withdraw_response.json +1227 -0
- data/spec/helper_spec.rb +44 -0
- data/spec/key_spec.rb +61 -0
- data/spec/rfc6979_spec.rb +59 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/withdraw_spec.rb +90 -0
- metadata +119 -35
- data/examples/change.rb +0 -117
data/examples/change.rb
DELETED
@@ -1,117 +0,0 @@
|
|
1
|
-
# 1. create an address (first_address), move all coins to it
|
2
|
-
# 2. create an address (second_address), as the change address for the first_address
|
3
|
-
# 3. send destination_amount to destination_address from first_address, and the rest of the coins to second_address
|
4
|
-
# 4. archive first_address since it will no longer be used
|
5
|
-
|
6
|
-
# execute with:
|
7
|
-
# $ DESTINATION_ADDRESS=AN_ADDRESS DESTINATION_AMOUNT=COINS_TO_SEND_TO_DESTINATION_ADDRESS API_KEY=TESTNET_API_KEY PIN=YOUR_SECRET_PIN ruby change.rb
|
8
|
-
|
9
|
-
require 'block_io'
|
10
|
-
require 'securerandom'
|
11
|
-
require 'bigdecimal'
|
12
|
-
|
13
|
-
# please use the Dogecoin/Bitcoin Testnet API key here
|
14
|
-
BlockIo.set_options :api_key => ENV['API_KEY'], :pin => ENV['PIN'], :version => 2
|
15
|
-
|
16
|
-
# create address A, and withdraw all coins to address A
|
17
|
-
first_address_label = SecureRandom.hex(8) # the source address
|
18
|
-
second_address_label = SecureRandom.hex(8) # the change address
|
19
|
-
|
20
|
-
# let's fill up the first address with whatever coins exist in this account
|
21
|
-
puts BlockIo.get_new_address(:label => first_address_label)
|
22
|
-
|
23
|
-
available_balance = BigDecimal(BlockIo.get_balance()['data']['available_balance'])
|
24
|
-
network_fee = BigDecimal('0.0')
|
25
|
-
|
26
|
-
puts "Available Balance: #{available_balance.truncate(8).to_s('F')}"
|
27
|
-
|
28
|
-
options = {:to_labels => first_address_label, :amounts => available_balance.to_s("F")}
|
29
|
-
|
30
|
-
tries = 2 # retry twice at most
|
31
|
-
|
32
|
-
begin
|
33
|
-
options[:amounts] = (available_balance - network_fee).truncate(8).to_s("F")
|
34
|
-
|
35
|
-
response = BlockIo.get_network_fee_estimate(options)
|
36
|
-
network_fee = BigDecimal(response['data']['estimated_network_fee'])
|
37
|
-
|
38
|
-
puts "Final Network Fee: #{network_fee.truncate(8).to_s('F')}"
|
39
|
-
|
40
|
-
options[:amounts] = (available_balance - network_fee).truncate(8).to_s("F")
|
41
|
-
rescue Exception => e
|
42
|
-
# extract the fee and subtract it from the available amount
|
43
|
-
|
44
|
-
network_fee = BigDecimal(e.to_s.split(' ')[7])
|
45
|
-
puts "Estimated Network Fee: #{e.to_s.split(' ')[7]}"
|
46
|
-
|
47
|
-
retry unless (tries -= 1).zero?
|
48
|
-
|
49
|
-
raise Exception.new("UNABLE TO ESTIMATE NETWORK FEE")
|
50
|
-
end
|
51
|
-
|
52
|
-
# make the withdrawal
|
53
|
-
puts BlockIo.withdraw(options)
|
54
|
-
|
55
|
-
# all balance has been transferred to first_address_label
|
56
|
-
|
57
|
-
# create the change address
|
58
|
-
puts BlockIo.get_new_address(:label => second_address_label)
|
59
|
-
|
60
|
-
destination_address = ENV['DESTINATION_ADDRESS']
|
61
|
-
destination_amount = BigDecimal(ENV['DESTINATION_AMOUNT'])
|
62
|
-
|
63
|
-
puts "Sending #{destination_amount} to #{destination_address}"
|
64
|
-
|
65
|
-
available_balance = BigDecimal(BlockIo.get_balance(:labels => first_address_label)['data']['available_balance'])
|
66
|
-
|
67
|
-
second_address = BlockIo.get_address_by_label(:label => second_address_label)['data']['address']
|
68
|
-
|
69
|
-
options = {}
|
70
|
-
|
71
|
-
tries = 2 # two tries to estimate the correct network fee
|
72
|
-
|
73
|
-
network_fee = BigDecimal('0.0')
|
74
|
-
|
75
|
-
to_addresses = []
|
76
|
-
amounts = []
|
77
|
-
|
78
|
-
# estimate the fee for this withdrawal
|
79
|
-
begin
|
80
|
-
|
81
|
-
change_amount = available_balance - destination_amount
|
82
|
-
|
83
|
-
if change_amount - network_fee > 0 then
|
84
|
-
to_addresses = [destination_address, second_address]
|
85
|
-
amounts = [destination_amount.truncate(8).to_s("F"), (change_amount - network_fee).truncate(8).to_s("F")]
|
86
|
-
else
|
87
|
-
to_addresses = [destination_address]
|
88
|
-
amounts = [destination_amount.truncate(8).to_s("F")]
|
89
|
-
end
|
90
|
-
|
91
|
-
response = BlockIo.get_network_fee_estimate(:from_labels => first_address_label, :to_addresses => to_addresses.join(','), :amounts => amounts.join(','))
|
92
|
-
network_fee = BigDecimal(response['data']['estimated_network_fee'])
|
93
|
-
|
94
|
-
puts "Final Network Fee: #{network_fee.truncate(8).to_s('F')}"
|
95
|
-
|
96
|
-
rescue Exception => e
|
97
|
-
# extract the fee and subtract it from the available amount
|
98
|
-
|
99
|
-
puts "Exception: #{e.to_s}"
|
100
|
-
|
101
|
-
network_fee = BigDecimal(e.to_s.split(' ')[7])
|
102
|
-
puts "Estimated Network Fee: #{e.to_s.split(' ')[7]}"
|
103
|
-
|
104
|
-
retry unless (tries -= 1).zero?
|
105
|
-
|
106
|
-
raise Exception.new("UNABLE TO ESTIMATE NETWORK FEE")
|
107
|
-
end
|
108
|
-
|
109
|
-
# make the withdrawal + send change to change address
|
110
|
-
|
111
|
-
puts BlockIo.withdraw(:from_labels => first_address_label, :to_addresses => to_addresses.join(','), :amounts => amounts.join(','))
|
112
|
-
|
113
|
-
# archive the first address
|
114
|
-
|
115
|
-
puts BlockIo.archive_address(:labels => first_address_label)
|
116
|
-
|
117
|
-
|