sequence-sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,256 @@
1
+ require 'securerandom'
2
+
3
+ require_relative './client_module'
4
+ require_relative './query'
5
+ require_relative './response_object'
6
+
7
+ module Chain
8
+ class Transaction < ResponseObject
9
+
10
+ # @!attribute [r] id
11
+ # Unique transaction identifier.
12
+ # @return [String]
13
+ attrib :id
14
+
15
+ # @!attribute [r] timestamp
16
+ # Time of transaction.
17
+ # @return [Time]
18
+ attrib :timestamp, rfc3339_time: true
19
+
20
+ # @!attribute [r] reference_data
21
+ # User specified, unstructured data embedded within a transaction.
22
+ # @return [Hash]
23
+ attrib :reference_data
24
+
25
+ # @!attribute [r] actions
26
+ # List of a transaction's actions.
27
+ # @return [Array<Input>]
28
+ attrib(:actions) { |raw| raw.map { |v| Action.new(v) } }
29
+
30
+ class ClientModule < Chain::ClientModule
31
+ # Builds, signs, and submits a transaction.
32
+ # @param [Builder] builder Builder object with actions defined. If provided, overrides block parameter.
33
+ # @yield Block defining transaction actions. A {Builder} object is passed as the only parameter.
34
+ # @return Transaction
35
+ def transact(builder = nil, &block)
36
+ if builder.nil?
37
+ builder = Builder.new(&block)
38
+ end
39
+
40
+ tpl = client.conn.request('build-transaction', builder)
41
+ tpl = client.conn.request(
42
+ '/sign-transaction',
43
+ transaction: tpl,
44
+ )
45
+ Transaction.new(client.conn.request(
46
+ 'submit-transaction',
47
+ {transaction: tpl}
48
+ ))
49
+ end
50
+
51
+ # List all transactions, optionally filtered
52
+ # @param [Hash] opts Filtering information
53
+ # @option opts [String] filter Filter string, see {https://chain.com/docs/core/build-applications/queries}.
54
+ # @option opts [Array<String|Integer>] filter_params Parameter values for filter string (if needed).
55
+ # @option opts [Integer] start_time A Unix timestamp in milliseconds. When specified, only transactions with a block time greater than the start time will be returned.
56
+ # @option opts [Integer] end_time A Unix timestamp in milliseconds. When specified, only transactions with a block time less than the start time will be returned.
57
+ # @option opts [Integer] timeout A time in milliseconds after which a server timeout should occur. Defaults to 1000 (1 second).
58
+ # @return [Query]
59
+ def query(opts = {})
60
+ Query.new(client, opts)
61
+ end
62
+ end
63
+
64
+ class Query < Chain::Query
65
+ def fetch(query)
66
+ client.conn.request('list-transactions', query)
67
+ end
68
+
69
+ def translate(raw)
70
+ Transaction.new(raw)
71
+ end
72
+ end
73
+
74
+ class Action < ResponseObject
75
+ # @!attribute [r] type
76
+ # The type of the action.
77
+ #
78
+ # Possible values are "issue", "transfer" and "retire".
79
+ # @return [String]
80
+ attrib :type
81
+
82
+ # @!attribute [r] asset_id
83
+ # The id of the action's asset.
84
+ # @return [String]
85
+ attrib :asset_id
86
+
87
+ # @!attribute [r] asset_alias
88
+ # The alias of the action's asset (possibly null).
89
+ # @return [String]
90
+ attrib :asset_alias
91
+
92
+ # @!attribute [r] asset_tags
93
+ # The tags of the action's asset (possibly null).
94
+ # @return [Hash]
95
+ attrib :asset_tags
96
+
97
+ # @!attribute [r] amount
98
+ # The number of units of the action's asset.
99
+ # @return [Integer]
100
+ attrib :amount
101
+
102
+ # @!attribute [r] source_account_id
103
+ # The id of the account transferring the asset (possibly null if the
104
+ # action is an issuance).
105
+ # @return [String]
106
+ attrib :source_account_id
107
+
108
+ # @!attribute [r] source_account_alias
109
+ # The alias of the account transferring the asset (possibly null if the
110
+ # action is an issuance).
111
+ # @return [String]
112
+ attrib :source_account_alias
113
+
114
+ # @!attribute [r] source_account_tags
115
+ # The tags associated with the source account (possibly null).
116
+ # @return [String]
117
+ attrib :source_account_tags
118
+
119
+ # @!attribute [r] destination_account_id
120
+ # The id of the account receiving the asset (possibly null if the
121
+ # action is a retirement).
122
+ # @return [String]
123
+ attrib :destination_account_id
124
+
125
+ # @!attribute [r] destination_account_alias
126
+ # The alias of the account receiving the asset (possibly null if the
127
+ # action is an retirement).
128
+ # @return [String]
129
+ attrib :destination_account_alias
130
+
131
+ # @!attribute [r] destination_account_tags
132
+ # The tags associated with the destination account (possibly null).
133
+ # @return [String]
134
+ attrib :destination_account_tags
135
+
136
+ # @!attribute [r] reference_data
137
+ # User specified, unstructured data embedded within an action
138
+ # (possibly null).
139
+ # @return [Hash]
140
+ attrib :reference_data
141
+ end
142
+
143
+ class Builder
144
+ attr_accessor :ttl, :reference_data
145
+
146
+ def initialize(&block)
147
+ block.call(self) if block
148
+ end
149
+
150
+ # @return [Array<Hash>]
151
+ def actions
152
+ @actions ||= []
153
+ end
154
+
155
+ # @param [Template, String] template_or_raw_tx
156
+ # @return [Builder]
157
+ def base_transaction(template_or_raw_tx)
158
+ if template_or_raw_tx.is_a?(Transaction::Template)
159
+ @base_transaction = template_or_raw_tx.raw_transaction
160
+ else
161
+ @base_transaction = template_or_raw_tx
162
+ end
163
+ self
164
+ end
165
+
166
+ # @return [Hash]
167
+ def to_h
168
+ {
169
+ actions: actions,
170
+ base_transaction: @base_transaction,
171
+ reference_data: reference_data,
172
+ ttl: ttl,
173
+ }
174
+ end
175
+
176
+ # @return [String]
177
+ def to_json(opts = nil)
178
+ to_h.to_json(opts)
179
+ end
180
+
181
+ # Add an action to the transaction builder
182
+ # @param [Hash] params Action parameters containing a type field and the
183
+ # required parameters for that type
184
+ # @return [Builder]
185
+ def add_action(params)
186
+ # Some actions require an idempotency token, so we'll add it here as a
187
+ # generic parameter.
188
+ params = {client_token: SecureRandom.uuid}.merge(params)
189
+ actions << params
190
+ self
191
+ end
192
+
193
+ # Issue new units of an asset into an account.
194
+ # @param [Hash] params Action parameters
195
+ # @option params [String] :asset_id Asset ID specifying the asset to be issued.
196
+ # You must specify either an ID or an alias.
197
+ # @option params [String] :asset_alias Asset alias specifying the asset to be issued.
198
+ # You must specify either an ID or an alias.
199
+ # @option params [Integer] :amount amount of the asset to be issued
200
+ # @option params [String] :destination_account_id ID of account receiving the newly-issued asset units.
201
+ # You must specify either an ID or an alias.
202
+ # @option params [String] :destination_account_alias Alias of account receiving the newly-issued asset units.
203
+ # You must specify either an ID or an alias.
204
+ # @option params [Hash] :destination_reference_data Reference data to add to the receiving output.
205
+ # @return [Builder]
206
+ def issue(params)
207
+ add_action(params.merge(type: :issue))
208
+ end
209
+
210
+ # Takes units of an asset from a source (an account or output) and retires them.
211
+ # @param [Hash] params Action parameters
212
+ # @option params [String] :source_account_id Account ID specifying the account controlling the asset.
213
+ # You must specify a source account ID, account alias, or output ID.
214
+ # @option params [String] :source_account_alias Account alias specifying the account controlling the asset.
215
+ # You must specify a source account ID, account alias, or output ID.
216
+ # @option params [String] :source_output_id Output controlling the asset.
217
+ # You must specify a source account ID, account alias, or output ID.
218
+ # @option params [String] :asset_id Asset ID specifying the asset to be retired.
219
+ # You must specify either an ID or an alias.
220
+ # @option params [String] :asset_alias Asset alias specifying the asset to be retired.
221
+ # You must specify either an ID or an alias.
222
+ # @option params [Integer] :amount Amount of the asset to be retired.
223
+ # @option params [Hash] :destination_reference_data Reference data to add to the receiving output.
224
+ # @option params [Hash] :change_reference_data Reference data to add to the change output, if it is necessary.
225
+ # @return [Builder]
226
+ def retire(params)
227
+ add_action(params.merge(type: :retire))
228
+ end
229
+
230
+ # Moves units of an asset from a source (an account or output) to a destination account.
231
+ #
232
+ # @param [Hash] params Action parameters
233
+ # @option params [String] :source_account_id Account ID specifying the account controlling the asset.
234
+ # You must specify a source account ID, account alias, or output ID.
235
+ # @option params [String] :source_account_alias Account alias specifying the account controlling the asset.
236
+ # You must specify a source account ID, account alias, or output ID.
237
+ # @option params [String] :source_output_id Output controlling the asset.
238
+ # You must specify a source account ID, account alias, or output ID.
239
+ # @option params [String] :asset_id Asset ID specifying the asset to be transferred.
240
+ # You must specify either an ID or an alias.
241
+ # @option params [String] :asset_alias Asset alias specifying the asset to be transferred.
242
+ # You must specify either an ID or an alias.
243
+ # @option params [Integer] :amount Amount of the asset to be transferred.
244
+ # @option params [String] :destination_account_id Account ID specifying the account controlling the asset.
245
+ # You must specify a destination account ID or alias.
246
+ # @option params [String] :destination_account_alias Account alias specifying the account controlling the asset.
247
+ # You must specify a destination account ID or alias.
248
+ # @option params [Hash] :destination_reference_data Reference data to add to the receiving output.
249
+ # @option params [Hash] :change_reference_data Reference data to add to the change output, if it is necessary.
250
+ # @return [Builder]
251
+ def transfer(params)
252
+ add_action(params.merge(type: :transfer))
253
+ end
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,72 @@
1
+ require_relative './client_module'
2
+ require_relative './response_object'
3
+ require_relative './query'
4
+
5
+ module Chain
6
+ class UnspentOutput < ResponseObject
7
+ # @!attribute [r] id
8
+ # @return [String]
9
+ attrib :id
10
+
11
+ # @!attribute [r] type
12
+ # @return [String]
13
+ attrib :type
14
+
15
+ # @!attribute [r] transaction_id
16
+ # @return [String]
17
+ attrib :transaction_id
18
+
19
+ # @!attribute [r] asset_id
20
+ # @return [String]
21
+ attrib :asset_id
22
+
23
+ # @!attribute [r] asset_alias
24
+ # @return [String]
25
+ attrib :asset_alias
26
+
27
+ # @!attribute [r] asset_tags
28
+ # @return [Hash]
29
+ attrib :asset_tags
30
+
31
+ # @!attribute [r] amount
32
+ # @return [Integer]
33
+ attrib :amount
34
+
35
+ # @!attribute [r] account_id
36
+ # @return [String]
37
+ attrib :account_id
38
+
39
+ # @!attribute [r] account_alias
40
+ # @return [String]
41
+ attrib :account_alias
42
+
43
+ # @!attribute [r] account_tags
44
+ # @return [Hash]
45
+ attrib :account_tags
46
+
47
+ # @!attribute [r] reference_data
48
+ # @return [Hash]
49
+ attrib :reference_data
50
+
51
+ class ClientModule < Chain::ClientModule
52
+ # @param [Hash] opts Filtering information
53
+ # @option opts [String] filter Filter string, see {https://chain.com/docs/core/build-applications/queries}.
54
+ # @option opts [Array<String|Integer>] filter_params Parameter values for filter string (if needed).
55
+ # @option opts [Integer] timestamp A millisecond Unix timestamp. By using this parameter, you can perform queries that reflect the state of the blockchain at different points in time.
56
+ # @return [Query]
57
+ def query(opts = {})
58
+ Query.new(client, opts)
59
+ end
60
+ end
61
+
62
+ class Query < Chain::Query
63
+ def fetch(query)
64
+ client.conn.request('list-unspent-outputs', query)
65
+ end
66
+
67
+ def translate(raw)
68
+ UnspentOutput.new(raw)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Chain
2
+ VERSION = '0.0.1'
3
+ end
data/lib/chain.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative './chain/client'
2
+ require_relative './chain/constants'
3
+ require_relative './chain/version'
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequence-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chain Engineering
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler-audit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.5.0
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.5.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: 3.5.0
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.5.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec-its
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 1.2.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 1.2.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: parallel_tests
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: 2.14.1
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: 2.14.1
103
+ - !ruby/object:Gem::Dependency
104
+ name: simplecov
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.14.1
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.14.1
117
+ - !ruby/object:Gem::Dependency
118
+ name: webmock
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: 2.3.2
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: 2.3.2
131
+ - !ruby/object:Gem::Dependency
132
+ name: yard
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: 0.9.5
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 0.9.5
141
+ type: :development
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: 0.9.5
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 0.9.5
151
+ description: SDK for Sequence
152
+ email:
153
+ executables: []
154
+ extensions: []
155
+ extra_rdoc_files: []
156
+ files:
157
+ - LICENSE
158
+ - README.md
159
+ - lib/chain.rb
160
+ - lib/chain/account.rb
161
+ - lib/chain/asset.rb
162
+ - lib/chain/balance.rb
163
+ - lib/chain/batch_response.rb
164
+ - lib/chain/client.rb
165
+ - lib/chain/client_module.rb
166
+ - lib/chain/connection.rb
167
+ - lib/chain/constants.rb
168
+ - lib/chain/dev_utils.rb
169
+ - lib/chain/errors.rb
170
+ - lib/chain/key.rb
171
+ - lib/chain/page.rb
172
+ - lib/chain/query.rb
173
+ - lib/chain/response_object.rb
174
+ - lib/chain/stats.rb
175
+ - lib/chain/transaction.rb
176
+ - lib/chain/unspent_output.rb
177
+ - lib/chain/version.rb
178
+ homepage: https://sequence.chain.com
179
+ licenses:
180
+ - Apache-2.0
181
+ metadata: {}
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - "~>"
189
+ - !ruby/object:Gem::Version
190
+ version: '2.0'
191
+ required_rubygems_version: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - ">="
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 2.6.8
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: SDK for Sequence
202
+ test_files: []