ton_sdk_client 1.11.0 → 1.16.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module TonSdk
2
2
 
3
3
  # NOTE
4
- # as of 3 feb 2021, in the main repository this module is still unstable
4
+ # as of 28 apr 2021, in the main repository this module is still unstable
5
5
  module Debot
6
6
 
7
7
  #
@@ -23,26 +23,15 @@ module TonSdk
23
23
  OPERATION_REJECTED = 812
24
24
  end
25
25
 
26
- class DebotAction
27
- attr_reader :description, :name, :action_type, :to, :attributes, :misc
28
-
29
- def initialize(description:, name:, action_type:, to:, attributes:, misc:)
30
- @description = description
31
- @name = name
32
- @action_type = action_type
33
- @to = to
34
- @attributes = attributes
35
- @misc = misc
36
- end
37
-
26
+ DebotAction = Struct.new(:description, :name, :action_type, :to, :attributes, :misc, keyword_init: true) do
38
27
  def to_h
39
28
  {
40
- description: @description,
41
- name: @name,
42
- action_type: @action_type,
43
- to: @to,
44
- attributes: @attributes,
45
- misc: @misc
29
+ description: self.description,
30
+ name: self.name,
31
+ action_type: self.action_type,
32
+ to: self.to,
33
+ attributes: self.attributes,
34
+ misc: self.misc
46
35
  }
47
36
  end
48
37
 
@@ -60,27 +49,18 @@ module TonSdk
60
49
  end
61
50
  end
62
51
 
63
- class ParamsOfStart
64
- attr_reader :address
65
-
66
- def initialize(a)
67
- @address = a
68
- end
52
+ ParamsOfStart = Struct.new(:debot_handle)
69
53
 
70
- def to_h = { address: @address }
71
- end
72
-
73
- class RegisteredDebot
74
- attr_reader :debot_handle
75
-
76
- def initialize(a)
77
- @debot_handle = a
78
- end
79
-
80
- def to_h = { debot_handle: @debot_handle }
54
+ RegisteredDebot = Struct.new(:debot_handle, :debot_abi, :info) do
55
+ def to_h = {
56
+ debot_handle: @debot_handle,
57
+ debot_abi: @debot_abi,
58
+ info: @info.to_h
59
+ }
81
60
  end
82
61
 
83
62
  class ParamsOfAppDebotBrowser
63
+ private_class_method :new
84
64
 
85
65
  # todo remove?
86
66
  TYPE_VALUES = [
@@ -91,50 +71,56 @@ module TonSdk
91
71
  :input,
92
72
  :get_signing_box,
93
73
  :invoke_debot,
94
- :send
74
+ :send,
75
+ :approve
95
76
  ]
96
77
 
97
- attr_reader :type_, :msg, :context_id, :action, :prompt, :debot_addr, :message
78
+ attr_reader :type_, :msg, :context_id, :action, :prompt, :debot_addr, :message, :activity
98
79
 
99
- def new_with_type_log(msg)
80
+ def self.new_with_type_log(msg)
100
81
  @type_ = :log
101
82
  @msg = msg
102
83
  end
103
84
 
104
- def new_with_type_switch(context_id)
85
+ def self.new_with_type_switch(context_id)
105
86
  @type_ = :switch
106
87
  @context_id = context_id
107
88
  end
108
89
 
109
- def new_with_type_switch_completed
90
+ def self.new_with_type_switch_completed
110
91
  @type_ = :switch_completed
111
92
  end
112
93
 
113
- def new_with_type_show_action(action)
94
+ def self.new_with_type_show_action(action)
114
95
  @type_ = :show_action
115
96
  @action = action
116
97
  end
117
98
 
118
- def new_with_type_input(prompt)
99
+ def self.new_with_type_input(prompt)
119
100
  @type_ = :input
120
101
  @prompt = prompt
121
102
  end
122
103
 
123
- def new_with_type_get_signing_box
104
+ def self.new_with_type_get_signing_box
124
105
  @type_ = :get_signing_box
125
106
  end
126
107
 
127
- def new_with_type_invoke_debot(debot_addr, action)
108
+ def self.new_with_type_invoke_debot(debot_addr, action)
128
109
  @type_ = :invoke_debot
129
110
  @debot_addr = debot_addr
130
111
  @action = action
131
112
  end
132
113
 
133
- def new_with_type_send(message)
114
+ def self.new_with_type_send(message)
134
115
  @type_ = :send
135
116
  @message = message
136
117
  end
137
118
 
119
+ def self.new_with_type_approve(activity)
120
+ @type_ = :approve
121
+ @activity = activity
122
+ end
123
+
138
124
  def to_h
139
125
  {
140
126
  type: Helper.sym_to_capitalized_case_str(@type_),
@@ -150,14 +136,38 @@ module TonSdk
150
136
  def self.from_json(j)
151
137
  return nil if j.nil?
152
138
 
153
- self.new(
154
- type_: self.parse_type(j["type"]),
155
- msg: j["msg"],
156
- context_id: j["context_id"],
157
- action: DebotAction.from_json(j["action"]),
158
- prompt: j["prompt"],
159
- debot_addr: j["debot_addr"]
160
- )
139
+ tp = self.parse_type(j["type"])
140
+ case tp
141
+ when :log
142
+ self.new_with_type_log(j["msg"])
143
+
144
+ when :switch
145
+ self.new_with_type_switch(j["context_id"])
146
+
147
+ when :switch_completed
148
+ self.new_with_type_switch_completed
149
+
150
+ when :show_action
151
+ self.new_with_type_show_action(DebotAction.from_json(j["action"]))
152
+
153
+ when :input
154
+ self.new_with_type_input(j["prompt"])
155
+
156
+ when :get_signing_box
157
+ self.new_with_type_get_signing_box
158
+
159
+ when :invoke_debot
160
+ self.new_with_type_invoke_debot(j["debot_addr"], DebotAction.from_json(j["action"]))
161
+
162
+ when :send
163
+ self.new_with_type_send(j["message"])
164
+
165
+ when :approve
166
+ self.new_with_type_send(DebotActivity.from_json(j["activity"]))
167
+
168
+ else
169
+ raise ArgumentError.new("no handler for type: #{tp}")
170
+ end
161
171
  end
162
172
 
163
173
  private
@@ -173,46 +183,33 @@ module TonSdk
173
183
  end
174
184
 
175
185
  class ResultOfAppDebotBrowser
186
+ private_class_method :new
187
+
176
188
  attr_reader :type_, :value, :signing_box, :is_approved
177
189
 
178
- def new_with_type_input(a)
190
+ def self.new_with_type_input(a)
179
191
  @type_ = :input
180
192
  @value = a
181
193
  end
182
194
 
183
- def new_with_type_get_signing_box(a)
195
+ def self.new_with_type_get_signing_box(a)
184
196
  @type_ = :get_signing_box
185
197
  @signing_box = signing_box
186
198
  end
187
199
 
188
- def new_with_type_invoke_debot
200
+ def self.new_with_type_invoke_debot
189
201
  @type_ = :invoke_debot
190
202
  end
191
203
 
192
- def new_with_type_approve(a)
204
+ def self.new_with_type_approve(a)
193
205
  @type_ = :approve
194
206
  @is_approved = a
195
207
  end
196
208
  end
197
209
 
198
- class ParamsOfFetch
199
- attr_reader :address
200
-
201
- def initialize(a)
202
- @address = a
203
- end
204
-
205
- def to_h = { address: @address }
206
- end
207
-
208
- class ParamsOfExecute
209
- attr_reader :debot_handle, :action
210
-
211
- def initialize(debot_handle:, action:)
212
- @debot_handle = debot_handle
213
- @action = action
214
- end
210
+ ParamsOfFetch = Struct.new(:address)
215
211
 
212
+ ParamsOfExecute = Struct.new(:debot_handle, :action) do
216
213
  def to_h
217
214
  {
218
215
  debot_handle: @debot_handle,
@@ -221,115 +218,91 @@ module TonSdk
221
218
  end
222
219
  end
223
220
 
224
- class ParamsOfSend
225
- attr_reader :debot_handle, :message
221
+ ParamsOfSend = Struct.new(:debot_handle, :message)
222
+ ParamsOfInit = Struct.new(:address)
223
+ DebotInfo = Struct.new(
224
+ :name,
225
+ :version,
226
+ :publisher,
227
+ :caption,
228
+ :author,
229
+ :support,
230
+ :hello,
231
+ :language,
232
+ :dabi,
233
+ :icon,
234
+ :interfaces,
235
+ keyword_init: true
236
+ ) do
237
+ def initialize(
238
+ name: nil,
239
+ version: nil,
240
+ publisher: nil,
241
+ caption: nil,
242
+ author: nil,
243
+ support: nil,
244
+ hello: nil,
245
+ language: nil,
246
+ dabi: nil,
247
+ icon: nil,
248
+ interfaces: []
249
+ )
250
+ super
251
+ end
252
+ end
226
253
 
227
- def initialize(debot_handle:, message:)
228
- @debot_handle = debot_handle
229
- @message = message
254
+ ResultOfFetch = Struct.new(:info)
255
+ Spending = Struct.new(:amount, :dst)
256
+
257
+ class DebotActivity
258
+ private_class_method :new
259
+
260
+ attr_reader :type_, :msg, :dst, :out, :fee, :setcode, :signkey, :signing_box_handle
261
+
262
+ def self.new_with_type_transaction(msg:, dst:, out:, fee:, setcode:, signkey:, signing_box_handle:)
263
+ @type_ = :transaction
264
+ @msg = msg
265
+ @dst = dst
266
+ @out = out
267
+ @fee = fee
268
+ @setcode = setcode
269
+ @signkey = signkey
270
+ @signing_box_handle = signing_box_handle
230
271
  end
231
272
 
232
- def to_h
233
- {
234
- debot_handle: @debot_handle,
235
- message: @message
236
- }
273
+ def self.from_json(j)
274
+ # todo
237
275
  end
238
276
  end
239
277
 
278
+
279
+
240
280
  #
241
281
  # functions
242
282
  #
243
283
 
244
- def self.start(ctx, params, app_browser_obj)
245
- # TODO
246
- # 1) the handlers in 'start' and 'fetch' are identical
247
- # verify that it works and get rid of repetition
248
-
249
- # 2) this all can be replaced with 'app_browser_obj.request(...)' and
250
- # 'app_browser_obj.notify(...)' calls, possibly
251
-
252
- app_resp_handler = Proc.new do |data|
253
- req_data = data["request_data"]
254
- case data["type"]
255
- when "Log"
256
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
257
- app_browser_obj.log(new_obj.msg)
258
-
259
- when "Switch"
260
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
261
- app_browser_obj.switch_to(new_obj.context_id)
262
-
263
- when "SwitchCompleted"
264
- app_browser_obj.switch_completed()
265
-
266
- when "ShowAction"
267
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
268
- app_browser_obj.show_action(new_obj.action)
269
-
270
- when "Input"
271
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
272
- # TODO possibly in a new thread or fiber
273
- app_req_res = begin
274
- res = app_browser_obj.input(new_obj.prompt)
275
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :input, value: res))
276
- rescue Exception => e
277
- Client::AppRequestResult(type_: :error, text: e.message)
278
- end
279
-
280
- params = Client::ParamsOfResolveAppRequest.new(
281
- app_request_id: data["app_request_id"],
282
- result: app_req_res
283
- )
284
- TonSdk::Client.resolve_app_request(c_ctx, params)
285
-
286
- when "GetSigningBox"
287
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
288
- # TODO possibly in a new thread or fiber
289
- app_req_res = begin
290
- res = app_browser_obj.get_signing_box()
291
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :get_signing_box, signing_box: res))
292
- rescue Exception => e
293
- Client::AppRequestResult(type_: :error, text: e.message)
294
- end
295
-
296
- params = Client::ParamsOfResolveAppRequest.new(
297
- app_request_id: data["app_request_id"],
298
- result: app_req_res
299
- )
300
- TonSdk::Client.resolve_app_request(c_ctx, params)
301
-
302
- when "InvokeDebot"
303
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
304
- # TODO possibly in a new thread or fiber
305
- app_req_res = begin
306
- res = app_browser_obj.invoke_debot(new_obj.debot_addr, new_obj.action)
307
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :invoke_debot))
308
- rescue Exception => e
309
- Client::AppRequestResult(type_: :error, text: e.message)
310
- end
311
-
312
- params = Client::ParamsOfResolveAppRequest.new(
313
- app_request_id: data["app_request_id"],
314
- result: app_req_res
284
+ def self.init(ctx, params, app_browser_obj)
285
+ Interop::request_to_native_lib(ctx, "debot.init", params) do |resp|
286
+ if resp.success?
287
+ yield NativeLibResponsetResult.new(
288
+ result: nil
315
289
  )
316
- TonSdk::Client.resolve_app_request(c_ctx, params)
317
-
318
290
  else
319
- # TODO log 'unknown option'
291
+ yield resp
320
292
  end
321
293
  end
294
+ end
322
295
 
296
+ def self.start(ctx, params)
323
297
  Interop::request_to_native_lib(
324
298
  ctx,
325
299
  "debot.start",
326
- params.to_h.to_json,
327
- debot_app_response_handler: app_resp_handler,
300
+ params,
328
301
  is_single_thread_only: false
329
302
  ) do |resp|
330
303
  if resp.success?
331
304
  yield NativeLibResponsetResult.new(
332
- result: RegisteredDebot.new(resp.result["debot_handle"])
305
+ result: nil
333
306
  )
334
307
  else
335
308
  yield resp
@@ -337,95 +310,17 @@ module TonSdk
337
310
  end
338
311
  end
339
312
 
340
- def self.fetch(ctx, params, app_browser_obj)
341
- # TODO
342
- # 1) the handlers in 'start' and 'fetch' are identical
343
- # verify that it works and get rid of repetition
344
-
345
- # 2) this all can be replaced with 'app_browser_obj.request(...)' and
346
- # 'app_browser_obj.notify(...)' calls, possibly
347
-
348
- app_resp_handler = Proc.new do |data|
349
- req_data = data["request_data"]
350
- case data["type"]
351
- when "Log"
352
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
353
- app_browser_obj.log(new_obj.msg)
354
-
355
- when "Switch"
356
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
357
- app_browser_obj.switch_to(new_obj.context_id)
358
-
359
- when "SwitchCompleted"
360
- app_browser_obj.switch_completed()
361
-
362
- when "ShowAction"
363
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
364
- app_browser_obj.show_action(new_obj.action)
365
-
366
- when "Input"
367
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
368
- # TODO possibly in a new thread or fiber
369
- app_req_res = begin
370
- res = app_browser_obj.input(new_obj.prompt)
371
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :input, value: res))
372
- rescue Exception => e
373
- Client::AppRequestResult(type_: :error, text: e.message)
374
- end
375
-
376
- params = Client::ParamsOfResolveAppRequest.new(
377
- app_request_id: data["app_request_id"],
378
- result: app_req_res
379
- )
380
- TonSdk::Client.resolve_app_request(c_ctx, params)
381
-
382
- when "GetSigningBox"
383
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
384
- # TODO possibly in a new thread or fiber
385
- app_req_res = begin
386
- res = app_browser_obj.get_signing_box()
387
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :get_signing_box, signing_box: res))
388
- rescue Exception => e
389
- Client::AppRequestResult(type_: :error, text: e.message)
390
- end
391
-
392
- params = Client::ParamsOfResolveAppRequest.new(
393
- app_request_id: data["app_request_id"],
394
- result: app_req_res
395
- )
396
- TonSdk::Client.resolve_app_request(c_ctx, params)
397
-
398
- when "InvokeDebot"
399
- new_obj = ParamsOfAppDebotBrowser.from_json(data)
400
- # TODO possibly in a new thread or fiber
401
- app_req_res = begin
402
- res = app_browser_obj.invoke_debot(new_obj.debot_addr, new_obj.action)
403
- Client::AppRequestResult(type_: :ok, result: ResultOfAppDebotBrowser.new(type_: :invoke_debot))
404
- rescue Exception => e
405
- Client::AppRequestResult(type_: :error, text: e.message)
406
- end
407
-
408
- params = Client::ParamsOfResolveAppRequest.new(
409
- app_request_id: data["app_request_id"],
410
- result: app_req_res
411
- )
412
- TonSdk::Client.resolve_app_request(c_ctx, params)
413
-
414
- else
415
- # TODO log 'unknown option'
416
- end
417
- end
418
-
313
+ def self.fetch(ctx, params)
419
314
  Interop::request_to_native_lib(
420
315
  ctx,
421
316
  "debot.fetch",
422
- params.to_h.to_json,
423
- debot_app_response_handler: app_resp_handler,
317
+ params,
424
318
  is_single_thread_only: false
425
319
  ) do |resp|
426
320
  if resp.success?
427
321
  yield NativeLibResponsetResult.new(
428
- result: RegisteredDebot.new(resp.result["debot_handle"])
322
+ # TODO: parse DebotInfo
323
+ result: ResultOfFetch.new(resp.result["info"])
429
324
  )
430
325
  else
431
326
  yield resp
@@ -434,7 +329,7 @@ module TonSdk
434
329
  end
435
330
 
436
331
  def self.execute(ctx, params)
437
- Interop::request_to_native_lib(ctx, "debot.execute", params.to_h.to_json) do |resp|
332
+ Interop::request_to_native_lib(ctx, "debot.execute", params) do |resp|
438
333
  if resp.success?
439
334
  yield NativeLibResponsetResult.new(
440
335
  result: nil
@@ -446,7 +341,7 @@ module TonSdk
446
341
  end
447
342
 
448
343
  def self.remove(ctx, params)
449
- Interop::request_to_native_lib(ctx, "debot.remove", params.to_h.to_json) do |resp|
344
+ Interop::request_to_native_lib(ctx, "debot.remove", params) do |resp|
450
345
  if resp.success?
451
346
  yield NativeLibResponsetResult.new(
452
347
  result: nil
@@ -458,7 +353,7 @@ module TonSdk
458
353
  end
459
354
 
460
355
  def self.send(ctx, params)
461
- Interop::request_to_native_lib(ctx, "debot.send", params.to_h.to_json) do |resp|
356
+ Interop::request_to_native_lib(ctx, "debot.send", params) do |resp|
462
357
  if resp.success?
463
358
  yield NativeLibResponsetResult.new(
464
359
  result: nil