glueby 0.2.0 → 0.3.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: d988a55f8acc63e5c2abb8f30ab88030a19c8e42212e372a8eeea6a2c5f3b27d
4
- data.tar.gz: 9cee6ba595dd3ba576d1190350396a4c55923c58620a529ec0ff55cc4249b69b
3
+ metadata.gz: d0b977d0a922975117e46c6777bf07fb13ad89d03bee8d9785ddec04e311f9c4
4
+ data.tar.gz: 5b0472f6ca379fcffd7dac8adcb8fe508237ae8fcf184c1372ba721c4104f13f
5
5
  SHA512:
6
- metadata.gz: 610751093275239e15a70726033be9a3eb4ca65e65c1b86874f09471f4391fbc764463f76da36619b7429dac7c2b70b1f6596e2e3b2eacb397f11029018fb8e1
7
- data.tar.gz: c48b89c3b4a61f652b051642420f1d8b3aa08ad3d90f8807ec8803f63d64e675a924a56990f42fc3d4201d6a90c99fa8101f423bbe66143ae735efdfae758cbf
6
+ metadata.gz: 1e2e21b82fe9fcb5ac8433d8f5952718624d7900b09f2ecf789163c3037a26a6b1939dbeb03dcccbf0051e890947f1612fc933c75d8093a3e74d26756ea43642
7
+ data.tar.gz: 8c6d28c5432a9e8a7d56072bf6398453da18d69ef564225987649ce6bf91e751ff40b42dba9c23d652a3a2b755539b919ad0c98ae83da6607c35b3a78b7f0fd1
@@ -0,0 +1,26 @@
1
+ module Glueby
2
+ module Contract
3
+ class BlockSyncerGenerator < Rails::Generators::Base
4
+ include ::Rails::Generators::Migration
5
+ include Glueby::Generator::MigrateGenerator
6
+ extend Glueby::Generator::MigrateGenerator::ClassMethod
7
+
8
+ source_root File.expand_path('templates', __dir__)
9
+
10
+ def create_migration_file
11
+ migration_dir = File.expand_path("db/migrate")
12
+
13
+ if self.class.migration_exists?(migration_dir, "create_system_information")
14
+ ::Kernel.warn "Migration already exists: create_system_information"
15
+ else
16
+ migration_template(
17
+ "system_information_table.rb.erb",
18
+ "db/migrate/create_system_information.rb",
19
+ migration_version: migration_version,
20
+ table_options: table_options,
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,12 @@
1
+ class CreateSystemInformation < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :system_informations<%= table_options %> do |t|
4
+ t.string :info_key
5
+ t.string :info_value
6
+ t.timestamps
7
+ end
8
+ add_index :system_informations, [:info_key], unique: true
9
+
10
+ Glueby::AR::SystemInformation.create(info_key: "synced_block_number", info_value: "0")
11
+ end
12
+ end
data/lib/glueby.rb CHANGED
@@ -6,6 +6,7 @@ module Glueby
6
6
  autoload :Generator, 'glueby/generator'
7
7
  autoload :Wallet, 'glueby/wallet'
8
8
  autoload :Internal, 'glueby/internal'
9
+ autoload :AR, 'glueby/active_record'
9
10
 
10
11
  begin
11
12
  class Railtie < ::Rails::Railtie
@@ -13,6 +14,7 @@ module Glueby
13
14
  load "tasks/glueby/contract.rake"
14
15
  load "tasks/glueby/contract/timestamp.rake"
15
16
  load "tasks/glueby/contract/wallet_adapter.rake"
17
+ load "tasks/glueby/contract/block_syncer.rake"
16
18
  end
17
19
  end
18
20
  rescue
@@ -0,0 +1,8 @@
1
+
2
+ require 'active_record'
3
+
4
+ module Glueby
5
+ module AR
6
+ autoload :SystemInformation, 'glueby/active_record/system_information'
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ module Glueby
2
+ module AR
3
+ class SystemInformation < ::ActiveRecord::Base
4
+
5
+ def self.synced_block_height
6
+ SystemInformation.find_by(info_key: "synced_block_number")
7
+ end
8
+
9
+ def int_value
10
+ info_value.to_i
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -11,6 +11,10 @@ module Glueby
11
11
  # or
12
12
  # Glueby::Wallet.create
13
13
  #
14
+ # Use `Glueby::Internal::Wallet#receive_address` to generate the address of a receiver
15
+ # receiver.internal_wallet.receive_address
16
+ # => '1CY6TSSARn8rAFD9chCghX5B7j4PKR8S1a'
17
+ #
14
18
  # Balance of sender and receiver before send
15
19
  # sender.balances[""]
16
20
  # => 100_000(tapyrus)
@@ -18,7 +22,7 @@ module Glueby
18
22
  # => 0(tapyrus)
19
23
  #
20
24
  # Send
21
- # Payment.transfer(sender: sender, receiver: receiver, amount: 10_000)
25
+ # Payment.transfer(sender: sender, receiver_address: '1CY6TSSARn8rAFD9chCghX5B7j4PKR8S1a', amount: 10_000)
22
26
  # sender.balances[""]
23
27
  # => 90_000
24
28
  # receiver.balances[""]
@@ -28,7 +32,7 @@ module Glueby
28
32
  extend Glueby::Contract::TxBuilder
29
33
 
30
34
  class << self
31
- def transfer(sender:, receiver:, amount:, fee_provider: FixedFeeProvider.new)
35
+ def transfer(sender:, receiver_address:, amount:, fee_provider: FixedFeeProvider.new)
32
36
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
33
37
 
34
38
  tx = Tapyrus::Tx.new
@@ -37,7 +41,7 @@ module Glueby
37
41
  sum, outputs = sender.internal_wallet.collect_uncolored_outputs(dummy_fee + amount)
38
42
  fill_input(tx, outputs)
39
43
 
40
- receiver_script = Tapyrus::Script.parse_from_addr(receiver.internal_wallet.receive_address)
44
+ receiver_script = Tapyrus::Script.parse_from_addr(receiver_address)
41
45
  tx.outputs << Tapyrus::TxOut.new(value: amount, script_pubkey: receiver_script)
42
46
 
43
47
  fee = fee_provider.fee(tx)
@@ -13,13 +13,17 @@ module Glueby
13
13
  # alice = Glueby::Wallet.create
14
14
  # bob = Glueby::Wallet.create
15
15
  #
16
+ # Use `Glueby::Internal::Wallet#receive_address` to generate the address of bob
17
+ # bob.internal_wallet.receive_address
18
+ # => '1CY6TSSARn8rAFD9chCghX5B7j4PKR8S1a'
19
+ #
16
20
  # Issue
17
21
  # token = Token.issue!(issuer: alice, amount: 100)
18
22
  # token.amount(wallet: alice)
19
23
  # => 100
20
24
  #
21
25
  # Send
22
- # token.transfer!(sender: alice, receiver: bob, amount: 1)
26
+ # token.transfer!(sender: alice, receiver_address: '1CY6TSSARn8rAFD9chCghX5B7j4PKR8S1a', amount: 1)
23
27
  # token.amount(wallet: alice)
24
28
  # => 99
25
29
  # token.amount(wallet: bob)
@@ -122,16 +126,16 @@ module Glueby
122
126
  # Send the token to other wallet
123
127
  #
124
128
  # @param sender [Glueby::Wallet] wallet to send this token
125
- # @param receiver [Glueby::Wallet] wallet to receive this token
129
+ # @param receiver_address [String] address to receive this token
126
130
  # @param amount [Integer]
127
131
  # @return [Token] receiver token
128
132
  # @raise [InsufficientFunds] if wallet does not have enough TPC to send transaction.
129
133
  # @raise [InsufficientTokens] if wallet does not have enough token to send.
130
134
  # @raise [InvalidAmount] if amount is not positive integer.
131
- def transfer!(sender:, receiver:, amount: 1)
135
+ def transfer!(sender:, receiver_address:, amount: 1)
132
136
  raise Glueby::Contract::Errors::InvalidAmount unless amount.positive?
133
137
 
134
- tx = create_transfer_tx(color_id: color_id, sender: sender, receiver: receiver, amount: amount)
138
+ tx = create_transfer_tx(color_id: color_id, sender: sender, receiver_address: receiver_address, amount: amount)
135
139
  sender.internal_wallet.broadcast(tx)
136
140
  end
137
141
 
@@ -100,14 +100,14 @@ module Glueby
100
100
  issuer.internal_wallet.sign_tx(tx, prev_txs)
101
101
  end
102
102
 
103
- def create_transfer_tx(color_id:, sender:, receiver:, amount:, fee_provider: FixedFeeProvider.new)
103
+ def create_transfer_tx(color_id:, sender:, receiver_address:, amount:, fee_provider: FixedFeeProvider.new)
104
104
  tx = Tapyrus::Tx.new
105
105
 
106
106
  utxos = sender.internal_wallet.list_unspent
107
107
  sum_token, outputs = collect_colored_outputs(utxos, color_id, amount)
108
108
  fill_input(tx, outputs)
109
109
 
110
- receiver_script = Tapyrus::Script.parse_from_addr(receiver.internal_wallet.receive_address)
110
+ receiver_script = Tapyrus::Script.parse_from_addr(receiver_address)
111
111
  receiver_colored_script = receiver_script.add_color(color_id)
112
112
  tx.outputs << Tapyrus::TxOut.new(value: amount, script_pubkey: receiver_colored_script)
113
113
 
@@ -1,3 +1,3 @@
1
1
  module Glueby
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,36 @@
1
+ module Glueby
2
+ module Contract
3
+ module Task
4
+ module BlockSyncer
5
+
6
+ def sync_block
7
+ latest_block_num = Glueby::Internal::RPC.client.getblockcount
8
+ synced_block = Glueby::AR::SystemInformation.synced_block_height
9
+ (synced_block.int_value + 1..latest_block_num).each do |i|
10
+ ::ActiveRecord::Base.transaction do
11
+ block_hash = Glueby::Internal::RPC.client.getblockhash(i)
12
+ import_block(block_hash)
13
+ synced_block.update(info_value: i.to_s)
14
+ puts "success in synchronization (block height=#{i.to_s})"
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ namespace :glueby do
25
+ namespace :contract do
26
+ namespace :block_syncer do
27
+ include Glueby::Contract::Task::BlockSyncer
28
+
29
+ desc 'sync block into database'
30
+ task :start, [] => [:environment] do |_, _|
31
+ Glueby::Contract::Task::BlockSyncer.sync_block
32
+ end
33
+
34
+ end
35
+ end
36
+ 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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tapyrus
@@ -72,15 +72,19 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - glueby.gemspec
75
+ - lib/generators/glueby/contract/block_syncer_generator.rb
75
76
  - lib/generators/glueby/contract/initializer_generator.rb
76
77
  - lib/generators/glueby/contract/templates/initializer.rb.erb
77
78
  - lib/generators/glueby/contract/templates/key_table.rb.erb
79
+ - lib/generators/glueby/contract/templates/system_information_table.rb.erb
78
80
  - lib/generators/glueby/contract/templates/timestamp_table.rb.erb
79
81
  - lib/generators/glueby/contract/templates/utxo_table.rb.erb
80
82
  - lib/generators/glueby/contract/templates/wallet_table.rb.erb
81
83
  - lib/generators/glueby/contract/timestamp_generator.rb
82
84
  - lib/generators/glueby/contract/wallet_adapter_generator.rb
83
85
  - lib/glueby.rb
86
+ - lib/glueby/active_record.rb
87
+ - lib/glueby/active_record/system_information.rb
84
88
  - lib/glueby/contract.rb
85
89
  - lib/glueby/contract/active_record.rb
86
90
  - lib/glueby/contract/active_record/timestamp.rb
@@ -106,6 +110,7 @@ files:
106
110
  - lib/glueby/version.rb
107
111
  - lib/glueby/wallet.rb
108
112
  - lib/tasks/glueby/contract.rake
113
+ - lib/tasks/glueby/contract/block_syncer.rake
109
114
  - lib/tasks/glueby/contract/timestamp.rake
110
115
  - lib/tasks/glueby/contract/wallet_adapter.rake
111
116
  homepage: https://github.com/chaintope/glueby