ton_sdk_client 1.9.0 → 1.15.0

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